JBoss Native SVN: r1157 - in trunk/sight: native/include and 2 other directories.
by jbossnative-commits@lists.jboss.org
Author: mladen.turk(a)jboss.com
Date: 2007-10-31 04:17:52 -0400 (Wed, 31 Oct 2007)
New Revision: 1157
Added:
trunk/sight/java/org/jboss/sight/GlobalMutex.java
Modified:
trunk/sight/java/org/jboss/sight/Mutex.java
trunk/sight/native/include/sight_local.h
trunk/sight/native/share/clazz.c
trunk/sight/native/share/mutex.c
trunk/sight/test/org/jboss/sight/MutexTest.java
Log:
Add APR global mutexes
Added: trunk/sight/java/org/jboss/sight/GlobalMutex.java
===================================================================
--- trunk/sight/java/org/jboss/sight/GlobalMutex.java (rev 0)
+++ trunk/sight/java/org/jboss/sight/GlobalMutex.java 2007-10-31 08:17:52 UTC (rev 1157)
@@ -0,0 +1,111 @@
+/*
+ * SIGHT - System information gathering hybrid tool
+ *
+ * Copyright(c) 2007 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ */
+
+package org.jboss.sight;
+
+/**
+ * GlobalMutex
+ * APR Process Locking Routines
+ *
+ * @author Mladen Turk
+ *
+ */
+
+public class GlobalMutex extends Mutex
+{
+
+ /**
+ * Create new Mutex with root as parent pool.
+ */
+ public GlobalMutex()
+ {
+ super();
+ global = true;
+ }
+
+ /**
+ * Create new Mutex
+ * @param parent The parent pool. If this is 0, the new pool is a root
+ * pool. If it is non-zero, the new pool will inherit all
+ * of its parent pool's attributes, except the apr_pool_t will
+ * be a sub-pool.
+ */
+ public GlobalMutex(Pool parent)
+ {
+ super(parent);
+ global = true;
+ }
+
+ /**
+ * Create new Mutex
+ * @param fname A file name to use if the lock mechanism requires one. This
+ * argument should always be provided. The lock code itself will
+ * determine if it should be used.
+ * @param mech The mechanism to use for the interprocess lock, if any; one of
+ * <PRE>
+ * APR_LOCK_FCNTL
+ * APR_LOCK_FLOCK
+ * APR_LOCK_SYSVSEM
+ * APR_LOCK_POSIXSEM
+ * APR_LOCK_PROC_PTHREAD
+ * APR_LOCK_DEFAULT pick the default mechanism for the platform
+ * </PRE>
+ */
+ public GlobalMutex(String fname, MutexType mech)
+ throws NullPointerException, OperatingSystemException
+ {
+ super();
+ global = true;
+ create(fname, mech);
+ }
+
+ /**
+ * Create new Mutex
+ * @param fname A file name to use if the lock mechanism requires one. This
+ * argument should always be provided. The lock code itself will
+ * determine if it should be used.
+ * @param mech The mechanism to use for the interprocess lock, if any; one of
+ * <PRE>
+ * APR_LOCK_FCNTL
+ * APR_LOCK_FLOCK
+ * APR_LOCK_SYSVSEM
+ * APR_LOCK_POSIXSEM
+ * APR_LOCK_PROC_PTHREAD
+ * APR_LOCK_DEFAULT pick the default mechanism for the platform
+ * </PRE>
+ * @param parent The parent pool. If this is 0, the new pool is a root
+ * pool. If it is non-zero, the new pool will inherit all
+ * of its parent pool's attributes, except the apr_pool_t will
+ * be a sub-pool.
+ */
+ public GlobalMutex(String fname, MutexType mech, Pool parent)
+ throws NullPointerException, OperatingSystemException
+ {
+ super(parent);
+ global = true;
+ create(fname, mech);
+ }
+
+}
Property changes on: trunk/sight/java/org/jboss/sight/GlobalMutex.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/sight/java/org/jboss/sight/Mutex.java
===================================================================
--- trunk/sight/java/org/jboss/sight/Mutex.java 2007-10-30 17:36:10 UTC (rev 1156)
+++ trunk/sight/java/org/jboss/sight/Mutex.java 2007-10-31 08:17:52 UTC (rev 1157)
@@ -25,7 +25,8 @@
package org.jboss.sight;
-/** Mutex
+/**
+ * Mutex
* APR Process Locking Routines
*
* @author Mladen Turk
@@ -35,9 +36,9 @@
public class Mutex extends NativeObject
{
- private static native int create0(long instance, String fname, int mech)
+ private static native int create0(long instance, String fname, int mech, boolean global)
throws NullPointerException, OperatingSystemException;
- private static native int child0(long instance, String fname)
+ private static native int child0(long instance, String fname, boolean global)
throws NullPointerException, OperatingSystemException;
private static native int lock0(long instance);
private static native int trylock0(long instance);
@@ -45,22 +46,26 @@
private static native String name0(long instance);
private static native String fname0(long instance);
+ /**
+ * Does the proc mutex lock threads too.
+ */
+ public static boolean PROC_MUTEX_IS_GLOBAL = false;
+ protected boolean global;
/**
* Display the name of the default mutex: APR_LOCK_DEFAULT
*/
public static native String getDefaultName();
-
/**
* Create new Mutex with root as parent pool.
*/
public Mutex()
{
super(0);
+ global = false;
}
-
/**
* Create new Mutex
* @param parent The parent pool. If this is 0, the new pool is a root
@@ -71,6 +76,7 @@
public Mutex(Pool parent)
{
super(parent.POOL);
+ global = false;
}
/**
@@ -92,7 +98,8 @@
throws NullPointerException, OperatingSystemException
{
super(0);
- create0(INSTANCE, fname, mech.valueOf());
+ global = false;
+ create(fname, mech);
}
/**
@@ -118,7 +125,8 @@
throws NullPointerException, OperatingSystemException
{
super(parent.POOL);
- create0(INSTANCE, fname, mech.valueOf());
+ global = false;
+ create(fname, mech);
}
/**
@@ -165,7 +173,7 @@
public int create(String fname, MutexType mech)
throws NullPointerException, OperatingSystemException
{
- return create0(INSTANCE, fname, mech.valueOf());
+ return create0(INSTANCE, fname, mech.valueOf(), global);
}
/**
@@ -181,7 +189,7 @@
public int childInit(String fname)
throws NullPointerException, OperatingSystemException
{
- return child0(INSTANCE, fname);
+ return child0(INSTANCE, fname, global);
}
/**
* Acquire the lock for the given mutex. If the mutex is already locked,
@@ -211,4 +219,14 @@
return unlock0(INSTANCE);
}
+ /**
+ * Does this mutex lock threads too.
+ */
+ public boolean isGlobal()
+ {
+ if (PROC_MUTEX_IS_GLOBAL)
+ return true;
+ else
+ return global;
+ }
}
Modified: trunk/sight/native/include/sight_local.h
===================================================================
--- trunk/sight/native/include/sight_local.h 2007-10-30 17:36:10 UTC (rev 1156)
+++ trunk/sight/native/include/sight_local.h 2007-10-31 08:17:52 UTC (rev 1157)
@@ -322,7 +322,13 @@
(*_E)->SetObjectField(_E, (O), _f##I##n.i, (jobject)(V)); \
} else (void)(0)
+#define SET_SFIELD_Z(I, V) \
+ if (_f##I##n.i) { \
+ if ((V)) (*_E)->SetStaticBooleanField(_E, _clazzn.i, _f##I##n.i, JNI_TRUE); \
+ else (*_E)->SetStaticBooleanField(_E, _clazzn.i, _f##I##n.i, JNI_FALSE); \
+ } else (void)(0)
+
#define CALL_METHOD1(I, O, V) \
if (_m##I##n.i) { \
(*_E)->CallVoidMethod(_E, (O), _m##I##n.i, (V), NULL); \
Modified: trunk/sight/native/share/clazz.c
===================================================================
--- trunk/sight/native/share/clazz.c 2007-10-30 17:36:10 UTC (rev 1156)
+++ trunk/sight/native/share/clazz.c 2007-10-31 08:17:52 UTC (rev 1157)
@@ -94,6 +94,7 @@
SIGHT_CLASS_LDEC(User);
SIGHT_CLASS_LDEC(Service);
SIGHT_CLASS_LDEC(Module);
+SIGHT_CLASS_LDEC(Mutex);
SIGHT_CLASS_LDEC(Network);
SIGHT_CLASS_LDEC(NetworkAdapter);
SIGHT_CLASS_LDEC(NetworkAddress);
@@ -120,6 +121,7 @@
SIGHT_CLASS_LCAL(User);
SIGHT_CLASS_LCAL(Service);
SIGHT_CLASS_LCAL(Module);
+ SIGHT_CLASS_LCAL(Mutex);
SIGHT_CLASS_LCAL(Network);
SIGHT_CLASS_LCAL(NetworkAdapter);
SIGHT_CLASS_LCAL(NetworkAddress);
@@ -147,6 +149,7 @@
SIGHT_CLASS_UCAL(User);
SIGHT_CLASS_UCAL(Service);
SIGHT_CLASS_UCAL(Module);
+ SIGHT_CLASS_UCAL(Mutex);
SIGHT_CLASS_UCAL(Network);
SIGHT_CLASS_UCAL(NetworkAdapter);
SIGHT_CLASS_UCAL(NetworkAddress);
Modified: trunk/sight/native/share/mutex.c
===================================================================
--- trunk/sight/native/share/mutex.c 2007-10-30 17:36:10 UTC (rev 1156)
+++ trunk/sight/native/share/mutex.c 2007-10-31 08:17:52 UTC (rev 1157)
@@ -29,10 +29,44 @@
#include "sight_local.h"
#include "sight_types.h"
+
+/*
+ * Mutex
+ */
+
+J_DECLARE_CLAZZ = {
+ NULL,
+ NULL,
+ SIGHT_CLASS_PATH "Mutex"
+};
+
+J_DECLARE_F_ID(0000) = {
+ NULL,
+ "PROC_MUTEX_IS_GLOBAL",
+ "Z"
+};
+
+SIGHT_CLASS_LDEF(Mutex)
+{
+ if (sight_load_class(_E, &_clazzn))
+ return 1;
+ J_LOAD_SFIELD(0000);
+ SET_SFIELD_Z(0000, APR_PROC_MUTEX_IS_GLOBAL);
+ return 0;
+}
+
+SIGHT_CLASS_UDEF(Mutex)
+{
+ sight_unload_class(_E, &_clazzn);
+}
+
static void mutex_cleanup(int mode, sight_object_t *no)
{
if (mode != POOL_CALLBACK && no && no->native) {
- apr_proc_mutex_destroy((apr_proc_mutex_t *)no->native);
+ if (no->opaque)
+ apr_global_mutex_destroy((apr_global_mutex_t *)no->native);
+ else
+ apr_proc_mutex_destroy((apr_proc_mutex_t *)no->native);
}
}
@@ -45,10 +79,12 @@
SIGHT_EXPORT_DECLARE(jint, Mutex, create0)(SIGHT_STDARGS,
jlong instance,
jstring name,
- jint mech)
+ jint mech,
+ jboolean global)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- apr_proc_mutex_t *mutex = NULL;
+ apr_proc_mutex_t *pmutex = NULL;
+ apr_global_mutex_t *gmutex = NULL;
apr_status_t rv;
SIGHT_ALLOC_CSTRING(name);
@@ -59,15 +95,39 @@
rv = APR_ENOPOOL;
goto cleanup;
}
-
- if ((rv = apr_proc_mutex_create(&mutex, J2S(name),
+#if APR_PROC_MUTEX_IS_GLOBAL
+ if ((rv = apr_proc_mutex_create(&pmutex, J2S(name),
(apr_lockmech_e)mech,
no->pool)) != APR_SUCCESS) {
throwAprException(_E, rv);
goto cleanup;
}
+ no->native = pmutex;
+ no->opaque = NULL;
+#else
+ if (global) {
+ if ((rv = apr_global_mutex_create(&gmutex, J2S(name),
+ (apr_lockmech_e)mech,
+ no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ goto cleanup;
+ }
+ no->native = gmutex;
+ no->opaque = gmutex;
+ } else {
+ if ((rv = apr_proc_mutex_create(&pmutex, J2S(name),
+ (apr_lockmech_e)mech,
+ no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ goto cleanup;
+ }
+ no->native = pmutex;
+ no->opaque = NULL;
+ }
+
+#endif
+
no->clean = mutex_cleanup;
- no->native = mutex;
cleanup:
SIGHT_FREE_CSTRING(name);
return rv;
@@ -75,10 +135,12 @@
SIGHT_EXPORT_DECLARE(jint, Mutex, child0)(SIGHT_STDARGS,
jlong instance,
- jstring name)
+ jstring name,
+ jboolean global)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- apr_proc_mutex_t *mutex = NULL;
+ apr_proc_mutex_t *pmutex = NULL;
+ apr_global_mutex_t *gmutex = NULL;
apr_status_t rv;
SIGHT_ALLOC_CSTRING(name);
@@ -89,14 +151,35 @@
rv = APR_ENOPOOL;
goto cleanup;
}
-
- if ((rv = apr_proc_mutex_child_init(&mutex, J2S(name),
+#if APR_PROC_MUTEX_IS_GLOBAL
+ if ((rv = apr_proc_mutex_child_init(&pmutex, J2S(name),
no->pool)) != APR_SUCCESS) {
throwAprException(_E, rv);
goto cleanup;
}
+ no->native = pmutex;
+#else
+ if (global) {
+ if ((rv = apr_global_mutex_child_init(&gmutex, J2S(name),
+ no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ goto cleanup;
+ }
+ no->native = gmutex;
+ no->opaque = gmutex;
+ }
+ else {
+ if ((rv = apr_proc_mutex_child_init(&pmutex, J2S(name),
+ no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ goto cleanup;
+ }
+ no->native = pmutex;
+ no->opaque = NULL;
+ }
+#endif
+
no->clean = mutex_cleanup;
- no->native = mutex;
cleanup:
SIGHT_FREE_CSTRING(name);
return rv;
@@ -112,7 +195,11 @@
return APR_ENOPOOL;
if (!no->native)
return APR_EINVAL;
- return apr_proc_mutex_lock((apr_proc_mutex_t *)no->native);
+ if (no->opaque)
+ return apr_global_mutex_lock((apr_global_mutex_t *)no->native);
+ else
+ return apr_proc_mutex_lock((apr_proc_mutex_t *)no->native);
+
}
SIGHT_EXPORT_DECLARE(jint, Mutex, trylock0)(SIGHT_STDARGS,
@@ -125,7 +212,11 @@
return APR_ENOPOOL;
if (!no->native)
return APR_EINVAL;
- return apr_proc_mutex_trylock((apr_proc_mutex_t *)no->native);
+ if (no->opaque)
+ return apr_global_mutex_trylock((apr_global_mutex_t *)no->native);
+ else
+ return apr_proc_mutex_trylock((apr_proc_mutex_t *)no->native);
+
}
SIGHT_EXPORT_DECLARE(jint, Mutex, unlock0)(SIGHT_STDARGS,
@@ -138,7 +229,10 @@
return APR_ENOPOOL;
if (!no->native)
return APR_EINVAL;
- return apr_proc_mutex_unlock((apr_proc_mutex_t *)no->native);
+ if (no->opaque)
+ return apr_global_mutex_unlock((apr_global_mutex_t *)no->native);
+ else
+ return apr_proc_mutex_unlock((apr_proc_mutex_t *)no->native);
}
SIGHT_EXPORT_DECLARE(jstring, Mutex, fname0)(SIGHT_STDARGS,
@@ -148,7 +242,18 @@
UNREFERENCED_O;
if (no && no->native) {
+#if APR_PROC_MUTEX_IS_GLOBAL
RETURN_JCSTR(apr_proc_mutex_lockfile((apr_proc_mutex_t *)no->native));
+#else
+ if (no->opaque) {
+ apr_os_global_mutex_t osm;
+ apr_os_global_mutex_get(&osm, (apr_global_mutex_t *)no->native);
+ RETURN_JCSTR(apr_proc_mutex_lockfile(osm.proc_mutex);
+ }
+ else {
+ RETURN_JCSTR(apr_proc_mutex_lockfile((apr_proc_mutex_t *)no->native));
+ }
+#endif
}
else
return NULL;
@@ -161,7 +266,18 @@
UNREFERENCED_O;
if (no && no->native) {
+#if APR_PROC_MUTEX_IS_GLOBAL
RETURN_JCSTR(apr_proc_mutex_name((apr_proc_mutex_t *)no->native));
+#else
+ if (no->opaque) {
+ apr_os_global_mutex_t osm;
+ apr_os_global_mutex_get(&osm, (apr_global_mutex_t *)no->native);
+ RETURN_JCSTR(apr_proc_mutex_name(osm.proc_mutex);
+ }
+ else {
+ RETURN_JCSTR(apr_proc_mutex_name((apr_proc_mutex_t *)no->native));
+ }
+#endif
}
else
return NULL;
Modified: trunk/sight/test/org/jboss/sight/MutexTest.java
===================================================================
--- trunk/sight/test/org/jboss/sight/MutexTest.java 2007-10-30 17:36:10 UTC (rev 1156)
+++ trunk/sight/test/org/jboss/sight/MutexTest.java 2007-10-31 08:17:52 UTC (rev 1157)
@@ -55,12 +55,12 @@
class MutexTestClient extends Thread
{
- Mutex m;
+ GlobalMutex m;
MutexTestClient()
throws Exception
{
- m = new Mutex("./ProcessMutex.lock", MutexType.DEFAULT);
+ m = new GlobalMutex("./ProcessMutex.lock", MutexType.DEFAULT);
}
public void run() {
@@ -87,7 +87,7 @@
public void testMutex()
throws Exception
{
- Mutex m = new Mutex("./ProcessMutex.lock", MutexType.DEFAULT);
+ GlobalMutex m = new GlobalMutex("./ProcessMutex.lock", MutexType.DEFAULT);
m.lock();
new MutexTestClient().start();