blob: 2d8c863e125fa81cfd6d3f146d25274b76b41a2a [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
Sjoerd Mullendered59d201993-01-06 13:36:38 +000059type_lock
60getlocklock(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 Rossum1984f1e1992-08-04 12:41:02 +0000255 exit_thread();
256}
257
Barry Warsawd0c10421996-12-17 00:05:22 +0000258static PyObject *
Guido van Rossuma027efa1997-05-05 20:56:21 +0000259thread_start_new_thread(self, fargs)
Barry Warsawd0c10421996-12-17 00:05:22 +0000260 PyObject *self; /* Not used */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000261 PyObject *fargs;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000263 PyObject *func, *args = NULL, *keyw = NULL;
264 struct bootstate *boot;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265
Guido van Rossuma027efa1997-05-05 20:56:21 +0000266 if (!PyArg_ParseTuple(fargs, "OO|O", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000268 if (!PyCallable_Check(func)) {
269 PyErr_SetString(PyExc_TypeError,
270 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271 return NULL;
272 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000273 if (!PyTuple_Check(args)) {
274 PyErr_SetString(PyExc_TypeError,
275 "optional 2nd arg must be a tuple");
276 return NULL;
277 }
278 if (keyw != NULL && !PyDict_Check(keyw)) {
279 PyErr_SetString(PyExc_TypeError,
280 "optional 3rd arg must be a dictionary");
281 return NULL;
282 }
283 boot = PyMem_NEW(struct bootstate, 1);
284 if (boot == NULL)
285 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000286 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000287 boot->func = func;
288 boot->args = args;
289 boot->keyw = keyw;
290 Py_INCREF(func);
291 Py_INCREF(args);
292 Py_XINCREF(keyw);
293 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
294 if (!start_new_thread(t_bootstrap, (void*) boot)) {
295 PyErr_SetString(ThreadError, "can't start new thread\n");
296 Py_DECREF(func);
297 Py_DECREF(args);
298 Py_XDECREF(keyw);
299 PyMem_DEL(boot);
300 return NULL;
301 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000302 Py_INCREF(Py_None);
303 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304}
305
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000306static char start_new_doc[] =
307"start_new_thread(functon, args[, kwargs])\n\
308(start_new() is an obsolete synonym)\n\
309\n\
310Start a new thread. The thread will call the function with positional\n\
311arguments from the tuple args and keyword arguments taken from the optional\n\
312dictionary kwargs. The thread exits when the function returns; the return\n\
313value is ignored. The thread will also exit when the function raises an\n\
314unhandled exception; a stack trace will be printed unless the exception is\n\
315SystemExit.";
316
Barry Warsawd0c10421996-12-17 00:05:22 +0000317static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318thread_exit_thread(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000319 PyObject *self; /* Not used */
320 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000321{
Barry Warsawd0c10421996-12-17 00:05:22 +0000322 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000324 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000325 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326}
327
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000328static char exit_doc[] =
329"exit()\n\
330(exit_thread() is an obsolete synonym)\n\
331\n\
332This is synonymous to ``raise SystemExit''. It will cause the current\n\
333thread to exit silently unless the exception is caught.";
334
Guido van Rossumb6775db1994-08-01 11:34:53 +0000335#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000336static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337thread_exit_prog(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000338 PyObject *self; /* Not used */
339 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340{
341 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000342 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000343 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000344 Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000345 for (;;) { } /* Should not be reached */
346}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000347#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000348
Barry Warsawd0c10421996-12-17 00:05:22 +0000349static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000350thread_allocate_lock(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000351 PyObject *self; /* Not used */
352 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000353{
Barry Warsawd0c10421996-12-17 00:05:22 +0000354 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000355 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000356 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000357}
358
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000359static char allocate_doc[] =
360"allocate_lock() -> lock object\n\
361(allocate() is an obsolete synonym)\n\
362\n\
363Create a new lock object. See LockType.__doc__ for information about locks.";
364
Barry Warsawd0c10421996-12-17 00:05:22 +0000365static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000366thread_get_ident(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000367 PyObject *self; /* Not used */
368 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000369{
370 long ident;
Barry Warsawd0c10421996-12-17 00:05:22 +0000371 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372 return NULL;
373 ident = get_thread_ident();
374 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000375 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376 return NULL;
377 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000378 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000379}
380
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000381static char get_ident_doc[] =
382"get_ident() -> integer\n\
383\n\
384Return a non-zero integer that uniquely identifies the current thread\n\
385amongst other threads that exist simultaneously.\n\
386This may be used to identify per-thread resources.\n\
387Even though on some platforms threads identities may appear to be\n\
388allocated consecutive numbers starting at 1, this behavior should not\n\
389be relied upon, and the number should be seen purely as a magic cookie.\n\
390A thread's identity may be reused for another thread after it exits.";
391
Barry Warsawd0c10421996-12-17 00:05:22 +0000392static PyMethodDef thread_methods[] = {
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000393 {"start_new_thread", (PyCFunction)thread_start_new_thread, 1,
394 start_new_doc},
395 {"start_new", (PyCFunction)thread_start_new_thread, 1,
396 start_new_doc},
397 {"allocate_lock", (PyCFunction)thread_allocate_lock, 0,
398 allocate_doc},
399 {"allocate", (PyCFunction)thread_allocate_lock, 0,
400 allocate_doc},
401 {"exit_thread", (PyCFunction)thread_exit_thread, 0,
402 exit_doc},
403 {"exit", (PyCFunction)thread_exit_thread, 0,
404 exit_doc},
405 {"get_ident", (PyCFunction)thread_get_ident, 0,
406 get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000407#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000408 {"exit_prog", (PyCFunction)thread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000409#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000410 {NULL, NULL} /* sentinel */
411};
412
413
414/* Initialization function */
415
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000416static char thread_doc[] =
417"This module provides primitive operations to write multi-threaded programs.\n\
418The 'threading' module provides a more convenient interface.";
419
420static char lock_doc[] =
421"A lock object is a synchronization primitive. To create a lock,\n\
422call the allocate_lock() function. Methods are:\n\
423\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000424acquire() -- lock the lock, possibly blocking until it can be obtained\n\
425release() -- unlock of the lock\n\
426locked() -- test whether the lock is currently locked\n\
427\n\
428A lock is not owned by the thread that locked it; another thread may\n\
429unlock it. A thread attempting to lock a lock that it has already locked\n\
430will block until another thread unlocks it. Deadlocks may ensue.";
431
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432void
433initthread()
434{
Barry Warsawd0c10421996-12-17 00:05:22 +0000435 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436
437 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000438 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439
440 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000441 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000442 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000443 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000444 Locktype.tp_doc = lock_doc;
445 Py_INCREF(&Locktype);
446 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000447
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000448 /* Initialize the C thread library */
449 init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450}