blob: 80a8c4fc289426105baee843728f97f2505312e9 [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 Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014
15******************************************************************/
16
17/* Thread module */
18/* Interface to Sjoerd's portable C thread library */
19
Barry Warsawd0c10421996-12-17 00:05:22 +000020#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +000023#error "Error! The rest of Python is not compiled with thread support."
24#error "Rerun configure, adding a --with-thread option."
25#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000026#endif
27
Guido van Rossum49b56061998-10-01 20:42:43 +000028#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
Barry Warsawd0c10421996-12-17 00:05:22 +000030static PyObject *ThreadError;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031
32
33/* Lock objects */
34
35typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000036 PyObject_HEAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000037 PyThread_type_lock lock_lock;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038} lockobject;
39
Barry Warsawd0c10421996-12-17 00:05:22 +000040staticforward PyTypeObject Locktype;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000041
Guido van Rossum1984f1e1992-08-04 12:41:02 +000042static lockobject *
43newlockobject()
44{
45 lockobject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000046 self = PyObject_New(lockobject, &Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000047 if (self == NULL)
48 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +000049 self->lock_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050 if (self->lock_lock == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052 self = NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +000053 PyErr_SetString(ThreadError, "can't allocate lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054 }
55 return self;
56}
57
58static void
59lock_dealloc(self)
60 lockobject *self;
61{
62 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000063 PyThread_acquire_lock(self->lock_lock, 0);
64 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065
Guido van Rossum65d5b571998-12-21 19:32:43 +000066 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000067 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000068}
69
Barry Warsawd0c10421996-12-17 00:05:22 +000070static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +000071lock_PyThread_acquire_lock(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000072 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +000073 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000074{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000075 int i;
76
77 if (args != NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000078 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum1984f1e1992-08-04 12:41:02 +000079 return NULL;
80 }
81 else
82 i = 1;
83
Barry Warsawd0c10421996-12-17 00:05:22 +000084 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000085 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000086 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087
88 if (args == NULL) {
Barry Warsawd0c10421996-12-17 00:05:22 +000089 Py_INCREF(Py_None);
90 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000091 }
92 else
Barry Warsawd0c10421996-12-17 00:05:22 +000093 return PyInt_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000094}
95
Guido van Rossum75e9fc31998-06-27 18:21:06 +000096static char acquire_doc[] =
97"acquire([wait]) -> None or Boolean\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +000098(PyThread_acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000099\n\
100Lock the lock. Without argument, this blocks if the lock is already\n\
101locked (even by the same thread), waiting for another thread to release\n\
102the lock, and return None when the lock is acquired.\n\
103With a Boolean argument, this will only block if the argument is true,\n\
104and the return value reflects whether the lock is acquired.\n\
105The blocking operation is not interruptible.";
106
Barry Warsawd0c10421996-12-17 00:05:22 +0000107static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000108lock_PyThread_release_lock(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000109 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000110 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000111{
Barry Warsawd0c10421996-12-17 00:05:22 +0000112 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000113 return NULL;
114
115 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000116 if (PyThread_acquire_lock(self->lock_lock, 0)) {
117 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000118 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119 return NULL;
120 }
121
Guido van Rossum65d5b571998-12-21 19:32:43 +0000122 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000123 Py_INCREF(Py_None);
124 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000125}
126
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000127static char release_doc[] =
128"release()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000129(PyThread_release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000130\n\
131Release the lock, allowing another thread that is blocked waiting for\n\
132the lock to acquire the lock. The lock must be in the locked state,\n\
133but it needn't be locked by the same thread that unlocks it.";
134
Barry Warsawd0c10421996-12-17 00:05:22 +0000135static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000136lock_locked_lock(self, args)
137 lockobject *self;
Barry Warsawd0c10421996-12-17 00:05:22 +0000138 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000139{
Barry Warsawd0c10421996-12-17 00:05:22 +0000140 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000141 return NULL;
142
Guido van Rossum65d5b571998-12-21 19:32:43 +0000143 if (PyThread_acquire_lock(self->lock_lock, 0)) {
144 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +0000145 return PyInt_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000147 return PyInt_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000148}
149
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000150static char locked_doc[] =
151"locked() -> Boolean\n\
152(locked_lock() is an obsolete synonym)\n\
153\n\
154Return whether the lock is in the locked state.";
155
Barry Warsawd0c10421996-12-17 00:05:22 +0000156static PyMethodDef lock_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000157 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
158 {"acquire", (PyCFunction)lock_PyThread_acquire_lock, 0, acquire_doc},
159 {"release_lock", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
160 {"release", (PyCFunction)lock_PyThread_release_lock, 0, release_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000161 {"locked_lock", (PyCFunction)lock_locked_lock, 0, locked_doc},
162 {"locked", (PyCFunction)lock_locked_lock, 0, locked_doc},
163 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000164};
165
Barry Warsawd0c10421996-12-17 00:05:22 +0000166static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167lock_getattr(self, name)
168 lockobject *self;
169 char *name;
170{
Barry Warsawd0c10421996-12-17 00:05:22 +0000171 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172}
173
Barry Warsawd0c10421996-12-17 00:05:22 +0000174static PyTypeObject Locktype = {
175 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 0, /*ob_size*/
177 "lock", /*tp_name*/
178 sizeof(lockobject), /*tp_size*/
179 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181 (destructor)lock_dealloc, /*tp_dealloc*/
182 0, /*tp_print*/
183 (getattrfunc)lock_getattr, /*tp_getattr*/
184 0, /*tp_setattr*/
185 0, /*tp_compare*/
186 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187};
188
189
190/* Module functions */
191
Guido van Rossuma027efa1997-05-05 20:56:21 +0000192struct bootstate {
193 PyInterpreterState *interp;
194 PyObject *func;
195 PyObject *args;
196 PyObject *keyw;
197};
198
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199static void
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200t_bootstrap(boot_raw)
201 void *boot_raw;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000202{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000203 struct bootstate *boot = (struct bootstate *) boot_raw;
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000204 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206
Guido van Rossuma027efa1997-05-05 20:56:21 +0000207 tstate = PyThreadState_New(boot->interp);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000208 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000209 res = PyEval_CallObjectWithKeywords(
210 boot->func, boot->args, boot->keyw);
211 Py_DECREF(boot->func);
212 Py_DECREF(boot->args);
213 Py_XDECREF(boot->keyw);
214 PyMem_DEL(boot_raw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000216 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000217 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000218 else {
219 fprintf(stderr, "Unhandled exception in thread:\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000220 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000221 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000223 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000224 Py_DECREF(res);
Guido van Rossumb02158e1997-08-02 03:13:11 +0000225 PyThreadState_Clear(tstate);
Guido van Rossum75aa0d61997-07-18 23:57:50 +0000226 PyEval_ReleaseThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227 PyThreadState_Delete(tstate);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000228 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229}
230
Barry Warsawd0c10421996-12-17 00:05:22 +0000231static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232thread_PyThread_start_new_thread(self, fargs)
Barry Warsawd0c10421996-12-17 00:05:22 +0000233 PyObject *self; /* Not used */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000234 PyObject *fargs;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000236 PyObject *func, *args = NULL, *keyw = NULL;
237 struct bootstate *boot;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238
Guido van Rossum43713e52000-02-29 13:59:29 +0000239 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000240 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000241 if (!PyCallable_Check(func)) {
242 PyErr_SetString(PyExc_TypeError,
243 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244 return NULL;
245 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000246 if (!PyTuple_Check(args)) {
247 PyErr_SetString(PyExc_TypeError,
248 "optional 2nd arg must be a tuple");
249 return NULL;
250 }
251 if (keyw != NULL && !PyDict_Check(keyw)) {
252 PyErr_SetString(PyExc_TypeError,
253 "optional 3rd arg must be a dictionary");
254 return NULL;
255 }
256 boot = PyMem_NEW(struct bootstate, 1);
257 if (boot == NULL)
258 return PyErr_NoMemory();
Guido van Rossumb02158e1997-08-02 03:13:11 +0000259 boot->interp = PyThreadState_Get()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000260 boot->func = func;
261 boot->args = args;
262 boot->keyw = keyw;
263 Py_INCREF(func);
264 Py_INCREF(args);
265 Py_XINCREF(keyw);
266 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 if (!PyThread_start_new_thread(t_bootstrap, (void*) boot)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000268 PyErr_SetString(ThreadError, "can't start new thread\n");
269 Py_DECREF(func);
270 Py_DECREF(args);
271 Py_XDECREF(keyw);
272 PyMem_DEL(boot);
273 return NULL;
274 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000275 Py_INCREF(Py_None);
276 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277}
278
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000279static char start_new_doc[] =
280"start_new_thread(functon, args[, kwargs])\n\
281(start_new() is an obsolete synonym)\n\
282\n\
283Start a new thread. The thread will call the function with positional\n\
284arguments from the tuple args and keyword arguments taken from the optional\n\
285dictionary kwargs. The thread exits when the function returns; the return\n\
286value is ignored. The thread will also exit when the function raises an\n\
287unhandled exception; a stack trace will be printed unless the exception is\n\
288SystemExit.";
289
Barry Warsawd0c10421996-12-17 00:05:22 +0000290static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000291thread_PyThread_exit_thread(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000292 PyObject *self; /* Not used */
293 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Barry Warsawd0c10421996-12-17 00:05:22 +0000295 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000297 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000298 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299}
300
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000301static char exit_doc[] =
302"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000303(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000304\n\
305This is synonymous to ``raise SystemExit''. It will cause the current\n\
306thread to exit silently unless the exception is caught.";
307
Guido van Rossumb6775db1994-08-01 11:34:53 +0000308#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000309static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310thread_PyThread_exit_prog(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000311 PyObject *self; /* Not used */
312 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313{
314 int sts;
Barry Warsawd0c10421996-12-17 00:05:22 +0000315 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000317 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318 for (;;) { } /* Should not be reached */
319}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000321
Barry Warsawd0c10421996-12-17 00:05:22 +0000322static PyObject *
Guido van Rossum65d5b571998-12-21 19:32:43 +0000323thread_PyThread_allocate_lock(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000324 PyObject *self; /* Not used */
325 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326{
Barry Warsawd0c10421996-12-17 00:05:22 +0000327 if (!PyArg_NoArgs(args))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328 return NULL;
Barry Warsawd0c10421996-12-17 00:05:22 +0000329 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330}
331
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000332static char allocate_doc[] =
333"allocate_lock() -> lock object\n\
334(allocate() is an obsolete synonym)\n\
335\n\
336Create a new lock object. See LockType.__doc__ for information about locks.";
337
Barry Warsawd0c10421996-12-17 00:05:22 +0000338static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339thread_get_ident(self, args)
Barry Warsawd0c10421996-12-17 00:05:22 +0000340 PyObject *self; /* Not used */
341 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000342{
343 long ident;
Barry Warsawd0c10421996-12-17 00:05:22 +0000344 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000346 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000347 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000348 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349 return NULL;
350 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000351 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000352}
353
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000354static char get_ident_doc[] =
355"get_ident() -> integer\n\
356\n\
357Return a non-zero integer that uniquely identifies the current thread\n\
358amongst other threads that exist simultaneously.\n\
359This may be used to identify per-thread resources.\n\
360Even though on some platforms threads identities may appear to be\n\
361allocated consecutive numbers starting at 1, this behavior should not\n\
362be relied upon, and the number should be seen purely as a magic cookie.\n\
363A thread's identity may be reused for another thread after it exits.";
364
Barry Warsawd0c10421996-12-17 00:05:22 +0000365static PyMethodDef thread_methods[] = {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000366 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000367 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000368 {"start_new", (PyCFunction)thread_PyThread_start_new_thread, 1,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000369 start_new_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000370 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000371 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000372 {"allocate", (PyCFunction)thread_PyThread_allocate_lock, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000373 allocate_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000374 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000375 exit_doc},
Guido van Rossum65d5b571998-12-21 19:32:43 +0000376 {"exit", (PyCFunction)thread_PyThread_exit_thread, 0,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000377 exit_doc},
378 {"get_ident", (PyCFunction)thread_get_ident, 0,
379 get_ident_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000381 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000382#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383 {NULL, NULL} /* sentinel */
384};
385
386
387/* Initialization function */
388
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000389static char thread_doc[] =
390"This module provides primitive operations to write multi-threaded programs.\n\
391The 'threading' module provides a more convenient interface.";
392
393static char lock_doc[] =
394"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000395call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000396\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000397acquire() -- lock the lock, possibly blocking until it can be obtained\n\
398release() -- unlock of the lock\n\
399locked() -- test whether the lock is currently locked\n\
400\n\
401A lock is not owned by the thread that locked it; another thread may\n\
402unlock it. A thread attempting to lock a lock that it has already locked\n\
403will block until another thread unlocks it. Deadlocks may ensue.";
404
Guido van Rossum3886bb61998-12-04 18:50:17 +0000405DL_EXPORT(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000406initthread()
407{
Barry Warsawd0c10421996-12-17 00:05:22 +0000408 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000409
410 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000411 m = Py_InitModule3("thread", thread_methods, thread_doc);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000412
413 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000414 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000415 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000416 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000417 Locktype.tp_doc = lock_doc;
418 Py_INCREF(&Locktype);
419 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000420
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000421 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000422 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423}