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 | |
| 76 | #ifdef DO_MILLI |
| 77 | |
| 78 | static object * |
| 79 | time_millisleep(self, args) |
| 80 | object *self; |
| 81 | object *args; |
| 82 | { |
| 83 | long msecs; |
| 84 | SIGTYPE (*sigsave)(); |
| 85 | if (!getlongarg(args, &msecs)) |
| 86 | return NULL; |
| 87 | if (setjmp(sleep_intr)) { |
| 88 | signal(SIGINT, sigsave); |
| 89 | err_set(KeyboardInterrupt); |
| 90 | return NULL; |
| 91 | } |
| 92 | sigsave = signal(SIGINT, SIG_IGN); |
| 93 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 94 | signal(SIGINT, sleep_catcher); |
| 95 | millisleep(msecs); |
| 96 | signal(SIGINT, sigsave); |
| 97 | INCREF(None); |
| 98 | return None; |
| 99 | } |
| 100 | |
| 101 | static object * |
| 102 | time_millitimer(self, args) |
| 103 | object *self; |
| 104 | object *args; |
| 105 | { |
| 106 | long msecs; |
| 107 | extern long millitimer(); |
| 108 | if (!getnoarg(args)) |
| 109 | return NULL; |
| 110 | msecs = millitimer(); |
| 111 | return newintobject(msecs); |
| 112 | } |
| 113 | |
| 114 | #endif /* DO_MILLI */ |
| 115 | |
| 116 | |
| 117 | static struct methodlist time_methods[] = { |
| 118 | #ifdef DO_MILLI |
| 119 | {"millisleep", time_millisleep}, |
| 120 | {"millitimer", time_millitimer}, |
| 121 | #endif /* DO_MILLI */ |
| 122 | {"sleep", time_sleep}, |
| 123 | {"time", time_time}, |
| 124 | {NULL, NULL} /* sentinel */ |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | void |
| 129 | inittime() |
| 130 | { |
| 131 | initmodule("time", time_methods); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | #ifdef THINK_C |
| 136 | |
| 137 | #define MacTicks (* (long *)0x16A) |
| 138 | |
| 139 | static |
| 140 | sleep(msecs) |
| 141 | int msecs; |
| 142 | { |
| 143 | register long deadline; |
| 144 | |
| 145 | deadline = MacTicks + msecs * 60; |
| 146 | while (MacTicks < deadline) { |
| 147 | if (intrcheck()) |
| 148 | sleep_catcher(SIGINT); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static |
| 153 | millisleep(msecs) |
| 154 | long msecs; |
| 155 | { |
| 156 | register long deadline; |
| 157 | |
| 158 | deadline = MacTicks + msecs * 3 / 50; /* msecs * 60 / 1000 */ |
| 159 | while (MacTicks < deadline) { |
| 160 | if (intrcheck()) |
| 161 | sleep_catcher(SIGINT); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static long |
| 166 | millitimer() |
| 167 | { |
| 168 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 169 | } |
| 170 | |
| 171 | #endif /* THINK_C */ |