1992-08-04 09:41:02 -03:00
|
|
|
/***********************************************************
|
1995-01-04 15:10:35 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03: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 09:41:02 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-08-04 09:41:02 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03: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 09:41:02 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Thread module */
|
|
|
|
/* Interface to Sjoerd's portable C thread library */
|
|
|
|
|
|
|
|
#include "allobjects.h"
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
#ifndef WITH_THREAD
|
|
|
|
Error! The rest of Python is not compiled with thread support.
|
|
|
|
Rerun configure, adding a --with-thread option.
|
|
|
|
#endif
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
#include "thread.h"
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
extern int threads_started;
|
1992-08-17 05:59:08 -03:00
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
static object *ThreadError;
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Lock objects */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
type_lock lock_lock;
|
|
|
|
} lockobject;
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
staticforward typeobject Locktype;
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
#define is_lockobject(v) ((v)->ob_type == &Locktype)
|
|
|
|
|
1993-01-06 09:36:38 -04:00
|
|
|
type_lock
|
|
|
|
getlocklock(lock)
|
|
|
|
object *lock;
|
|
|
|
{
|
|
|
|
if (lock == NULL || !is_lockobject(lock))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return ((lockobject *) lock)->lock_lock;
|
|
|
|
}
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
static lockobject *
|
|
|
|
newlockobject()
|
|
|
|
{
|
|
|
|
lockobject *self;
|
|
|
|
self = NEWOBJ(lockobject, &Locktype);
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->lock_lock = allocate_lock();
|
|
|
|
if (self->lock_lock == NULL) {
|
|
|
|
DEL(self);
|
|
|
|
self = NULL;
|
|
|
|
err_setstr(ThreadError, "can't allocate lock");
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
DEL(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
lock_acquire_lock(self, args)
|
|
|
|
lockobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args != NULL) {
|
|
|
|
if (!getargs(args, "i", &i))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i = 1;
|
|
|
|
|
1992-08-05 16:58:53 -03:00
|
|
|
BGN_SAVE
|
1992-08-04 09:41:02 -03:00
|
|
|
i = acquire_lock(self->lock_lock, i);
|
1992-08-05 16:58:53 -03:00
|
|
|
END_SAVE
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
if (args == NULL) {
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return newintobject((long)i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
lock_release_lock(self, args)
|
|
|
|
lockobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Sanity check: the lock must be locked */
|
|
|
|
if (acquire_lock(self->lock_lock, 0)) {
|
|
|
|
release_lock(self->lock_lock);
|
|
|
|
err_setstr(ThreadError, "release unlocked lock");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_lock(self->lock_lock);
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
lock_locked_lock(self, args)
|
|
|
|
lockobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (acquire_lock(self->lock_lock, 0)) {
|
|
|
|
release_lock(self->lock_lock);
|
|
|
|
return newintobject(0L);
|
|
|
|
}
|
|
|
|
return newintobject(1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct methodlist lock_methods[] = {
|
1994-08-01 08:34:53 -03:00
|
|
|
{"acquire_lock", (method)lock_acquire_lock},
|
|
|
|
{"acquire", (method)lock_acquire_lock},
|
|
|
|
{"release_lock", (method)lock_release_lock},
|
|
|
|
{"release", (method)lock_release_lock},
|
|
|
|
{"locked_lock", (method)lock_locked_lock},
|
|
|
|
{"locked", (method)lock_locked_lock},
|
1992-08-04 09:41:02 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
lock_getattr(self, name)
|
|
|
|
lockobject *self;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
return findmethod(lock_methods, (object *)self, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static typeobject Locktype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
1994-08-01 08:34:53 -03:00
|
|
|
0, /*ob_size*/
|
|
|
|
"lock", /*tp_name*/
|
|
|
|
sizeof(lockobject), /*tp_size*/
|
|
|
|
0, /*tp_itemsize*/
|
1992-08-04 09:41:02 -03:00
|
|
|
/* methods */
|
1994-08-01 08:34:53 -03: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 09:41:02 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Module functions */
|
|
|
|
|
|
|
|
static void
|
|
|
|
t_bootstrap(args_raw)
|
|
|
|
void *args_raw;
|
|
|
|
{
|
|
|
|
object *args = (object *) args_raw;
|
|
|
|
object *func, *arg, *res;
|
|
|
|
|
1992-08-17 05:59:08 -03:00
|
|
|
threads_started++;
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
restore_thread((void *)NULL);
|
|
|
|
func = gettupleitem(args, 0);
|
|
|
|
arg = gettupleitem(args, 1);
|
|
|
|
res = call_object(func, arg);
|
1993-12-03 12:54:45 -04:00
|
|
|
DECREF(args); /* Matches the INCREF(args) in thread_start_new_thread */
|
1992-08-04 09:41:02 -03:00
|
|
|
if (res == NULL) {
|
1995-03-17 06:42:27 -04:00
|
|
|
if (err_occurred() == SystemExit)
|
|
|
|
err_clear();
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unhandled exception in thread:\n");
|
|
|
|
print_error(); /* From pythonmain.c */
|
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1995-01-02 15:30:30 -04:00
|
|
|
else
|
|
|
|
DECREF(res);
|
|
|
|
(void) save_thread(); /* Should always be NULL */
|
1992-08-04 09:41:02 -03:00
|
|
|
exit_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
thread_start_new_thread(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *func, *arg;
|
|
|
|
|
|
|
|
if (!getargs(args, "(OO)", &func, &arg))
|
|
|
|
return NULL;
|
|
|
|
INCREF(args);
|
1993-01-06 09:36:38 -04:00
|
|
|
/* Initialize the interpreter's stack save/restore mechanism */
|
|
|
|
init_save_thread();
|
1992-08-04 09:41:02 -03:00
|
|
|
if (!start_new_thread(t_bootstrap, (void*) args)) {
|
|
|
|
DECREF(args);
|
|
|
|
err_setstr(ThreadError, "can't start new thread\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Otherwise the DECREF(args) is done by t_bootstrap */
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
thread_exit_thread(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
1995-03-17 06:42:27 -04:00
|
|
|
err_set(SystemExit);
|
|
|
|
return NULL;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
#ifndef NO_EXIT_PROG
|
1992-08-04 09:41:02 -03:00
|
|
|
static object *
|
|
|
|
thread_exit_prog(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int sts;
|
|
|
|
if (!getargs(args, "i", &sts))
|
|
|
|
return NULL;
|
1992-08-17 05:59:08 -03:00
|
|
|
goaway(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
|
1992-08-04 09:41:02 -03:00
|
|
|
for (;;) { } /* Should not be reached */
|
|
|
|
}
|
1994-08-01 08:34:53 -03:00
|
|
|
#endif
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
static object *
|
|
|
|
thread_allocate_lock(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
1992-08-05 16:58:53 -03:00
|
|
|
return (object *) newlockobject();
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
static object *
|
|
|
|
thread_get_ident(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
long ident;
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
ident = get_thread_ident();
|
|
|
|
if (ident == -1) {
|
|
|
|
err_setstr(ThreadError, "no current thread ident");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return newintobject(ident);
|
|
|
|
}
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
static struct methodlist thread_methods[] = {
|
1994-08-01 08:34:53 -03:00
|
|
|
{"start_new_thread", (method)thread_start_new_thread},
|
|
|
|
{"start_new", (method)thread_start_new_thread},
|
|
|
|
{"allocate_lock", (method)thread_allocate_lock},
|
|
|
|
{"allocate", (method)thread_allocate_lock},
|
|
|
|
{"exit_thread", (method)thread_exit_thread},
|
|
|
|
{"exit", (method)thread_exit_thread},
|
|
|
|
{"get_ident", (method)thread_get_ident},
|
|
|
|
#ifndef NO_EXIT_PROG
|
|
|
|
{"exit_prog", (method)thread_exit_prog},
|
|
|
|
#endif
|
1992-08-04 09:41:02 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization function */
|
|
|
|
|
|
|
|
void
|
|
|
|
initthread()
|
|
|
|
{
|
|
|
|
object *m, *d, *x;
|
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = initmodule("thread", thread_methods);
|
|
|
|
|
|
|
|
/* Add a symbolic constant */
|
|
|
|
d = getmoduledict(m);
|
|
|
|
ThreadError = newstringobject("thread.error");
|
|
|
|
INCREF(ThreadError);
|
|
|
|
dictinsert(d, "error", ThreadError);
|
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
if (err_occurred())
|
|
|
|
fatal("can't initialize module thread");
|
|
|
|
|
|
|
|
/* Initialize the C thread library */
|
|
|
|
init_thread();
|
|
|
|
}
|