X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=bb6527d4e040e5ef8e46263764350ffc52151733;hb=fb39d7ec4a7ab6f693cb2e1b18c4b421356a2129;hp=6b454deddb1bde972a51da169818ac284db18220;hpb=a694d39c28c9b357b7187872d2ef5ae2b35a21e8;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index 6b454de..bb6527d 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -4,15 +4,15 @@ #include #include -#ifdef WIN32 -# include +#ifdef WIN32 +# include # include HANDLE process; DWORD thread_id; -#else -# include -# include -# define NO_ERROR 0 +#else +# include +# include +# define NO_ERROR 0 # include int GetTickCount() // with thanks to Tord { struct timeval t; gettimeofday(&t, NULL); return t.tv_sec*1000 + t.tv_usec/1000; } @@ -20,33 +20,68 @@ #include #include -#ifdef _MSC_VER -#define SLEEP() Sleep(1) -#else -#define SLEEP() usleep(10) -#endif - // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) # define VARIANTS "normal,xiangqi" +#define DPRINT if(debug) printf + #define WHITE 0 #define BLACK 1 #define NONE 2 #define ANALYZE 3 char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix; -int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime; +int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug; int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode FILE *toE, *fromE; int pid; +#ifdef WIN32 +WinPipe(HANDLE *hRd, HANDLE *hWr) +{ + SECURITY_ATTRIBUTES saAttr; + + /* Set the bInheritHandle flag so pipe handles are inherited. */ + saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + + /* Create a pipe */ + return CreatePipe(hRd, hWr, &saAttr, 0); +} +#endif + +#define INIT 0 +#define WAKEUP 1 +#define PAUSE 2 + +void +Sync (int action) +{ +#ifdef WIN32 + static HANDLE hWr, hRd; DWORD d; char c; + switch(action) { + case INIT: WinPipe(&hRd, &hWr); break; + case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break; + case PAUSE: ReadFile(hRd, &c, 1, &d, NULL); + } +#else + static int syncPipe[2]; char c; + switch(action) { + case INIT: pipe(syncPipe); break; + case WAKEUP: write(syncPipe[1], "\n", 1); break; + case PAUSE: read(syncPipe[0], &c, 1); + } +#endif +} + void StartSearch(char *ponder) { // send the 'go' command to engine. Suffix by ponder. int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead! fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); - printf( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); + DPRINT( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); if(sTime > 0) fprintf(toE, " movetime %d", sTime),printf(" movetime %d", sTime); else if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc); @@ -61,8 +96,8 @@ StopPonder(int pondering) { if(!pondering) return; pause = 1; - fprintf(toE, "stop\n");printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' - while(pause) SLEEP(); // wait for engine to acknowledge 'stop' with 'bestmove'. + fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' + Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } void @@ -70,7 +105,7 @@ LoadPos(int moveNr) { int j; fprintf(toE, "%s moves", iniPos); - printf( "# %s moves", iniPos); + DPRINT( "# %s moves", iniPos); for(j=0; j 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; } if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; } - if(argc < 2) { printf("usage is: U%cI2WB [-s] []\n", sc-32); exit(-1); } + if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] []\n", sc-32); exit(-1); } if(argc > 2) dir = argv[2]; if(argc > 3) suffix = argv[3]; // spawn engine proc if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); } + Sync(INIT); + // create separate thread to handle engine->GUI traffic -#ifdef WIN32 - CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id); -#else - { pthread_t t; signal(SIGINT, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); } +#ifdef WIN32 + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id); +#else + { pthread_t t; signal(SIGINT, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); } #endif // handle GUI->engine traffic in original thread