ThreadPool Threads; // Global object
-extern void check_time();
+// Thread constructor makes some init and launches the thread that will go to
+// sleep in idle_loop().
-namespace {
+Thread::Thread() {
- // Helpers to launch a thread after creation and joining before delete. Must be
- // outside Thread c'tor and d'tor because the object must be fully initialized
- // when start_routine (and hence virtual idle_loop) is called and when joining.
+ searching = true; // Avoid a race with start_thinking()
+ exit = resetCalls = false;
+ maxPly = callsCnt = 0;
+ history.clear();
+ counterMoves.clear();
+ idx = Threads.size(); // Starts from 0
+ std::thread::operator=(std::thread(&Thread::idle_loop, this));
+}
- template<typename T> T* new_thread() {
- std::thread* th = new T;
- *th = std::thread(&T::idle_loop, (T*)th); // Will go to sleep
- return (T*)th;
- }
- void delete_thread(ThreadBase* th) {
+// Thread destructor waits for thread termination before deleting
- th->mutex.lock();
- th->exit = true; // Search must be already finished
- th->mutex.unlock();
+Thread::~Thread() {
- th->notify_one();
- th->join(); // Wait for thread termination
- delete th;
- }
+ mutex.lock();
+ exit = true; // Search must be already finished
+ mutex.unlock();
+ notify_one();
+ std::thread::join(); // Wait for thread termination
}
-// ThreadBase::notify_one() wakes up the thread when there is some work to do
-
-void ThreadBase::notify_one() {
+// Thread::join() waits for the thread to finish searching
+void Thread::join() {
std::unique_lock<Mutex> lk(mutex);
- sleepCondition.notify_one();
+ sleepCondition.wait(lk, [&]{ return !searching; });
}
-// ThreadBase::wait() set the thread to sleep until 'condition' turns true
+// Thread::notify_one() wakes up the thread when there is some work to do
-void ThreadBase::wait(volatile const bool& condition) {
+void Thread::notify_one() {
std::unique_lock<Mutex> lk(mutex);
- sleepCondition.wait(lk, [&]{ return condition; });
+ sleepCondition.notify_one();
}
-// ThreadBase::wait_while() set the thread to sleep until 'condition' turns false
+// Thread::wait() set the thread to sleep until 'condition' turns true
-void ThreadBase::wait_while(volatile const bool& condition) {
+void Thread::wait(std::atomic_bool& condition) {
std::unique_lock<Mutex> lk(mutex);
- sleepCondition.wait(lk, [&]{ return !condition; });
-}
-
-
-// Thread c'tor makes some init but does not launch any execution thread that
-// will be started only when c'tor returns.
-
-Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in MSVC
-
- searching = false;
- maxPly = 0;
- idx = Threads.size(); // Starts from 0
-}
-
-
-// TimerThread::idle_loop() is where the timer thread waits Resolution milliseconds
-// and then calls check_time(). When not searching, thread sleeps until it's woken up.
-
-void TimerThread::idle_loop() {
-
- while (!exit)
- {
- std::unique_lock<Mutex> lk(mutex);
-
- if (!exit)
- sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX));
-
- lk.unlock();
-
- if (!exit && run)
- check_time();
- }
+ sleepCondition.wait(lk, [&]{ return bool(condition); });
}
{
std::unique_lock<Mutex> lk(mutex);
- while (!searching && !exit)
- sleepCondition.wait(lk);
+ searching = false;
- lk.unlock();
-
- if (!exit && searching)
- search();
- }
-}
-
-
-// MainThread::idle_loop() is where the main thread is parked waiting to be started
-// when there is a new search. The main thread will launch all the slave threads.
-
-void MainThread::idle_loop() {
-
- while (!exit)
- {
- std::unique_lock<Mutex> lk(mutex);
-
- thinking = false;
-
- while (!thinking && !exit)
+ while (!searching && !exit)
{
- sleepCondition.notify_one(); // Wake up the UI thread if needed
+ sleepCondition.notify_one(); // Wake up main thread if needed
sleepCondition.wait(lk);
}
lk.unlock();
- if (!exit)
- think();
+ if (!exit && searching)
+ search();
}
}
-// MainThread::join() waits for main thread to finish thinking
-
-void MainThread::join() {
-
- std::unique_lock<Mutex> lk(mutex);
- sleepCondition.wait(lk, [&]{ return !thinking; });
-}
-
-
// ThreadPool::init() is called at startup to create and launch requested threads,
-// that will go immediately to sleep. We cannot use a c'tor because Threads is a
-// static object and we need a fully initialized engine at this point due to
-// allocation of Endgames in Thread c'tor.
+// that will go immediately to sleep. We cannot use a constructor because Threads
+// is a static object and we need a fully initialized engine at this point due to
+// allocation of Endgames in the Thread constructor.
void ThreadPool::init() {
- timer = new_thread<TimerThread>();
- push_back(new_thread<MainThread>());
+ push_back(new MainThread);
read_uci_options();
}
// ThreadPool::exit() terminates the threads before the program exits. Cannot be
-// done in d'tor because threads must be terminated before freeing us.
+// done in destructor because threads must be terminated before freeing us.
void ThreadPool::exit() {
- delete_thread(timer); // As first because check_time() accesses threads data
- timer = nullptr;
-
for (Thread* th : *this)
- delete_thread(th);
+ delete th;
clear(); // Get rid of stale pointers
}
assert(requested > 0);
while (size() < requested)
- push_back(new_thread<Thread>());
+ push_back(new Thread);
while (size() > requested)
{
- delete_thread(back());
+ delete back();
pop_back();
}
}
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
StateStackPtr& states) {
- main()->join();
+ for (Thread* th : Threads)
+ th->join();
Signals.stopOnPonderhit = Signals.firstRootMove = false;
Signals.stop = Signals.failedLowAtRoot = false;
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
main()->rootMoves.push_back(RootMove(m));
- main()->thinking = true;
- main()->notify_one(); // Wake up main thread: 'thinking' must be already set
+ main()->searching = true;
+ main()->notify_one(); // Wake up main thread: 'searching' must be already set
}