- // We have returned from the idle loop, which means that all threads are
- // finished. Note that setting is_searching and decreasing splitPointsCnt is
- // done under lock protection to avoid a race with Thread::is_available_to().
- lock_grab(sp->lock); // To protect sp->nodes
- lock_grab(splitLock);
-
- masterThread.is_searching = true;
- masterThread.splitPointsCnt--;
- masterThread.curSplitPoint = sp->parent;
- pos.set_nodes_searched(pos.nodes_searched() + sp->nodes);
- *bestMove = sp->bestMove;
-
- lock_release(splitLock);
- lock_release(sp->lock);
-
- return sp->bestValue;
-}
-
-// Explicit template instantiations
-template Value ThreadsManager::split<false>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
-template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
-
-
-// ThreadsManager::set_timer() is used to set the timer to trigger after msec
-// milliseconds. If msec is 0 then timer is stopped.
-
-void ThreadsManager::set_timer(int msec) {
-
- lock_grab(timer->sleepLock);
- timer->maxPly = msec;
- cond_signal(timer->sleepCond); // Wake up and restart the timer
- lock_release(timer->sleepLock);
-}
-
-
-// ThreadsManager::start_thinking() is used by UI thread to wake up the main
-// thread parked in main_loop() and starting a new search. If asyncMode is true
-// then function returns immediately, otherwise caller is blocked waiting for
-// the search to finish.
-
-void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits,
- const std::set<Move>& searchMoves, bool async) {
- Thread& main = *threads[0];
-
- lock_grab(main.sleepLock);
-
- // Wait main thread has finished before to launch a new search
- while (!main.do_sleep)
- cond_wait(sleepCond, main.sleepLock);
-
- // Copy input arguments to initialize the search
- RootPosition.copy(pos, 0);
- Limits = limits;
- RootMoves.clear();
-
- // Populate RootMoves with all the legal moves (default) or, if a searchMoves
- // set is given, with the subset of legal moves to search.
- for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
- if (searchMoves.empty() || searchMoves.count(ml.move()))
- RootMoves.push_back(RootMove(ml.move()));
-
- // Reset signals before to start the new search
- Signals.stopOnPonderhit = Signals.firstRootMove = false;
- Signals.stop = Signals.failedLowAtRoot = false;
-
- main.do_sleep = false;
- cond_signal(main.sleepCond); // Wake up main thread and start searching
-
- if (!async)
- while (!main.do_sleep)
- cond_wait(sleepCond, main.sleepLock);
-
- lock_release(main.sleepLock);
-}
-
-
-// ThreadsManager::stop_thinking() is used by UI thread to raise a stop request
-// and to wait for the main thread finishing the search. Needed to wait exiting
-// and terminate the threads after a 'quit' command.
-
-void ThreadsManager::stop_thinking() {
-
- Thread& main = *threads[0];
-
- Search::Signals.stop = true;
-
- lock_grab(main.sleepLock);
-
- cond_signal(main.sleepCond); // In case is waiting for stop or ponderhit
-
- while (!main.do_sleep)
- cond_wait(sleepCond, main.sleepLock);