diff options
author | Ivan Zhakov <ivan@apache.org> | 2019-05-19 17:23:14 +0000 |
---|---|---|
committer | Ivan Zhakov <ivan@apache.org> | 2019-05-19 17:23:14 +0000 |
commit | 3d69b20b8d05dff833cb192002066df6acfdd7c3 (patch) | |
tree | 4b4ca5431b3b66b50679dcaa07dc1142c0c571e9 /threadproc | |
parent | f72b4a217f23b77ace61209a9b1f4ed97664f377 (diff) | |
download | apr-3d69b20b8d05dff833cb192002066df6acfdd7c3.tar.gz |
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
Diffstat (limited to 'threadproc')
-rw-r--r-- | threadproc/win32/thread.c | 28 |
1 files changed, 24 insertions, 4 deletions
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; } |