This is the mail archive of the
java-patches@sourceware.cygnus.com
mailing list for the Java project.
Patch: pthreads error checking
- To: Java Patch List <java-patches@sourceware.cygnus.com>
- Subject: Patch: pthreads error checking
- From: Tom Tromey <tromey@cygnus.com>
- Date: 01 Sep 1999 13:14:30 -0600
- Reply-To: tromey@cygnus.com
The last missed patch. This one adds some error checking to the
pthreads thread implementation. It also makes things a bit slower,
which is unfortunate.
Re-reading this, it occurs to me that this patch is actually flawed.
pthread_mutex_trylock doesn't actually tell us if the thread holds the
lock, just whether the thread *can* hold the lock. The lock might be
unheld. I'll write a better patch shortly.
1999-09-01 Tom Tromey <tromey@cygnus.com>
* posix-threads.cc (_Jv_CondWait): Call _Jv_PthreadCheckMonitor.
* include/posix-threads.h (_Jv_PthreadCheckMonitor): New
function.
(_Jv_CondNotify): Use it.
(_Jv_CondNotifyAll): Likewise.
Tom
Index: posix-threads.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/posix-threads.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- posix-threads.cc 1999/08/25 00:46:23 1.5
+++ posix-threads.cc 1999/09/01 18:29:38 1.6
@@ -75,6 +75,9 @@
_Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
jlong millis, jint nanos)
{
+ if (_Jv_PthreadCheckMonitor (mu))
+ return 1;
+
int r;
pthread_mutex_t *pmu;
#ifdef HAVE_RECURSIVE_MUTEX
@@ -82,6 +85,7 @@
#else
pmu = &mu->mutex2;
#endif
+
if (millis == 0 && nanos == 0)
r = pthread_cond_wait (cv, pmu);
else
@@ -96,6 +100,7 @@
if (r && errno == ETIMEDOUT)
r = 0;
}
+
return r;
}
Index: include/posix-threads.h
===================================================================
RCS file: /cvs/java/libgcj/libjava/include/posix-threads.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- posix-threads.h 1999/04/07 14:52:35 1.1
+++ posix-threads.h 1999/09/01 18:29:39 1.2
@@ -65,6 +65,25 @@
typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
+// This is a convenience function used only by the pthreads thread
+// implementation. This is slow, but that's too bad -- we need to do
+// the checks for correctness. It might be nice to be able to compile
+// this out.
+inline int _Jv_PthreadCheckMonitor (_Jv_Mutex_t *mu)
+{
+ pthread_mutex_t *pmu;
+#ifdef HAVE_RECURSIVE_MUTEX
+ pmu = mu;
+#else
+ pmu = &mu->mutex2;
+#endif
+ // See if the mutex is locked by this thread.
+ if (pthread_mutex_trylock (pmu))
+ return 1;
+ pthread_mutex_unlock (pmu);
+ return 0;
+}
+
//
// Condition variables.
//
@@ -94,17 +113,15 @@
jlong millis, jint nanos);
inline int
-_Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
+_Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
{
- // FIXME: check to see if mutex is held by current thread.
- return pthread_cond_signal (cv);
+ return _Jv_PthreadCheckMonitor (mu) || pthread_cond_signal (cv);
}
inline int
-_Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
+_Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
{
- // FIXME: check to see if mutex is held by current thread.
- return pthread_cond_broadcast (cv);
+ return _Jv_PthreadCheckMonitor (mu) || pthread_cond_broadcast (cv);
}