]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Workaround value-initialization in MSVC
[stockfish] / src / thread.cpp
index 1c5c67bacfc2b587a5bac6f6f56fd84316aaa1bf..80765cdeba0a981e2bbe21120e1694f355c5040c 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <algorithm> // For std::count
 #include <cassert>
+#include <cstring> // For memset
 #include <iostream>
 
 #include "movegen.h"
@@ -43,7 +44,7 @@ namespace { extern "C" {
 // 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;
@@ -229,13 +230,13 @@ void ThreadPool::read_uci_options() {
 // 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 (const_iterator it = begin(); it != end(); ++it)
       if ((*it)->is_available_to(master))
-          return true;
+          return *it;
 
-  return false;
+  return NULL;
 }
 
 
@@ -249,15 +250,14 @@ bool ThreadPool::slave_available(Thread* master) const {
 // 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);
 
@@ -268,19 +268,21 @@ Value Thread::split(Position& pos, Stack* ss, Value alpha, Value beta,
   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 = &mp;
+  sp.movePicker = movePicker;
   sp.moveCount = moveCount;
   sp.pos = &pos;
   sp.nodes = 0;
   sp.cutoff = false;
   sp.ss = ss;
 
+  memset(sp.slavesPositions, 0, sizeof(sp.slavesPositions));
+
   // 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.
@@ -291,18 +293,15 @@ Value Thread::split(Position& pos, Stack* ss, Value alpha, Value beta,
   activeSplitPoint = &sp;
 
   size_t slavesCnt = 1; // This thread is always included
+  Thread* slave;
 
-  for (ThreadPool::iterator it = Threads.begin(); it != Threads.end() && !Fake; ++it)
+  while (    (slave = Threads.available_slave(this)) != NULL
+         && ++slavesCnt <= Threads.maxThreadsPerSplitPoint && !Fake)
   {
-      Thread* slave = *it;
-
-      if (slave->is_available_to(this) && ++slavesCnt <= Threads.maxThreadsPerSplitPoint)
-      {
-          sp.slavesMask |= 1ULL << slave->idx;
-          slave->activeSplitPoint = &sp;
-          slave->searching = true; // Slave leaves idle_loop()
-          slave->notify_one(); // Could be sleeping
-      }
+      sp.slavesMask |= 1ULL << slave->idx;
+      slave->activeSplitPoint = &sp;
+      slave->searching = true; // Slave leaves idle_loop()
+      slave->notify_one(); // Could be sleeping
   }
 
   sp.mutex.unlock();
@@ -332,16 +331,15 @@ Value Thread::split(Position& pos, Stack* ss, Value alpha, Value beta,
   activeSplitPoint = sp.parentSplitPoint;
   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