X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fthread.cpp;h=786258a2ed564961fdb75f67024944aaf8c25d49;hp=ff52576b062785c49f8eae6bfa360b0380dbae71;hb=856a5f3aaaf8b9d53599963decacd4476b55c034;hpb=6fa6da3ee13d2b7bdbec3cd24ff8ca43233c74fb diff --git a/src/thread.cpp b/src/thread.cpp index ff52576b..786258a2 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -33,13 +33,19 @@ extern void check_time(); namespace { + // start_routine() is the C function which is called when a new thread + // is launched. It is a wrapper to the virtual function idle_loop(). + + extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } } + + // 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. template T* new_thread() { T* th = new T(); - th->nativeThread = std::thread(&ThreadBase::idle_loop, th); // Will go to sleep + thread_create(th->handle, start_routine, th); // Will go to sleep return th; } @@ -50,7 +56,7 @@ namespace { th->mutex.unlock(); th->notify_one(); - th->nativeThread.join(); // Wait for thread termination + thread_join(th->handle); // Wait for thread termination delete th; } @@ -61,8 +67,9 @@ namespace { void ThreadBase::notify_one() { - std::unique_lock(this->mutex); + mutex.lock(); sleepCondition.notify_one(); + mutex.unlock(); } @@ -70,8 +77,9 @@ void ThreadBase::notify_one() { void ThreadBase::wait_for(volatile const bool& condition) { - std::unique_lock lk(mutex); - sleepCondition.wait(lk, [&]{ return condition; }); + mutex.lock(); + while (!condition) sleepCondition.wait(mutex); + mutex.unlock(); } @@ -83,8 +91,8 @@ Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in searching = false; maxPly = 0; splitPointsSize = 0; - activeSplitPoint = nullptr; - activePosition = nullptr; + activeSplitPoint = NULL; + activePosition = NULL; idx = Threads.size(); // Starts from 0 } @@ -102,13 +110,14 @@ bool Thread::cutoff_occurred() const { } -// Thread::can_join() checks whether the thread is available to join the split -// point 'sp'. An obvious requirement is that thread must be idle. With more than -// two threads, this is not sufficient: If the thread is the master of some split -// point, it is only available as a slave for the split points below his active -// one (the "helpful master" concept in YBWC terminology). +// Thread::available_to() checks whether the thread is available to help the +// thread 'master' at a split point. An obvious requirement is that thread must +// be idle. With more than two threads, this is not sufficient: If the thread is +// the master of some split point, it is only available as a slave to the slaves +// which are busy searching the split point at the top of slave's split point +// stack (the "helpful master concept" in YBWC terminology). -bool Thread::can_join(const SplitPoint* sp) const { +bool Thread::available_to(const Thread* master) const { if (searching) return false; @@ -119,7 +128,7 @@ bool Thread::can_join(const SplitPoint* sp) const { // No split points means that the thread is available as a slave for any // other thread otherwise apply the "helpful master" concept if possible. - return !size || splitPoints[size - 1].slavesMask.test(sp->master->idx); + return !size || splitPoints[size - 1].slavesMask.test(master->idx); } @@ -164,21 +173,21 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes // Try to allocate available threads and ask them to start searching setting // 'searching' flag. This must be done under lock protection to avoid concurrent // allocation of the same slave by another master. - Threads.spinlock.acquire(); - sp.spinlock.acquire(); + Threads.mutex.lock(); + sp.mutex.lock(); sp.allSlavesSearching = true; // Must be set under lock protection ++splitPointsSize; activeSplitPoint = &sp; - activePosition = nullptr; + activePosition = NULL; Thread* slave; while ( sp.slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT - && (slave = Threads.available_slave(activeSplitPoint)) != nullptr) + && (slave = Threads.available_slave(this)) != NULL) { sp.slavesMask.set(slave->idx); - slave->activeSplitPoint = activeSplitPoint; + slave->activeSplitPoint = &sp; slave->searching = true; // Slave leaves idle_loop() slave->notify_one(); // Could be sleeping } @@ -187,8 +196,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes // it will instantly launch a search, because its 'searching' flag is set. // The thread will return from the idle loop when all slaves have finished // their work at this split point. - sp.spinlock.release(); - Threads.spinlock.release(); + sp.mutex.unlock(); + Threads.mutex.unlock(); Thread::idle_loop(); // Force a call to base class idle_loop() @@ -201,8 +210,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes // We have returned from the idle loop, which means that all threads are // finished. Note that setting 'searching' and decreasing splitPointsSize must // be done under lock protection to avoid a race with Thread::available_to(). - Threads.spinlock.acquire(); - sp.spinlock.acquire(); + Threads.mutex.lock(); + sp.mutex.lock(); searching = true; --splitPointsSize; @@ -212,8 +221,8 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes *bestMove = sp.bestMove; *bestValue = sp.bestValue; - sp.spinlock.release(); - Threads.spinlock.release(); + sp.mutex.unlock(); + Threads.mutex.unlock(); } @@ -224,12 +233,12 @@ void TimerThread::idle_loop() { while (!exit) { - std::unique_lock lk(mutex); + mutex.lock(); if (!exit) - sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX)); + sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX); - lk.unlock(); + mutex.unlock(); if (run) check_time(); @@ -244,17 +253,17 @@ void MainThread::idle_loop() { while (!exit) { - std::unique_lock lk(mutex); + mutex.lock(); thinking = false; while (!thinking && !exit) { Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed - sleepCondition.wait(lk); + sleepCondition.wait(mutex); } - lk.unlock(); + mutex.unlock(); if (!exit) { @@ -290,8 +299,8 @@ void ThreadPool::exit() { delete_thread(timer); // As first because check_time() accesses threads data - for (Thread* th : *this) - delete_thread(th); + for (iterator it = begin(); it != end(); ++it) + delete_thread(*it); } @@ -324,15 +333,15 @@ void ThreadPool::read_uci_options() { // ThreadPool::available_slave() tries to find an idle thread which is available -// to join SplitPoint 'sp'. +// as a slave for the thread 'master'. -Thread* ThreadPool::available_slave(const SplitPoint* sp) const { +Thread* ThreadPool::available_slave(const Thread* master) const { - for (Thread* th : *this) - if (th->can_join(sp)) - return th; + for (const_iterator it = begin(); it != end(); ++it) + if ((*it)->available_to(master)) + return *it; - return nullptr; + return NULL; } @@ -340,8 +349,10 @@ Thread* ThreadPool::available_slave(const SplitPoint* sp) const { void ThreadPool::wait_for_think_finished() { - std::unique_lock lk(main()->mutex); - sleepCondition.wait(lk, [&]{ return !main()->thinking; }); + MainThread* th = main(); + th->mutex.lock(); + while (th->thinking) sleepCondition.wait(th->mutex); + th->mutex.unlock(); } @@ -352,7 +363,7 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, StateStackPtr& states) { wait_for_think_finished(); - SearchTime = now(); // As early as possible + SearchTime = Time::now(); // As early as possible Signals.stopOnPonderhit = Signals.firstRootMove = false; Signals.stop = Signals.failedLowAtRoot = false; @@ -362,14 +373,14 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, Limits = limits; if (states.get()) // If we don't set a new position, preserve current state { - SetupStates = std::move(states); // Ownership transfer here + SetupStates = states; // Ownership transfer here assert(!states.get()); } - for (const auto& m : MoveList(pos)) + for (MoveList it(pos); *it; ++it) if ( limits.searchmoves.empty() - || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) - RootMoves.push_back(RootMove(m)); + || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it)) + RootMoves.push_back(RootMove(*it)); main()->thinking = true; main()->notify_one(); // Starts main thread