Commit 544fb293 authored by Jim Fulton's avatar Jim Fulton

Added conditional compilation logic to allow compilation in

non-threaded environments.
parent 85b7c316
...@@ -53,11 +53,13 @@ ...@@ -53,11 +53,13 @@
static char ThreadLock_module_documentation[] = static char ThreadLock_module_documentation[] =
"" ""
"\n$Id: ThreadLock.c,v 1.2 1997/07/02 20:21:02 jim Exp $" "\n$Id: ThreadLock.c,v 1.3 1997/10/30 15:29:21 jim Exp $"
; ;
#include "Python.h" #include "Python.h"
#ifdef WITH_THREAD
#include "thread.h" #include "thread.h"
#endif
static PyObject *ErrorObject; static PyObject *ErrorObject;
...@@ -71,7 +73,9 @@ typedef struct { ...@@ -71,7 +73,9 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
int count; int count;
long id; long id;
#ifdef WITH_THREAD
type_lock lock; type_lock lock;
#endif
} ThreadLockObject; } ThreadLockObject;
staticforward PyTypeObject ThreadLockType; staticforward PyTypeObject ThreadLockType;
...@@ -81,7 +85,11 @@ staticforward PyTypeObject ThreadLockType; ...@@ -81,7 +85,11 @@ staticforward PyTypeObject ThreadLockType;
static int static int
cacquire(ThreadLockObject *self) cacquire(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
long id = get_thread_ident(); long id = get_thread_ident();
#else
long id = 1;
#endif
if(self->count >= 0 && self->id==id) if(self->count >= 0 && self->id==id)
{ {
/* Somebody has locked me. It is either the current thread or /* Somebody has locked me. It is either the current thread or
...@@ -93,9 +101,11 @@ cacquire(ThreadLockObject *self) ...@@ -93,9 +101,11 @@ cacquire(ThreadLockObject *self)
} }
else else
{ {
#ifdef WITH_THREAD
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
acquire_lock(self->lock, 1); acquire_lock(self->lock, 1);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
#endif
self->count=0; self->count=0;
self->id=id; self->id=id;
} }
...@@ -113,7 +123,11 @@ acquire(ThreadLockObject *self, PyObject *args) ...@@ -113,7 +123,11 @@ acquire(ThreadLockObject *self, PyObject *args)
static int static int
crelease(ThreadLockObject *self) crelease(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
long id = get_thread_ident(); long id = get_thread_ident();
#else
long id = 1;
#endif
if(self->count >= 0 && self->id==id) if(self->count >= 0 && self->id==id)
{ {
/* Somebody has locked me. It is either the current thread or /* Somebody has locked me. It is either the current thread or
...@@ -122,7 +136,9 @@ crelease(ThreadLockObject *self) ...@@ -122,7 +136,9 @@ crelease(ThreadLockObject *self)
if another thread had the lock, then the id would not be this if another thread had the lock, then the id would not be this
one. */ one. */
self->count--; self->count--;
#ifdef WITH_THREAD
if(self->count < 0) release_lock(self->lock); if(self->count < 0) release_lock(self->lock);
#endif
} }
else else
{ {
...@@ -177,7 +193,9 @@ static struct PyMethodDef ThreadLock_methods[] = { ...@@ -177,7 +193,9 @@ static struct PyMethodDef ThreadLock_methods[] = {
static void static void
ThreadLock_dealloc(ThreadLockObject *self) ThreadLock_dealloc(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
free_lock(self->lock); free_lock(self->lock);
#endif
PyMem_DEL(self); PyMem_DEL(self);
} }
...@@ -238,20 +256,26 @@ newThreadLockObject(ThreadLockObject *self, PyObject *args) ...@@ -238,20 +256,26 @@ newThreadLockObject(ThreadLockObject *self, PyObject *args)
UNLESS(PyArg_ParseTuple(args,"")) return NULL; UNLESS(PyArg_ParseTuple(args,"")) return NULL;
UNLESS(self = PyObject_NEW(ThreadLockObject, &ThreadLockType)) return NULL; UNLESS(self = PyObject_NEW(ThreadLockObject, &ThreadLockType)) return NULL;
self->lock = allocate_lock();
self->count=-1; self->count=-1;
#ifdef WITH_THREAD
self->lock = allocate_lock();
if (self->lock == NULL) { if (self->lock == NULL) {
PyMem_DEL(self); PyMem_DEL(self);
self = NULL; self = NULL;
PyErr_SetString(ErrorObject, "can't allocate lock"); PyErr_SetString(ErrorObject, "can't allocate lock");
} }
#endif
return (PyObject*)self; return (PyObject*)self;
} }
static PyObject * static PyObject *
ident(PyObject *self, PyObject *args) ident(PyObject *self, PyObject *args)
{ {
#ifdef WITH_THREAD
return PyInt_FromLong(get_thread_ident()); return PyInt_FromLong(get_thread_ident());
#else
return PyInt_FromLong(0);
#endif
} }
/* List of methods defined in the module */ /* List of methods defined in the module */
...@@ -272,7 +296,7 @@ void ...@@ -272,7 +296,7 @@ void
initThreadLock() initThreadLock()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.2 $"; char *rev="$Revision: 1.3 $";
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("ThreadLock", Module_methods, m = Py_InitModule4("ThreadLock", Module_methods,
...@@ -290,7 +314,12 @@ initThreadLock() ...@@ -290,7 +314,12 @@ initThreadLock()
PyDict_SetItemString(d, "__version__", PyDict_SetItemString(d, "__version__",
PyString_FromStringAndSize(rev+11,strlen(rev+11)-2)); PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
#ifdef WITH_THREAD
PyDict_SetItemString(d, "WITH_THREAD", PyInt_FromLong(1));
#else
PyDict_SetItemString(d, "WITH_THREAD", Py_None);
#endif
/* Check for errors */ /* Check for errors */
if (PyErr_Occurred()) if (PyErr_Occurred())
...@@ -301,6 +330,10 @@ initThreadLock() ...@@ -301,6 +330,10 @@ initThreadLock()
Revision Log: Revision Log:
$Log: ThreadLock.c,v $ $Log: ThreadLock.c,v $
Revision 1.3 1997/10/30 15:29:21 jim
Added conditional compilation logic to allow compilation in
non-threaded environments.
Revision 1.2 1997/07/02 20:21:02 jim Revision 1.2 1997/07/02 20:21:02 jim
Added stupid parens and other changes to make 'gcc -Wall -pedantic' Added stupid parens and other changes to make 'gcc -Wall -pedantic'
happy. Got rid of unused macros. happy. Got rid of unused macros.
......
...@@ -53,11 +53,13 @@ ...@@ -53,11 +53,13 @@
static char ThreadLock_module_documentation[] = static char ThreadLock_module_documentation[] =
"" ""
"\n$Id: ThreadLock.c,v 1.2 1997/07/02 20:21:02 jim Exp $" "\n$Id: ThreadLock.c,v 1.3 1997/10/30 15:29:21 jim Exp $"
; ;
#include "Python.h" #include "Python.h"
#ifdef WITH_THREAD
#include "thread.h" #include "thread.h"
#endif
static PyObject *ErrorObject; static PyObject *ErrorObject;
...@@ -71,7 +73,9 @@ typedef struct { ...@@ -71,7 +73,9 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
int count; int count;
long id; long id;
#ifdef WITH_THREAD
type_lock lock; type_lock lock;
#endif
} ThreadLockObject; } ThreadLockObject;
staticforward PyTypeObject ThreadLockType; staticforward PyTypeObject ThreadLockType;
...@@ -81,7 +85,11 @@ staticforward PyTypeObject ThreadLockType; ...@@ -81,7 +85,11 @@ staticforward PyTypeObject ThreadLockType;
static int static int
cacquire(ThreadLockObject *self) cacquire(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
long id = get_thread_ident(); long id = get_thread_ident();
#else
long id = 1;
#endif
if(self->count >= 0 && self->id==id) if(self->count >= 0 && self->id==id)
{ {
/* Somebody has locked me. It is either the current thread or /* Somebody has locked me. It is either the current thread or
...@@ -93,9 +101,11 @@ cacquire(ThreadLockObject *self) ...@@ -93,9 +101,11 @@ cacquire(ThreadLockObject *self)
} }
else else
{ {
#ifdef WITH_THREAD
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
acquire_lock(self->lock, 1); acquire_lock(self->lock, 1);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
#endif
self->count=0; self->count=0;
self->id=id; self->id=id;
} }
...@@ -113,7 +123,11 @@ acquire(ThreadLockObject *self, PyObject *args) ...@@ -113,7 +123,11 @@ acquire(ThreadLockObject *self, PyObject *args)
static int static int
crelease(ThreadLockObject *self) crelease(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
long id = get_thread_ident(); long id = get_thread_ident();
#else
long id = 1;
#endif
if(self->count >= 0 && self->id==id) if(self->count >= 0 && self->id==id)
{ {
/* Somebody has locked me. It is either the current thread or /* Somebody has locked me. It is either the current thread or
...@@ -122,7 +136,9 @@ crelease(ThreadLockObject *self) ...@@ -122,7 +136,9 @@ crelease(ThreadLockObject *self)
if another thread had the lock, then the id would not be this if another thread had the lock, then the id would not be this
one. */ one. */
self->count--; self->count--;
#ifdef WITH_THREAD
if(self->count < 0) release_lock(self->lock); if(self->count < 0) release_lock(self->lock);
#endif
} }
else else
{ {
...@@ -177,7 +193,9 @@ static struct PyMethodDef ThreadLock_methods[] = { ...@@ -177,7 +193,9 @@ static struct PyMethodDef ThreadLock_methods[] = {
static void static void
ThreadLock_dealloc(ThreadLockObject *self) ThreadLock_dealloc(ThreadLockObject *self)
{ {
#ifdef WITH_THREAD
free_lock(self->lock); free_lock(self->lock);
#endif
PyMem_DEL(self); PyMem_DEL(self);
} }
...@@ -238,20 +256,26 @@ newThreadLockObject(ThreadLockObject *self, PyObject *args) ...@@ -238,20 +256,26 @@ newThreadLockObject(ThreadLockObject *self, PyObject *args)
UNLESS(PyArg_ParseTuple(args,"")) return NULL; UNLESS(PyArg_ParseTuple(args,"")) return NULL;
UNLESS(self = PyObject_NEW(ThreadLockObject, &ThreadLockType)) return NULL; UNLESS(self = PyObject_NEW(ThreadLockObject, &ThreadLockType)) return NULL;
self->lock = allocate_lock();
self->count=-1; self->count=-1;
#ifdef WITH_THREAD
self->lock = allocate_lock();
if (self->lock == NULL) { if (self->lock == NULL) {
PyMem_DEL(self); PyMem_DEL(self);
self = NULL; self = NULL;
PyErr_SetString(ErrorObject, "can't allocate lock"); PyErr_SetString(ErrorObject, "can't allocate lock");
} }
#endif
return (PyObject*)self; return (PyObject*)self;
} }
static PyObject * static PyObject *
ident(PyObject *self, PyObject *args) ident(PyObject *self, PyObject *args)
{ {
#ifdef WITH_THREAD
return PyInt_FromLong(get_thread_ident()); return PyInt_FromLong(get_thread_ident());
#else
return PyInt_FromLong(0);
#endif
} }
/* List of methods defined in the module */ /* List of methods defined in the module */
...@@ -272,7 +296,7 @@ void ...@@ -272,7 +296,7 @@ void
initThreadLock() initThreadLock()
{ {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.2 $"; char *rev="$Revision: 1.3 $";
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("ThreadLock", Module_methods, m = Py_InitModule4("ThreadLock", Module_methods,
...@@ -290,7 +314,12 @@ initThreadLock() ...@@ -290,7 +314,12 @@ initThreadLock()
PyDict_SetItemString(d, "__version__", PyDict_SetItemString(d, "__version__",
PyString_FromStringAndSize(rev+11,strlen(rev+11)-2)); PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
#ifdef WITH_THREAD
PyDict_SetItemString(d, "WITH_THREAD", PyInt_FromLong(1));
#else
PyDict_SetItemString(d, "WITH_THREAD", Py_None);
#endif
/* Check for errors */ /* Check for errors */
if (PyErr_Occurred()) if (PyErr_Occurred())
...@@ -301,6 +330,10 @@ initThreadLock() ...@@ -301,6 +330,10 @@ initThreadLock()
Revision Log: Revision Log:
$Log: ThreadLock.c,v $ $Log: ThreadLock.c,v $
Revision 1.3 1997/10/30 15:29:21 jim
Added conditional compilation logic to allow compilation in
non-threaded environments.
Revision 1.2 1997/07/02 20:21:02 jim Revision 1.2 1997/07/02 20:21:02 jim
Added stupid parens and other changes to make 'gcc -Wall -pedantic' Added stupid parens and other changes to make 'gcc -Wall -pedantic'
happy. Got rid of unused macros. happy. Got rid of unused macros.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment