Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame^] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 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 | |
| 36 | #ifdef __STDC__ |
| 37 | #include <time.h> |
| 38 | #else /* !__STDC__ */ |
| 39 | typedef unsigned long time_t; |
| 40 | extern time_t time(); |
| 41 | #endif /* !__STDC__ */ |
| 42 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | |
| 44 | /* Time methods */ |
| 45 | |
| 46 | static object * |
| 47 | time_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 | |
| 58 | static jmp_buf sleep_intr; |
| 59 | |
| 60 | static void |
| 61 | sleep_catcher(sig) |
| 62 | int sig; |
| 63 | { |
| 64 | longjmp(sleep_intr, 1); |
| 65 | } |
| 66 | |
| 67 | static object * |
| 68 | time_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 |
| 96 | extern long sys_milli(); |
| 97 | #define millitimer sys_milli |
| 98 | #endif /* AMOEBA */ |
| 99 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 100 | #ifdef BSD_TIME |
| 101 | #define DO_MILLI |
| 102 | #endif /* BSD_TIME */ |
| 103 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | #ifdef DO_MILLI |
| 105 | |
| 106 | static object * |
| 107 | time_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 | |
| 129 | static object * |
| 130 | time_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 | |
| 145 | static 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 | |
| 156 | void |
| 157 | inittime() |
| 158 | { |
| 159 | initmodule("time", time_methods); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | #ifdef THINK_C |
| 164 | |
| 165 | #define MacTicks (* (long *)0x16A) |
| 166 | |
| 167 | static |
| 168 | sleep(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 | |
| 180 | static |
| 181 | millisleep(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 | |
| 193 | static long |
| 194 | millitimer() |
| 195 | { |
| 196 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 197 | } |
| 198 | |
| 199 | #endif /* THINK_C */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | #ifdef BSD_TIME |
| 203 | |
| 204 | #include <sys/types.h> |
| 205 | #include <sys/time.h> |
| 206 | |
| 207 | static long |
| 208 | millitimer() |
| 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 | |
| 218 | static |
| 219 | millisleep(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 | |