Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame^] | 1 | /* Time module */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <signal.h> |
| 5 | #include <setjmp.h> |
| 6 | |
| 7 | #ifdef __STDC__ |
| 8 | #include <time.h> |
| 9 | #else /* !__STDC__ */ |
| 10 | typedef unsigned long time_t; |
| 11 | extern time_t time(); |
| 12 | #endif /* !__STDC__ */ |
| 13 | |
| 14 | #include "PROTO.h" |
| 15 | #include "object.h" |
| 16 | #include "intobject.h" |
| 17 | #include "dictobject.h" |
| 18 | #include "methodobject.h" |
| 19 | #include "moduleobject.h" |
| 20 | #include "objimpl.h" |
| 21 | #include "import.h" |
| 22 | #include "sigtype.h" |
| 23 | #include "modsupport.h" |
| 24 | #include "errors.h" |
| 25 | |
| 26 | |
| 27 | /* Time methods */ |
| 28 | |
| 29 | static object * |
| 30 | time_time(self, args) |
| 31 | object *self; |
| 32 | object *args; |
| 33 | { |
| 34 | long secs; |
| 35 | if (!getnoarg(args)) |
| 36 | return NULL; |
| 37 | secs = time((time_t *)NULL); |
| 38 | return newintobject(secs); |
| 39 | } |
| 40 | |
| 41 | static jmp_buf sleep_intr; |
| 42 | |
| 43 | static void |
| 44 | sleep_catcher(sig) |
| 45 | int sig; |
| 46 | { |
| 47 | longjmp(sleep_intr, 1); |
| 48 | } |
| 49 | |
| 50 | static object * |
| 51 | time_sleep(self, args) |
| 52 | object *self; |
| 53 | object *args; |
| 54 | { |
| 55 | int secs; |
| 56 | SIGTYPE (*sigsave)(); |
| 57 | if (!getintarg(args, &secs)) |
| 58 | return NULL; |
| 59 | if (setjmp(sleep_intr)) { |
| 60 | signal(SIGINT, sigsave); |
| 61 | err_set(KeyboardInterrupt); |
| 62 | return NULL; |
| 63 | } |
| 64 | sigsave = signal(SIGINT, SIG_IGN); |
| 65 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 66 | signal(SIGINT, sleep_catcher); |
| 67 | sleep(secs); |
| 68 | signal(SIGINT, sigsave); |
| 69 | INCREF(None); |
| 70 | return None; |
| 71 | } |
| 72 | |
| 73 | #ifdef THINK_C |
| 74 | #define DO_MILLI |
| 75 | #endif /* THINK_C */ |
| 76 | |
| 77 | #ifdef AMOEBA |
| 78 | #define DO_MILLI |
| 79 | extern long sys_milli(); |
| 80 | #define millitimer sys_milli |
| 81 | #endif /* AMOEBA */ |
| 82 | |
| 83 | #ifdef DO_MILLI |
| 84 | |
| 85 | static object * |
| 86 | time_millisleep(self, args) |
| 87 | object *self; |
| 88 | object *args; |
| 89 | { |
| 90 | long msecs; |
| 91 | SIGTYPE (*sigsave)(); |
| 92 | if (!getlongarg(args, &msecs)) |
| 93 | return NULL; |
| 94 | if (setjmp(sleep_intr)) { |
| 95 | signal(SIGINT, sigsave); |
| 96 | err_set(KeyboardInterrupt); |
| 97 | return NULL; |
| 98 | } |
| 99 | sigsave = signal(SIGINT, SIG_IGN); |
| 100 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 101 | signal(SIGINT, sleep_catcher); |
| 102 | millisleep(msecs); |
| 103 | signal(SIGINT, sigsave); |
| 104 | INCREF(None); |
| 105 | return None; |
| 106 | } |
| 107 | |
| 108 | static object * |
| 109 | time_millitimer(self, args) |
| 110 | object *self; |
| 111 | object *args; |
| 112 | { |
| 113 | long msecs; |
| 114 | extern long millitimer(); |
| 115 | if (!getnoarg(args)) |
| 116 | return NULL; |
| 117 | msecs = millitimer(); |
| 118 | return newintobject(msecs); |
| 119 | } |
| 120 | |
| 121 | #endif /* DO_MILLI */ |
| 122 | |
| 123 | |
| 124 | static struct methodlist time_methods[] = { |
| 125 | #ifdef DO_MILLI |
| 126 | {"millisleep", time_millisleep}, |
| 127 | {"millitimer", time_millitimer}, |
| 128 | #endif /* DO_MILLI */ |
| 129 | {"sleep", time_sleep}, |
| 130 | {"time", time_time}, |
| 131 | {NULL, NULL} /* sentinel */ |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | void |
| 136 | inittime() |
| 137 | { |
| 138 | initmodule("time", time_methods); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | #ifdef THINK_C |
| 143 | |
| 144 | #define MacTicks (* (long *)0x16A) |
| 145 | |
| 146 | static |
| 147 | sleep(msecs) |
| 148 | int msecs; |
| 149 | { |
| 150 | register long deadline; |
| 151 | |
| 152 | deadline = MacTicks + msecs * 60; |
| 153 | while (MacTicks < deadline) { |
| 154 | if (intrcheck()) |
| 155 | sleep_catcher(SIGINT); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static |
| 160 | millisleep(msecs) |
| 161 | long msecs; |
| 162 | { |
| 163 | register long deadline; |
| 164 | |
| 165 | deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */ |
| 166 | while (MacTicks < deadline) { |
| 167 | if (intrcheck()) |
| 168 | sleep_catcher(SIGINT); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static long |
| 173 | millitimer() |
| 174 | { |
| 175 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 176 | } |
| 177 | |
| 178 | #endif /* THINK_C */ |