X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fthread.cpp;h=c76b4b707e9f97dfe641670de06021b982481287;hp=88b4592143950c8ca2077b080d1bf1248c044396;hb=9c9205860c5ab0e4f3180298e3f7082be259772c;hpb=ecc5ff6693f116f4a8ae5f5080252f29b279c0a1 diff --git a/src/thread.cpp b/src/thread.cpp index 88b45921..c76b4b70 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -29,12 +29,10 @@ using namespace Search; ThreadPool Threads; // Global object -extern void check_time(); - namespace { - // 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 + // Helpers to launch a thread after creation and joining before delete. Outside the + // Thread constructor and destructor because the object must be fully initialized // when start_routine (and hence virtual idle_loop) is called and when joining. template T* new_thread() { @@ -68,53 +66,34 @@ void ThreadBase::notify_one() { // ThreadBase::wait() set the thread to sleep until 'condition' turns true -void ThreadBase::wait(volatile const bool& condition) { +void ThreadBase::wait(std::atomic_bool& condition) { std::unique_lock lk(mutex); - sleepCondition.wait(lk, [&]{ return condition; }); + sleepCondition.wait(lk, [&]{ return bool(condition); }); } // ThreadBase::wait_while() set the thread to sleep until 'condition' turns false - -void ThreadBase::wait_while(volatile const bool& condition) { +void ThreadBase::wait_while(std::atomic_bool& condition) { std::unique_lock 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 constructor makes some init but does not launch any execution thread, +// which will be started only when the constructor returns. -Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in MSVC +Thread::Thread() { - searching = false; - maxPly = 0; + searching = resetCallsCnt = false; + maxPly = callsCnt = 0; + history.clear(); + counterMoves.clear(); 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 lk(mutex); - - if (!exit) - sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX)); - - lk.unlock(); - - if (!exit && run) - check_time(); - } -} - - // Thread::idle_loop() is where the thread is parked when it has no work to do void Thread::idle_loop() { @@ -169,26 +148,22 @@ void MainThread::join() { // 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(); push_back(new_thread()); 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);