1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #include <config.h>
13 
14 #include <windows.h>
15 
16 #include <isc/once.h>
17 #include <isc/assertions.h>
18 #include <isc/util.h>
19 
20 isc_result_t
isc_once_do(isc_once_t * controller,void (* function)(void))21 isc_once_do(isc_once_t *controller, void(*function)(void)) {
22 	REQUIRE(controller != NULL && function != NULL);
23 
24 	if (controller->status == ISC_ONCE_INIT_NEEDED) {
25 
26 		if (InterlockedDecrement(&controller->counter) == 0) {
27 			if (controller->status == ISC_ONCE_INIT_NEEDED) {
28 				function();
29 				controller->status = ISC_ONCE_INIT_DONE;
30 			}
31 		} else {
32 			while (controller->status == ISC_ONCE_INIT_NEEDED) {
33 				/*
34 				 * Sleep(0) indicates that this thread
35 				 * should be suspended to allow other
36 				 * waiting threads to execute.
37 				 */
38 				Sleep(0);
39 			}
40 		}
41 	}
42 
43 	return (ISC_R_SUCCESS);
44 }
45