// and last thread are special. First one is the main search thread while the
// last one mimics a timer, they run in main_loop() and timer_loop().
// and last thread are special. First one is the main search thread while the
// last one mimics a timer, they run in main_loop() and timer_loop().
// Make a local copy to be sure doesn't become zero under our feet while
// testing next condition and so leading to an out of bound access.
// Make a local copy to be sure doesn't become zero under our feet while
// testing next condition and so leading to an out of bound access.
// No active split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible.
// No active split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible.
-#if defined(_MSC_VER)
- threads[i].handle = CreateThread(NULL, 0, start_routine, &threads[i], 0, NULL);
- bool ok = (threads[i].handle != NULL);
-#else
- bool ok = !pthread_create(&threads[i].handle, NULL, start_routine, &threads[i]);
-#endif
-
- if (!ok)
+ if (!thread_create(threads[i].handle, start_routine, threads[i]))
- // Wait for thread termination
-#if defined(_MSC_VER)
- WaitForSingleObject(threads[i].handle, 0);
- CloseHandle(threads[i].handle);
-#else
- pthread_join(threads[i].handle, NULL);
-#endif
+ thread_join(threads[i].handle); // Wait for thread termination
- // Now we can safely destroy associated locks and wait conditions
- lock_destroy(&threads[i].sleepLock);
- cond_destroy(&threads[i].sleepCond);
+ lock_destroy(threads[i].sleepLock);
+ cond_destroy(threads[i].sleepCond);
-// split_point_finished() checks if all the slave threads of a given split
-// point have finished searching.
-
-bool ThreadsManager::split_point_finished(SplitPoint* sp) const {
-
- for (int i = 0; i < activeThreads; i++)
- if (sp->is_slave[i])
- return false;
-
- return true;
-}
-
-
// split() does the actual work of distributing the work at a node between
// several available threads. If it does not succeed in splitting the node
// (because no idle threads are available, or because we have no unused split
// split() does the actual work of distributing the work at a node between
// several available threads. If it does not succeed in splitting the node
// (because no idle threads are available, or because we have no unused split
if (masterThread.activeSplitPoints >= MAX_ACTIVE_SPLIT_POINTS)
return bestValue;
// Pick the next available split point from the split point stack
SplitPoint* sp = &masterThread.splitPoints[masterThread.activeSplitPoints];
if (masterThread.activeSplitPoints >= MAX_ACTIVE_SPLIT_POINTS)
return bestValue;
// Pick the next available split point from the split point stack
SplitPoint* sp = &masterThread.splitPoints[masterThread.activeSplitPoints];
// 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.
// 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_release(&threadsLock);
-
- // We failed to allocate even one slave, return
- if (!Fake && workersCnt == 1)
- return bestValue;
+ if (++slavesCnt + 1 >= maxThreadsPerSplitPoint) // Master is always included
+ break;
+ }
// 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
// their work at this split point.
// 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
// their work at this split point.
- masterThread.idle_loop(sp);
-
- // 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.
- assert(!masterThread.is_searching);
+ if (slavesCnt || Fake)
+ masterThread.idle_loop(sp);
- // finished. Note that changing state and decreasing activeSplitPoints is done
- // under lock protection to avoid a race with Thread::is_available_to().
- lock_grab(&threadsLock);
+ // finished. Note that setting is_searching and decreasing activeSplitPoints is
+ // done under lock protection to avoid a race with Thread::is_available_to().
+ lock_grab(splitLock);
+ lock_grab(sp->lock); // To protect sp->nodes
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
- lock_grab(&sleepLock);
- timed_wait(&sleepCond, &sleepLock, maxPly ? maxPly : INT_MAX);
- lock_release(&sleepLock);
- do_timer_event();
+ lock_grab(sleepLock);
+ timed_wait(sleepCond, sleepLock, maxPly ? maxPly : INT_MAX);
+ lock_release(sleepLock);
+ check_time();
- cond_signal(&Threads.sleepCond); // Wake up UI thread if needed
- cond_wait(&sleepCond, &sleepLock);
+ cond_signal(Threads.sleepCond); // Wake up UI thread if needed
+ cond_wait(sleepCond, sleepLock);
// Copy input arguments to initialize the search
RootPosition.copy(pos, 0);
Limits = limits;
// Copy input arguments to initialize the search
RootPosition.copy(pos, 0);
Limits = limits;
- SearchMoves = searchMoves;
+ RootMoves.clear();
+
+ // Populate RootMoves with all the legal moves (default) or, if a searchMoves
+ // set is given, with the subset of legal moves to search.
+ for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+ if (searchMoves.empty() || searchMoves.count(ml.move()))
+ RootMoves.push_back(RootMove(ml.move()));