xref: /freebsd/contrib/ntp/libntp/lib/isc/win32/once.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1999-2001  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id: once.c,v 1.12 2007/06/18 23:47:49 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /* Principal Authors: DCL */
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert #include <windows.h>
25*a466cc55SCy Schubert 
26*a466cc55SCy Schubert #include <isc/once.h>
27*a466cc55SCy Schubert #include <isc/assertions.h>
28*a466cc55SCy Schubert #include <isc/util.h>
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert isc_result_t
isc_once_do(isc_once_t * controller,void (* function)(void))31*a466cc55SCy Schubert isc_once_do(isc_once_t *controller, void(*function)(void)) {
32*a466cc55SCy Schubert 	REQUIRE(controller != NULL && function != NULL);
33*a466cc55SCy Schubert 
34*a466cc55SCy Schubert 	if (controller->status == ISC_ONCE_INIT_NEEDED) {
35*a466cc55SCy Schubert 
36*a466cc55SCy Schubert 		if (InterlockedDecrement(&controller->counter) == 0) {
37*a466cc55SCy Schubert 			if (controller->status == ISC_ONCE_INIT_NEEDED) {
38*a466cc55SCy Schubert 				function();
39*a466cc55SCy Schubert 				controller->status = ISC_ONCE_INIT_DONE;
40*a466cc55SCy Schubert 			}
41*a466cc55SCy Schubert 		} else {
42*a466cc55SCy Schubert 			while (controller->status == ISC_ONCE_INIT_NEEDED) {
43*a466cc55SCy Schubert 				/*
44*a466cc55SCy Schubert 				 * Sleep(0) indicates that this thread
45*a466cc55SCy Schubert 				 * should be suspended to allow other
46*a466cc55SCy Schubert 				 * waiting threads to execute.
47*a466cc55SCy Schubert 				 */
48*a466cc55SCy Schubert 				Sleep(0);
49*a466cc55SCy Schubert 			}
50*a466cc55SCy Schubert 		}
51*a466cc55SCy Schubert 	}
52*a466cc55SCy Schubert 
53*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
54*a466cc55SCy Schubert }
55