-// Explicit template instantiations
-template Value ThreadsManager::split<false>(Position&, SearchStack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
-template Value ThreadsManager::split<true>(Position&, SearchStack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
+
+// ThreadsManager::wait_for_search_finished() waits for main thread to go to
+// sleep, this means search is finished. Then returns.
+
+void ThreadsManager::wait_for_search_finished() {
+
+ Thread* t = main_thread();
+ lock_grab(t->sleepLock);
+ cond_signal(t->sleepCond); // In case is waiting for stop or ponderhit
+ while (!t->do_sleep) cond_wait(sleepCond, t->sleepLock);
+ lock_release(t->sleepLock);
+}
+
+
+// ThreadsManager::start_searching() wakes up the main thread sleeping in
+// main_loop() so to start a new search, then returns immediately.
+
+void ThreadsManager::start_searching(const Position& pos, const LimitsType& limits,
+ const std::vector<Move>& searchMoves) {
+ wait_for_search_finished();
+
+ SearchTime.restart(); // As early as possible
+
+ Signals.stopOnPonderhit = Signals.firstRootMove = false;
+ Signals.stop = Signals.failedLowAtRoot = false;
+
+ RootPosition = pos;
+ Limits = limits;
+ RootMoves.clear();
+
+ for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+ if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
+ RootMoves.push_back(RootMove(ml.move()));
+
+ main_thread()->do_sleep = false;
+ main_thread()->wake_up();
+}