blob: 62fd76ada340ba8d6102236c5aed920531268e9d [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."
Neal Norwitz884baa12002-09-05 21:31:04 +00009#error "Rerun configure, adding a --with-threads option."
Guido van Rossuma027efa1997-05-05 20:56:21 +000010#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
Jeremy Hylton938ace62002-07-17 16:30:39 +000025static 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{
Neal Norwitzba3a16c2002-03-31 15:27:00 +000057 int i = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000058
Neal Norwitzba3a16c2002-03-31 15:27:00 +000059 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
60 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000061
Barry Warsawd0c10421996-12-17 00:05:22 +000062 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000063 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000064 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065
66 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000067 Py_INCREF(Py_None);
68 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069 }
70 else
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000071 return PyBool_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000072}
73
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(acquire_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000075"acquire([wait]) -> None or bool\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +000076(PyThread_acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000077\n\
78Lock the lock. Without argument, this blocks if the lock is already\n\
79locked (even by the same thread), waiting for another thread to release\n\
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000080the lock, and return None once the lock is acquired.\n\
81With an argument, this will only block if the argument is true,\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000082and the return value reflects whether the lock is acquired.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083The blocking operation is not interruptible.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000084
Barry Warsawd0c10421996-12-17 00:05:22 +000085static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000086lock_PyThread_release_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000088 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +000089 if (PyThread_acquire_lock(self->lock_lock, 0)) {
90 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000091 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092 return NULL;
93 }
94
Guido van Rossum65d5b571998-12-21 19:32:43 +000095 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000096 Py_INCREF(Py_None);
97 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000098}
99
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100PyDoc_STRVAR(release_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000101"release()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000102(PyThread_release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000103\n\
104Release the lock, allowing another thread that is blocked waiting for\n\
105the lock to acquire the lock. The lock must be in the locked state,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106but it needn't be locked by the same thread that unlocks it.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000107
Barry Warsawd0c10421996-12-17 00:05:22 +0000108static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000109lock_locked_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000111 if (PyThread_acquire_lock(self->lock_lock, 0)) {
112 PyThread_release_lock(self->lock_lock);
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000113 return PyBool_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114 }
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000115 return PyBool_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000116}
117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000118PyDoc_STRVAR(locked_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000119"locked() -> bool\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000120(locked_lock() is an obsolete synonym)\n\
121\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122Return whether the lock is in the locked state.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000123
Barry Warsawd0c10421996-12-17 00:05:22 +0000124static PyMethodDef lock_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000125 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000126 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000127 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000128 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000129 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000130 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000131 {"release", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000132 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000133 {"locked_lock", (PyCFunction)lock_locked_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000134 METH_NOARGS, locked_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000135 {"locked", (PyCFunction)lock_locked_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000136 METH_NOARGS, locked_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000137 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000138};
139
Barry Warsawd0c10421996-12-17 00:05:22 +0000140static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000141lock_getattr(lockobject *self, char *name)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000142{
Barry Warsawd0c10421996-12-17 00:05:22 +0000143 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000144}
145
Barry Warsawd0c10421996-12-17 00:05:22 +0000146static PyTypeObject Locktype = {
147 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000149 "thread.lock", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150 sizeof(lockobject), /*tp_size*/
151 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000152 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153 (destructor)lock_dealloc, /*tp_dealloc*/
154 0, /*tp_print*/
155 (getattrfunc)lock_getattr, /*tp_getattr*/
156 0, /*tp_setattr*/
157 0, /*tp_compare*/
158 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000159};
160
161
162/* Module functions */
163
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164struct bootstate {
165 PyInterpreterState *interp;
166 PyObject *func;
167 PyObject *args;
168 PyObject *keyw;
169};
170
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000172t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000175 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000176 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000179 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180 res = PyEval_CallObjectWithKeywords(
181 boot->func, boot->args, boot->keyw);
182 Py_DECREF(boot->func);
183 Py_DECREF(boot->args);
184 Py_XDECREF(boot->keyw);
185 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000187 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000188 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000189 else {
Fred Drake9c801ab2000-10-20 20:02:37 +0000190 PySys_WriteStderr("Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000191 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000192 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000194 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000195 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000196 PyThreadState_Clear(tstate);
Guido van Rossum2528b192001-01-23 01:47:18 +0000197 PyThreadState_DeleteCurrent();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000198 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199}
200
Barry Warsawd0c10421996-12-17 00:05:22 +0000201static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000202thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203{
Guido van Rossum38d45b72000-09-01 20:47:58 +0000204 PyObject *func, *args, *keyw = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205 struct bootstate *boot;
Guido van Rossum3c288632001-10-16 21:13:49 +0000206 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207
Guido van Rossum43713e52000-02-29 13:59:29 +0000208 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000210 if (!PyCallable_Check(func)) {
211 PyErr_SetString(PyExc_TypeError,
212 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213 return NULL;
214 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000215 if (!PyTuple_Check(args)) {
216 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38d45b72000-09-01 20:47:58 +0000217 "2nd arg must be a tuple");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000218 return NULL;
219 }
220 if (keyw != NULL && !PyDict_Check(keyw)) {
221 PyErr_SetString(PyExc_TypeError,
222 "optional 3rd arg must be a dictionary");
223 return NULL;
224 }
225 boot = PyMem_NEW(struct bootstate, 1);
226 if (boot == NULL)
227 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000228 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229 boot->func = func;
230 boot->args = args;
231 boot->keyw = keyw;
232 Py_INCREF(func);
233 Py_INCREF(args);
234 Py_XINCREF(keyw);
235 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum3c288632001-10-16 21:13:49 +0000236 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
237 if (ident == -1) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000238 PyErr_SetString(ThreadError, "can't start new thread\n");
239 Py_DECREF(func);
240 Py_DECREF(args);
241 Py_XDECREF(keyw);
242 PyMem_DEL(boot);
243 return NULL;
244 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000245 return PyInt_FromLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246}
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000249"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000250(start_new() is an obsolete synonym)\n\
251\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000252Start a new thread and return its identifier. The thread will call the\n\
253function with positional arguments from the tuple args and keyword arguments\n\
254taken from the optional dictionary kwargs. The thread exits when the\n\
255function returns; the return value is ignored. The thread will also exit\n\
256when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000258
Barry Warsawd0c10421996-12-17 00:05:22 +0000259static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000260thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261{
Barry Warsawd0c10421996-12-17 00:05:22 +0000262 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000263 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264}
265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000266PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000267"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000269\n\
270This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000272
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000274static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000275thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
277 int sts;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000278 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000280 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281 for (;;) { } /* Should not be reached */
282}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000284
Barry Warsawd0c10421996-12-17 00:05:22 +0000285static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000286thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287{
Barry Warsawd0c10421996-12-17 00:05:22 +0000288 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000292"allocate_lock() -> lock object\n\
293(allocate() is an obsolete synonym)\n\
294\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000295Create a new lock object. See LockType.__doc__ for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000296
Barry Warsawd0c10421996-12-17 00:05:22 +0000297static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000298thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299{
300 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000301 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000303 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304 return NULL;
305 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000306 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307}
308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000310"get_ident() -> integer\n\
311\n\
312Return a non-zero integer that uniquely identifies the current thread\n\
313amongst other threads that exist simultaneously.\n\
314This may be used to identify per-thread resources.\n\
315Even though on some platforms threads identities may appear to be\n\
316allocated consecutive numbers starting at 1, this behavior should not\n\
317be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000319
Barry Warsawd0c10421996-12-17 00:05:22 +0000320static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000321 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
322 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000323 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000324 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
325 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000326 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000327 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000328 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000329 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000330 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000331 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000332 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000333 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000334 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000335 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000336 METH_NOARGS, get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000337#ifndef NO_EXIT_PROG
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000338 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
339 METH_VARARGS},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000340#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341 {NULL, NULL} /* sentinel */
342};
343
344
345/* Initialization function */
346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000348"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000352"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000353call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000354\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000355acquire() -- lock the lock, possibly blocking until it can be obtained\n\
356release() -- unlock of the lock\n\
357locked() -- test whether the lock is currently locked\n\
358\n\
359A lock is not owned by the thread that locked it; another thread may\n\
360unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000361will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000362
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000363PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000364initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000365{
Barry Warsawd0c10421996-12-17 00:05:22 +0000366 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000367
368 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000369 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000370
371 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000372 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000373 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000374 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000375 Locktype.tp_doc = lock_doc;
376 Py_INCREF(&Locktype);
377 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000378
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000380 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381}