shithub: moonfish

Download patch

ref: a0266a89597a4b65947dd39fd01d519c1f50766c
parent: 55bbbb87c5c93ccdd0f996d10d5296b2c6480611
author: zamfofex <zamfofex@twdb.moe>
date: Wed Nov 15 13:27:33 EST 2023

fix multithreading edge case handling

--- a/search.c
+++ b/search.c
@@ -114,8 +114,31 @@
 	
 	moves = move_array;
 	i = 0;
-	while (moves->piece != moonfish_outside)
+	for (;;)
 	{
+		if (i == ctx->cpu_count || moves->piece == moonfish_outside)
+		{
+			for (j = 0 ; j < i ; j++)
+			{
+				result = pthread_join(infos[j].thread, NULL);
+				if (result)
+				{
+					fprintf(stderr, "%s: %s\n", ctx->argv0, strerror(result));
+					exit(1);
+				}
+				
+				if (infos[j].score > best_score)
+				{
+					*best_move = infos[j].move;
+					best_score = infos[j].score;
+				}
+			}
+			
+			if (moves->piece == moonfish_outside) break;
+			
+			i = 0;
+		}
+		
 		moonfish_play(&ctx->chess, moves);
 		
 		if (!moonfish_validate(&ctx->chess))
@@ -139,27 +162,6 @@
 		
 		i++;
 		moves++;
-		
-		if (i == ctx->cpu_count || moves->piece == moonfish_outside)
-		{
-			for (j = 0 ; j < i ; j++)
-			{
-				result = pthread_join(infos[j].thread, NULL);
-				if (result)
-				{
-					fprintf(stderr, "%s: %s\n", ctx->argv0, strerror(result));
-					exit(1);
-				}
-				
-				if (infos[j].score > best_score)
-				{
-					*best_move = infos[j].move;
-					best_score = infos[j].score;
-				}
-			}
-			
-			i = 0;
-		}
 	}
 	
 	return best_score;
--