1999-09-23 18:14:16 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-09-23 18:14:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <isc/once.h>
|
|
|
|
#include <isc/assertions.h>
|
2001-07-06 05:08:39 +00:00
|
|
|
#include <isc/util.h>
|
1999-09-23 18:14:16 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2001-07-09 21:06:30 +00:00
|
|
|
isc_once_do(isc_once_t *controller, void(*function)(void)) {
|
1999-09-23 18:14:16 +00:00
|
|
|
REQUIRE(controller != NULL && function != NULL);
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
if (controller->status == ISC_ONCE_INIT_NEEDED) {
|
|
|
|
|
1999-09-23 18:14:16 +00:00
|
|
|
if (InterlockedDecrement(&controller->counter) == 0) {
|
|
|
|
if (controller->status == ISC_ONCE_INIT_NEEDED) {
|
|
|
|
function();
|
|
|
|
controller->status = ISC_ONCE_INIT_DONE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (controller->status == ISC_ONCE_INIT_NEEDED) {
|
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Sleep(0) indicates that this thread
|
|
|
|
* should be suspended to allow other
|
2007-06-18 01:16:46 +00:00
|
|
|
* waiting threads to execute.
|
1999-09-23 18:14:16 +00:00
|
|
|
*/
|
2007-06-18 01:16:46 +00:00
|
|
|
Sleep(0);
|
1999-09-23 18:14:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|