X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fthread.cpp;h=786258a2ed564961fdb75f67024944aaf8c25d49;hb=3017e8c60401ecd92468a9ede3368002c4ddd440;hp=1217e3ab020656787c9dd1b8a5886fecdadcaa1a;hpb=38112060dc2da351c6dde8f12d0ee5cfaeac5084;p=stockfish diff --git a/src/thread.cpp b/src/thread.cpp index 1217e3ab..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 } @@ -165,18 +173,18 @@ 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(this)) != nullptr) + && (slave = Threads.available_slave(this)) != NULL) { sp.slavesMask.set(slave->idx); slave->activeSplitPoint = &sp; @@ -188,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() @@ -202,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; @@ -213,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(); } @@ -225,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(); @@ -245,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) { @@ -291,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); } @@ -329,11 +337,11 @@ void ThreadPool::read_uci_options() { Thread* ThreadPool::available_slave(const Thread* master) const { - for (Thread* th : *this) - if (th->available_to(master)) - return th; + for (const_iterator it = begin(); it != end(); ++it) + if ((*it)->available_to(master)) + return *it; - return nullptr; + return NULL; } @@ -341,8 +349,10 @@ Thread* ThreadPool::available_slave(const Thread* master) 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(); } @@ -363,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