Updating to version 1.3.2, last public release by Mike Vanier.
[gnushogi.git] / gnushogi / sysdeps.c
1 /*
2  * FILE: sysdeps.c
3  *
4  *     System-dependent functions for GNU Shogi.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  *
10  * GNU SHOGI is based on GNU CHESS
11  *
12  * Copyright (c) 1988, 1989, 1990 John Stanback
13  * Copyright (c) 1992 Free Software Foundation
14  *
15  * This file is part of GNU SHOGI.
16  *
17  * GNU Shogi is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the
19  * Free Software Foundation; either version 1, or (at your option) any
20  * later version.
21  *
22  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with GNU Shogi; see the file COPYING.  If not, write to the Free
29  * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30  * ----------------------------------------------------------------------
31  *
32  */
33
34 #include "gnushogi.h"
35
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #if HAVE_SYS_FILIO_H
41 /* Definition of FIONREAD */
42 #include <sys/filio.h>
43 #endif
44
45 #if HAVE_ERRNO_H
46 /* Definition of errno(). */
47 #include <errno.h>
48 #endif
49
50 /* Forward declarations. */
51
52 void ElapsedTime_NOFIONREAD(ElapsedTime_mode iop);
53 void ElapsedTime_FIONREAD(ElapsedTime_mode iop);
54
55
56 /*
57  * Determine the time that has passed since the search was started. If the
58  * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
59  * to true which will terminate the search.
60  * iop = COMPUTE_MODE calculate et, bump ETnodes
61  * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
62  *     set reference time
63  */
64
65 /*
66  * ElapsedTime() is actually a wrapper function around the different
67  * versions of ElapsedTime_XXX().  This allows us to compile all the
68  * different ways of measuring time in one executable.
69  */
70
71 void
72 ElapsedTime(ElapsedTime_mode iop)
73 {
74     switch (display_type)
75     {
76     case DISPLAY_RAW:
77         ElapsedTime_NOFIONREAD(iop);
78         break;
79
80     default:
81         ElapsedTime_FIONREAD(iop);
82         break;
83     }
84 }
85
86
87
88 #ifdef HAVE_GETTIMEOFDAY
89 void
90 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
91 {
92     long current_time;
93     int  i;
94     int  nchar;
95
96     struct timeval tv;
97     extern int errno;
98
99     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
100     {
101         perror("FIONREAD");
102         fprintf(stderr,
103                 "You probably have a non-ANSI <ioctl.h>; "
104                 "see README. %d %d %x\n",
105                 i, errno, FIONREAD);
106         exit(1);
107     }
108
109     if (nchar)
110     {
111         if (!flag.timeout)
112             flag.back = true;
113
114         flag.bothsides = false;
115     }
116
117     gettimeofday(&tv, NULL);
118     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
119
120 #  ifdef INTERRUPT_TEST
121     if (iop == INIT_INTERRUPT_MODE)
122     {
123         itime0 = current_time;
124     }
125     else if (iop == COMPUTE_INTERRUPT_MODE)
126     {
127         it = current_time - itime0;
128     }
129     else
130 #  endif
131     {
132         et = current_time - time0;
133         ETnodes = NodeCnt + znodes;
134
135         if (et < 0)
136         {
137 #  ifdef INTERRUPT_TEST
138             printf("elapsed time %ld not positive\n", et);
139 #  endif
140             et = 0;
141         }
142
143         if (iop == COMPUTE_AND_INIT_MODE)
144         {
145             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
146                 flag.timeout = true;
147
148             time0 = current_time;
149         }
150
151         if (!NOT_CURSES)
152         {
153 #  ifdef QUIETBACKGROUND
154             if (!background)
155 #  endif /* QUIETBACKGROUND */
156                 UpdateClocks();
157         }
158     }
159 }
160
161
162 void
163 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
164 {
165     struct timeval tv;
166     long current_time;
167
168     gettimeofday(&tv, NULL);
169     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
170
171 #  ifdef INTERRUPT_TEST
172     if (iop == INIT_INTERRUPT_MODE)
173     {
174         itime0 = current_time;
175     }
176     else if (iop == COMPUTE_INTERRUPT_MODE)
177     {
178         it = current_time - itime0;
179     }
180     else
181 #  endif
182     {
183         et = current_time - time0;
184         ETnodes = NodeCnt + znodes;
185
186         if (et < 0)
187         {
188 #  ifdef INTERRUPT_TEST
189             printf("elapsed time %ld not positive\n", et);
190 #  endif
191             et = 0;
192         }
193
194         if (iop == COMPUTE_AND_INIT_MODE)
195         {
196             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
197                 flag.timeout = true;
198
199             time0 = current_time;
200         }
201
202         if (!NOT_CURSES)
203         {
204 #  ifdef QUIETBACKGROUND
205             if (!background)
206 #  endif /* QUIETBACKGROUND */
207                 UpdateClocks();
208         }
209     }
210 }
211
212
213 #else /* !HAVE_GETTIMEOFDAY */
214
215
216 /*
217  * Determine the time that has passed since the search was started.  If the
218  * elapsed time exceeds the target (ResponseTime + ExtraTime) then set
219  * timeout to true which will terminate the search.
220  *
221  * iop = 0   calculate et, bump ETnodes
222  * iop = 1   calculate et, set timeout if time exceeded, calculate et
223  *
224  */
225
226 void
227 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
228 {
229     long current_time;
230     int  nchar;
231     int  i;
232
233     extern int errno;
234
235
236     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
237     {
238         perror("FIONREAD");
239         fprintf(stderr,
240                 "You probably have a non-ANSI <ioctl.h>; "
241                 "see README. %d %d %x\n",
242                 i, errno, FIONREAD);
243         exit(1);
244     }
245
246     if (nchar)
247     {
248         if (!flag.timeout)
249             flag.back = true;
250         flag.bothsides = false;
251     }
252
253     et = ((current_time = time((long *) 0)) - time0) * 100;
254
255 #ifdef INTERRUPT_TEST
256     if (iop == INIT_INTERRUPT_MODE)
257     {
258         itime0 = current_time;
259     }
260     else if (iop == COMPUTE_INTERRUPT_MODE)
261     {
262         it = current_time - itime0;
263     }
264     else
265 #endif
266     {
267         ETnodes = NodeCnt + znodes;
268
269         if (et < 0)
270         {
271 #ifdef INTERRUPT_TEST
272             printf("elapsed time %ld not positive\n", et);
273 #endif
274             et = 0;
275         }
276
277         if (iop == COMPUTE_AND_INIT_MODE)
278         {
279             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
280                 flag.timeout = true;
281
282             time0 = current_time;
283         }
284
285         if (!NOT_CURSES)
286         {
287 #ifdef QUIETBACKGROUND
288             if (!background)
289 #endif /* QUIETBACKGROUND */
290                 UpdateClocks();
291         }
292     }
293 }
294
295
296
297 void
298 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
299 {
300     long current_time;
301
302     et = ((current_time = time((long *) 0)) - time0) * 100;
303
304 #ifdef INTERRUPT_TEST
305     if (iop == INIT_INTERRUPT_MODE)
306     {
307         itime0 = current_time;
308     }
309     else if (iop == COMPUTE_INTERRUPT_MODE)
310     {
311         it = current_time - itime0;
312     }
313     else
314 #endif
315     {
316         ETnodes = NodeCnt + znodes;
317
318         if (et < 0)
319         {
320 #ifdef INTERRUPT_TEST
321             printf("elapsed time %ld not positive\n", et);
322 #endif
323             et = 0;
324         }
325
326         if (iop == COMPUTE_AND_INIT_MODE)
327         {
328             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
329                 flag.timeout = true;
330
331             time0 = current_time;
332         }
333
334         if (!NOT_CURSES)
335         {
336 #ifdef QUIETBACKGROUND
337             if (!background)
338 #endif /* QUIETBACKGROUND */
339                 UpdateClocks();
340         }
341     }
342 }
343
344
345 #endif /* HAVE_GETTIMEOFDAY */
346
347
348
349
350