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