Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992 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 | |
| 25 | /* Thread module */ |
| 26 | /* Interface to Sjoerd's portable C thread library */ |
| 27 | |
| 28 | #include "allobjects.h" |
| 29 | #include "modsupport.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 30 | #include "ceval.h" |
| 31 | |
| 32 | #include "thread.h" |
| 33 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 34 | object *ThreadError; |
| 35 | |
| 36 | |
| 37 | /* Lock objects */ |
| 38 | |
| 39 | typedef struct { |
| 40 | OB_HEAD |
| 41 | type_lock lock_lock; |
| 42 | } lockobject; |
| 43 | |
| 44 | extern typeobject Locktype; /* Really static, forward */ |
| 45 | |
| 46 | #define is_lockobject(v) ((v)->ob_type == &Locktype) |
| 47 | |
| 48 | static lockobject * |
| 49 | newlockobject() |
| 50 | { |
| 51 | lockobject *self; |
| 52 | self = NEWOBJ(lockobject, &Locktype); |
| 53 | if (self == NULL) |
| 54 | return NULL; |
| 55 | self->lock_lock = allocate_lock(); |
| 56 | if (self->lock_lock == NULL) { |
| 57 | DEL(self); |
| 58 | self = NULL; |
| 59 | err_setstr(ThreadError, "can't allocate lock"); |
| 60 | } |
| 61 | return self; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | lock_dealloc(self) |
| 66 | lockobject *self; |
| 67 | { |
| 68 | /* Unlock the lock so it's safe to free it */ |
| 69 | acquire_lock(self->lock_lock, 0); |
| 70 | release_lock(self->lock_lock); |
| 71 | |
| 72 | free_lock(self->lock_lock); |
| 73 | DEL(self); |
| 74 | } |
| 75 | |
| 76 | static object * |
| 77 | lock_acquire_lock(self, args) |
| 78 | lockobject *self; |
| 79 | object *args; |
| 80 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 81 | int i; |
| 82 | |
| 83 | if (args != NULL) { |
| 84 | if (!getargs(args, "i", &i)) |
| 85 | return NULL; |
| 86 | } |
| 87 | else |
| 88 | i = 1; |
| 89 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame^] | 90 | BGN_SAVE |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 91 | i = acquire_lock(self->lock_lock, i); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame^] | 92 | END_SAVE |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 93 | |
| 94 | if (args == NULL) { |
| 95 | INCREF(None); |
| 96 | return None; |
| 97 | } |
| 98 | else |
| 99 | return newintobject((long)i); |
| 100 | } |
| 101 | |
| 102 | static object * |
| 103 | lock_release_lock(self, args) |
| 104 | lockobject *self; |
| 105 | object *args; |
| 106 | { |
| 107 | if (!getnoarg(args)) |
| 108 | return NULL; |
| 109 | |
| 110 | /* Sanity check: the lock must be locked */ |
| 111 | if (acquire_lock(self->lock_lock, 0)) { |
| 112 | release_lock(self->lock_lock); |
| 113 | err_setstr(ThreadError, "release unlocked lock"); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | release_lock(self->lock_lock); |
| 118 | INCREF(None); |
| 119 | return None; |
| 120 | } |
| 121 | |
| 122 | static object * |
| 123 | lock_locked_lock(self, args) |
| 124 | lockobject *self; |
| 125 | object *args; |
| 126 | { |
| 127 | if (!getnoarg(args)) |
| 128 | return NULL; |
| 129 | |
| 130 | if (acquire_lock(self->lock_lock, 0)) { |
| 131 | release_lock(self->lock_lock); |
| 132 | return newintobject(0L); |
| 133 | } |
| 134 | return newintobject(1L); |
| 135 | } |
| 136 | |
| 137 | static struct methodlist lock_methods[] = { |
| 138 | {"acquire_lock", lock_acquire_lock}, |
| 139 | {"acquire", lock_acquire_lock}, |
| 140 | {"release_lock", lock_release_lock}, |
| 141 | {"release", lock_release_lock}, |
| 142 | {"locked_lock", lock_locked_lock}, |
| 143 | {"locked", lock_locked_lock}, |
| 144 | {NULL, NULL} /* sentinel */ |
| 145 | }; |
| 146 | |
| 147 | static object * |
| 148 | lock_getattr(self, name) |
| 149 | lockobject *self; |
| 150 | char *name; |
| 151 | { |
| 152 | return findmethod(lock_methods, (object *)self, name); |
| 153 | } |
| 154 | |
| 155 | static typeobject Locktype = { |
| 156 | OB_HEAD_INIT(&Typetype) |
| 157 | 0, /*ob_size*/ |
| 158 | "lock", /*tp_name*/ |
| 159 | sizeof(lockobject), /*tp_size*/ |
| 160 | 0, /*tp_itemsize*/ |
| 161 | /* methods */ |
| 162 | lock_dealloc, /*tp_dealloc*/ |
| 163 | 0, /*tp_print*/ |
| 164 | lock_getattr, /*tp_getattr*/ |
| 165 | 0, /*tp_setattr*/ |
| 166 | 0, /*tp_compare*/ |
| 167 | 0, /*tp_repr*/ |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | /* Module functions */ |
| 172 | |
| 173 | static void |
| 174 | t_bootstrap(args_raw) |
| 175 | void *args_raw; |
| 176 | { |
| 177 | object *args = (object *) args_raw; |
| 178 | object *func, *arg, *res; |
| 179 | |
| 180 | restore_thread((void *)NULL); |
| 181 | func = gettupleitem(args, 0); |
| 182 | arg = gettupleitem(args, 1); |
| 183 | res = call_object(func, arg); |
| 184 | DECREF(arg); /* Matches the INCREF(arg) in thread_start_new_thread */ |
| 185 | if (res == NULL) { |
| 186 | fprintf(stderr, "Unhandled exception in thread:\n"); |
| 187 | print_error(); /* From pythonmain.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 188 | } |
| 189 | (void) save_thread(); |
| 190 | exit_thread(); |
| 191 | } |
| 192 | |
| 193 | static object * |
| 194 | thread_start_new_thread(self, args) |
| 195 | object *self; /* Not used */ |
| 196 | object *args; |
| 197 | { |
| 198 | object *func, *arg; |
| 199 | |
| 200 | if (!getargs(args, "(OO)", &func, &arg)) |
| 201 | return NULL; |
| 202 | INCREF(args); |
| 203 | if (!start_new_thread(t_bootstrap, (void*) args)) { |
| 204 | DECREF(args); |
| 205 | err_setstr(ThreadError, "can't start new thread\n"); |
| 206 | return NULL; |
| 207 | } |
| 208 | /* Otherwise the DECREF(args) is done by t_bootstrap */ |
| 209 | INCREF(None); |
| 210 | return None; |
| 211 | } |
| 212 | |
| 213 | static object * |
| 214 | thread_exit_thread(self, args) |
| 215 | object *self; /* Not used */ |
| 216 | object *args; |
| 217 | { |
| 218 | if (!getnoarg(args)) |
| 219 | return NULL; |
| 220 | (void) save_thread(); |
| 221 | exit_thread(); |
| 222 | for (;;) { } /* Should not be reached */ |
| 223 | } |
| 224 | |
| 225 | static object * |
| 226 | thread_exit_prog(self, args) |
| 227 | object *self; /* Not used */ |
| 228 | object *args; |
| 229 | { |
| 230 | int sts; |
| 231 | if (!getargs(args, "i", &sts)) |
| 232 | return NULL; |
| 233 | goaway(sts); |
| 234 | for (;;) { } /* Should not be reached */ |
| 235 | } |
| 236 | |
| 237 | static object * |
| 238 | thread_allocate_lock(self, args) |
| 239 | object *self; /* Not used */ |
| 240 | object *args; |
| 241 | { |
| 242 | if (!getnoarg(args)) |
| 243 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame^] | 244 | return (object *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static struct methodlist thread_methods[] = { |
| 248 | {"start_new_thread", thread_start_new_thread}, |
| 249 | {"start_new", thread_start_new_thread}, |
| 250 | {"allocate_lock", thread_allocate_lock}, |
| 251 | {"allocate", thread_allocate_lock}, |
| 252 | {"exit_thread", thread_exit_thread}, |
| 253 | {"exit", thread_exit_thread}, |
| 254 | {"exit_prog", thread_exit_prog}, |
| 255 | {NULL, NULL} /* sentinel */ |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | /* Initialization function */ |
| 260 | |
| 261 | void |
| 262 | initthread() |
| 263 | { |
| 264 | object *m, *d, *x; |
| 265 | |
| 266 | /* Create the module and add the functions */ |
| 267 | m = initmodule("thread", thread_methods); |
| 268 | |
| 269 | /* Add a symbolic constant */ |
| 270 | d = getmoduledict(m); |
| 271 | ThreadError = newstringobject("thread.error"); |
| 272 | INCREF(ThreadError); |
| 273 | dictinsert(d, "error", ThreadError); |
| 274 | |
| 275 | /* Check for errors */ |
| 276 | if (err_occurred()) |
| 277 | fatal("can't initialize module thread"); |
| 278 | |
| 279 | /* Initialize the C thread library */ |
| 280 | init_thread(); |
| 281 | |
| 282 | /* Initialize the interpreter's stack save/restore mechanism */ |
| 283 | init_save_thread(); |
| 284 | } |