blob: 21313b53aca051349d1ab1b1483cbcca305cbde3 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Thread module */
3/* Interface to Sjoerd's portable C thread library */
4
Barry Warsawd0c10421996-12-17 00:05:22 +00005#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00006
Guido van Rossumb6775db1994-08-01 11:34:53 +00007#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +00008#error "Error! The rest of Python is not compiled with thread support."
9#error "Rerun configure, adding a --with-thread option."
10#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000011#endif
12
Guido van Rossum49b56061998-10-01 20:42:43 +000013#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014
Barry Warsawd0c10421996-12-17 00:05:22 +000015static PyObject *ThreadError;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
17
18/* Lock objects */
19
20typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000021 PyObject_HEAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000022 PyThread_type_lock lock_lock;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000023} lockobject;
24
Barry Warsawd0c10421996-12-17 00:05:22 +000025staticforward PyTypeObject Locktype;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026
Guido van Rossum1984f1e1992-08-04 12:41:02 +000027static lockobject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000028newlockobject(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029{
30 lockobject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000031 self = PyObject_New(lockobject, &Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032 if (self == NULL)
33 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +000034 self->lock_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035 if (self->lock_lock == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000036 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000037 self = NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +000038 PyErr_SetString(ThreadError, "can't allocate lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039 }
40 return self;
41}
42
43static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000044lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045{
46 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000047 PyThread_acquire_lock(self->lock_lock, 0);
48 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000049
Guido van Rossum65d5b571998-12-21 19:32:43 +000050 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052}
53
Barry Warsawd0c10421996-12-17 00:05:22 +000054static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000055lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000056{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000057 int i;
58
59 if (args != NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000060 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum1984f1e1992-08-04 12:41:02 +000061 return NULL;
62 }
63 else
64 i = 1;
65
Barry Warsawd0c10421996-12-17 00:05:22 +000066 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000067 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000068 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069
70 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000071 Py_INCREF(Py_None);
72 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000073 }
74 else
Barry Warsawd0c10421996-12-17 00:05:22 +000075 return PyInt_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000076}
77
Guido van Rossum75e9fc31998-06-27 18:21:06 +000078static char acquire_doc[] =
79"acquire([wait]) -> None or Boolean\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +000080(PyThread_acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000081\n\
82Lock the lock. Without argument, this blocks if the lock is already\n\
83locked (even by the same thread), waiting for another thread to release\n\
84the lock, and return None when the lock is acquired.\n\
85With a Boolean argument, this will only block if the argument is true,\n\
86and the return value reflects whether the lock is acquired.\n\
87The blocking operation is not interruptible.";
88
Barry Warsawd0c10421996-12-17 00:05:22 +000089static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000090lock_PyThread_release_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000091{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +000093 if (PyThread_acquire_lock(self->lock_lock, 0)) {
94 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000095 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096 return NULL;
97 }
98
Guido van Rossum65d5b571998-12-21 19:32:43 +000099 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000100 Py_INCREF(Py_None);
101 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102}
103
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000104static char release_doc[] =
105"release()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000106(PyThread_release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000107\n\
108Release the lock, allowing another thread that is blocked waiting for\n\
109the lock to acquire the lock. The lock must be in the locked state,\n\
110but it needn't be locked by the same thread that unlocks it.";
111
Barry Warsawd0c10421996-12-17 00:05:22 +0000112static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000113lock_locked_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000115 if (PyThread_acquire_lock(self->lock_lock, 0)) {
116 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000117 return PyInt_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000118 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000119 return PyInt_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120}
121
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000122static char locked_doc[] =
123"locked() -> Boolean\n\
124(locked_lock() is an obsolete synonym)\n\
125\n\
126Return whether the lock is in the locked state.";
127
Barry Warsawd0c10421996-12-17 00:05:22 +0000128static PyMethodDef lock_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000129 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
130 METH_OLDARGS, acquire_doc},
131 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
132 METH_OLDARGS, acquire_doc},
133 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000134 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000135 {"release", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000136 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000137 {"locked_lock", (PyCFunction)lock_locked_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000138 METH_NOARGS, locked_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000139 {"locked", (PyCFunction)lock_locked_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000140 METH_NOARGS, locked_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000141 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000142};
143
Barry Warsawd0c10421996-12-17 00:05:22 +0000144static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000145lock_getattr(lockobject *self, char *name)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146{
Barry Warsawd0c10421996-12-17 00:05:22 +0000147 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000148}
149
Barry Warsawd0c10421996-12-17 00:05:22 +0000150static PyTypeObject Locktype = {
151 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000153 "thread.lock", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154 sizeof(lockobject), /*tp_size*/
155 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157 (destructor)lock_dealloc, /*tp_dealloc*/
158 0, /*tp_print*/
159 (getattrfunc)lock_getattr, /*tp_getattr*/
160 0, /*tp_setattr*/
161 0, /*tp_compare*/
162 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163};
164
165
166/* Module functions */
167
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168struct bootstate {
169 PyInterpreterState *interp;
170 PyObject *func;
171 PyObject *args;
172 PyObject *keyw;
173};
174
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000176t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000179 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000183 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184 res = PyEval_CallObjectWithKeywords(
185 boot->func, boot->args, boot->keyw);
186 Py_DECREF(boot->func);
187 Py_DECREF(boot->args);
188 Py_XDECREF(boot->keyw);
189 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000190 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000191 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000192 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000193 else {
Fred Drake9c801ab2000-10-20 20:02:37 +0000194 PySys_WriteStderr("Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000195 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000196 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000198 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000199 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000200 PyThreadState_Clear(tstate);
Guido van Rossum2528b192001-01-23 01:47:18 +0000201 PyThreadState_DeleteCurrent();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000202 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203}
204
Barry Warsawd0c10421996-12-17 00:05:22 +0000205static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000206thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207{
Guido van Rossum38d45b72000-09-01 20:47:58 +0000208 PyObject *func, *args, *keyw = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000209 struct bootstate *boot;
Guido van Rossum3c288632001-10-16 21:13:49 +0000210 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Guido van Rossum43713e52000-02-29 13:59:29 +0000212 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214 if (!PyCallable_Check(func)) {
215 PyErr_SetString(PyExc_TypeError,
216 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217 return NULL;
218 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000219 if (!PyTuple_Check(args)) {
220 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38d45b72000-09-01 20:47:58 +0000221 "2nd arg must be a tuple");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000222 return NULL;
223 }
224 if (keyw != NULL && !PyDict_Check(keyw)) {
225 PyErr_SetString(PyExc_TypeError,
226 "optional 3rd arg must be a dictionary");
227 return NULL;
228 }
229 boot = PyMem_NEW(struct bootstate, 1);
230 if (boot == NULL)
231 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000232 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233 boot->func = func;
234 boot->args = args;
235 boot->keyw = keyw;
236 Py_INCREF(func);
237 Py_INCREF(args);
238 Py_XINCREF(keyw);
239 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum3c288632001-10-16 21:13:49 +0000240 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
241 if (ident == -1) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000242 PyErr_SetString(ThreadError, "can't start new thread\n");
243 Py_DECREF(func);
244 Py_DECREF(args);
245 Py_XDECREF(keyw);
246 PyMem_DEL(boot);
247 return NULL;
248 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000249 return PyInt_FromLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250}
251
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000252static char start_new_doc[] =
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000253"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000254(start_new() is an obsolete synonym)\n\
255\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000256Start a new thread and return its identifier. The thread will call the\n\
257function with positional arguments from the tuple args and keyword arguments\n\
258taken from the optional dictionary kwargs. The thread exits when the\n\
259function returns; the return value is ignored. The thread will also exit\n\
260when the function raises an unhandled exception; a stack trace will be\n\
261printed unless the exception is SystemExit.\n";
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000262
Barry Warsawd0c10421996-12-17 00:05:22 +0000263static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000264thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Barry Warsawd0c10421996-12-17 00:05:22 +0000266 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000267 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268}
269
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000270static char exit_doc[] =
271"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000273\n\
274This is synonymous to ``raise SystemExit''. It will cause the current\n\
275thread to exit silently unless the exception is caught.";
276
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000278static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000279thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000280{
281 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000282 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000284 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285 for (;;) { } /* Should not be reached */
286}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000288
Barry Warsawd0c10421996-12-17 00:05:22 +0000289static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000290thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291{
Barry Warsawd0c10421996-12-17 00:05:22 +0000292 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293}
294
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000295static char allocate_doc[] =
296"allocate_lock() -> lock object\n\
297(allocate() is an obsolete synonym)\n\
298\n\
299Create a new lock object. See LockType.__doc__ for information about locks.";
300
Barry Warsawd0c10421996-12-17 00:05:22 +0000301static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000302thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303{
304 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000305 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000306 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000307 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000308 return NULL;
309 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000310 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311}
312
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000313static char get_ident_doc[] =
314"get_ident() -> integer\n\
315\n\
316Return a non-zero integer that uniquely identifies the current thread\n\
317amongst other threads that exist simultaneously.\n\
318This may be used to identify per-thread resources.\n\
319Even though on some platforms threads identities may appear to be\n\
320allocated consecutive numbers starting at 1, this behavior should not\n\
321be relied upon, and the number should be seen purely as a magic cookie.\n\
322A thread's identity may be reused for another thread after it exits.";
323
Barry Warsawd0c10421996-12-17 00:05:22 +0000324static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000325 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
326 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000327 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000328 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
329 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000330 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000331 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000332 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000333 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000334 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000335 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000336 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000337 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000338 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000339 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000340 METH_NOARGS, get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000341#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000342 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000343#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344 {NULL, NULL} /* sentinel */
345};
346
347
348/* Initialization function */
349
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000350static char thread_doc[] =
351"This module provides primitive operations to write multi-threaded programs.\n\
352The 'threading' module provides a more convenient interface.";
353
354static char lock_doc[] =
355"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000356call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000357\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000358acquire() -- lock the lock, possibly blocking until it can be obtained\n\
359release() -- unlock of the lock\n\
360locked() -- test whether the lock is currently locked\n\
361\n\
362A lock is not owned by the thread that locked it; another thread may\n\
363unlock it. A thread attempting to lock a lock that it has already locked\n\
364will block until another thread unlocks it. Deadlocks may ensue.";
365
Guido van Rossum3886bb61998-12-04 18:50:17 +0000366DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000367initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368{
Barry Warsawd0c10421996-12-17 00:05:22 +0000369 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000370
371 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000372 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373
374 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000375 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000376 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000377 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000378 Locktype.tp_doc = lock_doc;
379 Py_INCREF(&Locktype);
380 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000382 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000383 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384}