You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by iv...@apache.org on 2019/05/19 17:23:14 UTC

svn commit: r1859517 - in /apr/apr/trunk: CHANGES include/arch/win32/apr_arch_threadproc.h threadproc/win32/thread.c

Author: ivan
Date: Sun May 19 17:23:14 2019
New Revision: 1859517

URL: http://svn.apache.org/viewvc?rev=1859517&view=rev
Log:
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

Modified:
    apr/apr/trunk/CHANGES
    apr/apr/trunk/include/arch/win32/apr_arch_threadproc.h
    apr/apr/trunk/threadproc/win32/thread.c

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?rev=1859517&r1=1859516&r2=1859517&view=diff
==============================================================================
--- apr/apr/trunk/CHANGES [utf-8] (original)
+++ apr/apr/trunk/CHANGES [utf-8] Sun May 19 17:23:14 2019
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) apr_thread_once: Fix problem that apr_thread_once can return before
+     the other thread completes initialization on Windows.
+
   *) Windows platform: Remove support for Windows 2000, XP, Vista. The minimum
      supported OS versions are now Windows 7 / Windows Server 2008 R2.
 

Modified: apr/apr/trunk/include/arch/win32/apr_arch_threadproc.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/arch/win32/apr_arch_threadproc.h?rev=1859517&r1=1859516&r2=1859517&view=diff
==============================================================================
--- apr/apr/trunk/include/arch/win32/apr_arch_threadproc.h (original)
+++ apr/apr/trunk/include/arch/win32/apr_arch_threadproc.h Sun May 19 17:23:14 2019
@@ -65,7 +65,7 @@ struct apr_procattr_t {
 };
 
 struct apr_thread_once_t {
-    long value;
+    INIT_ONCE once;
 };
 
 extern apr_status_t apr_threadproc_init(apr_pool_t *pool);

Modified: apr/apr/trunk/threadproc/win32/thread.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/threadproc/win32/thread.c?rev=1859517&r1=1859516&r2=1859517&view=diff
==============================================================================
--- apr/apr/trunk/threadproc/win32/thread.c (original)
+++ apr/apr/trunk/threadproc/win32/thread.c Sun May 19 17:23:14 2019
@@ -257,19 +257,39 @@ APR_DECLARE(apr_status_t) apr_os_thread_
     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;
 }