Updating to version 1.3, release made by Mike Vanier (mvanier@bbb.caltech.edu).
[gnushogi.git] / gnushogi / sysdeps.c
blobc88dc43cde9ac021c639798527d4a1c735ec121f
1 /*
2 * FILE: sysdeps.c
4 * System-dependent functions for GNU Shogi.
6 * ----------------------------------------------------------------------
7 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10 * GNU SHOGI is based on GNU CHESS
12 * Copyright (c) 1988, 1989, 1990 John Stanback
13 * Copyright (c) 1992 Free Software Foundation
15 * This file is part of GNU SHOGI.
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.
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.
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 * ----------------------------------------------------------------------
34 #include "gnushogi.h"
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #if HAVE_SYS_FILIO_H
41 /* Definition of FIONREAD */
42 #include <sys/filio.h>
43 #endif
46 /* Forward declarations. */
48 void ElapsedTime_NOFIONREAD(ElapsedTime_mode iop);
49 void ElapsedTime_FIONREAD(ElapsedTime_mode iop);
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
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.
67 void
68 ElapsedTime(ElapsedTime_mode iop)
70 switch (display_type)
72 case DISPLAY_RAW:
73 ElapsedTime_NOFIONREAD(iop);
74 break;
76 default:
77 ElapsedTime_FIONREAD(iop);
78 break;
84 #ifdef HAVE_GETTIMEOFDAY
85 void
86 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
88 long current_time;
89 int i;
90 int nchar;
92 struct timeval tv;
93 extern int errno;
95 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
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);
105 if (nchar)
107 if (!flag.timeout)
108 flag.back = true;
110 flag.bothsides = false;
113 gettimeofday(&tv, NULL);
114 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
116 # ifdef INTERRUPT_TEST
117 if (iop == INIT_INTERRUPT_MODE)
119 itime0 = current_time;
121 else if (iop == COMPUTE_INTERRUPT_MODE)
123 it = current_time - itime0;
125 else
126 # endif
128 et = current_time - time0;
129 ETnodes = NodeCnt + znodes;
131 if (et < 0)
133 # ifdef INTERRUPT_TEST
134 printf("elapsed time %ld not positive\n", et);
135 # endif
136 et = 0;
139 if (iop == COMPUTE_AND_INIT_MODE)
141 if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
142 flag.timeout = true;
144 time0 = current_time;
147 if (!NOT_CURSES)
149 # ifdef QUIETBACKGROUND
150 if (!background)
151 # endif /* QUIETBACKGROUND */
152 UpdateClocks();
158 void
159 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
161 struct timeval tv;
162 long current_time;
164 gettimeofday(&tv, NULL);
165 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
167 # ifdef INTERRUPT_TEST
168 if (iop == INIT_INTERRUPT_MODE)
170 itime0 = current_time;
172 else if (iop == COMPUTE_INTERRUPT_MODE)
174 it = current_time - itime0;
176 else
177 # endif
179 et = current_time - time0;
180 ETnodes = NodeCnt + znodes;
182 if (et < 0)
184 # ifdef INTERRUPT_TEST
185 printf("elapsed time %ld not positive\n", et);
186 # endif
187 et = 0;
190 if (iop == COMPUTE_AND_INIT_MODE)
192 if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
193 flag.timeout = true;
195 time0 = current_time;
198 if (!NOT_CURSES)
200 # ifdef QUIETBACKGROUND
201 if (!background)
202 # endif /* QUIETBACKGROUND */
203 UpdateClocks();
209 #else /* !HAVE_GETTIMEOFDAY */
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.
217 * iop = 0 calculate et, bump ETnodes
218 * iop = 1 calculate et, set timeout if time exceeded, calculate et
222 void
223 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
225 long current_time;
226 int nchar;
227 int i;
229 extern int errno;
232 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
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);
242 if (nchar)
244 if (!flag.timeout)
245 flag.back = true;
246 flag.bothsides = false;
249 et = ((current_time = time((long *) 0)) - time0) * 100;
251 #ifdef INTERRUPT_TEST
252 if (iop == INIT_INTERRUPT_MODE)
254 itime0 = current_time;
256 else if (iop == COMPUTE_INTERRUPT_MODE)
258 it = current_time - itime0;
260 else
261 #endif
263 ETnodes = NodeCnt + znodes;
265 if (et < 0)
267 #ifdef INTERRUPT_TEST
268 printf("elapsed time %ld not positive\n", et);
269 #endif
270 et = 0;
273 if (iop == COMPUTE_AND_INIT_MODE)
275 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
276 flag.timeout = true;
278 time0 = current_time;
281 if (!NOT_CURSES)
283 #ifdef QUIETBACKGROUND
284 if (!background)
285 #endif /* QUIETBACKGROUND */
286 UpdateClocks();
293 void
294 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
296 long current_time;
298 et = ((current_time = time((long *) 0)) - time0) * 100;
300 #ifdef INTERRUPT_TEST
301 if (iop == INIT_INTERRUPT_MODE)
303 itime0 = current_time;
305 else if (iop == COMPUTE_INTERRUPT_MODE)
307 it = current_time - itime0;
309 else
310 #endif
312 ETnodes = NodeCnt + znodes;
314 if (et < 0)
316 #ifdef INTERRUPT_TEST
317 printf("elapsed time %ld not positive\n", et);
318 #endif
319 et = 0;
322 if (iop == COMPUTE_AND_INIT_MODE)
324 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
325 flag.timeout = true;
327 time0 = current_time;
330 if (!NOT_CURSES)
332 #ifdef QUIETBACKGROUND
333 if (!background)
334 #endif /* QUIETBACKGROUND */
335 UpdateClocks();
341 #endif /* HAVE_GETTIMEOFDAY */