Regularly check for user input when searching, to have a chance to catch EOF from...
authorYann Dirson <ydirson@free.fr>
Mon, 14 Oct 2013 19:38:24 +0000 (21:38 +0200)
committerYann Dirson <ydirson@free.fr>
Thu, 17 Oct 2013 22:38:27 +0000 (00:38 +0200)
When an engine quits without terminating a searching gnushogi, it would
continue to search and never realize there was no reason to continue.

This is a poll-based implementation of what H.G.Muller wrote for WIN32 to
circumvent the lack of UNIX signals there.

gnushogi/search.c

index 2cfff17..1491057 100644 (file)
@@ -30,6 +30,8 @@
  */
 
 #include "gnushogi.h"
+#include <poll.h>
+#include <unistd.h>
 
 #if !defined OLDTIME && defined HAVE_GETTIMEOFDAY
 double pow(double x, double y);
@@ -176,6 +178,7 @@ SelectMove(short side, SelectMove_mode iop)
     }
     else
     {
+        background = false; /* [HGM] with ponder on we did not switch back to foreground mode??? */
         player = side;
         SetResponseTime(side);
     }
@@ -529,6 +532,8 @@ search(short side,
     short best = -(SCORE_LIMIT + 3000);
     short bestwidth = 0;
     short mustcut;
+    static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
+                                                      /* .events = */ POLLIN } };
 
 #ifdef NULLMOVE
     short PVsave;
@@ -546,6 +551,19 @@ search(short side,
         {
             ElapsedTime(COMPUTE_MODE);
 
+            if(background) {
+                int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
+                if (cnt < 0) {
+                    perror("polling standard input");
+                    ExitShogi();
+                }
+                if (cnt) { /* if anything to read, or error occured */
+                    if (!flag.timeout)
+                        flag.back = true; /* previous: flag.timeout = true; */
+                    flag.bothsides = false;
+                }
+            }
+
             if (flag.back)
             {
                 flag.back = false;