blob: ef73c26fc6a278552044cfdb3df843ebda7dce18 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009******************************************************************/
10
11/* Thread module */
12/* Interface to Sjoerd's portable C thread library */
13
Barry Warsawd0c10421996-12-17 00:05:22 +000014#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000015
Guido van Rossumb6775db1994-08-01 11:34:53 +000016#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +000017#error "Error! The rest of Python is not compiled with thread support."
18#error "Rerun configure, adding a --with-thread option."
19#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000020#endif
21
Guido van Rossum49b56061998-10-01 20:42:43 +000022#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000023
Barry Warsawd0c10421996-12-17 00:05:22 +000024static PyObject *ThreadError;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000025
26
27/* Lock objects */
28
29typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000030 PyObject_HEAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000031 PyThread_type_lock lock_lock;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032} lockobject;
33
Barry Warsawd0c10421996-12-17 00:05:22 +000034staticforward PyTypeObject Locktype;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036static lockobject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000037newlockobject(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038{
39 lockobject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000040 self = PyObject_New(lockobject, &Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000041 if (self == NULL)
42 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +000043 self->lock_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044 if (self->lock_lock == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000045 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000046 self = NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +000047 PyErr_SetString(ThreadError, "can't allocate lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048 }
49 return self;
50}
51
52static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000053lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054{
55 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000056 PyThread_acquire_lock(self->lock_lock, 0);
57 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000058
Guido van Rossum65d5b571998-12-21 19:32:43 +000059 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000061}
62
Barry Warsawd0c10421996-12-17 00:05:22 +000063static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000064lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000066 int i;
67
68 if (args != NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000069 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum1984f1e1992-08-04 12:41:02 +000070 return NULL;
71 }
72 else
73 i = 1;
74
Barry Warsawd0c10421996-12-17 00:05:22 +000075 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000076 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000077 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000078
79 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000080 Py_INCREF(Py_None);
81 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000082 }
83 else
Barry Warsawd0c10421996-12-17 00:05:22 +000084 return PyInt_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000085}
86
Guido van Rossum75e9fc31998-06-27 18:21:06 +000087static char acquire_doc[] =
88"acquire([wait]) -> None or Boolean\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +000089(PyThread_acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000090\n\
91Lock the lock. Without argument, this blocks if the lock is already\n\
92locked (even by the same thread), waiting for another thread to release\n\
93the lock, and return None when the lock is acquired.\n\
94With a Boolean argument, this will only block if the argument is true,\n\
95and the return value reflects whether the lock is acquired.\n\
96The blocking operation is not interruptible.";
97
Barry Warsawd0c10421996-12-17 00:05:22 +000098static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000099lock_PyThread_release_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000100{
Barry Warsawd0c10421996-12-17 00:05:22 +0000101 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102 return NULL;
103
104 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000105 if (PyThread_acquire_lock(self->lock_lock, 0)) {
106 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000107 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108 return NULL;
109 }
110
Guido van Rossum65d5b571998-12-21 19:32:43 +0000111 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000112 Py_INCREF(Py_None);
113 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114}
115
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000116static char release_doc[] =
117"release()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000118(PyThread_release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000119\n\
120Release the lock, allowing another thread that is blocked waiting for\n\
121the lock to acquire the lock. The lock must be in the locked state,\n\
122but it needn't be locked by the same thread that unlocks it.";
123
Barry Warsawd0c10421996-12-17 00:05:22 +0000124static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000125lock_locked_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126{
Barry Warsawd0c10421996-12-17 00:05:22 +0000127 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000128 return NULL;
129
Guido van Rossum65d5b571998-12-21 19:32:43 +0000130 if (PyThread_acquire_lock(self->lock_lock, 0)) {
131 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000132 return PyInt_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000133 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000134 return PyInt_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135}
136
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000137static char locked_doc[] =
138"locked() -> Boolean\n\
139(locked_lock() is an obsolete synonym)\n\
140\n\
141Return whether the lock is in the locked state.";
142
Barry Warsawd0c10421996-12-17 00:05:22 +0000143static PyMethodDef lock_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000144 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
145 {"acquire", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
146 {"release_lock", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
147 {"release", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000148 {"locked_lock", (PyCFunction)lock_locked_lock, 0, locked_doc},
149 {"locked", (PyCFunction)lock_locked_lock, 0, locked_doc},
150 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000151};
152
Barry Warsawd0c10421996-12-17 00:05:22 +0000153static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000154lock_getattr(lockobject *self, char *name)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000155{
Barry Warsawd0c10421996-12-17 00:05:22 +0000156 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000157}
158
Barry Warsawd0c10421996-12-17 00:05:22 +0000159static PyTypeObject Locktype = {
160 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161 0, /*ob_size*/
162 "lock", /*tp_name*/
163 sizeof(lockobject), /*tp_size*/
164 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166 (destructor)lock_dealloc, /*tp_dealloc*/
167 0, /*tp_print*/
168 (getattrfunc)lock_getattr, /*tp_getattr*/
169 0, /*tp_setattr*/
170 0, /*tp_compare*/
171 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172};
173
174
175/* Module functions */
176
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177struct bootstate {
178 PyInterpreterState *interp;
179 PyObject *func;
180 PyObject *args;
181 PyObject *keyw;
182};
183
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000185t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000187 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000188 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000190
Guido van Rossuma027efa1997-05-05 20:56:21 +0000191 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000192 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000193 res = PyEval_CallObjectWithKeywords(
194 boot->func, boot->args, boot->keyw);
195 Py_DECREF(boot->func);
196 Py_DECREF(boot->args);
197 Py_XDECREF(boot->keyw);
198 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000200 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000201 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000202 else {
203 fprintf(stderr, "Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000204 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000205 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000207 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000208 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000209 PyThreadState_Clear(tstate);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000210 PyEval_ReleaseThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211 PyThreadState_Delete(tstate);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000212 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213}
214
Barry Warsawd0c10421996-12-17 00:05:22 +0000215static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000216thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000218 PyObject *func, *args = NULL, *keyw = NULL;
219 struct bootstate *boot;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220
Guido van Rossum43713e52000-02-29 13:59:29 +0000221 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000223 if (!PyCallable_Check(func)) {
224 PyErr_SetString(PyExc_TypeError,
225 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226 return NULL;
227 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000228 if (!PyTuple_Check(args)) {
229 PyErr_SetString(PyExc_TypeError,
230 "optional 2nd arg must be a tuple");
231 return NULL;
232 }
233 if (keyw != NULL && !PyDict_Check(keyw)) {
234 PyErr_SetString(PyExc_TypeError,
235 "optional 3rd arg must be a dictionary");
236 return NULL;
237 }
238 boot = PyMem_NEW(struct bootstate, 1);
239 if (boot == NULL)
240 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000241 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000242 boot->func = func;
243 boot->args = args;
244 boot->keyw = keyw;
245 Py_INCREF(func);
246 Py_INCREF(args);
247 Py_XINCREF(keyw);
248 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 if (!PyThread_start_new_thread(t_bootstrap, (void*) boot)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000250 PyErr_SetString(ThreadError, "can't start new thread\n");
251 Py_DECREF(func);
252 Py_DECREF(args);
253 Py_XDECREF(keyw);
254 PyMem_DEL(boot);
255 return NULL;
256 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000257 Py_INCREF(Py_None);
258 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000259}
260
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000261static char start_new_doc[] =
262"start_new_thread(functon, args[, kwargs])\n\
263(start_new() is an obsolete synonym)\n\
264\n\
265Start a new thread. The thread will call the function with positional\n\
266arguments from the tuple args and keyword arguments taken from the optional\n\
267dictionary kwargs. The thread exits when the function returns; the return\n\
268value is ignored. The thread will also exit when the function raises an\n\
269unhandled exception; a stack trace will be printed unless the exception is\n\
270SystemExit.";
271
Barry Warsawd0c10421996-12-17 00:05:22 +0000272static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000273thread_PyThread_exit_thread(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274{
Barry Warsawd0c10421996-12-17 00:05:22 +0000275 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000277 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000278 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279}
280
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000281static char exit_doc[] =
282"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000283(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000284\n\
285This is synonymous to ``raise SystemExit''. It will cause the current\n\
286thread to exit silently unless the exception is caught.";
287
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000289static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000290thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291{
292 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000293 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000295 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296 for (;;) { } /* Should not be reached */
297}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299
Barry Warsawd0c10421996-12-17 00:05:22 +0000300static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000301thread_PyThread_allocate_lock(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302{
Barry Warsawd0c10421996-12-17 00:05:22 +0000303 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000305 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306}
307
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000308static char allocate_doc[] =
309"allocate_lock() -> lock object\n\
310(allocate() is an obsolete synonym)\n\
311\n\
312Create a new lock object. See LockType.__doc__ for information about locks.";
313
Barry Warsawd0c10421996-12-17 00:05:22 +0000314static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000315thread_get_ident(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000316{
317 long ident;
Barry Warsawd0c10421996-12-17 00:05:22 +0000318 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000319 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000320 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000321 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000322 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000323 return NULL;
324 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000325 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326}
327
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000328static char get_ident_doc[] =
329"get_ident() -> integer\n\
330\n\
331Return a non-zero integer that uniquely identifies the current thread\n\
332amongst other threads that exist simultaneously.\n\
333This may be used to identify per-thread resources.\n\
334Even though on some platforms threads identities may appear to be\n\
335allocated consecutive numbers starting at 1, this behavior should not\n\
336be relied upon, and the number should be seen purely as a magic cookie.\n\
337A thread's identity may be reused for another thread after it exits.";
338
Barry Warsawd0c10421996-12-17 00:05:22 +0000339static PyMethodDef thread_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000340 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000341 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000342 {"start_new", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000343 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000344 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000345 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000346 {"allocate", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000347 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000348 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000349 exit_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000350 {"exit", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000351 exit_doc},
352 {"get_ident", (PyCFunction)thread_get_ident, 0,
353 get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000354#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000355 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000357 {NULL, NULL} /* sentinel */
358};
359
360
361/* Initialization function */
362
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000363static char thread_doc[] =
364"This module provides primitive operations to write multi-threaded programs.\n\
365The 'threading' module provides a more convenient interface.";
366
367static char lock_doc[] =
368"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000369call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000370\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000371acquire() -- lock the lock, possibly blocking until it can be obtained\n\
372release() -- unlock of the lock\n\
373locked() -- test whether the lock is currently locked\n\
374\n\
375A lock is not owned by the thread that locked it; another thread may\n\
376unlock it. A thread attempting to lock a lock that it has already locked\n\
377will block until another thread unlocks it. Deadlocks may ensue.";
378
Guido van Rossum3886bb61998-12-04 18:50:17 +0000379DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000380initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Barry Warsawd0c10421996-12-17 00:05:22 +0000382 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383
384 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000385 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000386
387 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000388 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000389 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000390 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000391 Locktype.tp_doc = lock_doc;
392 Py_INCREF(&Locktype);
393 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000394
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000395 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000396 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000397}