2011-11-12 20:44:06 +01:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2013-05-03 19:03:42 +02:00
|
|
|
Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
#include <algorithm> // For std::count
|
2012-06-04 18:25:51 +02:00
|
|
|
#include <cassert>
|
2011-11-12 20:44:06 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-01-15 02:13:33 +01:00
|
|
|
#include "movegen.h"
|
2012-01-01 01:52:19 +01:00
|
|
|
#include "search.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "ucioption.h"
|
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
using namespace Search;
|
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
ThreadPool Threads; // Global object
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
namespace { extern "C" {
|
|
|
|
|
|
|
|
// start_routine() is the C function which is called when a new thread
|
2013-05-03 19:03:42 +02:00
|
|
|
// is launched. It is a wrapper to the virtual function idle_loop().
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
long start_routine(Thread* th) { th->idle_loop(); return 0; }
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
} }
|
|
|
|
|
|
|
|
|
|
|
|
// Thread c'tor starts a newly-created thread of execution that will call
|
2013-05-03 19:03:42 +02:00
|
|
|
// the the virtual function idle_loop(), going immediately to sleep.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
searching = exit = false;
|
|
|
|
maxPly = splitPointsSize = 0;
|
|
|
|
activeSplitPoint = NULL;
|
|
|
|
activePosition = NULL;
|
2012-06-04 18:25:51 +02:00
|
|
|
idx = Threads.size();
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
if (!thread_create(handle, start_routine, this))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to create thread number " << idx << std::endl;
|
|
|
|
::exit(EXIT_FAILURE);
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// Thread d'tor waits for thread termination before to return
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
Thread::~Thread() {
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
exit = true; // Search must be already finished
|
|
|
|
notify_one();
|
2012-06-04 18:25:51 +02:00
|
|
|
thread_join(handle); // Wait for thread termination
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// TimerThread::idle_loop() is where the timer thread waits msec milliseconds
|
|
|
|
// and then calls check_time(). If msec is 0 thread sleeps until is woken up.
|
2012-06-04 18:25:51 +02:00
|
|
|
extern void check_time();
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void TimerThread::idle_loop() {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (!exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.lock();
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
if (!exit)
|
|
|
|
sleepCondition.wait_for(mutex, msec ? msec : INT_MAX);
|
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.unlock();
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
if (msec)
|
|
|
|
check_time();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
2012-06-04 18:25:51 +02:00
|
|
|
// when there is a new search. Main thread will launch all the slave threads.
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void MainThread::idle_loop() {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.lock();
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
thinking = false;
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (!thinking && !exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2012-09-16 17:16:15 +02:00
|
|
|
Threads.sleepCondition.notify_one(); // Wake up UI thread if needed
|
|
|
|
sleepCondition.wait(mutex);
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.unlock();
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
if (exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
return;
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
searching = true;
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
Search::think();
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
assert(searching);
|
|
|
|
|
|
|
|
searching = false;
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// Thread::notify_one() wakes up the thread when there is some search to do
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void Thread::notify_one() {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.lock();
|
|
|
|
sleepCondition.notify_one();
|
|
|
|
mutex.unlock();
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// Thread::wait_for() set the thread to sleep until condition 'b' turns true
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void Thread::wait_for(volatile const bool& b) {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.lock();
|
2013-05-03 19:03:42 +02:00
|
|
|
while (!b) sleepCondition.wait(mutex);
|
2012-09-16 17:16:15 +02:00
|
|
|
mutex.unlock();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
|
|
|
// current active split point, or in some ancestor of the split point.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
bool Thread::cutoff_occurred() const {
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
2012-06-04 18:25:51 +02:00
|
|
|
if (sp->cutoff)
|
2011-11-12 20:44:06 +01:00
|
|
|
return true;
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2011-11-12 20:44:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
// Thread::is_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
|
2013-05-03 19:03:42 +02:00
|
|
|
// 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 slaves split point
|
|
|
|
// stack (the "helpful master concept" in YBWC terminology).
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
bool Thread::is_available_to(Thread* master) const {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
if (searching)
|
2011-11-12 20:44:06 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// 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.
|
2013-05-03 19:03:42 +02:00
|
|
|
int size = splitPointsSize;
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// No split points means that the thread is available as a slave for any
|
2011-11-12 20:44:06 +01:00
|
|
|
// other thread otherwise apply the "helpful master" concept if possible.
|
2013-05-03 19:03:42 +02:00
|
|
|
return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx));
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// init() is called at startup to create and launch requested threads, that will
|
|
|
|
// go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use
|
2012-06-04 18:25:51 +02:00
|
|
|
// a c'tor becuase Threads is a static object and we need a fully initialized
|
2013-05-03 19:03:42 +02:00
|
|
|
// engine at this point due to allocation of Endgames in Thread c'tor.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::init() {
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
sleepWhileIdle = true;
|
|
|
|
timer = new TimerThread();
|
|
|
|
push_back(new MainThread());
|
2012-06-04 18:25:51 +02:00
|
|
|
read_uci_options();
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// exit() cleanly terminates the threads before the program exits
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::exit() {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
delete timer; // As first because check_time() accesses threads data
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
for (iterator it = begin(); it != end(); ++it)
|
|
|
|
delete *it;
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
// read_uci_options() updates internal threads parameters from the corresponding
|
|
|
|
// UCI options and creates/destroys threads to match the requested number. Thread
|
|
|
|
// objects are dynamically allocated to avoid creating in advance all possible
|
|
|
|
// threads, with included pawns and material tables, if only few are used.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::read_uci_options() {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
|
|
|
|
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
2012-09-16 17:16:15 +02:00
|
|
|
size_t requested = Options["Threads"];
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
assert(requested > 0);
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (size() < requested)
|
|
|
|
push_back(new Thread());
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (size() > requested)
|
2011-11-12 20:44:06 +01:00
|
|
|
{
|
2013-05-03 19:03:42 +02:00
|
|
|
delete back();
|
|
|
|
pop_back();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// slave_available() tries to find an idle thread which is available as a slave
|
|
|
|
// for the thread 'master'.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
Thread* ThreadPool::available_slave(Thread* master) const {
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
|
|
|
if ((*it)->is_available_to(master))
|
|
|
|
return *it;
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
return NULL;
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-12 20:44:06 +01:00
|
|
|
// split() does the actual work of distributing the work at a node between
|
2012-01-01 01:52:19 +01:00
|
|
|
// several available threads. If it does not succeed in splitting the node
|
2013-05-03 19:03:42 +02:00
|
|
|
// (because no idle threads are available), the function immediately returns.
|
|
|
|
// If splitting is possible, a SplitPoint object is initialized with all the
|
|
|
|
// data that must be copied to the helper threads and then helper threads are
|
|
|
|
// told that they have been assigned work. This will cause them to instantly
|
|
|
|
// leave their idle loops and call search(). When all threads have returned from
|
|
|
|
// search() then split() returns.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
template <bool Fake>
|
2013-05-03 19:03:42 +02:00
|
|
|
void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bestValue,
|
|
|
|
Move* bestMove, Depth depth, Move threatMove, int moveCount,
|
|
|
|
MovePicker* movePicker, int nodeType) {
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
assert(pos.pos_is_ok());
|
2013-05-03 19:03:42 +02:00
|
|
|
assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
|
|
|
assert(*bestValue > -VALUE_INFINITE);
|
|
|
|
assert(depth >= Threads.minimumSplitDepth);
|
|
|
|
assert(searching);
|
|
|
|
assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
|
2012-01-01 01:52:19 +01:00
|
|
|
|
|
|
|
// Pick the next available split point from the split point stack
|
2013-05-03 19:03:42 +02:00
|
|
|
SplitPoint& sp = splitPoints[splitPointsSize];
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
sp.masterThread = this;
|
|
|
|
sp.parentSplitPoint = activeSplitPoint;
|
|
|
|
sp.slavesMask = 1ULL << idx;
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.depth = depth;
|
2013-05-03 19:03:42 +02:00
|
|
|
sp.bestValue = *bestValue;
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.bestMove = *bestMove;
|
|
|
|
sp.threatMove = threatMove;
|
|
|
|
sp.alpha = alpha;
|
|
|
|
sp.beta = beta;
|
|
|
|
sp.nodeType = nodeType;
|
2013-05-03 19:03:42 +02:00
|
|
|
sp.movePicker = movePicker;
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.moveCount = moveCount;
|
|
|
|
sp.pos = &pos;
|
|
|
|
sp.nodes = 0;
|
2013-05-03 19:03:42 +02:00
|
|
|
sp.cutoff = false;
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.ss = ss;
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
// Try to allocate available threads and ask them to start searching setting
|
2013-05-03 19:03:42 +02:00
|
|
|
// 'searching' flag. This must be done under lock protection to avoid concurrent
|
2012-01-01 01:52:19 +01:00
|
|
|
// allocation of the same slave by another master.
|
2013-05-03 19:03:42 +02:00
|
|
|
Threads.mutex.lock();
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.mutex.lock();
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
splitPointsSize++;
|
|
|
|
activeSplitPoint = &sp;
|
|
|
|
activePosition = NULL;
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
size_t slavesCnt = 1; // This thread is always included
|
|
|
|
Thread* slave;
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while ( (slave = Threads.available_slave(this)) != NULL
|
|
|
|
&& ++slavesCnt <= Threads.maxThreadsPerSplitPoint && !Fake)
|
|
|
|
{
|
|
|
|
sp.slavesMask |= 1ULL << slave->idx;
|
|
|
|
slave->activeSplitPoint = &sp;
|
|
|
|
slave->searching = true; // Slave leaves idle_loop()
|
|
|
|
slave->notify_one(); // Could be sleeping
|
|
|
|
}
|
2012-01-01 01:52:19 +01:00
|
|
|
|
|
|
|
// Everything is set up. The master thread enters the idle loop, from which
|
2013-05-03 19:03:42 +02:00
|
|
|
// it will instantly launch a search, because its 'searching' flag is set.
|
2012-09-16 17:16:15 +02:00
|
|
|
// The thread will return from the idle loop when all slaves have finished
|
2012-01-01 01:52:19 +01:00
|
|
|
// their work at this split point.
|
2013-05-03 19:03:42 +02:00
|
|
|
if (slavesCnt > 1 || Fake)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2013-05-03 19:03:42 +02:00
|
|
|
sp.mutex.unlock();
|
|
|
|
Threads.mutex.unlock();
|
|
|
|
|
|
|
|
Thread::idle_loop(); // Force a call to base class idle_loop()
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
// 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.
|
2013-05-03 19:03:42 +02:00
|
|
|
assert(!searching);
|
|
|
|
assert(!activePosition);
|
|
|
|
|
|
|
|
// We have returned from the idle loop, which means that all threads are
|
|
|
|
// finished. Note that setting 'searching' and decreasing splitPointsSize is
|
|
|
|
// done under lock protection to avoid a race with Thread::is_available_to().
|
|
|
|
Threads.mutex.lock();
|
|
|
|
sp.mutex.lock();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
searching = true;
|
|
|
|
splitPointsSize--;
|
|
|
|
activeSplitPoint = sp.parentSplitPoint;
|
|
|
|
activePosition = &pos;
|
2012-09-16 17:16:15 +02:00
|
|
|
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
|
|
|
*bestMove = sp.bestMove;
|
2013-05-03 19:03:42 +02:00
|
|
|
*bestValue = sp.bestValue;
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
sp.mutex.unlock();
|
2013-05-03 19:03:42 +02:00
|
|
|
Threads.mutex.unlock();
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit template instantiations
|
2013-05-03 19:03:42 +02:00
|
|
|
template void Thread::split<false>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int);
|
|
|
|
template void Thread::split< true>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int);
|
2012-01-01 01:52:19 +01:00
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void ThreadPool::wait_for_think_finished() {
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
MainThread* t = main_thread();
|
2012-09-16 17:16:15 +02:00
|
|
|
t->mutex.lock();
|
2013-05-03 19:03:42 +02:00
|
|
|
while (t->thinking) sleepCondition.wait(t->mutex);
|
2012-09-16 17:16:15 +02:00
|
|
|
t->mutex.unlock();
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// start_thinking() wakes up the main thread sleeping in MainThread::idle_loop()
|
|
|
|
// so to start a new search, then returns immediately.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
|
|
|
const std::vector<Move>& searchMoves, StateStackPtr& states) {
|
|
|
|
wait_for_think_finished();
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
SearchTime = Time::now(); // As early as possible
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
|
|
|
Signals.stop = Signals.failedLowAtRoot = false;
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
RootPos = pos;
|
2012-01-01 01:52:19 +01:00
|
|
|
Limits = limits;
|
2012-09-16 17:16:15 +02:00
|
|
|
SetupStates = states; // Ownership transfer here
|
2012-01-15 02:13:33 +01:00
|
|
|
RootMoves.clear();
|
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
2013-05-03 19:03:42 +02:00
|
|
|
if ( searchMoves.empty()
|
|
|
|
|| std::count(searchMoves.begin(), searchMoves.end(), ml.move()))
|
2012-01-15 02:13:33 +01:00
|
|
|
RootMoves.push_back(RootMove(ml.move()));
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
main_thread()->thinking = true;
|
|
|
|
main_thread()->notify_one(); // Starts main thread
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|