+template Value ThreadsManager::split<false>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
+template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
+
+
+// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
+// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
+extern void do_timer_event();
+
+void Thread::timer_loop() {
+
+ while (!do_terminate)
+ {
+ lock_grab(&sleepLock);
+ timed_wait(&sleepCond, &sleepLock, maxPly ? maxPly : INT_MAX);
+ lock_release(&sleepLock);
+ do_timer_event();
+ }
+}
+
+
+// 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) {
+
+ Thread& timer = threads[MAX_THREADS];
+
+ lock_grab(&timer.sleepLock);
+ timer.maxPly = msec;
+ cond_signal(&timer.sleepCond); // Wake up and restart the timer
+ lock_release(&timer.sleepLock);
+}
+
+
+// Thread::main_loop() is where the main thread is parked waiting to be started
+// when there is a new search. Main thread will launch all the slave threads.
+
+void Thread::main_loop() {
+
+ while (true)
+ {
+ lock_grab(&sleepLock);
+
+ do_sleep = true; // Always return to sleep after a search
+ is_searching = false;
+
+ while (do_sleep && !do_terminate)
+ {
+ cond_signal(&Threads.sleepCond); // Wake up UI thread if needed
+ cond_wait(&sleepCond, &sleepLock);
+ }
+
+ is_searching = true;
+
+ lock_release(&sleepLock);
+
+ if (do_terminate)
+ return;
+
+ think(); // This is the search entry point
+ }
+}
+
+
+// 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::vector<Move>& searchMoves, bool asyncMode) {
+ 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;
+ SearchMoves = searchMoves;
+
+ // Reset signals before to start the new search
+ memset((void*)&Signals, 0, sizeof(Signals));
+
+ main.do_sleep = false;
+ cond_signal(&main.sleepCond); // Wake up main thread and start searching
+
+ if (!asyncMode)
+ cond_wait(&sleepCond, &main.sleepLock);
+
+ lock_release(&main.sleepLock);
+}
+
+
+// ThreadsManager::wait_for_stop_or_ponderhit() is called when the maximum depth
+// is reached while the program is pondering. The point is to work around a wrinkle
+// in the UCI protocol: When pondering, the engine is not allowed to give a
+// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
+// wait here until one of these commands (that raise StopRequest) is sent and
+// then return, after which the bestmove and pondermove will be printed.
+
+void ThreadsManager::wait_for_stop_or_ponderhit() {
+
+ Signals.stopOnPonderhit = true;
+
+ Thread& main = threads[0];
+
+ lock_grab(&main.sleepLock);
+
+ while (!Signals.stop)
+ cond_wait(&main.sleepCond, &main.sleepLock);
+
+ lock_release(&main.sleepLock);
+}