blob: 9ea1f870187090d8711130209d4eade176d8274f [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Time module */
2
Guido van Rossum3f5da241990-12-20 15:06:42 +00003#include "allobjects.h"
4
5#include "modsupport.h"
6
7#include "sigtype.h"
8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009#include <signal.h>
10#include <setjmp.h>
11
12#ifdef __STDC__
13#include <time.h>
14#else /* !__STDC__ */
15typedef unsigned long time_t;
16extern time_t time();
17#endif /* !__STDC__ */
18
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019
20/* Time methods */
21
22static object *
23time_time(self, args)
24 object *self;
25 object *args;
26{
27 long secs;
28 if (!getnoarg(args))
29 return NULL;
30 secs = time((time_t *)NULL);
31 return newintobject(secs);
32}
33
34static jmp_buf sleep_intr;
35
36static void
37sleep_catcher(sig)
38 int sig;
39{
40 longjmp(sleep_intr, 1);
41}
42
43static object *
44time_sleep(self, args)
45 object *self;
46 object *args;
47{
48 int secs;
49 SIGTYPE (*sigsave)();
50 if (!getintarg(args, &secs))
51 return NULL;
52 if (setjmp(sleep_intr)) {
53 signal(SIGINT, sigsave);
54 err_set(KeyboardInterrupt);
55 return NULL;
56 }
57 sigsave = signal(SIGINT, SIG_IGN);
58 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
59 signal(SIGINT, sleep_catcher);
60 sleep(secs);
61 signal(SIGINT, sigsave);
62 INCREF(None);
63 return None;
64}
65
66#ifdef THINK_C
67#define DO_MILLI
68#endif /* THINK_C */
69
70#ifdef AMOEBA
71#define DO_MILLI
72extern long sys_milli();
73#define millitimer sys_milli
74#endif /* AMOEBA */
75
Guido van Rossum426035c1991-02-19 12:27:35 +000076#ifdef BSD_TIME
77#define DO_MILLI
78#endif /* BSD_TIME */
79
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080#ifdef DO_MILLI
81
82static object *
83time_millisleep(self, args)
84 object *self;
85 object *args;
86{
87 long msecs;
88 SIGTYPE (*sigsave)();
89 if (!getlongarg(args, &msecs))
90 return NULL;
91 if (setjmp(sleep_intr)) {
92 signal(SIGINT, sigsave);
93 err_set(KeyboardInterrupt);
94 return NULL;
95 }
96 sigsave = signal(SIGINT, SIG_IGN);
97 if (sigsave != (SIGTYPE (*)()) SIG_IGN)
98 signal(SIGINT, sleep_catcher);
99 millisleep(msecs);
100 signal(SIGINT, sigsave);
101 INCREF(None);
102 return None;
103}
104
105static object *
106time_millitimer(self, args)
107 object *self;
108 object *args;
109{
110 long msecs;
111 extern long millitimer();
112 if (!getnoarg(args))
113 return NULL;
114 msecs = millitimer();
115 return newintobject(msecs);
116}
117
118#endif /* DO_MILLI */
119
120
121static struct methodlist time_methods[] = {
122#ifdef DO_MILLI
123 {"millisleep", time_millisleep},
124 {"millitimer", time_millitimer},
125#endif /* DO_MILLI */
126 {"sleep", time_sleep},
127 {"time", time_time},
128 {NULL, NULL} /* sentinel */
129};
130
131
132void
133inittime()
134{
135 initmodule("time", time_methods);
136}
137
138
139#ifdef THINK_C
140
141#define MacTicks (* (long *)0x16A)
142
143static
144sleep(msecs)
145 int msecs;
146{
147 register long deadline;
148
149 deadline = MacTicks + msecs * 60;
150 while (MacTicks < deadline) {
151 if (intrcheck())
152 sleep_catcher(SIGINT);
153 }
154}
155
156static
157millisleep(msecs)
158 long msecs;
159{
160 register long deadline;
161
162 deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */
163 while (MacTicks < deadline) {
164 if (intrcheck())
165 sleep_catcher(SIGINT);
166 }
167}
168
169static long
170millitimer()
171{
172 return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */
173}
174
175#endif /* THINK_C */
Guido van Rossum426035c1991-02-19 12:27:35 +0000176
177
178#ifdef BSD_TIME
179
180#include <sys/types.h>
181#include <sys/time.h>
182
183static long
184millitimer()
185{
186 struct timeval t;
187 struct timezone tz;
188 if (gettimeofday(&t, &tz) != 0)
189 return -1;
190 return t.tv_sec*1000 + t.tv_usec/1000;
191
192}
193
194static
195millisleep(msecs)
196 long msecs;
197{
198 struct timeval t;
199 t.tv_sec = msecs/1000;
200 t.tv_usec = (msecs%1000)*1000;
201 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
202}
203
204#endif /* BSD_TIME */
205