- // 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().
- sp.mutex.lock(); // To protect sp.nodes
- mutex.lock();
-
- master->is_searching = true;
- master->splitPointsCnt--;
- master->curSplitPoint = sp.parent;
- pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
- *bestMove = sp.bestMove;
-
- mutex.unlock();
- sp.mutex.unlock();
-
- return sp.bestValue;
-}
-
-// Explicit template instantiations
-template Value ThreadPool::split<false>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
-template Value ThreadPool::split<true>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
-
-
-// set_timer() is used to set the timer to trigger after msec milliseconds.
-// If msec is 0 then timer is stopped.
-
-void ThreadPool::set_timer(int msec) {
-
- timer->mutex.lock();
- timer->maxPly = msec;
- timer->sleepCondition.notify_one(); // Wake up and restart the timer
- timer->mutex.unlock();
-}
-
-
-// wait_for_search_finished() waits for main thread to go to sleep, this means
-// search is finished. Then returns.
-
-void ThreadPool::wait_for_search_finished() {
-
- Thread* t = main_thread();
- t->mutex.lock();
- t->sleepCondition.notify_one(); // In case is waiting for stop or ponderhit
- while (!t->do_sleep) sleepCondition.wait(t->mutex);
- t->mutex.unlock();
-}
-
-
-// start_searching() wakes up the main thread sleeping in main_loop() so to start
-// a new search, then returns immediately.
-
-void ThreadPool::start_searching(const Position& pos, const LimitsType& limits,
- const std::vector<Move>& searchMoves, StateStackPtr& states) {
- wait_for_search_finished();
-
- SearchTime = Time::current_time(); // As early as possible
-
- Signals.stopOnPonderhit = Signals.firstRootMove = false;
- Signals.stop = Signals.failedLowAtRoot = false;
-
- RootPosition = pos;
- Limits = limits;
- SetupStates = states; // Ownership transfer here
- RootMoves.clear();
-
- for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
- if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
- RootMoves.push_back(RootMove(ml.move()));