This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: replace mutex with object synchronization
- From: Anthony Green <green at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Sun, 29 Feb 2004 19:37:19 -0800
- Subject: Patch: replace mutex with object synchronization
- Organization: Red Hat, Inc.
Since I've been looking at libgcj's use of posix threads I came across
this bit of code which uses _Jv_Mutex_t to synchronize access to a data
structure. This is guaranteed to use the underlying system's mutex
primitives. We can do a little better by using object synchronization
and our own lightweight locks.
Perhaps this is a micro-optimization, but it also collapses the locking
code down from three places to a single code fragment.
Ok for HEAD?
AG
2004-02-29 Anthony Green <green@redhat.com>
* java/lang/natClass.cc (_Jv_FindIIndex): Replace mutex usage with
faster object synchronization.
Index: libjava/java/lang/natClass.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClass.cc,v
retrieving revision 1.74
diff -c -p -r1.74 natClass.cc
*** libjava/java/lang/natClass.cc 3 Dec 2003 21:26:59 -0000 1.74
--- libjava/java/lang/natClass.cc 1 Mar 2004 03:24:56 -0000
*************** _Jv_AppendPartialITable (jclass klass, j
*** 1357,1365 ****
return pos;
}
- static _Jv_Mutex_t iindex_mutex;
- static bool iindex_mutex_initialized = false;
-
// We need to find the correct offset in the Class Interface Dispatch
// Table for a given interface. Once we have that, invoking an interface
// method just requires combining the Method's index in the interface
--- 1334,1339 ----
*************** _Jv_FindIIndex (jclass *ifaces, jshort *
*** 1382,1398 ****
int i;
int j;
! // Acquire a global lock to prevent itable corruption in case of multiple
! // classes that implement an intersecting set of interfaces being linked
! // simultaneously. We can assume that the mutex will be initialized
! // single-threaded.
! if (! iindex_mutex_initialized)
! {
! _Jv_MutexInit (&iindex_mutex);
! iindex_mutex_initialized = true;
! }
!
! _Jv_MutexLock (&iindex_mutex);
for (i=1;; i++) /* each potential position in ioffsets */
{
--- 1356,1370 ----
int i;
int j;
! // Acquire a global lock to prevent itable corruption in case of
! // multiple classes that implement an intersecting set of interfaces
! // being linked simultaneously. We can assume that the sync object
! // will be initialized single-threaded.
! static jobject iindex_sync_object = NULL;
! if (__builtin_expect (iindex_sync_object == NULL, false))
! iindex_sync_object = _Jv_AllocObject(&java::lang::Object::class$,
! sizeof (java::lang::Object));
! JvSynchronize lock(iindex_sync_object);
for (i=1;; i++) /* each potential position in ioffsets */
{
*************** _Jv_FindIIndex (jclass *ifaces, jshort *
*** 1430,1437 ****
}
ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
}
-
- _Jv_MutexUnlock (&iindex_mutex);
return i;
}
--- 1402,1407 ----
--
Anthony Green <green@redhat.com>
Red Hat, Inc.