blob: 52d2eb2c9cf0447db7c0292374fafa91c20abf98 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
30******************************************************************/
31
32/* Thread module */
33/* Interface to Sjoerd's portable C thread library */
34
Barry Warsawd0c10421996-12-17 00:05:22 +000035#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036
Guido van Rossumb6775db1994-08-01 11:34:53 +000037#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +000038#error "Error! The rest of Python is not compiled with thread support."
39#error "Rerun configure, adding a --with-thread option."
40#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#endif
42
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043#include "thread.h"
44
Barry Warsawd0c10421996-12-17 00:05:22 +000045static PyObject *ThreadError;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000046
47
48/* Lock objects */
49
50typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000051 PyObject_HEAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052 type_lock lock_lock;
53} lockobject;
54
Barry Warsawd0c10421996-12-17 00:05:22 +000055staticforward PyTypeObject Locktype;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000056
57#define is_lockobject(v) ((v)->ob_type == &Locktype)
58
Guido van Rossumbcc20741998-08-04 22:53:56 +000059static type_lock
Sjoerd Mullendered59d201993-01-06 13:36:38 +000060getlocklock(lock)
Barry Warsawd0c10421996-12-17 00:05:22 +000061 PyObject *lock;
Sjoerd Mullendered59d201993-01-06 13:36:38 +000062{
63 if (lock == NULL || !is_lockobject(lock))
64 return NULL;
65 else
66 return ((lockobject *) lock)->lock_lock;
67}
68
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069static lockobject *
70newlockobject()
71{
72 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +000073 self = PyObject_NEW(lockobject, &Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000074 if (self == NULL)
75 return NULL;
76 self->lock_lock = allocate_lock();
77 if (self->lock_lock == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000078 PyMem_DEL(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000079 self = NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +000080 PyErr_SetString(ThreadError, "can't allocate lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000081 }
82 return self;
83}
84
85static void
86lock_dealloc(self)
87 lockobject *self;
88{
89 /* Unlock the lock so it's safe to free it */
90 acquire_lock(self->lock_lock, 0);
91 release_lock(self->lock_lock);
92
93 free_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000094 PyMem_DEL(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095}
96
Barry Warsawd0c10421996-12-17 00:05:22 +000097static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +000098lock_acquire_lock(self, args)
99 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000100 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000101{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102 int i;
103
104 if (args != NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000105 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106 return NULL;
107 }
108 else
109 i = 1;
110
Barry Warsawd0c10421996-12-17 00:05:22 +0000111 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112 i = acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +0000113 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114
115 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000116 Py_INCREF(Py_None);
117 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000118 }
119 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000120 return PyInt_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121}
122
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000123static char acquire_doc[] =
124"acquire([wait]) -> None or Boolean\n\
125(acquire_lock() is an obsolete synonym)\n\
126\n\
127Lock the lock. Without argument, this blocks if the lock is already\n\
128locked (even by the same thread), waiting for another thread to release\n\
129the lock, and return None when the lock is acquired.\n\
130With a Boolean argument, this will only block if the argument is true,\n\
131and the return value reflects whether the lock is acquired.\n\
132The blocking operation is not interruptible.";
133
Barry Warsawd0c10421996-12-17 00:05:22 +0000134static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135lock_release_lock(self, args)
136 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000137 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000138{
Barry Warsawd0c10421996-12-17 00:05:22 +0000139 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000140 return NULL;
141
142 /* Sanity check: the lock must be locked */
143 if (acquire_lock(self->lock_lock, 0)) {
144 release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000145 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146 return NULL;
147 }
148
149 release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000150 Py_INCREF(Py_None);
151 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000152}
153
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000154static char release_doc[] =
155"release()\n\
156(release_lock() is an obsolete synonym)\n\
157\n\
158Release the lock, allowing another thread that is blocked waiting for\n\
159the lock to acquire the lock. The lock must be in the locked state,\n\
160but it needn't be locked by the same thread that unlocks it.";
161
Barry Warsawd0c10421996-12-17 00:05:22 +0000162static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163lock_locked_lock(self, args)
164 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000165 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166{
Barry Warsawd0c10421996-12-17 00:05:22 +0000167 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000168 return NULL;
169
170 if (acquire_lock(self->lock_lock, 0)) {
171 release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000172 return PyInt_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000174 return PyInt_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175}
176
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000177static char locked_doc[] =
178"locked() -> Boolean\n\
179(locked_lock() is an obsolete synonym)\n\
180\n\
181Return whether the lock is in the locked state.";
182
Barry Warsawd0c10421996-12-17 00:05:22 +0000183static PyMethodDef lock_methods[] = {
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000184 {"acquire_lock", (PyCFunction)lock_acquire_lock, 0, acquire_doc},
185 {"acquire", (PyCFunction)lock_acquire_lock, 0, acquire_doc},
186 {"release_lock", (PyCFunction)lock_release_lock, 0, release_doc},
187 {"release", (PyCFunction)lock_release_lock, 0, release_doc},
188 {"locked_lock", (PyCFunction)lock_locked_lock, 0, locked_doc},
189 {"locked", (PyCFunction)lock_locked_lock, 0, locked_doc},
190 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191};
192
Barry Warsawd0c10421996-12-17 00:05:22 +0000193static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000194lock_getattr(self, name)
195 lockobject *self;
196 char *name;
197{
Barry Warsawd0c10421996-12-17 00:05:22 +0000198 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199}
200
Barry Warsawd0c10421996-12-17 00:05:22 +0000201static PyTypeObject Locktype = {
202 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203 0, /*ob_size*/
204 "lock", /*tp_name*/
205 sizeof(lockobject), /*tp_size*/
206 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208 (destructor)lock_dealloc, /*tp_dealloc*/
209 0, /*tp_print*/
210 (getattrfunc)lock_getattr, /*tp_getattr*/
211 0, /*tp_setattr*/
212 0, /*tp_compare*/
213 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000214};
215
216
217/* Module functions */
218
Guido van Rossuma027efa1997-05-05 20:56:21 +0000219struct bootstate {
220 PyInterpreterState *interp;
221 PyObject *func;
222 PyObject *args;
223 PyObject *keyw;
224};
225
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226static void
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227t_bootstrap(boot_raw)
228 void *boot_raw;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000231 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000232 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000233
Guido van Rossuma027efa1997-05-05 20:56:21 +0000234 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000235 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000236 res = PyEval_CallObjectWithKeywords(
237 boot->func, boot->args, boot->keyw);
238 Py_DECREF(boot->func);
239 Py_DECREF(boot->args);
240 Py_XDECREF(boot->keyw);
241 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000243 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000244 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000245 else {
246 fprintf(stderr, "Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000247 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000248 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000251 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000252 PyThreadState_Clear(tstate);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000253 PyEval_ReleaseThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000254 PyThreadState_Delete(tstate);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000255#ifdef __BEOS__
256 /* Dunno if this will cause problems with other ports; the BeOS thread
257 * support features only 100% renamed functions. [cjh]
258 */
259 PyThread_exit_thread();
260#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261 exit_thread();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000262#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000263}
264
Barry Warsawd0c10421996-12-17 00:05:22 +0000265static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +0000266thread_start_new_thread(self, fargs)
Barry Warsawd0c10421996-12-17 00:05:22 +0000267 PyObject *self; /* Not used */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000268 PyObject *fargs;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000270 PyObject *func, *args = NULL, *keyw = NULL;
271 struct bootstate *boot;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000272
Guido van Rossuma027efa1997-05-05 20:56:21 +0000273 if (!PyArg_ParseTuple(fargs, "OO|O", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000275 if (!PyCallable_Check(func)) {
276 PyErr_SetString(PyExc_TypeError,
277 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278 return NULL;
279 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000280 if (!PyTuple_Check(args)) {
281 PyErr_SetString(PyExc_TypeError,
282 "optional 2nd arg must be a tuple");
283 return NULL;
284 }
285 if (keyw != NULL && !PyDict_Check(keyw)) {
286 PyErr_SetString(PyExc_TypeError,
287 "optional 3rd arg must be a dictionary");
288 return NULL;
289 }
290 boot = PyMem_NEW(struct bootstate, 1);
291 if (boot == NULL)
292 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000293 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000294 boot->func = func;
295 boot->args = args;
296 boot->keyw = keyw;
297 Py_INCREF(func);
298 Py_INCREF(args);
299 Py_XINCREF(keyw);
300 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
301 if (!start_new_thread(t_bootstrap, (void*) boot)) {
302 PyErr_SetString(ThreadError, "can't start new thread\n");
303 Py_DECREF(func);
304 Py_DECREF(args);
305 Py_XDECREF(keyw);
306 PyMem_DEL(boot);
307 return NULL;
308 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000309 Py_INCREF(Py_None);
310 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311}
312
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000313static char start_new_doc[] =
314"start_new_thread(functon, args[, kwargs])\n\
315(start_new() is an obsolete synonym)\n\
316\n\
317Start a new thread. The thread will call the function with positional\n\
318arguments from the tuple args and keyword arguments taken from the optional\n\
319dictionary kwargs. The thread exits when the function returns; the return\n\
320value is ignored. The thread will also exit when the function raises an\n\
321unhandled exception; a stack trace will be printed unless the exception is\n\
322SystemExit.";
323
Barry Warsawd0c10421996-12-17 00:05:22 +0000324static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325thread_exit_thread(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000326 PyObject *self; /* Not used */
327 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328{
Barry Warsawd0c10421996-12-17 00:05:22 +0000329 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000331 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000332 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333}
334
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000335static char exit_doc[] =
336"exit()\n\
337(exit_thread() is an obsolete synonym)\n\
338\n\
339This is synonymous to ``raise SystemExit''. It will cause the current\n\
340thread to exit silently unless the exception is caught.";
341
Guido van Rossumb6775db1994-08-01 11:34:53 +0000342#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000343static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344thread_exit_prog(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000345 PyObject *self; /* Not used */
346 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347{
348 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000349 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000350 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000351 Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352 for (;;) { } /* Should not be reached */
353}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000354#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000355
Barry Warsawd0c10421996-12-17 00:05:22 +0000356static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000357thread_allocate_lock(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000358 PyObject *self; /* Not used */
359 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360{
Barry Warsawd0c10421996-12-17 00:05:22 +0000361 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000363 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364}
365
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000366static char allocate_doc[] =
367"allocate_lock() -> lock object\n\
368(allocate() is an obsolete synonym)\n\
369\n\
370Create a new lock object. See LockType.__doc__ for information about locks.";
371
Barry Warsawd0c10421996-12-17 00:05:22 +0000372static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000373thread_get_ident(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000374 PyObject *self; /* Not used */
375 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376{
377 long ident;
Barry Warsawd0c10421996-12-17 00:05:22 +0000378 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000379 return NULL;
380 ident = get_thread_ident();
381 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000382 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000383 return NULL;
384 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000385 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000386}
387
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000388static char get_ident_doc[] =
389"get_ident() -> integer\n\
390\n\
391Return a non-zero integer that uniquely identifies the current thread\n\
392amongst other threads that exist simultaneously.\n\
393This may be used to identify per-thread resources.\n\
394Even though on some platforms threads identities may appear to be\n\
395allocated consecutive numbers starting at 1, this behavior should not\n\
396be relied upon, and the number should be seen purely as a magic cookie.\n\
397A thread's identity may be reused for another thread after it exits.";
398
Barry Warsawd0c10421996-12-17 00:05:22 +0000399static PyMethodDef thread_methods[] = {
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000400 {"start_new_thread", (PyCFunction)thread_start_new_thread, 1,
401 start_new_doc},
402 {"start_new", (PyCFunction)thread_start_new_thread, 1,
403 start_new_doc},
404 {"allocate_lock", (PyCFunction)thread_allocate_lock, 0,
405 allocate_doc},
406 {"allocate", (PyCFunction)thread_allocate_lock, 0,
407 allocate_doc},
408 {"exit_thread", (PyCFunction)thread_exit_thread, 0,
409 exit_doc},
410 {"exit", (PyCFunction)thread_exit_thread, 0,
411 exit_doc},
412 {"get_ident", (PyCFunction)thread_get_ident, 0,
413 get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000415 {"exit_prog", (PyCFunction)thread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000416#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417 {NULL, NULL} /* sentinel */
418};
419
420
421/* Initialization function */
422
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000423static char thread_doc[] =
424"This module provides primitive operations to write multi-threaded programs.\n\
425The 'threading' module provides a more convenient interface.";
426
427static char lock_doc[] =
428"A lock object is a synchronization primitive. To create a lock,\n\
429call the allocate_lock() function. Methods are:\n\
430\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000431acquire() -- lock the lock, possibly blocking until it can be obtained\n\
432release() -- unlock of the lock\n\
433locked() -- test whether the lock is currently locked\n\
434\n\
435A lock is not owned by the thread that locked it; another thread may\n\
436unlock it. A thread attempting to lock a lock that it has already locked\n\
437will block until another thread unlocks it. Deadlocks may ensue.";
438
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439void
440initthread()
441{
Barry Warsawd0c10421996-12-17 00:05:22 +0000442 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000443
444 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000445 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000446
447 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000448 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000449 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000450 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000451 Locktype.tp_doc = lock_doc;
452 Py_INCREF(&Locktype);
453 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455 /* Initialize the C thread library */
456 init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000457}