Updating to version 1.3, release made by Mike Vanier (mvanier@bbb.caltech.edu).
[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
46 /* Forward declarations. */
47
48 void ElapsedTime_NOFIONREAD(ElapsedTime_mode iop);
49 void ElapsedTime_FIONREAD(ElapsedTime_mode iop);
50
51
52 /*
53  * Determine the time that has passed since the search was started. If the
54  * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
55  * to true which will terminate the search.
56  * iop = COMPUTE_MODE calculate et, bump ETnodes
57  * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
58  *     set reference time
59  */
60
61 /*
62  * ElapsedTime() is actually a wrapper function around the different
63  * versions of ElapsedTime_XXX().  This allows us to compile all the
64  * different ways of measuring time in one executable.
65  */
66
67 void
68 ElapsedTime(ElapsedTime_mode iop)
69 {
70     switch (display_type)
71     {
72     case DISPLAY_RAW:
73         ElapsedTime_NOFIONREAD(iop);
74         break;
75
76     default:
77         ElapsedTime_FIONREAD(iop);
78         break;
79     }
80 }
81
82
83
84 #ifdef HAVE_GETTIMEOFDAY
85 void
86 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
87 {
88     long current_time;
89     int  i;
90     int  nchar;
91
92     struct timeval tv;
93     extern int errno;
94
95     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
96     {
97         perror("FIONREAD");
98         fprintf(stderr,
99                 "You probably have a non-ANSI <ioctl.h>; "
100                 "see README. %d %d %x\n",
101                 i, errno, FIONREAD);
102         exit(1);
103     }
104
105     if (nchar)
106     {
107         if (!flag.timeout)
108             flag.back = true;
109
110         flag.bothsides = false;
111     }
112
113     gettimeofday(&tv, NULL);
114     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
115
116 #  ifdef INTERRUPT_TEST
117     if (iop == INIT_INTERRUPT_MODE)
118     {
119         itime0 = current_time;
120     }
121     else if (iop == COMPUTE_INTERRUPT_MODE)
122     {
123         it = current_time - itime0;
124     }
125     else
126 #  endif
127     {
128         et = current_time - time0;
129         ETnodes = NodeCnt + znodes;
130
131         if (et < 0)
132         {
133 #  ifdef INTERRUPT_TEST
134             printf("elapsed time %ld not positive\n", et);
135 #  endif
136             et = 0;
137         }
138
139         if (iop == COMPUTE_AND_INIT_MODE)
140         {
141             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
142                 flag.timeout = true;
143
144             time0 = current_time;
145         }
146
147         if (!NOT_CURSES)
148         {
149 #  ifdef QUIETBACKGROUND
150             if (!background)
151 #  endif /* QUIETBACKGROUND */
152                 UpdateClocks();
153         }
154     }
155 }
156
157
158 void
159 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
160 {
161     struct timeval tv;
162     long current_time;
163
164     gettimeofday(&tv, NULL);
165     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
166
167 #  ifdef INTERRUPT_TEST
168     if (iop == INIT_INTERRUPT_MODE)
169     {
170         itime0 = current_time;
171     }
172     else if (iop == COMPUTE_INTERRUPT_MODE)
173     {
174         it = current_time - itime0;
175     }
176     else
177 #  endif
178     {
179         et = current_time - time0;
180         ETnodes = NodeCnt + znodes;
181
182         if (et < 0)
183         {
184 #  ifdef INTERRUPT_TEST
185             printf("elapsed time %ld not positive\n", et);
186 #  endif
187             et = 0;
188         }
189
190         if (iop == COMPUTE_AND_INIT_MODE)
191         {
192             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
193                 flag.timeout = true;
194
195             time0 = current_time;
196         }
197
198         if (!NOT_CURSES)
199         {
200 #  ifdef QUIETBACKGROUND
201             if (!background)
202 #  endif /* QUIETBACKGROUND */
203                 UpdateClocks();
204         }
205     }
206 }
207
208
209 #else /* !HAVE_GETTIMEOFDAY */
210
211
212 /*
213  * Determine the time that has passed since the search was started.  If the
214  * elapsed time exceeds the target (ResponseTime + ExtraTime) then set
215  * timeout to true which will terminate the search.
216  *
217  * iop = 0   calculate et, bump ETnodes
218  * iop = 1   calculate et, set timeout if time exceeded, calculate et
219  *
220  */
221
222 void
223 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
224 {
225     long current_time;
226     int  nchar;
227     int  i;
228
229     extern int errno;
230
231
232     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
233     {
234         perror("FIONREAD");
235         fprintf(stderr,
236                 "You probably have a non-ANSI <ioctl.h>; "
237                 "see README. %d %d %x\n",
238                 i, errno, FIONREAD);
239         exit(1);
240     }
241
242     if (nchar)
243     {
244         if (!flag.timeout)
245             flag.back = true;
246         flag.bothsides = false;
247     }
248
249     et = ((current_time = time((long *) 0)) - time0) * 100;
250
251 #ifdef INTERRUPT_TEST
252     if (iop == INIT_INTERRUPT_MODE)
253     {
254         itime0 = current_time;
255     }
256     else if (iop == COMPUTE_INTERRUPT_MODE)
257     {
258         it = current_time - itime0;
259     }
260     else
261 #endif
262     {
263         ETnodes = NodeCnt + znodes;
264
265         if (et < 0)
266         {
267 #ifdef INTERRUPT_TEST
268             printf("elapsed time %ld not positive\n", et);
269 #endif
270             et = 0;
271         }
272
273         if (iop == COMPUTE_AND_INIT_MODE)
274         {
275             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
276                 flag.timeout = true;
277
278             time0 = current_time;
279         }
280
281         if (!NOT_CURSES)
282         {
283 #ifdef QUIETBACKGROUND
284             if (!background)
285 #endif /* QUIETBACKGROUND */
286                 UpdateClocks();
287         }
288     }
289 }
290
291
292
293 void
294 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
295 {
296     long current_time;
297
298     et = ((current_time = time((long *) 0)) - time0) * 100;
299
300 #ifdef INTERRUPT_TEST
301     if (iop == INIT_INTERRUPT_MODE)
302     {
303         itime0 = current_time;
304     }
305     else if (iop == COMPUTE_INTERRUPT_MODE)
306     {
307         it = current_time - itime0;
308     }
309     else
310 #endif
311     {
312         ETnodes = NodeCnt + znodes;
313
314         if (et < 0)
315         {
316 #ifdef INTERRUPT_TEST
317             printf("elapsed time %ld not positive\n", et);
318 #endif
319             et = 0;
320         }
321
322         if (iop == COMPUTE_AND_INIT_MODE)
323         {
324             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
325                 flag.timeout = true;
326
327             time0 = current_time;
328         }
329
330         if (!NOT_CURSES)
331         {
332 #ifdef QUIETBACKGROUND
333             if (!background)
334 #endif /* QUIETBACKGROUND */
335                 UpdateClocks();
336         }
337     }
338 }
339
340
341 #endif /* HAVE_GETTIMEOFDAY */
342
343
344
345
346