]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Correctly handle handover of setup states
[stockfish] / src / search.cpp
index a29fd0f64772e9ae7048a52973697f5da99f7fc5..3309947e1c20f91b1870adde6c8a0e7c5228e504 100644 (file)
@@ -43,6 +43,7 @@ namespace Search {
   std::vector<RootMove> RootMoves;
   Position RootPosition;
   Time SearchTime;
+  StateStackPtr SetupStates;
 }
 
 using std::string;
@@ -159,7 +160,7 @@ namespace {
         &&  type_of(pos.piece_on(to_sq(m))) != PAWN
         &&  type_of(m) == NORMAL
         && (  pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK)
-            - PieceValueMidgame[pos.piece_on(to_sq(m))] == VALUE_ZERO))
+            - PieceValue[Mg][pos.piece_on(to_sq(m))] == VALUE_ZERO))
         return true;
 
     return false;
@@ -688,7 +689,7 @@ namespace {
         Depth R = 3 * ONE_PLY + depth / 4;
 
         // Null move dynamic reduction based on value
-        if (refinedValue - PawnValueMidgame > beta)
+        if (refinedValue - PawnValueMg > beta)
             R += ONE_PLY;
 
         pos.do_null_move<true>(st);
@@ -819,7 +820,7 @@ split_point_start: // At split points actual search starts from here
       if (SpNode)
       {
           moveCount = ++sp->moveCount;
-          lock_release(sp->lock);
+          sp->mutex.unlock();
       }
       else
           moveCount++;
@@ -885,7 +886,7 @@ split_point_start: // At split points actual search starts from here
               && (!threatMove || !connected_threat(pos, move, threatMove)))
           {
               if (SpNode)
-                  lock_grab(sp->lock);
+                  sp->mutex.lock();
 
               continue;
           }
@@ -900,7 +901,7 @@ split_point_start: // At split points actual search starts from here
           if (futilityValue < beta)
           {
               if (SpNode)
-                  lock_grab(sp->lock);
+                  sp->mutex.lock();
 
               continue;
           }
@@ -910,7 +911,7 @@ split_point_start: // At split points actual search starts from here
               && pos.see_sign(move) < 0)
           {
               if (SpNode)
-                  lock_grab(sp->lock);
+                  sp->mutex.lock();
 
               continue;
           }
@@ -974,7 +975,7 @@ split_point_start: // At split points actual search starts from here
       // Step 18. Check for new best move
       if (SpNode)
       {
-          lock_grab(sp->lock);
+          sp->mutex.lock();
           bestValue = sp->bestValue;
           alpha = sp->alpha;
       }
@@ -1176,7 +1177,7 @@ split_point_start: // At split points actual search starts from here
             alpha = bestValue;
 
         futilityBase = ss->eval + evalMargin + FutilityMarginQS;
-        enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame;
+        enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMg;
     }
 
     // Initialize a MovePicker object for the current position, and prepare
@@ -1204,8 +1205,8 @@ split_point_start: // At split points actual search starts from here
           && !pos.is_passed_pawn_push(move))
       {
           futilityValue =  futilityBase
-                         + PieceValueEndgame[pos.piece_on(to_sq(move))]
-                         + (type_of(move) == ENPASSANT ? PawnValueEndgame : VALUE_ZERO);
+                         + PieceValue[Eg][pos.piece_on(to_sq(move))]
+                         + (type_of(move) == ENPASSANT ? PawnValueEg : VALUE_ZERO);
 
           if (futilityValue < beta)
           {
@@ -1243,7 +1244,7 @@ split_point_start: // At split points actual search starts from here
           &&  givesCheck
           &&  move != ttMove
           && !pos.is_capture_or_promotion(move)
-          &&  ss->eval + PawnValueMidgame / 4 < beta
+          &&  ss->eval + PawnValueMg / 4 < beta
           && !check_is_dangerous(pos, move, futilityBase, beta))
           continue;
 
@@ -1328,7 +1329,7 @@ split_point_start: // At split points actual search starts from here
     while (b)
     {
         // Note that here we generate illegal "double move"!
-        if (futilityBase + PieceValueEndgame[pos.piece_on(pop_lsb(&b))] >= beta)
+        if (futilityBase + PieceValue[Eg][pos.piece_on(pop_lsb(&b))] >= beta)
             return true;
     }
 
@@ -1440,7 +1441,7 @@ split_point_start: // At split points actual search starts from here
     // Case 2: If the threatened piece has value less than or equal to the
     // value of the threatening piece, don't prune moves which defend it.
     if (   pos.is_capture(threat)
-        && (   PieceValueMidgame[pos.piece_on(tfrom)] >= PieceValueMidgame[pos.piece_on(tto)]
+        && (   PieceValue[Mg][pos.piece_on(tfrom)] >= PieceValue[Mg][pos.piece_on(tto)]
             || type_of(pos.piece_on(tfrom)) == KING)
         && pos.move_attacks_square(m, tto))
         return true;
@@ -1500,7 +1501,7 @@ split_point_start: // At split points actual search starts from here
 
     // RootMoves are already sorted by score in descending order
     size_t size = std::min(MultiPV, RootMoves.size());
-    int variance = std::min(RootMoves[0].score - RootMoves[size - 1].score, PawnValueMidgame);
+    int variance = std::min(RootMoves[0].score - RootMoves[size - 1].score, PawnValueMg);
     int weakness = 120 - 2 * SkillLevel;
     int max_s = -VALUE_INFINITE;
     Move best = MOVE_NONE;
@@ -1670,12 +1671,12 @@ void Thread::idle_loop() {
           }
 
           // Grab the lock to avoid races with Thread::wake_up()
-          lock_grab(sleepLock);
+          mutex.lock();
 
           // If we are master and all slaves have finished don't go to sleep
           if (sp_master && !sp_master->slavesMask)
           {
-              lock_release(sleepLock);
+              mutex.unlock();
               break;
           }
 
@@ -1684,9 +1685,9 @@ void Thread::idle_loop() {
           // in the meanwhile, allocated us and sent the wake_up() call before we
           // had the chance to grab the lock.
           if (do_sleep || !is_searching)
-              cond_wait(sleepCond, sleepLock);
+              sleepCondition.wait(mutex);
 
-          lock_release(sleepLock);
+          mutex.unlock();
       }
 
       // If this thread has been assigned work, launch a search
@@ -1694,12 +1695,12 @@ void Thread::idle_loop() {
       {
           assert(!do_sleep && !do_exit);
 
-          lock_grab(Threads.splitLock);
+          Threads.mutex.lock();
 
           assert(is_searching);
           SplitPoint* sp = curSplitPoint;
 
-          lock_release(Threads.splitLock);
+          Threads.mutex.unlock();
 
           Stack ss[MAX_PLY_PLUS_2];
           Position pos(*sp->pos, this);
@@ -1707,7 +1708,7 @@ void Thread::idle_loop() {
           memcpy(ss, sp->ss - 1, 4 * sizeof(Stack));
           (ss+1)->sp = sp;
 
-          lock_grab(sp->lock);
+          sp->mutex.lock();
 
           if (sp->nodeType == Root)
               search<SplitPointRoot>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
@@ -1738,7 +1739,7 @@ void Thread::idle_loop() {
           // related data in a safe way becuase it could have been released under
           // our feet by the sp master. Also accessing other Thread objects is
           // unsafe because if we are exiting there is a chance are already freed.
-          lock_release(sp->lock);
+          sp->mutex.unlock();
       }
   }
 }