1992-08-04 12:41:02 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:10:35 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1992-08-04 12:41:02 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-08-04 12:41:02 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Thread module */
|
|
|
|
/* Interface to Sjoerd's portable C thread library */
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
#include "Python.h"
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
#ifndef WITH_THREAD
|
1997-05-05 20:56:21 +00:00
|
|
|
#error "Error! The rest of Python is not compiled with thread support."
|
|
|
|
#error "Rerun configure, adding a --with-thread option."
|
|
|
|
#error "Then run `make clean' followed by `make'."
|
1994-08-01 11:34:53 +00:00
|
|
|
#endif
|
|
|
|
|
1992-08-04 12:41:02 +00:00
|
|
|
#include "thread.h"
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *ThreadError;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Lock objects */
|
|
|
|
|
|
|
|
typedef struct {
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject_HEAD
|
1992-08-04 12:41:02 +00:00
|
|
|
type_lock lock_lock;
|
|
|
|
} lockobject;
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
staticforward PyTypeObject Locktype;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
#define is_lockobject(v) ((v)->ob_type == &Locktype)
|
|
|
|
|
1993-01-06 13:36:38 +00:00
|
|
|
type_lock
|
|
|
|
getlocklock(lock)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *lock;
|
1993-01-06 13:36:38 +00:00
|
|
|
{
|
|
|
|
if (lock == NULL || !is_lockobject(lock))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return ((lockobject *) lock)->lock_lock;
|
|
|
|
}
|
|
|
|
|
1992-08-04 12:41:02 +00:00
|
|
|
static lockobject *
|
|
|
|
newlockobject()
|
|
|
|
{
|
|
|
|
lockobject *self;
|
1996-12-17 00:05:22 +00:00
|
|
|
self = PyObject_NEW(lockobject, &Locktype);
|
1992-08-04 12:41:02 +00:00
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->lock_lock = allocate_lock();
|
|
|
|
if (self->lock_lock == NULL) {
|
1996-12-17 00:05:22 +00:00
|
|
|
PyMem_DEL(self);
|
1992-08-04 12:41:02 +00:00
|
|
|
self = NULL;
|
1996-12-17 00:05:22 +00:00
|
|
|
PyErr_SetString(ThreadError, "can't allocate lock");
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lock_dealloc(self)
|
|
|
|
lockobject *self;
|
|
|
|
{
|
|
|
|
/* Unlock the lock so it's safe to free it */
|
|
|
|
acquire_lock(self->lock_lock, 0);
|
|
|
|
release_lock(self->lock_lock);
|
|
|
|
|
|
|
|
free_lock(self->lock_lock);
|
1996-12-17 00:05:22 +00:00
|
|
|
PyMem_DEL(self);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
lock_acquire_lock(self, args)
|
|
|
|
lockobject *self;
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args != NULL) {
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_Parse(args, "i", &i))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i = 1;
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-04 12:41:02 +00:00
|
|
|
i = acquire_lock(self->lock_lock, i);
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
if (args == NULL) {
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
else
|
1996-12-17 00:05:22 +00:00
|
|
|
return PyInt_FromLong((long)i);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
lock_release_lock(self, args)
|
|
|
|
lockobject *self;
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Sanity check: the lock must be locked */
|
|
|
|
if (acquire_lock(self->lock_lock, 0)) {
|
|
|
|
release_lock(self->lock_lock);
|
1996-12-17 00:05:22 +00:00
|
|
|
PyErr_SetString(ThreadError, "release unlocked lock");
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_lock(self->lock_lock);
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
lock_locked_lock(self, args)
|
|
|
|
lockobject *self;
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (acquire_lock(self->lock_lock, 0)) {
|
|
|
|
release_lock(self->lock_lock);
|
1996-12-17 00:05:22 +00:00
|
|
|
return PyInt_FromLong(0L);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1996-12-17 00:05:22 +00:00
|
|
|
return PyInt_FromLong(1L);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyMethodDef lock_methods[] = {
|
|
|
|
{"acquire_lock", (PyCFunction)lock_acquire_lock},
|
|
|
|
{"acquire", (PyCFunction)lock_acquire_lock},
|
|
|
|
{"release_lock", (PyCFunction)lock_release_lock},
|
|
|
|
{"release", (PyCFunction)lock_release_lock},
|
|
|
|
{"locked_lock", (PyCFunction)lock_locked_lock},
|
|
|
|
{"locked", (PyCFunction)lock_locked_lock},
|
1992-08-04 12:41:02 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
lock_getattr(self, name)
|
|
|
|
lockobject *self;
|
|
|
|
char *name;
|
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
return Py_FindMethod(lock_methods, (PyObject *)self, name);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyTypeObject Locktype = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1994-08-01 11:34:53 +00:00
|
|
|
0, /*ob_size*/
|
|
|
|
"lock", /*tp_name*/
|
|
|
|
sizeof(lockobject), /*tp_size*/
|
|
|
|
0, /*tp_itemsize*/
|
1992-08-04 12:41:02 +00:00
|
|
|
/* methods */
|
1994-08-01 11:34:53 +00:00
|
|
|
(destructor)lock_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)lock_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
1992-08-04 12:41:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Module functions */
|
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
struct bootstate {
|
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *keyw;
|
|
|
|
};
|
|
|
|
|
1992-08-04 12:41:02 +00:00
|
|
|
static void
|
1997-05-05 20:56:21 +00:00
|
|
|
t_bootstrap(boot_raw)
|
|
|
|
void *boot_raw;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-05-05 20:56:21 +00:00
|
|
|
struct bootstate *boot = (struct bootstate *) boot_raw;
|
1997-07-18 23:57:50 +00:00
|
|
|
PyThreadState *tstate;
|
1997-05-05 20:56:21 +00:00
|
|
|
PyObject *res;
|
1992-08-17 08:59:08 +00:00
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
tstate = PyThreadState_New(boot->interp);
|
1997-07-18 23:57:50 +00:00
|
|
|
PyEval_AcquireThread(tstate);
|
1997-05-05 20:56:21 +00:00
|
|
|
res = PyEval_CallObjectWithKeywords(
|
|
|
|
boot->func, boot->args, boot->keyw);
|
|
|
|
Py_DECREF(boot->func);
|
|
|
|
Py_DECREF(boot->args);
|
|
|
|
Py_XDECREF(boot->keyw);
|
|
|
|
PyMem_DEL(boot_raw);
|
1992-08-04 12:41:02 +00:00
|
|
|
if (res == NULL) {
|
1996-12-17 00:05:22 +00:00
|
|
|
if (PyErr_Occurred() == PyExc_SystemExit)
|
|
|
|
PyErr_Clear();
|
1995-03-17 10:42:27 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unhandled exception in thread:\n");
|
1998-02-06 22:32:08 +00:00
|
|
|
PyErr_PrintEx(0);
|
1995-03-17 10:42:27 +00:00
|
|
|
}
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1995-01-02 19:30:30 +00:00
|
|
|
else
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_DECREF(res);
|
1997-08-02 03:13:11 +00:00
|
|
|
PyThreadState_Clear(tstate);
|
1997-07-18 23:57:50 +00:00
|
|
|
PyEval_ReleaseThread(tstate);
|
1997-05-05 20:56:21 +00:00
|
|
|
PyThreadState_Delete(tstate);
|
1992-08-04 12:41:02 +00:00
|
|
|
exit_thread();
|
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1997-05-05 20:56:21 +00:00
|
|
|
thread_start_new_thread(self, fargs)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *self; /* Not used */
|
1997-05-05 20:56:21 +00:00
|
|
|
PyObject *fargs;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-05-05 20:56:21 +00:00
|
|
|
PyObject *func, *args = NULL, *keyw = NULL;
|
|
|
|
struct bootstate *boot;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
if (!PyArg_ParseTuple(fargs, "OO|O", &func, &args, &keyw))
|
|
|
|
return NULL;
|
|
|
|
if (!PyCallable_Check(func)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first arg must be callable");
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
1997-05-05 20:56:21 +00:00
|
|
|
}
|
|
|
|
if (!PyTuple_Check(args)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"optional 2nd arg must be a tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (keyw != NULL && !PyDict_Check(keyw)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"optional 3rd arg must be a dictionary");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
boot = PyMem_NEW(struct bootstate, 1);
|
|
|
|
if (boot == NULL)
|
|
|
|
return PyErr_NoMemory();
|
1997-08-02 03:13:11 +00:00
|
|
|
boot->interp = PyThreadState_Get()->interp;
|
1997-05-05 20:56:21 +00:00
|
|
|
boot->func = func;
|
|
|
|
boot->args = args;
|
|
|
|
boot->keyw = keyw;
|
|
|
|
Py_INCREF(func);
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_INCREF(args);
|
1997-05-05 20:56:21 +00:00
|
|
|
Py_XINCREF(keyw);
|
|
|
|
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
|
|
|
|
if (!start_new_thread(t_bootstrap, (void*) boot)) {
|
1996-12-17 00:05:22 +00:00
|
|
|
PyErr_SetString(ThreadError, "can't start new thread\n");
|
1997-05-05 20:56:21 +00:00
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_XDECREF(keyw);
|
|
|
|
PyMem_DEL(boot);
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
thread_exit_thread(self, args)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
1996-12-17 00:05:22 +00:00
|
|
|
PyErr_SetNone(PyExc_SystemExit);
|
1995-03-17 10:42:27 +00:00
|
|
|
return NULL;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
#ifndef NO_EXIT_PROG
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
thread_exit_prog(self, args)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
|
|
|
int sts;
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_Parse(args, "i", &sts))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
1996-12-17 00:05:22 +00:00
|
|
|
Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
|
1992-08-04 12:41:02 +00:00
|
|
|
for (;;) { } /* Should not be reached */
|
|
|
|
}
|
1994-08-01 11:34:53 +00:00
|
|
|
#endif
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
thread_allocate_lock(self, args)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1992-08-04 12:41:02 +00:00
|
|
|
return NULL;
|
1996-12-17 00:05:22 +00:00
|
|
|
return (PyObject *) newlockobject();
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyObject *
|
1994-08-01 11:34:53 +00:00
|
|
|
thread_get_ident(self, args)
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1994-08-01 11:34:53 +00:00
|
|
|
{
|
|
|
|
long ident;
|
1996-12-17 00:05:22 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
ident = get_thread_ident();
|
|
|
|
if (ident == -1) {
|
1996-12-17 00:05:22 +00:00
|
|
|
PyErr_SetString(ThreadError, "no current thread ident");
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-17 00:05:22 +00:00
|
|
|
return PyInt_FromLong(ident);
|
1994-08-01 11:34:53 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:05:22 +00:00
|
|
|
static PyMethodDef thread_methods[] = {
|
1997-05-05 20:56:21 +00:00
|
|
|
{"start_new_thread", (PyCFunction)thread_start_new_thread, 1},
|
|
|
|
{"start_new", (PyCFunction)thread_start_new_thread, 1},
|
1996-12-17 00:05:22 +00:00
|
|
|
{"allocate_lock", (PyCFunction)thread_allocate_lock},
|
|
|
|
{"allocate", (PyCFunction)thread_allocate_lock},
|
|
|
|
{"exit_thread", (PyCFunction)thread_exit_thread},
|
|
|
|
{"exit", (PyCFunction)thread_exit_thread},
|
|
|
|
{"get_ident", (PyCFunction)thread_get_ident},
|
1994-08-01 11:34:53 +00:00
|
|
|
#ifndef NO_EXIT_PROG
|
1996-12-17 00:05:22 +00:00
|
|
|
{"exit_prog", (PyCFunction)thread_exit_prog},
|
1994-08-01 11:34:53 +00:00
|
|
|
#endif
|
1992-08-04 12:41:02 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization function */
|
|
|
|
|
|
|
|
void
|
|
|
|
initthread()
|
|
|
|
{
|
1996-12-17 00:05:22 +00:00
|
|
|
PyObject *m, *d;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
1996-12-17 00:05:22 +00:00
|
|
|
m = Py_InitModule("thread", thread_methods);
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
/* Add a symbolic constant */
|
1996-12-17 00:05:22 +00:00
|
|
|
d = PyModule_GetDict(m);
|
1997-10-01 04:29:29 +00:00
|
|
|
ThreadError = PyErr_NewException("thread.error", NULL, NULL);
|
1996-12-17 00:05:22 +00:00
|
|
|
PyDict_SetItemString(d, "error", ThreadError);
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
/* Initialize the C thread library */
|
|
|
|
init_thread();
|
|
|
|
}
|