From 3d69b20b8d05dff833cb192002066df6acfdd7c3 Mon Sep 17 00:00:00 2001 From: Ivan Zhakov Date: Sun, 19 May 2019 17:23:14 +0000 Subject: Use native one-time initialization [1] to implement apr_thread_once_t on Windows. This also fixes problem that apr_thread_once() may return before the other read completes initialization on Windows. [1] https://docs.microsoft.com/en-gb/windows/desktop/Sync/one-time-initialization git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1859517 13f79535-47bb-0310-9956-ffa450edef68 --- threadproc/win32/thread.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'threadproc') diff --git a/threadproc/win32/thread.c b/threadproc/win32/thread.c index c713e1444..3204a1c2c 100644 --- a/threadproc/win32/thread.c +++ b/threadproc/win32/thread.c @@ -257,19 +257,39 @@ APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, return APR_SUCCESS; } -APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control, +APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control_p, apr_pool_t *p) { - (*control) = apr_pcalloc(p, sizeof(**control)); + apr_thread_once_t *control = apr_pcalloc(p, sizeof(*control)); + + InitOnceInitialize(&control->once); + + *control_p = control; + return APR_SUCCESS; } +static BOOL WINAPI init_once_callback(PINIT_ONCE InitOnce, + PVOID Parameter, + PVOID *Context) +{ + void (*func)(void) = Parameter; + + func(); + + return TRUE; +} + APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, void (*func)(void)) { - if (!InterlockedExchange(&control->value, 1)) { - func(); + PVOID lpContext; + + if (!InitOnceExecuteOnce(&control->once, init_once_callback, func, + &lpContext)) { + return apr_get_os_error(); } + return APR_SUCCESS; } -- cgit v1.2.1