]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Fix endless reaparenting loop
[stockfish] / src / thread.cpp
index b8686f4c48e5f69d0bed038ab9cab3bcfdf8cc18..631834da4b9f60c159facb502b5f7e2c0ac3fe04 100644 (file)
@@ -34,15 +34,11 @@ namespace { extern "C" {
  // start_routine() is the C function which is called when a new thread
  // is launched. It is a wrapper to member function pointed by start_fn.
 
- long start_routine(Thread* th) {
-
-   Threads.set_this_thread(th); // Save pointer into thread local storage
-   (th->*(th->start_fn))();
-   return 0;
- }
+ long start_routine(Thread* th) { (th->*(th->start_fn))(); return 0; }
 
 } }
 
+
 // Thread c'tor starts a newly-created thread of execution that will call
 // the idle loop function pointed by start_fn going immediately to sleep.
 
@@ -183,7 +179,7 @@ bool Thread::cutoff_occurred() const {
 // slaves which are busy searching the split point at the top of slaves split
 // point stack (the "helpful master concept" in YBWC terminology).
 
-bool Thread::is_available_to(const Thread& master) const {
+bool Thread::is_available_to(Thread* master) const {
 
   if (is_searching)
       return false;
@@ -194,7 +190,7 @@ bool Thread::is_available_to(const Thread& master) const {
 
   // No active split points means that the thread is available as a slave for any
   // other thread otherwise apply the "helpful master" concept if possible.
-  return !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master.idx));
+  return !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master->idx));
 }
 
 
@@ -205,7 +201,6 @@ bool Thread::is_available_to(const Thread& master) const {
 
 void ThreadsManager::init() {
 
-  tls_init(tlsKey);
   cond_init(sleepCond);
   lock_init(splitLock);
   timer = new Thread(&Thread::timer_loop);
@@ -224,7 +219,6 @@ ThreadsManager::~ThreadsManager() {
   delete timer;
   lock_destroy(splitLock);
   cond_destroy(sleepCond);
-  tls_destroy(tlsKey);
 }
 
 
@@ -283,7 +277,7 @@ void ThreadsManager::sleep() const {
 // available_slave_exists() tries to find an idle thread which is available as
 // a slave for the thread 'master'.
 
-bool ThreadsManager::available_slave_exists(const Thread& master) const {
+bool ThreadsManager::available_slave_exists(Thread* master) const {
 
   for (int i = 0; i < size(); i++)
       if (threads[i]->is_available_to(master))
@@ -313,18 +307,19 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
   assert(beta <= VALUE_INFINITE);
   assert(depth > DEPTH_ZERO);
 
-  Thread& master = pos.this_thread();
+  Thread* master = pos.this_thread();
 
-  if (master.splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
+  if (master->splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
       return bestValue;
 
   // Pick the next available split point from the split point stack
-  SplitPoint* sp = &master.splitPoints[master.splitPointsCnt++];
+  SplitPoint* sp = &master->splitPoints[master->splitPointsCnt];
 
-  sp->parent = master.curSplitPoint;
-  sp->master = &master;
+  sp->parent = master->curSplitPoint;
+  sp->master = master;
   sp->cutoff = false;
-  sp->slavesMask = 1ULL << master.idx;
+  sp->slavesMask = 1ULL << master->idx;
+  sp->allSlavesRunning = true;
   sp->depth = depth;
   sp->bestMove = *bestMove;
   sp->threatMove = threatMove;
@@ -338,9 +333,9 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
   sp->nodes = 0;
   sp->ss = ss;
 
-  assert(master.is_searching);
+  assert(master->is_searching);
 
-  master.curSplitPoint = sp;
+  master->curSplitPoint = sp;
   int slavesCnt = 0;
 
   // Try to allocate available threads and ask them to start searching setting
@@ -363,6 +358,8 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
               break;
       }
 
+  master->splitPointsCnt++;
+
   lock_release(splitLock);
   lock_release(sp->lock);
 
@@ -373,11 +370,11 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
   // their work at this split point.
   if (slavesCnt || Fake)
   {
-      master.idle_loop(sp);
+      master->idle_loop(sp);
 
       // In helpful master concept a master can help only a sub-tree of its split
       // point, and because here is all finished is not possible master is booked.
-      assert(!master.is_searching);
+      assert(!master->is_searching);
   }
 
   // We have returned from the idle loop, which means that all threads are
@@ -386,9 +383,9 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
   lock_grab(sp->lock); // To protect sp->nodes
   lock_grab(splitLock);
 
-  master.is_searching = true;
-  master.splitPointsCnt--;
-  master.curSplitPoint = sp->parent;
+  master->is_searching = true;
+  master->splitPointsCnt--;
+  master->curSplitPoint = sp->parent;
   pos.set_nodes_searched(pos.nodes_searched() + sp->nodes);
   *bestMove = sp->bestMove;
 
@@ -440,7 +437,7 @@ void ThreadsManager::start_searching(const Position& pos, const LimitsType& limi
   Signals.stopOnPonderhit = Signals.firstRootMove = false;
   Signals.stop = Signals.failedLowAtRoot = false;
 
-  RootPosition.copy(pos, main_thread());
+  RootPosition = pos;
   Limits = limits;
   RootMoves.clear();