once.c revision 285612
160107Sobrien/* 22786Ssos * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC") 32786Ssos * Copyright (C) 1999-2001 Internet Software Consortium. 42786Ssos * 52786Ssos * Permission to use, copy, modify, and/or distribute this software for any 62786Ssos * purpose with or without fee is hereby granted, provided that the above 72786Ssos * copyright notice and this permission notice appear in all copies. 82786Ssos * 92786Ssos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 102786Ssos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 112786Ssos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 122786Ssos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 132786Ssos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 142786Ssos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 152786Ssos * PERFORMANCE OF THIS SOFTWARE. 162786Ssos */ 172786Ssos 1838140Syokota/* $Id: once.c,v 1.12 2007/06/18 23:47:49 tbox Exp $ */ 192786Ssos 206867Sache/* Principal Authors: DCL */ 214686Sache 222786Ssos#include <config.h> 232786Ssos 242786Ssos#include <windows.h> 252786Ssos 262786Ssos#include <isc/once.h> 272786Ssos#include <isc/assertions.h> 282786Ssos#include <isc/util.h> 292786Ssos 302786Ssosisc_result_t 312786Ssosisc_once_do(isc_once_t *controller, void(*function)(void)) { 322786Ssos REQUIRE(controller != NULL && function != NULL); 332786Ssos 342786Ssos if (controller->status == ISC_ONCE_INIT_NEEDED) { 352786Ssos 362786Ssos if (InterlockedDecrement(&controller->counter) == 0) { 372786Ssos if (controller->status == ISC_ONCE_INIT_NEEDED) { 382786Ssos function(); 392786Ssos controller->status = ISC_ONCE_INIT_DONE; 402786Ssos } 412786Ssos } else { 422786Ssos while (controller->status == ISC_ONCE_INIT_NEEDED) { 432786Ssos /* 442786Ssos * Sleep(0) indicates that this thread 452786Ssos * should be suspended to allow other 462786Ssos * waiting threads to execute. 472786Ssos */ 482786Ssos Sleep(0); 492786Ssos } 502786Ssos } 512786Ssos } 522786Ssos 532786Ssos return (ISC_R_SUCCESS); 542786Ssos} 552786Ssos