// Thread c'tor starts a newly-created thread of execution that will call
// the the virtual function idle_loop(), going immediately to sleep.
-Thread::Thread() : splitPoints() {
+Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
searching = exit = false;
maxPly = splitPointsSize = 0;
activeSplitPoint = NULL;
+ activePosition = NULL;
idx = Threads.size();
if (!thread_create(handle, start_routine, this))
sleepWhileIdle = true;
timer = new TimerThread();
- threads.push_back(new MainThread());
+ push_back(new MainThread());
read_uci_options();
}
delete timer; // As first because check_time() accesses threads data
- for (size_t i = 0; i < threads.size(); i++)
- delete threads[i];
+ for (iterator it = begin(); it != end(); ++it)
+ delete *it;
}
assert(requested > 0);
- while (threads.size() < requested)
- threads.push_back(new Thread());
+ while (size() < requested)
+ push_back(new Thread());
- while (threads.size() > requested)
+ while (size() > requested)
{
- delete threads.back();
- threads.pop_back();
+ delete back();
+ pop_back();
}
}
// slave_available() tries to find an idle thread which is available as a slave
// for the thread 'master'.
-bool ThreadPool::slave_available(Thread* master) const {
+Thread* ThreadPool::available_slave(Thread* master) const {
- for (size_t i = 0; i < threads.size(); i++)
- if (threads[i]->is_available_to(master))
- return true;
+ for (const_iterator it = begin(); it != end(); ++it)
+ if ((*it)->is_available_to(master))
+ return *it;
- return false;
+ return NULL;
}
// search() then split() returns.
template <bool Fake>
-Value Thread::split(Position& pos, Stack* ss, Value alpha, Value beta,
- Value bestValue, Move* bestMove, Depth depth, Move threatMove,
- int moveCount, MovePicker& mp, int nodeType) {
+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) {
assert(pos.pos_is_ok());
- assert(bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
- assert(bestValue > -VALUE_INFINITE);
+ assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
+ assert(*bestValue > -VALUE_INFINITE);
assert(depth >= Threads.minimumSplitDepth);
-
assert(searching);
assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
sp.parentSplitPoint = activeSplitPoint;
sp.slavesMask = 1ULL << idx;
sp.depth = depth;
+ sp.bestValue = *bestValue;
sp.bestMove = *bestMove;
sp.threatMove = threatMove;
sp.alpha = alpha;
sp.beta = beta;
sp.nodeType = nodeType;
- sp.bestValue = bestValue;
- sp.movePicker = ∓
+ sp.movePicker = movePicker;
sp.moveCount = moveCount;
sp.pos = &pos;
sp.nodes = 0;
splitPointsSize++;
activeSplitPoint = &sp;
+ activePosition = NULL;
- size_t slavesCnt = 1; // Master is always included
+ size_t slavesCnt = 1; // This thread is always included
+ Thread* slave;
- for (size_t i = 0; i < Threads.size() && !Fake; ++i)
- if (Threads[i].is_available_to(this) && ++slavesCnt <= Threads.maxThreadsPerSplitPoint)
- {
- sp.slavesMask |= 1ULL << Threads[i].idx;
- Threads[i].activeSplitPoint = &sp;
- Threads[i].searching = true; // Slave leaves idle_loop()
- Threads[i].notify_one(); // Could be sleeping
- }
+ 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
+ }
sp.mutex.unlock();
Threads.mutex.unlock();
// 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(!searching);
+ assert(!activePosition);
}
// We have returned from the idle loop, which means that all threads are
searching = true;
splitPointsSize--;
activeSplitPoint = sp.parentSplitPoint;
+ activePosition = &pos;
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
*bestMove = sp.bestMove;
+ *bestValue = sp.bestValue;
sp.mutex.unlock();
Threads.mutex.unlock();
-
- return sp.bestValue;
}
// Explicit template instantiations
-template Value Thread::split<false>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker&, int);
-template Value Thread::split<true>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker&, int);
+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);
// wait_for_think_finished() waits for main thread to go to sleep then returns