- 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 = NULL;
- 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->masterThread
- && sp->slavesMask.none())
- {
- assert(!sp->masterThread->searching);
- sp->masterThread->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->mutex.unlock();
-
- // Try to late join to another split point if none of its slaves has
- // already finished.
- if (Threads.size() > 2)
- for (size_t i = 0; i < Threads.size(); ++i)
- {
- const int size = Threads[i]->splitPointsSize; // Local copy
- sp = size ? &Threads[i]->splitPoints[size - 1] : NULL;
-
- if ( sp
- && sp->allSlavesSearching
- && available_to(Threads[i]))
- {
- // Recheck the conditions under lock protection
- Threads.mutex.lock();
- sp->mutex.lock();
-
- if ( sp->allSlavesSearching
- && available_to(Threads[i]))
- {
- sp->slavesMask.set(idx);
- activeSplitPoint = sp;
- searching = true;
- }
-
- sp->mutex.unlock();
- Threads.mutex.unlock();
-
- break; // Just a single attempt
- }
- }
- }
-
- // 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.
- if (this_sp && this_sp->slavesMask.none())
- {
- this_sp->mutex.lock();
- bool finished = this_sp->slavesMask.none(); // Retest under lock protection
- this_sp->mutex.unlock();
- if (finished)
- return;
- }