]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#9181 Fix race on Windows mutex init
authorHoward Chu <hyc@openldap.org>
Mon, 16 Mar 2020 17:07:43 +0000 (17:07 +0000)
committerQuanah Gibson-Mount <quanah@openldap.org>
Thu, 2 Apr 2020 21:28:37 +0000 (21:28 +0000)
include/ldap_int_thread.h
libraries/libldap_r/thr_nt.c

index 54d330c1225f813f398108c71df5d654ce3ac624..ad94f3cd805a18e57d66d696c28adbae1d68b16b 100644 (file)
@@ -180,10 +180,13 @@ typedef HANDLE    ldap_int_thread_mutex_t;
 typedef HANDLE ldap_int_thread_cond_t;
 typedef DWORD  ldap_int_thread_key_t;
 
+LDAP_F( int )
+ldap_pvt_thread_mutex_init_first LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
+
 #ifndef LDAP_INT_MUTEX_NULL
 #define LDAP_INT_MUTEX_NULL            ((HANDLE)0)
 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
-               ((void) ((m) || ldap_pvt_thread_mutex_init(&(m))))
+               ldap_pvt_thread_mutex_init_first(&(m))
 #endif
 
 LDAP_END_DECL
index 19a0549bd83e820833238491235537e6d6a678f1..5929556b8f1bd7e20ab4bc24a469d407275a0237 100644 (file)
@@ -161,6 +161,17 @@ ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
        return ( 0 );
 }
 
+int
+ldap_pvt_thread_mutex_init_first( ldap_pvt_thread_mutex_t *mutex )
+{
+       if ( *mutex == NULL ) {
+               HANDLE p = CreateMutex( NULL, 0, NULL );
+               if ( InterlockedCompareExchangePointer((PVOID*)mutex, (PVOID)p, NULL) != NULL)
+                       CloseHandle( p );
+       }
+       return ( 0 );
+}
+
 int 
 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
 {