Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | f1644a5 | 1992-04-05 14:25:41 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Time module */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | |
| 31 | #include "sigtype.h" |
| 32 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <setjmp.h> |
| 35 | |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame^] | 36 | #ifdef BSD_TIME |
| 37 | #define HAVE_GETTIMEOFDAY |
| 38 | #endif |
| 39 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 40 | #ifdef macintosh |
| 41 | #define NO_UNISTD |
| 42 | #endif |
| 43 | |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 44 | #ifndef NO_UNISTD |
| 45 | #include <unistd.h> |
| 46 | #endif |
| 47 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 48 | /* What happens here is not trivial. |
| 49 | The BSD_TIME code needs <sys/time.h> (for struct timeval). |
| 50 | The rest of the code needs only time_t, except some MS-DOS |
| 51 | code which needs clock_t as well. |
| 52 | Standard C says that time_t is defined in <time.h>, and |
| 53 | does not have <sys/types.h>; THINK C agrees (MS-DOS too?). |
| 54 | What's worse, in pure 4.3 BSD, older SunOS versions, and |
| 55 | probably everything derived from BSD, you can't #include |
| 56 | both <time.h> and <sys/time.h> in the same file, since |
| 57 | <sys/time.h> includes <time.h> without any protection, |
| 58 | and <time.h> contains a typedef, which can't be parsed twice! |
| 59 | So on traditional UNIX systems we include <sys/types.h> |
| 60 | and <sys/time.h> and hope this implies <time.h> and time_t, |
| 61 | while on other systems, including conforming Standard C |
| 62 | systems (where 'unix' can't be defined), we rely on <time.h>. |
| 63 | Still one problem: BSD_TIME won't work with strict Standard C... |
| 64 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 66 | #ifdef unix |
| 67 | #include <sys/types.h> |
| 68 | #include <sys/time.h> /* Implies <time.h> everywhere, as far as I know */ |
| 69 | #else /* !unix */ |
| 70 | #include <time.h> |
| 71 | #endif /* !unix */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 73 | #ifdef DO_TIMES |
| 74 | #include <sys/times.h> |
| 75 | #include <sys/param.h> |
| 76 | #include <errno.h> |
| 77 | #endif |
| 78 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | /* Time methods */ |
| 80 | |
| 81 | static object * |
| 82 | time_time(self, args) |
| 83 | object *self; |
| 84 | object *args; |
| 85 | { |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame^] | 86 | #ifdef HAVE_GETTIMEOFDAY |
| 87 | struct timeval t; |
| 88 | struct timezone tz; |
| 89 | if (!getnoarg(args)) |
| 90 | return NULL; |
| 91 | if (gettimeofday(&t, &tz) != 0) { |
| 92 | err_errno(IOError); |
| 93 | return NULL; |
| 94 | } |
| 95 | return newfloatobject(t.tv_sec*1.0 + t.tv_usec*0.000001); |
| 96 | #else /* !HAVE_GETTIMEOFDAY */ |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 97 | time_t secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | if (!getnoarg(args)) |
| 99 | return NULL; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 100 | time(&secs); |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 101 | #ifdef macintosh |
| 102 | /* The Mac epoch is 1904, while UNIX uses 1970; Python prefers 1970 */ |
| 103 | /* Moreover, the Mac returns local time. This we cannot fix... */ |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 104 | #define TIMEDIFF ((time_t) \ |
| 105 | (((1970-1904)*365L + (1970-1904)/4) * 24 * 3600)) |
| 106 | secs -= TIMEDIFF; |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame^] | 108 | return newfloatobject((double)secs); |
| 109 | #endif /* !HAVE_GETTIMEOFDAY */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static jmp_buf sleep_intr; |
| 113 | |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 114 | /* ARGSUSED */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | static void |
| 116 | sleep_catcher(sig) |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 117 | int sig; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | { |
| 119 | longjmp(sleep_intr, 1); |
| 120 | } |
| 121 | |
| 122 | static object * |
| 123 | time_sleep(self, args) |
| 124 | object *self; |
| 125 | object *args; |
| 126 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 127 | long secs; |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 128 | SIGTYPE (*sigsave)() = 0; /* Initialized to shut lint up */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 129 | if (!getargs(args, "l", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 131 | BGN_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | if (setjmp(sleep_intr)) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 133 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | signal(SIGINT, sigsave); |
| 135 | err_set(KeyboardInterrupt); |
| 136 | return NULL; |
| 137 | } |
| 138 | sigsave = signal(SIGINT, SIG_IGN); |
| 139 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 140 | signal(SIGINT, sleep_catcher); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 141 | #ifdef BSD_TIME |
| 142 | longsleep(secs); |
| 143 | #else |
| 144 | sleep((int)secs); |
| 145 | #endif |
| 146 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | signal(SIGINT, sigsave); |
| 148 | INCREF(None); |
| 149 | return None; |
| 150 | } |
| 151 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 152 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | #define DO_MILLI |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | |
| 156 | #ifdef AMOEBA |
| 157 | #define DO_MILLI |
| 158 | extern long sys_milli(); |
| 159 | #define millitimer sys_milli |
| 160 | #endif /* AMOEBA */ |
| 161 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 162 | #ifdef BSD_TIME |
| 163 | #define DO_MILLI |
| 164 | #endif /* BSD_TIME */ |
| 165 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 166 | #ifdef TURBO_C |
| 167 | #define DO_MILLI |
| 168 | #endif |
| 169 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | #ifdef DO_MILLI |
| 171 | |
| 172 | static object * |
| 173 | time_millisleep(self, args) |
| 174 | object *self; |
| 175 | object *args; |
| 176 | { |
| 177 | long msecs; |
| 178 | SIGTYPE (*sigsave)(); |
| 179 | if (!getlongarg(args, &msecs)) |
| 180 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 181 | BGN_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | if (setjmp(sleep_intr)) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 183 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | signal(SIGINT, sigsave); |
| 185 | err_set(KeyboardInterrupt); |
| 186 | return NULL; |
| 187 | } |
| 188 | sigsave = signal(SIGINT, SIG_IGN); |
| 189 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 190 | signal(SIGINT, sleep_catcher); |
| 191 | millisleep(msecs); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 192 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | signal(SIGINT, sigsave); |
| 194 | INCREF(None); |
| 195 | return None; |
| 196 | } |
| 197 | |
| 198 | static object * |
| 199 | time_millitimer(self, args) |
| 200 | object *self; |
| 201 | object *args; |
| 202 | { |
| 203 | long msecs; |
| 204 | extern long millitimer(); |
| 205 | if (!getnoarg(args)) |
| 206 | return NULL; |
| 207 | msecs = millitimer(); |
| 208 | return newintobject(msecs); |
| 209 | } |
| 210 | |
| 211 | #endif /* DO_MILLI */ |
| 212 | |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 213 | #ifdef DO_TIMES |
| 214 | |
| 215 | static object * |
| 216 | time_times(self, args) |
| 217 | object *self; |
| 218 | object *args; |
| 219 | { |
| 220 | struct tms t; |
| 221 | clock_t c; |
| 222 | object *tuple; |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 223 | if (!getnoarg(args)) |
| 224 | return NULL; |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 225 | errno = 0; |
| 226 | c = times(&t); |
| 227 | if (c == (clock_t) -1) { |
| 228 | err_errno(IOError); |
| 229 | return NULL; |
| 230 | } |
| 231 | tuple = newtupleobject(4); |
| 232 | if (tuple == NULL) |
| 233 | return NULL; |
| 234 | settupleitem(tuple, 0, newfloatobject((double)t.tms_utime / HZ)); |
| 235 | settupleitem(tuple, 1, newfloatobject((double)t.tms_stime / HZ)); |
| 236 | settupleitem(tuple, 2, newfloatobject((double)t.tms_cutime / HZ)); |
| 237 | settupleitem(tuple, 3, newfloatobject((double)t.tms_cstime / HZ)); |
| 238 | if (err_occurred()) { |
| 239 | DECREF(tuple); |
| 240 | return NULL; |
| 241 | } |
| 242 | return tuple; |
| 243 | } |
| 244 | |
| 245 | #endif |
| 246 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | |
| 248 | static struct methodlist time_methods[] = { |
| 249 | #ifdef DO_MILLI |
| 250 | {"millisleep", time_millisleep}, |
| 251 | {"millitimer", time_millitimer}, |
| 252 | #endif /* DO_MILLI */ |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 253 | #ifdef DO_TIMES |
| 254 | {"times", time_times}, |
| 255 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | {"sleep", time_sleep}, |
| 257 | {"time", time_time}, |
| 258 | {NULL, NULL} /* sentinel */ |
| 259 | }; |
| 260 | |
| 261 | |
| 262 | void |
| 263 | inittime() |
| 264 | { |
| 265 | initmodule("time", time_methods); |
| 266 | } |
| 267 | |
| 268 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 269 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | |
| 271 | #define MacTicks (* (long *)0x16A) |
| 272 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 273 | #ifdef THINK_C_3_0 |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 274 | sleep(secs) |
| 275 | int secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | { |
| 277 | register long deadline; |
| 278 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 279 | deadline = MacTicks + mecs * 60; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | while (MacTicks < deadline) { |
| 281 | if (intrcheck()) |
| 282 | sleep_catcher(SIGINT); |
| 283 | } |
| 284 | } |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 285 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | millisleep(msecs) |
| 288 | long msecs; |
| 289 | { |
| 290 | register long deadline; |
| 291 | |
| 292 | deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */ |
| 293 | while (MacTicks < deadline) { |
| 294 | if (intrcheck()) |
| 295 | sleep_catcher(SIGINT); |
| 296 | } |
| 297 | } |
| 298 | |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 299 | long |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | millitimer() |
| 301 | { |
| 302 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 303 | } |
| 304 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 305 | #endif /* macintosh */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | #ifdef BSD_TIME |
| 309 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 310 | #include "myselect.h" /* Implies <sys/types.h>, <sys/time.h>, <sys/param.h> */ |
Guido van Rossum | 6a1f54c | 1991-05-05 20:15:54 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 5662746 | 1991-04-03 19:15:32 +0000 | [diff] [blame] | 312 | long |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 313 | millitimer() |
| 314 | { |
| 315 | struct timeval t; |
| 316 | struct timezone tz; |
| 317 | if (gettimeofday(&t, &tz) != 0) |
| 318 | return -1; |
| 319 | return t.tv_sec*1000 + t.tv_usec/1000; |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 322 | millisleep(msecs) |
| 323 | long msecs; |
| 324 | { |
| 325 | struct timeval t; |
| 326 | t.tv_sec = msecs/1000; |
| 327 | t.tv_usec = (msecs%1000)*1000; |
| 328 | (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 329 | } |
| 330 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 331 | longsleep(secs) |
| 332 | long secs; |
| 333 | { |
| 334 | struct timeval t; |
| 335 | t.tv_sec = secs; |
| 336 | t.tv_usec = 0; |
| 337 | (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 338 | } |
| 339 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 340 | #endif /* BSD_TIME */ |
| 341 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 342 | |
| 343 | #ifdef TURBO_C /* Maybe also for MS-DOS? */ |
| 344 | |
| 345 | #ifndef CLOCKS_PER_SEC |
| 346 | #define CLOCKS_PER_SEC 55 /* 54.945 msec per tick (18.2 HZ clock) */ |
| 347 | #endif |
| 348 | |
| 349 | static |
| 350 | millisleep(msecs) |
| 351 | long msecs; |
| 352 | { |
| 353 | delay(msecs); |
| 354 | } |
| 355 | |
| 356 | static long |
| 357 | millitimer() |
| 358 | { |
| 359 | clock_t ticks; |
| 360 | |
| 361 | ticks = clock(); /* ticks since program start */ |
| 362 | return ticks * CLOCKS_PER_SEC; |
| 363 | } |
| 364 | |
| 365 | #endif /* TURBO_C */ |