blob: ab6340f5570d277ee9a08dab9e85d0242ab61451 [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 *
37newlockobject()
38{
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
53lock_dealloc(self)
54 lockobject *self;
55{
56 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000057 PyThread_acquire_lock(self->lock_lock, 0);
58 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000059
Guido van Rossum65d5b571998-12-21 19:32:43 +000060 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000061 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000062}
63
Barry Warsawd0c10421996-12-17 00:05:22 +000064static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +000065lock_PyThread_acquire_lock(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000066 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +000067 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000068{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069 int i;
70
71 if (args != NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000072 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum1984f1e1992-08-04 12:41:02 +000073 return NULL;
74 }
75 else
76 i = 1;
77
Barry Warsawd0c10421996-12-17 00:05:22 +000078 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000079 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000080 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000081
82 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000083 Py_INCREF(Py_None);
84 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000085 }
86 else
Barry Warsawd0c10421996-12-17 00:05:22 +000087 return PyInt_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000088}
89
Guido van Rossum75e9fc31998-06-27 18:21:06 +000090static char acquire_doc[] =
91"acquire([wait]) -> None or Boolean\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +000092(PyThread_acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000093\n\
94Lock the lock. Without argument, this blocks if the lock is already\n\
95locked (even by the same thread), waiting for another thread to release\n\
96the lock, and return None when the lock is acquired.\n\
97With a Boolean argument, this will only block if the argument is true,\n\
98and the return value reflects whether the lock is acquired.\n\
99The blocking operation is not interruptible.";
100
Barry Warsawd0c10421996-12-17 00:05:22 +0000101static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000102lock_PyThread_release_lock(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000103 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000104 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000105{
Barry Warsawd0c10421996-12-17 00:05:22 +0000106 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000107 return NULL;
108
109 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000110 if (PyThread_acquire_lock(self->lock_lock, 0)) {
111 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000112 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000113 return NULL;
114 }
115
Guido van Rossum65d5b571998-12-21 19:32:43 +0000116 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000117 Py_INCREF(Py_None);
118 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119}
120
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000121static char release_doc[] =
122"release()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000123(PyThread_release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000124\n\
125Release the lock, allowing another thread that is blocked waiting for\n\
126the lock to acquire the lock. The lock must be in the locked state,\n\
127but it needn't be locked by the same thread that unlocks it.";
128
Barry Warsawd0c10421996-12-17 00:05:22 +0000129static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000130lock_locked_lock(self, args)
131 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000132 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000133{
Barry Warsawd0c10421996-12-17 00:05:22 +0000134 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135 return NULL;
136
Guido van Rossum65d5b571998-12-21 19:32:43 +0000137 if (PyThread_acquire_lock(self->lock_lock, 0)) {
138 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000139 return PyInt_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000140 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000141 return PyInt_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000142}
143
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000144static char locked_doc[] =
145"locked() -> Boolean\n\
146(locked_lock() is an obsolete synonym)\n\
147\n\
148Return whether the lock is in the locked state.";
149
Barry Warsawd0c10421996-12-17 00:05:22 +0000150static PyMethodDef lock_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000151 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
152 {"acquire", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
153 {"release_lock", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
154 {"release", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000155 {"locked_lock", (PyCFunction)lock_locked_lock, 0, locked_doc},
156 {"locked", (PyCFunction)lock_locked_lock, 0, locked_doc},
157 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158};
159
Barry Warsawd0c10421996-12-17 00:05:22 +0000160static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000161lock_getattr(self, name)
162 lockobject *self;
163 char *name;
164{
Barry Warsawd0c10421996-12-17 00:05:22 +0000165 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166}
167
Barry Warsawd0c10421996-12-17 00:05:22 +0000168static PyTypeObject Locktype = {
169 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170 0, /*ob_size*/
171 "lock", /*tp_name*/
172 sizeof(lockobject), /*tp_size*/
173 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175 (destructor)lock_dealloc, /*tp_dealloc*/
176 0, /*tp_print*/
177 (getattrfunc)lock_getattr, /*tp_getattr*/
178 0, /*tp_setattr*/
179 0, /*tp_compare*/
180 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181};
182
183
184/* Module functions */
185
Guido van Rossuma027efa1997-05-05 20:56:21 +0000186struct bootstate {
187 PyInterpreterState *interp;
188 PyObject *func;
189 PyObject *args;
190 PyObject *keyw;
191};
192
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193static void
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194t_bootstrap(boot_raw)
195 void *boot_raw;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000197 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000198 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000200
Guido van Rossuma027efa1997-05-05 20:56:21 +0000201 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000202 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000203 res = PyEval_CallObjectWithKeywords(
204 boot->func, boot->args, boot->keyw);
205 Py_DECREF(boot->func);
206 Py_DECREF(boot->args);
207 Py_XDECREF(boot->keyw);
208 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000210 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000211 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000212 else {
213 fprintf(stderr, "Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000214 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000215 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000216 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000218 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000219 PyThreadState_Clear(tstate);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000220 PyEval_ReleaseThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000221 PyThreadState_Delete(tstate);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000222 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000223}
224
Barry Warsawd0c10421996-12-17 00:05:22 +0000225static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000226thread_PyThread_start_new_thread(self, fargs)
Barry Warsawd0c10421996-12-17 00:05:22 +0000227 PyObject *self; /* Not used */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000228 PyObject *fargs;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 PyObject *func, *args = NULL, *keyw = NULL;
231 struct bootstate *boot;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000232
Guido van Rossum43713e52000-02-29 13:59:29 +0000233 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000235 if (!PyCallable_Check(func)) {
236 PyErr_SetString(PyExc_TypeError,
237 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238 return NULL;
239 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000240 if (!PyTuple_Check(args)) {
241 PyErr_SetString(PyExc_TypeError,
242 "optional 2nd arg must be a tuple");
243 return NULL;
244 }
245 if (keyw != NULL && !PyDict_Check(keyw)) {
246 PyErr_SetString(PyExc_TypeError,
247 "optional 3rd arg must be a dictionary");
248 return NULL;
249 }
250 boot = PyMem_NEW(struct bootstate, 1);
251 if (boot == NULL)
252 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000253 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000254 boot->func = func;
255 boot->args = args;
256 boot->keyw = keyw;
257 Py_INCREF(func);
258 Py_INCREF(args);
259 Py_XINCREF(keyw);
260 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 if (!PyThread_start_new_thread(t_bootstrap, (void*) boot)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000262 PyErr_SetString(ThreadError, "can't start new thread\n");
263 Py_DECREF(func);
264 Py_DECREF(args);
265 Py_XDECREF(keyw);
266 PyMem_DEL(boot);
267 return NULL;
268 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000269 Py_INCREF(Py_None);
270 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271}
272
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000273static char start_new_doc[] =
274"start_new_thread(functon, args[, kwargs])\n\
275(start_new() is an obsolete synonym)\n\
276\n\
277Start a new thread. The thread will call the function with positional\n\
278arguments from the tuple args and keyword arguments taken from the optional\n\
279dictionary kwargs. The thread exits when the function returns; the return\n\
280value is ignored. The thread will also exit when the function raises an\n\
281unhandled exception; a stack trace will be printed unless the exception is\n\
282SystemExit.";
283
Barry Warsawd0c10421996-12-17 00:05:22 +0000284static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000285thread_PyThread_exit_thread(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000286 PyObject *self; /* Not used */
287 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000288{
Barry Warsawd0c10421996-12-17 00:05:22 +0000289 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000291 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000292 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293}
294
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000295static char exit_doc[] =
296"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000298\n\
299This is synonymous to ``raise SystemExit''. It will cause the current\n\
300thread to exit silently unless the exception is caught.";
301
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000303static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000304thread_PyThread_exit_prog(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000305 PyObject *self; /* Not used */
306 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307{
308 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000309 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000311 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312 for (;;) { } /* Should not be reached */
313}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315
Barry Warsawd0c10421996-12-17 00:05:22 +0000316static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000317thread_PyThread_allocate_lock(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000318 PyObject *self; /* Not used */
319 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320{
Barry Warsawd0c10421996-12-17 00:05:22 +0000321 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000323 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000324}
325
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000326static char allocate_doc[] =
327"allocate_lock() -> lock object\n\
328(allocate() is an obsolete synonym)\n\
329\n\
330Create a new lock object. See LockType.__doc__ for information about locks.";
331
Barry Warsawd0c10421996-12-17 00:05:22 +0000332static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000333thread_get_ident(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000334 PyObject *self; /* Not used */
335 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000336{
337 long ident;
Barry Warsawd0c10421996-12-17 00:05:22 +0000338 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000340 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000341 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000342 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000343 return NULL;
344 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000345 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000346}
347
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000348static char get_ident_doc[] =
349"get_ident() -> integer\n\
350\n\
351Return a non-zero integer that uniquely identifies the current thread\n\
352amongst other threads that exist simultaneously.\n\
353This may be used to identify per-thread resources.\n\
354Even though on some platforms threads identities may appear to be\n\
355allocated consecutive numbers starting at 1, this behavior should not\n\
356be relied upon, and the number should be seen purely as a magic cookie.\n\
357A thread's identity may be reused for another thread after it exits.";
358
Barry Warsawd0c10421996-12-17 00:05:22 +0000359static PyMethodDef thread_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000360 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000361 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000362 {"start_new", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000363 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000364 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000365 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000366 {"allocate", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000367 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000368 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000369 exit_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000370 {"exit", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000371 exit_doc},
372 {"get_ident", (PyCFunction)thread_get_ident, 0,
373 get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000375 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377 {NULL, NULL} /* sentinel */
378};
379
380
381/* Initialization function */
382
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000383static char thread_doc[] =
384"This module provides primitive operations to write multi-threaded programs.\n\
385The 'threading' module provides a more convenient interface.";
386
387static char lock_doc[] =
388"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000389call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000390\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000391acquire() -- lock the lock, possibly blocking until it can be obtained\n\
392release() -- unlock of the lock\n\
393locked() -- test whether the lock is currently locked\n\
394\n\
395A lock is not owned by the thread that locked it; another thread may\n\
396unlock it. A thread attempting to lock a lock that it has already locked\n\
397will block until another thread unlocks it. Deadlocks may ensue.";
398
Guido van Rossum3886bb61998-12-04 18:50:17 +0000399DL_EXPORT(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000400initthread()
401{
Barry Warsawd0c10421996-12-17 00:05:22 +0000402 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000403
404 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000405 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000406
407 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000408 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000409 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000410 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000411 Locktype.tp_doc = lock_doc;
412 Py_INCREF(&Locktype);
413 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000414
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000416 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417}