- return false;
-}
-
-
-/// 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)
- {
- // If this thread has been assigned work, launch a search
- while (searching)
- {
- Threads.spinlock.acquire();
-
- assert(activeSplitPoint);
-
- SplitPoint* sp = activeSplitPoint;
-
- Threads.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();
-
- // Wake up the master thread so to allow it to return from the idle
- // loop in case we are the last slave of the split point.
- if (this != sp->master && sp->slavesMask.none())
- {
- assert(!sp->master->searching);
-
- sp->master->notify_one();
- }
-
- // 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
- Threads.spinlock.acquire();
- sp->spinlock.acquire();
-
- if ( sp->allSlavesSearching
- && sp->slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT
- && can_join(sp))
- {
- sp->slavesMask.set(idx);
- activeSplitPoint = sp;
- searching = true;
- }
-
- sp->spinlock.release();
- Threads.spinlock.release();
- }
- }
-
- // Avoid races with notify_one() fired from last slave of the split point
- std::unique_lock<std::mutex> lk(mutex);
-
- // If we are master and all slaves have finished then exit idle_loop
- if (this_sp && this_sp->slavesMask.none())
- {
- assert(!searching);
- break;
- }
-
- // If we are not searching, wait for a condition to be signaled instead of
- // wasting CPU time polling for work.
- if (!searching && !exit)
- sleepCondition.wait(lk);
- }