Retire Thread::TERMINATED
authorMarco Costalba <mcostalba@gmail.com>
Mon, 8 Aug 2011 20:25:37 +0000 (21:25 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 8 Aug 2011 21:10:01 +0000 (22:10 +0100)
Use proper way to detect for thread terimnation instead of
our homegrown flag.

It adds more code than it removes and adds also platform specific
code, neverthless I think is the way to go becuase Thread::TERMINATED
flag is intrinsecly racy given that when we raise it thread is still
_not_ terminated nor it can be, and also we don't want to reinvent
the (broken) wheel of thread termination detection when there is
already available the proper one.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>

src/search.cpp
src/thread.cpp
src/thread.h

index eda9d29..9992049 100644 (file)
@@ -2168,7 +2168,6 @@ void Thread::idle_loop(SplitPoint* sp) {
           if (do_terminate)
           {
               assert(!sp);
-              state = Thread::TERMINATED;
               lock_release(&sleepLock);
               return;
           }
index e631645..a8e26b9 100644 (file)
@@ -163,12 +163,12 @@ void ThreadsManager::init() {
       threads[i].threadID = i;
 
 #if defined(_MSC_VER)
-      bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID , 0, NULL) != NULL);
+      threads[i].handle = CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID, 0, NULL);
+      bool ok = (threads[i].handle != NULL);
 #else
-      pthread_t pthreadID;
-      bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threads[i].threadID) == 0);
-      pthread_detach(pthreadID);
+      bool ok = (pthread_create(&threads[i].handle, NULL, start_routine, (void*)&threads[i].threadID) == 0);
 #endif
+
       if (!ok)
       {
           std::cout << "Failed to create thread number " << i << std::endl;
@@ -189,7 +189,14 @@ void ThreadsManager::exit() {
       {
           threads[i].do_terminate = true;
           threads[i].wake_up();
-          while (threads[i].state != Thread::TERMINATED) {}
+
+#if defined(_MSC_VER)
+          WaitForSingleObject(threads[i].handle, 0);
+          CloseHandle(threads[i].handle);
+#else
+          pthread_join(threads[i].handle, NULL);
+          pthread_detach(threads[i].handle);
+#endif
       }
 
       // Now we can safely destroy locks and wait conditions
index aba43c9..5325deb 100644 (file)
@@ -67,10 +67,9 @@ struct Thread {
 
   enum ThreadState
   {
-    SEARCHING,     // Thread is performing work
     AVAILABLE,     // Thread is waiting for work
     WORKISWAITING, // Master has ordered us to start searching
-    TERMINATED     // We are quitting and thread is terminated
+    SEARCHING      // Thread is performing work
   };
 
   void wake_up();
@@ -90,6 +89,12 @@ struct Thread {
   volatile int activeSplitPoints;
   volatile bool do_sleep;
   volatile bool do_terminate;
+
+#if defined(_MSC_VER)
+  HANDLE handle;
+#else
+  pthread_t handle;
+#endif
 };