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" |
| 28 | |
| 29 | #include "modsupport.h" |
| 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 | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 36 | #ifndef NO_UNISTD |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 40 | /* 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 58 | #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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 65 | #ifdef DO_TIMES |
| 66 | #include <sys/times.h> |
| 67 | #include <sys/param.h> |
| 68 | #include <errno.h> |
| 69 | #endif |
| 70 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | /* Time methods */ |
| 72 | |
| 73 | static object * |
| 74 | time_time(self, args) |
| 75 | object *self; |
| 76 | object *args; |
| 77 | { |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 78 | time_t secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | if (!getnoarg(args)) |
| 80 | return NULL; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 81 | time(&secs); |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 82 | #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 Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 85 | #define TIMEDIFF ((time_t) \ |
| 86 | (((1970-1904)*365L + (1970-1904)/4) * 24 * 3600)) |
| 87 | secs -= TIMEDIFF; |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 88 | #endif |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 89 | return newintobject((long)secs); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static jmp_buf sleep_intr; |
| 93 | |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 94 | /* ARGSUSED */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | static void |
| 96 | sleep_catcher(sig) |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 97 | int sig; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | { |
| 99 | longjmp(sleep_intr, 1); |
| 100 | } |
| 101 | |
| 102 | static object * |
| 103 | time_sleep(self, args) |
| 104 | object *self; |
| 105 | object *args; |
| 106 | { |
| 107 | int secs; |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 108 | SIGTYPE (*sigsave)() = 0; /* Initialized to shut lint up */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | 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 Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 125 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | #define DO_MILLI |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 127 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef AMOEBA |
| 130 | #define DO_MILLI |
| 131 | extern long sys_milli(); |
| 132 | #define millitimer sys_milli |
| 133 | #endif /* AMOEBA */ |
| 134 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 135 | #ifdef BSD_TIME |
| 136 | #define DO_MILLI |
| 137 | #endif /* BSD_TIME */ |
| 138 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 139 | #ifdef TURBO_C |
| 140 | #define DO_MILLI |
| 141 | #endif |
| 142 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | #ifdef DO_MILLI |
| 144 | |
| 145 | static object * |
| 146 | time_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 | |
| 168 | static object * |
| 169 | time_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 Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 183 | #ifdef DO_TIMES |
| 184 | |
| 185 | static object * |
| 186 | time_times(self, args) |
| 187 | object *self; |
| 188 | object *args; |
| 189 | { |
| 190 | struct tms t; |
| 191 | clock_t c; |
| 192 | object *tuple; |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 193 | if (!getnoarg(args)) |
| 194 | return NULL; |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 195 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | |
| 218 | static struct methodlist time_methods[] = { |
| 219 | #ifdef DO_MILLI |
| 220 | {"millisleep", time_millisleep}, |
| 221 | {"millitimer", time_millitimer}, |
| 222 | #endif /* DO_MILLI */ |
Guido van Rossum | 27aaa6d | 1992-03-12 17:33:14 +0000 | [diff] [blame] | 223 | #ifdef DO_TIMES |
| 224 | {"times", time_times}, |
| 225 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | {"sleep", time_sleep}, |
| 227 | {"time", time_time}, |
| 228 | {NULL, NULL} /* sentinel */ |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | void |
| 233 | inittime() |
| 234 | { |
| 235 | initmodule("time", time_methods); |
| 236 | } |
| 237 | |
| 238 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 239 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | |
| 241 | #define MacTicks (* (long *)0x16A) |
| 242 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 243 | #ifdef THINK_C_3_0 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | sleep(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 Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 255 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | millisleep(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 Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 269 | long |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | millitimer() |
| 271 | { |
| 272 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 273 | } |
| 274 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 275 | #endif /* macintosh */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 276 | |
| 277 | |
| 278 | #ifdef BSD_TIME |
| 279 | |
Guido van Rossum | 0bb1a51 | 1991-11-27 14:55:18 +0000 | [diff] [blame] | 280 | #ifdef _AIX /* I *think* this works */ |
Guido van Rossum | 6a1f54c | 1991-05-05 20:15:54 +0000 | [diff] [blame] | 281 | /* AIX defines fd_set in a separate file. Sigh... */ |
| 282 | #include <sys/select.h> |
| 283 | #endif |
| 284 | |
Guido van Rossum | 5662746 | 1991-04-03 19:15:32 +0000 | [diff] [blame] | 285 | long |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 286 | millitimer() |
| 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 Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 296 | millisleep(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 Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 307 | |
| 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 | |
| 314 | static |
| 315 | millisleep(msecs) |
| 316 | long msecs; |
| 317 | { |
| 318 | delay(msecs); |
| 319 | } |
| 320 | |
| 321 | static long |
| 322 | millitimer() |
| 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 */ |