blob: b1c1ce395bcf1dba691d2a5e925d5b3211731547 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
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
36#ifdef __STDC__
37#include <time.h>
38#else /* !__STDC__ */
39typedef unsigned long time_t;
40extern time_t time();
41#endif /* !__STDC__ */
42
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
44/* Time methods */
45
46static object *
47time_time(self, args)
48 object *self;
49 object *args;
50{
51 long secs;
52 if (!getnoarg(args))
53 return NULL;
54 secs = time((time_t *)NULL);
55 return newintobject(secs);
56}
57
58static jmp_buf sleep_intr;
59
60static void
61sleep_catcher(sig)
62 int sig;
63{
64 longjmp(sleep_intr, 1);
65}
66
67static object *
68time_sleep(self, args)
69 object *self;
70 object *args;
71{
72 int secs;
73 SIGTYPE (*sigsave)();
74 if (!getintarg(args, &secs))
75 return NULL;
76 if (setjmp(sleep_intr)) {
77 signal(SIGINT, sigsave);
78 err_set(KeyboardInterrupt);
79 return NULL;
80 }
81 sigsave = signal(SIGINT, SIG_IGN);
82 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
83 signal(SIGINT, sleep_catcher);
84 sleep(secs);
85 signal(SIGINT, sigsave);
86 INCREF(None);
87 return None;
88}
89
90#ifdef THINK_C
91#define DO_MILLI
92#endif /* THINK_C */
93
94#ifdef AMOEBA
95#define DO_MILLI
96extern long sys_milli();
97#define millitimer sys_milli
98#endif /* AMOEBA */
99
Guido van Rossum426035c1991-02-19 12:27:35 +0000100#ifdef BSD_TIME
101#define DO_MILLI
102#endif /* BSD_TIME */
103
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104#ifdef DO_MILLI
105
106static object *
107time_millisleep(self, args)
108 object *self;
109 object *args;
110{
111 long msecs;
112 SIGTYPE (*sigsave)();
113 if (!getlongarg(args, &msecs))
114 return NULL;
115 if (setjmp(sleep_intr)) {
116 signal(SIGINT, sigsave);
117 err_set(KeyboardInterrupt);
118 return NULL;
119 }
120 sigsave = signal(SIGINT, SIG_IGN);
121 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
122 signal(SIGINT, sleep_catcher);
123 millisleep(msecs);
124 signal(SIGINT, sigsave);
125 INCREF(None);
126 return None;
127}
128
129static object *
130time_millitimer(self, args)
131 object *self;
132 object *args;
133{
134 long msecs;
135 extern long millitimer();
136 if (!getnoarg(args))
137 return NULL;
138 msecs = millitimer();
139 return newintobject(msecs);
140}
141
142#endif /* DO_MILLI */
143
144
145static struct methodlist time_methods[] = {
146#ifdef DO_MILLI
147 {"millisleep", time_millisleep},
148 {"millitimer", time_millitimer},
149#endif /* DO_MILLI */
150 {"sleep", time_sleep},
151 {"time", time_time},
152 {NULL, NULL} /* sentinel */
153};
154
155
156void
157inittime()
158{
159 initmodule("time", time_methods);
160}
161
162
163#ifdef THINK_C
164
165#define MacTicks (* (long *)0x16A)
166
167static
168sleep(msecs)
169 int msecs;
170{
171 register long deadline;
172
173 deadline = MacTicks + msecs * 60;
174 while (MacTicks < deadline) {
175 if (intrcheck())
176 sleep_catcher(SIGINT);
177 }
178}
179
180static
181millisleep(msecs)
182 long msecs;
183{
184 register long deadline;
185
186 deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */
187 while (MacTicks < deadline) {
188 if (intrcheck())
189 sleep_catcher(SIGINT);
190 }
191}
192
193static long
194millitimer()
195{
196 return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */
197}
198
199#endif /* THINK_C */
Guido van Rossum426035c1991-02-19 12:27:35 +0000200
201
202#ifdef BSD_TIME
203
204#include <sys/types.h>
205#include <sys/time.h>
206
207static long
208millitimer()
209{
210 struct timeval t;
211 struct timezone tz;
212 if (gettimeofday(&t, &tz) != 0)
213 return -1;
214 return t.tv_sec*1000 + t.tv_usec/1000;
215
216}
217
218static
219millisleep(msecs)
220 long msecs;
221{
222 struct timeval t;
223 t.tv_sec = msecs/1000;
224 t.tv_usec = (msecs%1000)*1000;
225 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
226}
227
228#endif /* BSD_TIME */
229