- // idle_loop() is where the threads are parked when they have no work to do.
- // The parameter 'sp', if non-NULL, is a pointer to an active SplitPoint
- // object for which the current thread is the master.
-
- void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
-
- assert(threadID >= 0 && threadID < MAX_THREADS);
-
- int i;
- bool allFinished = false;
-
- while (true)
- {
- // Slave threads can exit as soon as AllThreadsShouldExit raises,
- // master should exit as last one.
- if (allThreadsShouldExit)
- {
- assert(!sp);
- threads[threadID].state = THREAD_TERMINATED;
- return;
- }
-
- // If we are not thinking, wait for a condition to be signaled
- // instead of wasting CPU time polling for work.
- while ( threadID >= activeThreads || threads[threadID].state == THREAD_INITIALIZING
- || (useSleepingThreads && threads[threadID].state == THREAD_AVAILABLE))
- {
- assert(!sp || useSleepingThreads);
- assert(threadID != 0 || useSleepingThreads);
-
- if (threads[threadID].state == THREAD_INITIALIZING)
- threads[threadID].state = THREAD_AVAILABLE;
-
- // Grab the lock to avoid races with wake_sleeping_thread()
- lock_grab(&sleepLock[threadID]);
-
- // If we are master and all slaves have finished do not go to sleep
- for (i = 0; sp && i < activeThreads && !sp->slaves[i]; i++) {}
- allFinished = (i == activeThreads);
-
- if (allFinished || allThreadsShouldExit)
- {
- lock_release(&sleepLock[threadID]);
- break;
- }
-
- // Do sleep here after retesting sleep conditions
- if (threadID >= activeThreads || threads[threadID].state == THREAD_AVAILABLE)
- cond_wait(&sleepCond[threadID], &sleepLock[threadID]);
-
- lock_release(&sleepLock[threadID]);
- }
-
- // If this thread has been assigned work, launch a search
- if (threads[threadID].state == THREAD_WORKISWAITING)
- {
- assert(!allThreadsShouldExit);
-
- threads[threadID].state = THREAD_SEARCHING;
-
- // Copy SplitPoint position and search stack and call search()
- // with SplitPoint template parameter set to true.
- SearchStack ss[PLY_MAX_PLUS_2];
- SplitPoint* tsp = threads[threadID].splitPoint;
- Position pos(*tsp->pos, threadID);
-
- memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
- (ss+1)->sp = tsp;
-
- if (tsp->pvNode)
- search<PV, true, false>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth, tsp->ply);
- else
- search<NonPV, true, false>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth, tsp->ply);
-
- assert(threads[threadID].state == THREAD_SEARCHING);
-
- threads[threadID].state = THREAD_AVAILABLE;
-
- // Wake up master thread so to allow it to return from the idle loop in
- // case we are the last slave of the split point.
- if (useSleepingThreads && threadID != tsp->master && threads[tsp->master].state == THREAD_AVAILABLE)
- wake_sleeping_thread(tsp->master);
- }
-
- // If this thread is the master of a split point and all slaves have
- // finished their work at this split point, return from the idle loop.
- for (i = 0; sp && i < activeThreads && !sp->slaves[i]; i++) {}
- allFinished = (i == activeThreads);
-
- if (allFinished)
- {
- // Because sp->slaves[] is reset under lock protection,
- // be sure sp->lock has been released before to return.
- lock_grab(&(sp->lock));
- lock_release(&(sp->lock));
-
- // In helpful master concept a master can help only a sub-tree, and
- // because here is all finished is not possible master is booked.
- assert(threads[threadID].state == THREAD_AVAILABLE);