]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Split at root!
[stockfish] / src / thread.cpp
index dee37d0dd740b7f6bb784c8cdf8eb0115282ff91..4ef145781511aacbf61930844dcf5153b8942ba4 100644 (file)
 
 ThreadsManager Threads; // Global object definition
 
-namespace {
+namespace { extern "C" {
 
- // init_thread() is the function which is called when a new thread is
- // launched. It simply calls the idle_loop() function with the supplied
- // threadID. There are two versions of this function; one for POSIX
- // threads and one for Windows threads.
+ // start_routine() is the C function which is called when a new thread
+ // is launched. It simply calls idle_loop() with the supplied threadID.
+ // There are two versions of this function; one for POSIX threads and
+ // one for Windows threads.
 
-#if !defined(_MSC_VER)
+#if defined(_MSC_VER)
 
-  void* init_thread(void* threadID) {
+  DWORD WINAPI start_routine(LPVOID threadID) {
 
     Threads.idle_loop(*(int*)threadID, NULL);
-    return NULL;
+    return 0;
   }
 
 #else
 
-  DWORD WINAPI init_thread(LPVOID threadID) {
+  void* start_routine(void* threadID) {
 
     Threads.idle_loop(*(int*)threadID, NULL);
-    return 0;
+    return NULL;
   }
 
 #endif
 
-}
+} }
 
 
 // wake_up() wakes up the thread, normally at the beginning of the search or,
@@ -115,12 +115,12 @@ void ThreadsManager::read_uci_options() {
 }
 
 
-// init_threads() is called during startup. Initializes locks and condition
-// variables and launches all threads sending them immediately to sleep.
+// init() is called during startup. Initializes locks and condition variables
+// and launches all threads sending them immediately to sleep.
 
 void ThreadsManager::init() {
 
-  int arg[MAX_THREADS];
+  int threadID[MAX_THREADS];
 
   // This flag is needed to properly end the threads when program exits
   allThreadsShouldExit = false;
@@ -148,14 +148,14 @@ void ThreadsManager::init() {
   for (int i = 1; i < MAX_THREADS; i++)
   {
       threads[i].state = Thread::INITIALIZING;
-      arg[i] = i;
+      threadID[i] = i;
 
-#if !defined(_MSC_VER)
-      pthread_t pthread[1];
-      bool ok = (pthread_create(pthread, NULL, init_thread, (void*)(&arg[i])) == 0);
-      pthread_detach(pthread[0]);
+#if defined(_MSC_VER)
+      bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threadID[i], 0, NULL) != NULL);
 #else
-      bool ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&arg[i]), 0, NULL) != NULL);
+      pthread_t pthreadID;
+      bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threadID[i]) == 0);
+      pthread_detach(pthreadID);
 #endif
       if (!ok)
       {
@@ -169,8 +169,7 @@ void ThreadsManager::init() {
 }
 
 
-// exit_threads() is called when the program exits. It makes all the
-// helper threads exit cleanly.
+// exit() is called to cleanly exit the threads when the program finishes
 
 void ThreadsManager::exit() {
 
@@ -241,7 +240,7 @@ bool ThreadsManager::available_slave_exists(int master) const {
 template <bool Fake>
 void ThreadsManager::split(Position& pos, SearchStack* ss, Value* alpha, const Value beta,
                            Value* bestValue, Depth depth, Move threatMove,
-                           int moveCount, MovePicker* mp, bool pvNode) {
+                           int moveCount, MovePicker* mp, int nodeType) {
   assert(pos.is_ok());
   assert(*bestValue >= -VALUE_INFINITE);
   assert(*bestValue <= *alpha);
@@ -276,7 +275,7 @@ void ThreadsManager::split(Position& pos, SearchStack* ss, Value* alpha, const V
   splitPoint.threatMove = threatMove;
   splitPoint.alpha = *alpha;
   splitPoint.beta = beta;
-  splitPoint.pvNode = pvNode;
+  splitPoint.nodeType = nodeType;
   splitPoint.bestValue = *bestValue;
   splitPoint.mp = mp;
   splitPoint.moveCount = moveCount;
@@ -342,5 +341,5 @@ void ThreadsManager::split(Position& pos, SearchStack* ss, Value* alpha, const V
 }
 
 // Explicit template instantiations
-template void ThreadsManager::split<false>(Position&, SearchStack*, Value*, const Value, Value*, Depth, Move, int, MovePicker*, bool);
-template void ThreadsManager::split<true>(Position&, SearchStack*, Value*, const Value, Value*, Depth, Move, int, MovePicker*, bool);
+template void ThreadsManager::split<false>(Position&, SearchStack*, Value*, const Value, Value*, Depth, Move, int, MovePicker*, int);
+template void ThreadsManager::split<true>(Position&, SearchStack*, Value*, const Value, Value*, Depth, Move, int, MovePicker*, int);