[Zope-Checkins] CVS: Zope2 - ThreadLock.c:1.7.122.1

Brian Lloyd brian@digiciool.com
Thu, 15 Mar 2001 10:05:51 -0500 (EST)


Update of /cvs-repository/Zope2/lib/Components/ExtensionClass/src
In directory korak:/home/brian/temp/zope-23-branch/lib/Components/ExtensionClass/src

Modified Files:
      Tag: zope-2_3-branch
	ThreadLock.c 
Log Message:
merged missing changes



--- Updated File ThreadLock.c in package Zope2 --
--- ThreadLock.c	1999/02/19 16:10:05	1.7
+++ ThreadLock.c	2001/03/15 15:05:51	1.7.122.1
@@ -93,8 +93,9 @@
 staticforward PyTypeObject ThreadLockType;
 
 static int
-cacquire(ThreadLockObject *self)
+cacquire(ThreadLockObject *self, int wait)
 {
+  int acquired = 1;
 #ifdef WITH_THREAD
   long id = get_thread_ident();
 #else
@@ -113,19 +114,26 @@
     {
 #ifdef WITH_THREAD
       Py_BEGIN_ALLOW_THREADS
-      acquire_lock(self->lock, 1);
+      acquired = acquire_lock(self->lock, wait ? WAIT_LOCK : NOWAIT_LOCK);
       Py_END_ALLOW_THREADS
 #endif
-      self->count=0;
-      self->id=id;
+      if (acquired)
+        {
+          self->count=0;
+          self->id=id;
+        }
     }
-  return 0;
+  return acquired;
 }
 
 static PyObject *
 acquire(ThreadLockObject *self, PyObject *args)
 {
-  if(cacquire(self) < 0) return NULL;
+  int wait = -1, acquired;
+  if (! PyArg_ParseTuple(args, "|i", &wait)) return NULL;
+  acquired=cacquire(self, wait);
+  if(acquired < 0) return NULL;
+  if (wait >= 0) return PyInt_FromLong(acquired);
   Py_INCREF(Py_None);
   return Py_None;
 }
@@ -138,6 +146,7 @@
 #else
   long id = 1;
 #endif
+
   if(self->count >= 0 && self->id==id)
     {
       /* Somebody has locked me.  It is either the current thread or
@@ -161,6 +170,7 @@
 static PyObject *
 release(ThreadLockObject *self, PyObject *args)
 {
+  if (! PyArg_ParseTuple(args, "")) return NULL;
   if(crelease(self) < 0) return NULL;
   Py_INCREF(Py_None);
   return Py_None;
@@ -172,7 +182,7 @@
   PyObject *f, *a=0, *k=0;
 
   UNLESS(PyArg_ParseTuple(args,"OO|O",&f, &a, &k)) return NULL;
-  if(cacquire(self) < 0) return NULL;
+  if(cacquire(self, -1) < 0) return NULL;
   f=PyEval_CallObjectWithKeywords(f,a,k);
   if(crelease(self) < 0)
     {
@@ -189,7 +199,7 @@
    "Acquire the lock, call the function, and then release the lock.\n"
   },
   {"acquire", (PyCFunction)acquire, 1,
-   "acquire() -- Acquire a lock, taking the thread ID into account"
+   "acquire([wait]) -- Acquire a lock, taking the thread ID into account"
   },
   {"release", (PyCFunction)release, 1,
    "release() -- Release a lock, taking the thread ID into account"