Finally get rid of that highly non-standard use of SIGINT.
[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 3 of the License,
20  * or (at your option) any 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, see
29  * <http://www.gnu.org/licenses/>.
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
98     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
99     {
100         perror("FIONREAD");
101         fprintf(stderr,
102                 "You probably have a non-ANSI <ioctl.h>; "
103                 "see README. %d %d %x\n",
104                 i, errno, FIONREAD);
105         exit(1);
106     }
107
108     if (nchar)
109     {
110         if (!flag.timeout)
111             flag.back = true;
112
113         flag.bothsides = false;
114     }
115
116     gettimeofday(&tv, NULL);
117     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
118
119 #  ifdef INTERRUPT_TEST
120     if (iop == INIT_INTERRUPT_MODE)
121     {
122         itime0 = current_time;
123     }
124     else if (iop == COMPUTE_INTERRUPT_MODE)
125     {
126         it = current_time - itime0;
127     }
128     else
129 #  endif
130     {
131         et = current_time - time0;
132         ETnodes = NodeCnt + znodes;
133
134         if (et < 0)
135         {
136 #  ifdef INTERRUPT_TEST
137             printf("elapsed time %ld not positive\n", et);
138 #  endif
139             et = 0;
140         }
141
142         if (iop == COMPUTE_AND_INIT_MODE)
143         {
144             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
145                 flag.timeout = true;
146
147             time0 = current_time;
148         }
149
150         if (!NOT_CURSES)
151         {
152 #  ifdef QUIETBACKGROUND
153             if (!background)
154 #  endif /* QUIETBACKGROUND */
155                 UpdateClocks();
156         }
157     }
158 }
159
160
161 void
162 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
163 {
164     struct timeval tv;
165     long current_time;
166
167     gettimeofday(&tv, NULL);
168     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
169
170 #  ifdef INTERRUPT_TEST
171     if (iop == INIT_INTERRUPT_MODE)
172     {
173         itime0 = current_time;
174     }
175     else if (iop == COMPUTE_INTERRUPT_MODE)
176     {
177         it = current_time - itime0;
178     }
179     else
180 #  endif
181     {
182         et = current_time - time0;
183         ETnodes = NodeCnt + znodes;
184
185         if (et < 0)
186         {
187 #  ifdef INTERRUPT_TEST
188             printf("elapsed time %ld not positive\n", et);
189 #  endif
190             et = 0;
191         }
192
193         if (iop == COMPUTE_AND_INIT_MODE)
194         {
195             if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
196                 flag.timeout = true;
197
198             time0 = current_time;
199         }
200
201         if (!NOT_CURSES)
202         {
203 #  ifdef QUIETBACKGROUND
204             if (!background)
205 #  endif /* QUIETBACKGROUND */
206                 UpdateClocks();
207         }
208     }
209 }
210
211
212 #else /* !HAVE_GETTIMEOFDAY */
213
214
215 /*
216  * Determine the time that has passed since the search was started.  If the
217  * elapsed time exceeds the target (ResponseTime + ExtraTime) then set
218  * timeout to true which will terminate the search.
219  *
220  * iop = 0   calculate et, bump ETnodes
221  * iop = 1   calculate et, set timeout if time exceeded, calculate et
222  *
223  */
224
225 void
226 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
227 {
228     long current_time;
229     int  nchar;
230     int  i;
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