-/// Thread::idle_loop() is where the thread is parked when it has no work to do
-
-void Thread::idle_loop() {
-
- // Pointer 'this_sp' is not null only if we are called from split(), and not
- // at the thread creation. This means we are the split point's master.
- SplitPoint* this_sp = activeSplitPoint;
-
- assert(!this_sp || (this_sp->master == this && searching));
-
- while (!exit && !(this_sp && this_sp->slavesMask.none()))
- {
- // If this thread has been assigned work, launch a search
- while (searching)
- {
- spinlock.acquire();
-
- assert(activeSplitPoint);
- SplitPoint* sp = activeSplitPoint;
-
- spinlock.release();
-
- Stack stack[MAX_PLY+4], *ss = stack+2; // To allow referencing (ss-2) and (ss+2)
- Position pos(*sp->pos, this);
-
- std::memcpy(ss-2, sp->ss-2, 5 * sizeof(Stack));
- ss->splitPoint = sp;
-
- sp->spinlock.acquire();
-
- assert(activePosition == nullptr);
-
- activePosition = &pos;
-
- if (sp->nodeType == NonPV)
- search<NonPV, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
-
- else if (sp->nodeType == PV)
- search<PV, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
-
- else if (sp->nodeType == Root)
- search<Root, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
-
- else
- assert(false);
-
- assert(searching);
-
- searching = false;
- activePosition = nullptr;
- sp->slavesMask.reset(idx);
- sp->allSlavesSearching = false;
- sp->nodes += pos.nodes_searched();
-
- // After releasing the lock we can't access any SplitPoint related data
- // in a safe way because it could have been released under our feet by
- // the sp master.
- sp->spinlock.release();
-
- // Try to late join to another split point if none of its slaves has
- // already finished.
- SplitPoint* bestSp = NULL;
- int minLevel = INT_MAX;
-
- for (Thread* th : Threads)
- {
- const size_t size = th->splitPointsSize; // Local copy
- sp = size ? &th->splitPoints[size - 1] : nullptr;
-
- if ( sp
- && sp->allSlavesSearching
- && sp->slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT
- && can_join(sp))
- {
- assert(this != th);
- assert(!(this_sp && this_sp->slavesMask.none()));
- assert(Threads.size() > 2);
-
- // Prefer to join to SP with few parents to reduce the probability
- // that a cut-off occurs above us, and hence we waste our work.
- int level = 0;
- for (SplitPoint* p = th->activeSplitPoint; p; p = p->parentSplitPoint)
- level++;
-
- if (level < minLevel)
- {
- bestSp = sp;
- minLevel = level;
- }
- }
- }
-
- if (bestSp)
- {
- sp = bestSp;
-
- // Recheck the conditions under lock protection
- sp->spinlock.acquire();
-
- if ( sp->allSlavesSearching
- && sp->slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT)
- {
- spinlock.acquire();
-
- if (can_join(sp))
- {
- sp->slavesMask.set(idx);
- activeSplitPoint = sp;
- searching = true;
- }
-
- spinlock.release();
- }
-
- sp->spinlock.release();
- }
- }
-
- // If search is finished then sleep, otherwise just yield
- if (!Threads.main()->thinking)
- {
- assert(!this_sp);
-
- std::unique_lock<Mutex> lk(mutex);
- while (!exit && !Threads.main()->thinking)
- sleepCondition.wait(lk);
- }
- else
- std::this_thread::yield(); // Wait for a new job or for our slaves to finish
- }
-}
-
-
-/// check_time() is called by the timer thread when the timer triggers. It is
-/// used to print debug info and, more importantly, to detect when we are out of
-/// available time and thus stop the search.
-
-void check_time() {
-
- static TimePoint lastInfoTime = now();
- int elapsed = Time.elapsed();
-
- if (now() - lastInfoTime >= 1000)
- {
- lastInfoTime = now();
- dbg_print();
- }
-
- // An engine may not stop pondering until told so by the GUI
- if (Limits.ponder)
- return;