blob: e7bf1514b6900f896d58fa045c0baad136ddd962 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumf1644a51992-04-05 14:25:41 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Time module */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
28
29#include "modsupport.h"
30
31#include "sigtype.h"
32
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033#include <signal.h>
34#include <setjmp.h>
35
Guido van Rossum2762f251992-03-27 17:22:13 +000036#ifndef NO_UNISTD
37#include <unistd.h>
38#endif
39
Guido van Rossum80c9d881991-04-16 08:47:51 +000040/* What happens here is not trivial.
41 The BSD_TIME code needs <sys/time.h> (for struct timeval).
42 The rest of the code needs only time_t, except some MS-DOS
43 code which needs clock_t as well.
44 Standard C says that time_t is defined in <time.h>, and
45 does not have <sys/types.h>; THINK C agrees (MS-DOS too?).
46 What's worse, in pure 4.3 BSD, older SunOS versions, and
47 probably everything derived from BSD, you can't #include
48 both <time.h> and <sys/time.h> in the same file, since
49 <sys/time.h> includes <time.h> without any protection,
50 and <time.h> contains a typedef, which can't be parsed twice!
51 So on traditional UNIX systems we include <sys/types.h>
52 and <sys/time.h> and hope this implies <time.h> and time_t,
53 while on other systems, including conforming Standard C
54 systems (where 'unix' can't be defined), we rely on <time.h>.
55 Still one problem: BSD_TIME won't work with strict Standard C...
56*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
Guido van Rossum80c9d881991-04-16 08:47:51 +000058#ifdef unix
59#include <sys/types.h>
60#include <sys/time.h> /* Implies <time.h> everywhere, as far as I know */
61#else /* !unix */
62#include <time.h>
63#endif /* !unix */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064
Guido van Rossum27aaa6d1992-03-12 17:33:14 +000065#ifdef DO_TIMES
66#include <sys/times.h>
67#include <sys/param.h>
68#include <errno.h>
69#endif
70
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071/* Time methods */
72
73static object *
74time_time(self, args)
75 object *self;
76 object *args;
77{
Guido van Rossum6590d4a1991-04-04 10:49:03 +000078 time_t secs;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 if (!getnoarg(args))
80 return NULL;
Guido van Rossum80c9d881991-04-16 08:47:51 +000081 time(&secs);
Guido van Rossum00c567c1991-07-27 23:09:30 +000082#ifdef macintosh
83/* The Mac epoch is 1904, while UNIX uses 1970; Python prefers 1970 */
84/* Moreover, the Mac returns local time. This we cannot fix... */
Guido van Rossum6590d4a1991-04-04 10:49:03 +000085#define TIMEDIFF ((time_t) \
86 (((1970-1904)*365L + (1970-1904)/4) * 24 * 3600))
87 secs -= TIMEDIFF;
Guido van Rossum00c567c1991-07-27 23:09:30 +000088#endif
Guido van Rossum6590d4a1991-04-04 10:49:03 +000089 return newintobject((long)secs);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
92static jmp_buf sleep_intr;
93
Guido van Rossum2762f251992-03-27 17:22:13 +000094/* ARGSUSED */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095static void
96sleep_catcher(sig)
Guido van Rossum2762f251992-03-27 17:22:13 +000097 int sig; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
99 longjmp(sleep_intr, 1);
100}
101
102static object *
103time_sleep(self, args)
104 object *self;
105 object *args;
106{
107 int secs;
Guido van Rossum2762f251992-03-27 17:22:13 +0000108 SIGTYPE (*sigsave)() = 0; /* Initialized to shut lint up */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 if (!getintarg(args, &secs))
110 return NULL;
111 if (setjmp(sleep_intr)) {
112 signal(SIGINT, sigsave);
113 err_set(KeyboardInterrupt);
114 return NULL;
115 }
116 sigsave = signal(SIGINT, SIG_IGN);
117 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
118 signal(SIGINT, sleep_catcher);
119 sleep(secs);
120 signal(SIGINT, sigsave);
121 INCREF(None);
122 return None;
123}
124
Guido van Rossum1d2a9ad1991-06-24 22:23:45 +0000125#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126#define DO_MILLI
Guido van Rossum1d2a9ad1991-06-24 22:23:45 +0000127#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128
129#ifdef AMOEBA
130#define DO_MILLI
131extern long sys_milli();
132#define millitimer sys_milli
133#endif /* AMOEBA */
134
Guido van Rossum426035c1991-02-19 12:27:35 +0000135#ifdef BSD_TIME
136#define DO_MILLI
137#endif /* BSD_TIME */
138
Guido van Rossum80c9d881991-04-16 08:47:51 +0000139#ifdef TURBO_C
140#define DO_MILLI
141#endif
142
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143#ifdef DO_MILLI
144
145static object *
146time_millisleep(self, args)
147 object *self;
148 object *args;
149{
150 long msecs;
151 SIGTYPE (*sigsave)();
152 if (!getlongarg(args, &msecs))
153 return NULL;
154 if (setjmp(sleep_intr)) {
155 signal(SIGINT, sigsave);
156 err_set(KeyboardInterrupt);
157 return NULL;
158 }
159 sigsave = signal(SIGINT, SIG_IGN);
160 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
161 signal(SIGINT, sleep_catcher);
162 millisleep(msecs);
163 signal(SIGINT, sigsave);
164 INCREF(None);
165 return None;
166}
167
168static object *
169time_millitimer(self, args)
170 object *self;
171 object *args;
172{
173 long msecs;
174 extern long millitimer();
175 if (!getnoarg(args))
176 return NULL;
177 msecs = millitimer();
178 return newintobject(msecs);
179}
180
181#endif /* DO_MILLI */
182
Guido van Rossum27aaa6d1992-03-12 17:33:14 +0000183#ifdef DO_TIMES
184
185static object *
186time_times(self, args)
187 object *self;
188 object *args;
189{
190 struct tms t;
191 clock_t c;
192 object *tuple;
Guido van Rossum2762f251992-03-27 17:22:13 +0000193 if (!getnoarg(args))
194 return NULL;
Guido van Rossum27aaa6d1992-03-12 17:33:14 +0000195 errno = 0;
196 c = times(&t);
197 if (c == (clock_t) -1) {
198 err_errno(IOError);
199 return NULL;
200 }
201 tuple = newtupleobject(4);
202 if (tuple == NULL)
203 return NULL;
204 settupleitem(tuple, 0, newfloatobject((double)t.tms_utime / HZ));
205 settupleitem(tuple, 1, newfloatobject((double)t.tms_stime / HZ));
206 settupleitem(tuple, 2, newfloatobject((double)t.tms_cutime / HZ));
207 settupleitem(tuple, 3, newfloatobject((double)t.tms_cstime / HZ));
208 if (err_occurred()) {
209 DECREF(tuple);
210 return NULL;
211 }
212 return tuple;
213}
214
215#endif
216
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
218static struct methodlist time_methods[] = {
219#ifdef DO_MILLI
220 {"millisleep", time_millisleep},
221 {"millitimer", time_millitimer},
222#endif /* DO_MILLI */
Guido van Rossum27aaa6d1992-03-12 17:33:14 +0000223#ifdef DO_TIMES
224 {"times", time_times},
225#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226 {"sleep", time_sleep},
227 {"time", time_time},
228 {NULL, NULL} /* sentinel */
229};
230
231
232void
233inittime()
234{
235 initmodule("time", time_methods);
236}
237
238
Guido van Rossum1d2a9ad1991-06-24 22:23:45 +0000239#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240
241#define MacTicks (* (long *)0x16A)
242
Guido van Rossum80c9d881991-04-16 08:47:51 +0000243#ifdef THINK_C_3_0
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244sleep(msecs)
245 int msecs;
246{
247 register long deadline;
248
249 deadline = MacTicks + msecs * 60;
250 while (MacTicks < deadline) {
251 if (intrcheck())
252 sleep_catcher(SIGINT);
253 }
254}
Guido van Rossum80c9d881991-04-16 08:47:51 +0000255#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257millisleep(msecs)
258 long msecs;
259{
260 register long deadline;
261
262 deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */
263 while (MacTicks < deadline) {
264 if (intrcheck())
265 sleep_catcher(SIGINT);
266 }
267}
268
Guido van Rossum6590d4a1991-04-04 10:49:03 +0000269long
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270millitimer()
271{
272 return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */
273}
274
Guido van Rossum1d2a9ad1991-06-24 22:23:45 +0000275#endif /* macintosh */
Guido van Rossum426035c1991-02-19 12:27:35 +0000276
277
278#ifdef BSD_TIME
279
Guido van Rossum0bb1a511991-11-27 14:55:18 +0000280#ifdef _AIX /* I *think* this works */
Guido van Rossum6a1f54c1991-05-05 20:15:54 +0000281/* AIX defines fd_set in a separate file. Sigh... */
282#include <sys/select.h>
283#endif
284
Guido van Rossum56627461991-04-03 19:15:32 +0000285long
Guido van Rossum426035c1991-02-19 12:27:35 +0000286millitimer()
287{
288 struct timeval t;
289 struct timezone tz;
290 if (gettimeofday(&t, &tz) != 0)
291 return -1;
292 return t.tv_sec*1000 + t.tv_usec/1000;
293
294}
295
Guido van Rossum426035c1991-02-19 12:27:35 +0000296millisleep(msecs)
297 long msecs;
298{
299 struct timeval t;
300 t.tv_sec = msecs/1000;
301 t.tv_usec = (msecs%1000)*1000;
302 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
303}
304
305#endif /* BSD_TIME */
306
Guido van Rossum80c9d881991-04-16 08:47:51 +0000307
308#ifdef TURBO_C /* Maybe also for MS-DOS? */
309
310#ifndef CLOCKS_PER_SEC
311#define CLOCKS_PER_SEC 55 /* 54.945 msec per tick (18.2 HZ clock) */
312#endif
313
314static
315millisleep(msecs)
316 long msecs;
317{
318 delay(msecs);
319}
320
321static long
322millitimer()
323{
324 clock_t ticks;
325
326 ticks = clock(); /* ticks since program start */
327 return ticks * CLOCKS_PER_SEC;
328}
329
330#endif /* TURBO_C */