X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fthread.cpp;h=fc938eb10216a6872fd043555a9edfaf2cadd9b6;hp=0fbcc1fc65795395d7678c297da49267cee1ff10;hb=4e619a13d6671db198bc9c39d54dd7f243ef5873;hpb=dc7fd868f4fa41251a9521a0b25e3adb483bfd83 diff --git a/src/thread.cpp b/src/thread.cpp index 0fbcc1fc..fc938eb1 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -213,7 +213,7 @@ void ThreadPool::init() { ThreadPool::~ThreadPool() { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) delete threads[i]; delete timer; @@ -232,7 +232,7 @@ void ThreadPool::read_uci_options() { maxThreadsPerSplitPoint = Options["Max Threads per Split Point"]; minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY; useSleepingThreads = Options["Use Sleeping Threads"]; - int requested = Options["Threads"]; + size_t requested = Options["Threads"]; assert(requested > 0); @@ -253,7 +253,7 @@ void ThreadPool::read_uci_options() { void ThreadPool::wake_up() const { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) { threads[i]->maxPly = 0; threads[i]->do_sleep = false; @@ -269,7 +269,7 @@ void ThreadPool::wake_up() const { void ThreadPool::sleep() const { - for (int i = 1; i < size(); i++) // Main thread will go to sleep by itself + for (size_t i = 1; i < size(); i++) // Main thread will go to sleep by itself threads[i]->do_sleep = true; // to avoid a race with start_searching() } @@ -279,7 +279,7 @@ void ThreadPool::sleep() const { bool ThreadPool::available_slave_exists(Thread* master) const { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) if (threads[i]->is_available_to(master)) return true; @@ -314,41 +314,41 @@ Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta, return bestValue; // Pick the next available split point from the split point stack - SplitPoint* sp = &master->splitPoints[master->splitPointsCnt]; - - sp->parent = master->curSplitPoint; - sp->master = master; - sp->cutoff = false; - sp->slavesMask = 1ULL << master->idx; - sp->depth = depth; - sp->bestMove = *bestMove; - sp->threatMove = threatMove; - sp->alpha = alpha; - sp->beta = beta; - sp->nodeType = nodeType; - sp->bestValue = bestValue; - sp->mp = mp; - sp->moveCount = moveCount; - sp->pos = &pos; - sp->nodes = 0; - sp->ss = ss; + SplitPoint& sp = master->splitPoints[master->splitPointsCnt]; + + sp.parent = master->curSplitPoint; + sp.master = master; + sp.cutoff = false; + sp.slavesMask = 1ULL << master->idx; + sp.depth = depth; + sp.bestMove = *bestMove; + sp.threatMove = threatMove; + sp.alpha = alpha; + sp.beta = beta; + sp.nodeType = nodeType; + sp.bestValue = bestValue; + sp.mp = mp; + sp.moveCount = moveCount; + sp.pos = &pos; + sp.nodes = 0; + sp.ss = ss; assert(master->is_searching); - master->curSplitPoint = sp; + master->curSplitPoint = &sp; int slavesCnt = 0; // Try to allocate available threads and ask them to start searching setting // is_searching flag. This must be done under lock protection to avoid concurrent // allocation of the same slave by another master. - lock_grab(sp->lock); + lock_grab(sp.lock); lock_grab(splitLock); - for (int i = 0; i < size() && !Fake; ++i) + for (size_t i = 0; i < size() && !Fake; ++i) if (threads[i]->is_available_to(master)) { - sp->slavesMask |= 1ULL << i; - threads[i]->curSplitPoint = sp; + sp.slavesMask |= 1ULL << i; + threads[i]->curSplitPoint = &sp; threads[i]->is_searching = true; // Slave leaves idle_loop() if (useSleepingThreads) @@ -361,16 +361,15 @@ Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta, master->splitPointsCnt++; lock_release(splitLock); - lock_release(sp->lock); + lock_release(sp.lock); // Everything is set up. The master thread enters the idle loop, from which // it will instantly launch a search, because its is_searching flag is set. - // We pass the split point as a parameter to the idle loop, which means that - // the thread will return from the idle loop when all slaves have finished + // The thread will return from the idle loop when all slaves have finished // their work at this split point. if (slavesCnt || Fake) { - master->idle_loop(sp); + master->idle_loop(); // In helpful master concept a master can help only a sub-tree of its split // point, and because here is all finished is not possible master is booked. @@ -380,19 +379,19 @@ Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta, // We have returned from the idle loop, which means that all threads are // finished. Note that setting is_searching and decreasing splitPointsCnt is // done under lock protection to avoid a race with Thread::is_available_to(). - lock_grab(sp->lock); // To protect sp->nodes + lock_grab(sp.lock); // To protect sp.nodes lock_grab(splitLock); master->is_searching = true; master->splitPointsCnt--; - master->curSplitPoint = sp->parent; - pos.set_nodes_searched(pos.nodes_searched() + sp->nodes); - *bestMove = sp->bestMove; + master->curSplitPoint = sp.parent; + pos.set_nodes_searched(pos.nodes_searched() + sp.nodes); + *bestMove = sp.bestMove; lock_release(splitLock); - lock_release(sp->lock); + lock_release(sp.lock); - return sp->bestValue; + return sp.bestValue; } // Explicit template instantiations @@ -429,7 +428,7 @@ void ThreadPool::wait_for_search_finished() { // a new search, then returns immediately. void ThreadPool::start_searching(const Position& pos, const LimitsType& limits, - const std::vector& searchMoves) { + const std::vector& searchMoves) { wait_for_search_finished(); SearchTime.restart(); // As early as possible