]> git.sesse.net Git - stockfish/commitdiff
Destroy wait conditions before exiting
authorMarco Costalba <mcostalba@gmail.com>
Sun, 17 Oct 2010 07:16:46 +0000 (08:16 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 17 Oct 2010 06:36:14 +0000 (07:36 +0100)
We already do this for locks. Also rename SitIdleEvent
in WaitCond to be uniform with Lunix naming.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/lock.h
src/search.cpp

index f46f93d89f7c8da8fba069cb8f4e49058e8e4fef..5ad56085e997a07e592a177b0b59f24b47074c8c 100644 (file)
 #  include <pthread.h>
 
 typedef pthread_mutex_t Lock;
+typedef pthread_cond_t WaitCondition;
 
 #  define lock_init(x) pthread_mutex_init(x, NULL)
 #  define lock_grab(x) pthread_mutex_lock(x)
 #  define lock_release(x) pthread_mutex_unlock(x)
 #  define lock_destroy(x) pthread_mutex_destroy(x)
-
+#  define cond_destroy(x) pthread_cond_destroy(x);
 
 #else
 
@@ -41,11 +42,13 @@ typedef pthread_mutex_t Lock;
 #undef WIN32_LEAN_AND_MEAN
 
 typedef CRITICAL_SECTION Lock;
+typedef HANDLE WaitCondition;
+
 #  define lock_init(x) InitializeCriticalSection(x)
 #  define lock_grab(x) EnterCriticalSection(x)
 #  define lock_release(x) LeaveCriticalSection(x)
 #  define lock_destroy(x) DeleteCriticalSection(x)
-
+#  define cond_destroy(x) CloseHandle(*x);
 
 #endif
 
index b38b45058ee474d7c9445d6310248436999b4114..f955f96d4180e10f6467f0ee29c24a23b81fbb23 100644 (file)
@@ -96,15 +96,8 @@ namespace {
     int ActiveThreads;
     volatile bool AllThreadsShouldExit, AllThreadsShouldSleep;
     Thread threads[MAX_THREADS];
-
     Lock MPLock, WaitLock;
-
-#if !defined(_MSC_VER)
-    pthread_cond_t WaitCond[MAX_THREADS];
-#else
-    HANDLE SitIdleEvent[MAX_THREADS];
-#endif
-
+    WaitCondition WaitCond[MAX_THREADS];
   };
 
 
@@ -2243,7 +2236,7 @@ split_point_start: // At split points actual search starts from here
                 pthread_cond_wait(&WaitCond[threadID], &WaitLock);
             lock_release(&WaitLock);
 #else
-            WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
+            WaitForSingleObject(WaitCond[threadID], INFINITE);
 #endif
         }
 
@@ -2306,10 +2299,6 @@ split_point_start: // At split points actual search starts from here
     volatile int i;
     bool ok;
 
-#if !defined(_MSC_VER)
-    pthread_t pthread[1];
-#endif
-
     // Initialize global locks
     lock_init(&MPLock);
     lock_init(&WaitLock);
@@ -2318,7 +2307,7 @@ split_point_start: // At split points actual search starts from here
 #if !defined(_MSC_VER)
         pthread_cond_init(&WaitCond[i], NULL);
 #else
-        SitIdleEvent[i] = CreateEvent(0, FALSE, FALSE, 0);
+        WaitCond[i] = CreateEvent(0, FALSE, FALSE, 0);
 #endif
 
     // Initialize splitPoints[] locks
@@ -2343,6 +2332,7 @@ split_point_start: // At split points actual search starts from here
     {
 
 #if !defined(_MSC_VER)
+        pthread_t pthread[1];
         ok = (pthread_create(pthread, NULL, init_thread, (void*)(&i)) == 0);
 #else
         ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&i), 0, NULL) != NULL);
@@ -2382,6 +2372,10 @@ split_point_start: // At split points actual search starts from here
 
     lock_destroy(&WaitLock);
     lock_destroy(&MPLock);
+
+    // Now we can safely destroy the wait conditions
+    for (int i = 0; i < MAX_THREADS; i++)
+        cond_destroy(&WaitCond[i]);
   }
 
 
@@ -2579,7 +2573,7 @@ split_point_start: // At split points actual search starts from here
         pthread_cond_signal(&WaitCond[threadID]);
         pthread_mutex_unlock(&WaitLock);
 #else
-        SetEvent(SitIdleEvent[threadID]);
+        SetEvent(WaitCond[threadID]);
 #endif
   }