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