We don't need to clear it. Additionally, this has the added
benefit of removing what's technically a "data race" in C11,
i.e. mixed atomic and non-atomic accesses on a variable.

That's not a problem in practice: the compiler generates
plain read instructions which are atomic on all architectures.
That said, removing what's technically Undefined Behavior is
always nice.
---
 mingw-w64-libraries/winpthreads/src/mutex.c | 45 +++++++++------------
 1 file changed, 18 insertions(+), 27 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index ef9d36c7..614534fe 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -741,16 +741,13 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex 
**wMutex, const Winpthre
 
 static void WinpthreadsNormalMutexDestroy (WinpthreadsMutex *wMutex) {
   WinpthreadsNormalMutex *mutex = &wMutex->NormalMutex;
-  HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
 
-  if (event != NULL) {
-    /**
-     * `Destroy` can run concurrently with `Unlock`. Wait on the event
-     * to ensure that `Unlock` has finished working [1].
-     */
-    _pthread_wait_for_single_object (event, INFINITE);
-    CloseHandle (event);
-  }
+  /**
+   * `Destroy` can run concurrently with `Unlock`. Wait on the event
+   * to ensure that `Unlock` has finished working [1].
+   */
+  _pthread_wait_for_single_object (mutex->Event, INFINITE);
+  CloseHandle (mutex->Event);
 
   free (mutex);
 }
@@ -864,16 +861,13 @@ static int WinpthreadsErrorCheckMutexInit 
(WinpthreadsMutex **wMutex, const Winp
 
 static void WinpthreadsErrorCheckMutexDestroy (WinpthreadsMutex *wMutex) {
   WinpthreadsErrorCheckMutex *mutex = &wMutex->ErrorCheckMutex;
-  HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
 
-  if (event != NULL) {
-    /**
-     * `Destroy` can run concurrently with `Unlock`. Wait on the event
-     * to ensure that `Unlock` has finished working [1].
-     */
-    _pthread_wait_for_single_object (event, INFINITE);
-    CloseHandle (event);
-  }
+  /**
+   * `Destroy` can run concurrently with `Unlock`. Wait on the event
+   * to ensure that `Unlock` has finished working [1].
+   */
+  _pthread_wait_for_single_object (mutex->Event, INFINITE);
+  CloseHandle (mutex->Event);
 
   free (mutex);
 }
@@ -1011,16 +1005,13 @@ static int WinpthreadsRecursiveMutexInit 
(WinpthreadsMutex **wMutex, const Winpt
 
 static void WinpthreadsRecursiveMutexDestroy (WinpthreadsMutex *wMutex) {
   WinpthreadsRecursiveMutex *mutex = &wMutex->RecursiveMutex;
-  HANDLE event = InterlockedExchangePointer ((void **) &mutex->Event, NULL);
 
-  if (event != NULL) {
-    /**
-     * `Destroy` can run concurrently with `Unlock`. Wait on the event
-     * to ensure that `Unlock` has finished working [1].
-     */
-    _pthread_wait_for_single_object (event, INFINITE);
-    CloseHandle (event);
-  }
+  /**
+   * `Destroy` can run concurrently with `Unlock`. Wait on the event
+   * to ensure that `Unlock` has finished working [1].
+   */
+  _pthread_wait_for_single_object (mutex->Event, INFINITE);
+  CloseHandle (mutex->Event);
 
   free (mutex);
 }
-- 
2.55.0.windows.5



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to