Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Time module */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | #include "allobjects.h" |
| 4 | |
| 5 | #include "modsupport.h" |
| 6 | |
| 7 | #include "sigtype.h" |
| 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | #include <signal.h> |
| 10 | #include <setjmp.h> |
| 11 | |
| 12 | #ifdef __STDC__ |
| 13 | #include <time.h> |
| 14 | #else /* !__STDC__ */ |
| 15 | typedef unsigned long time_t; |
| 16 | extern time_t time(); |
| 17 | #endif /* !__STDC__ */ |
| 18 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | |
| 20 | /* Time methods */ |
| 21 | |
| 22 | static object * |
| 23 | time_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 | |
| 34 | static jmp_buf sleep_intr; |
| 35 | |
| 36 | static void |
| 37 | sleep_catcher(sig) |
| 38 | int sig; |
| 39 | { |
| 40 | longjmp(sleep_intr, 1); |
| 41 | } |
| 42 | |
| 43 | static object * |
| 44 | time_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 |
| 72 | extern long sys_milli(); |
| 73 | #define millitimer sys_milli |
| 74 | #endif /* AMOEBA */ |
| 75 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame^] | 76 | #ifdef BSD_TIME |
| 77 | #define DO_MILLI |
| 78 | #endif /* BSD_TIME */ |
| 79 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | #ifdef DO_MILLI |
| 81 | |
| 82 | static object * |
| 83 | time_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 | |
| 105 | static object * |
| 106 | time_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 | |
| 121 | static 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 | |
| 132 | void |
| 133 | inittime() |
| 134 | { |
| 135 | initmodule("time", time_methods); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | #ifdef THINK_C |
| 140 | |
| 141 | #define MacTicks (* (long *)0x16A) |
| 142 | |
| 143 | static |
| 144 | sleep(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 | |
| 156 | static |
| 157 | millisleep(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 | |
| 169 | static long |
| 170 | millitimer() |
| 171 | { |
| 172 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 173 | } |
| 174 | |
| 175 | #endif /* THINK_C */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame^] | 176 | |
| 177 | |
| 178 | #ifdef BSD_TIME |
| 179 | |
| 180 | #include <sys/types.h> |
| 181 | #include <sys/time.h> |
| 182 | |
| 183 | static long |
| 184 | millitimer() |
| 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 | |
| 194 | static |
| 195 | millisleep(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 | |