Author: mladen.turk(a)jboss.com
Date: 2007-09-29 02:48:13 -0400 (Sat, 29 Sep 2007)
New Revision: 1062
Modified:
trunk/sight/java/org/jboss/sight/NativeObject.java
trunk/sight/java/org/jboss/sight/Process.java
trunk/sight/native/include/sight_types.h
trunk/sight/native/os/windows/service.c
trunk/sight/native/share/dir.c
trunk/sight/native/share/no.c
trunk/sight/native/share/proc.c
Log:
I cannot belive I did that. Use Java locks instead APR atomics
Modified: trunk/sight/java/org/jboss/sight/NativeObject.java
===================================================================
--- trunk/sight/java/org/jboss/sight/NativeObject.java 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/java/org/jboss/sight/NativeObject.java 2007-09-29 06:48:13 UTC (rev 1062)
@@ -24,6 +24,7 @@
*/
package org.jboss.sight;
+import java.util.concurrent.locks.ReentrantLock;
/**
* Native Object abstract class
*
@@ -48,16 +49,30 @@
private static native void cbset0(long instance, Object cb)
throws OutOfMemoryError;
+ private ReentrantLock jniMutex;
+
+
/**
* Create new NativeObject without APR pool
*/
public NativeObject()
throws OutOfMemoryError
{
+ jniMutex = new ReentrantLock();
POOL = 0;
INSTANCE = alloc();
}
+ protected void nativeLock()
+ {
+ jniMutex.lock();
+ }
+
+ protected void nativeUnlock()
+ {
+ jniMutex.unlock();
+ }
+
/**
* Create new NativeObject
* @param parent The parent pool. If this is 0, the new pool is a root
@@ -68,6 +83,7 @@
protected NativeObject(long parent)
throws OutOfMemoryError
{
+ jniMutex = new ReentrantLock();
POOL = 0;
INSTANCE = alloc();
init0(this, INSTANCE, parent);
@@ -78,9 +94,15 @@
*/
public void destroy()
{
- free0(INSTANCE);
- INSTANCE = 0;
- POOL = 0;
+ jniMutex.lock();
+ try {
+ free0(INSTANCE);
+ }
+ finally {
+ INSTANCE = 0;
+ POOL = 0;
+ jniMutex.unlock();
+ }
}
/**
Modified: trunk/sight/java/org/jboss/sight/Process.java
===================================================================
--- trunk/sight/java/org/jboss/sight/Process.java 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/java/org/jboss/sight/Process.java 2007-09-29 06:48:13 UTC (rev 1062)
@@ -436,7 +436,13 @@
*/
public int wait(WaitHow waithow)
{
- return wait0(INSTANCE, waithow.valueOf());
+ nativeLock();
+ try {
+ return wait0(INSTANCE, waithow.valueOf());
+ }
+ finally {
+ nativeUnlock();
+ }
}
/**
@@ -457,7 +463,13 @@
*/
public int waitFor()
{
- return wait0(INSTANCE, 0);
+ nativeLock();
+ try {
+ return wait0(INSTANCE, 0);
+ }
+ finally {
+ nativeUnlock();
+ }
}
/**
@@ -482,7 +494,13 @@
*/
public int waitFor(long timeout)
{
- return wait1(INSTANCE, 0, timeout);
+ nativeLock();
+ try {
+ return wait1(INSTANCE, 0, timeout);
+ }
+ finally {
+ nativeUnlock();
+ }
}
/**
@@ -507,7 +525,13 @@
*/
public int waitFor(IProgressNotificationCallback progress, long timeout)
{
- return wait2(INSTANCE, progress, timeout);
+ nativeLock();
+ try {
+ return wait2(INSTANCE, progress, timeout);
+ }
+ finally {
+ nativeUnlock();
+ }
}
/**
@@ -530,7 +554,13 @@
*/
public int waitFor(IProgressNotificationCallback progress)
{
- return wait2(INSTANCE, progress, 0);
+ nativeLock();
+ try {
+ return wait2(INSTANCE, progress, 0);
+ }
+ finally {
+ nativeUnlock();
+ }
}
@@ -601,15 +631,20 @@
public int exec(String progname, String [] args, String [] env)
throws NullPointerException, OperatingSystemException
{
-
- int rv = exec0(INSTANCE, progname, args, env);
- if (rv == Error.APR_SUCCESS) {
- Id = pid0(INSTANCE);
- redirIs = new File(POOL, getios0(INSTANCE, 0));
- redirOs = new File(POOL, getios0(INSTANCE, 1));
- redirEs = new File(POOL, getios0(INSTANCE, 2));
+ nativeLock();
+ try {
+ int rv = exec0(INSTANCE, progname, args, env);
+ if (rv == Error.APR_SUCCESS) {
+ Id = pid0(INSTANCE);
+ redirIs = new File(POOL, getios0(INSTANCE, 0));
+ redirOs = new File(POOL, getios0(INSTANCE, 1));
+ redirEs = new File(POOL, getios0(INSTANCE, 2));
+ }
+ return rv;
}
- return rv;
+ finally {
+ nativeUnlock();
+ }
}
/**
Modified: trunk/sight/native/include/sight_types.h
===================================================================
--- trunk/sight/native/include/sight_types.h 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/native/include/sight_types.h 2007-09-29 06:48:13 UTC (rev 1062)
@@ -110,7 +110,6 @@
/* org.jboss.sight.NativeObject instance */
struct sight_object_t {
- volatile apr_uint32_t references;
apr_pool_t *pool;
void *native;
void *opaque;
@@ -120,9 +119,6 @@
void (*clean)(int, sight_object_t *);
};
-#define SIGHT_NO_IREF(N) apr_atomic_inc32(&((N)->references))
-#define SIGHT_NO_DREF(N) apr_atomic_dec32(&((N)->references))
-
#define CACHE_HASH_MASK 255
#define CACHE_HASH_SIZE 256
Modified: trunk/sight/native/os/windows/service.c
===================================================================
--- trunk/sight/native/os/windows/service.c 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/native/os/windows/service.c 2007-09-29 06:48:13 UTC (rev 1062)
@@ -356,7 +356,6 @@
return APR_EINVAL;
}
hsvc = no->native;
- SIGHT_NO_IREF(no);
if (!QueryServiceStatusEx(hsvc, SC_STATUS_PROCESS_INFO,
buf, sizeof(buf), &cbBytesNeeded)) {
rc = GetLastError();
@@ -398,7 +397,6 @@
CALL_METHOD1(0000, thiz, lpStatus->dwCurrentState);
cleanup:
- SIGHT_NO_DREF(no);
return APR_FROM_OS_ERROR(rc);
}
@@ -426,7 +424,6 @@
return APR_EINVAL;
}
hsvc = no->native;
- SIGHT_NO_IREF(no);
if (!QueryServiceStatusEx(hsvc, SC_STATUS_PROCESS_INFO,
buf, sizeof(buf), &cbBytesNeeded)) {
@@ -494,6 +491,5 @@
CALL_METHOD1(0000, thiz, lpStatus->dwCurrentState);
cleanup:
- SIGHT_NO_DREF(no);
return APR_FROM_OS_ERROR(rc);
}
Modified: trunk/sight/native/share/dir.c
===================================================================
--- trunk/sight/native/share/dir.c 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/native/share/dir.c 2007-09-29 06:48:13 UTC (rev 1062)
@@ -35,7 +35,7 @@
static void dir_cleanup(int mode, sight_object_t *no)
{
/*
- * In case this is a pool callback do not
+ * In case this is a pool callback do not
* close the directory. It will be closed
* by the original apr pool callback
*/
@@ -279,12 +279,10 @@
}
else
d = (apr_dir_t *)no->native;
- SIGHT_NO_IREF(no);
if (recursive)
len = calc_size_r((apr_dir_t *)no->native, J2S(path), no->pool);
else
len = calc_size_d((apr_dir_t *)no->native);
- SIGHT_NO_DREF(no);
cleanup:
SIGHT_FREE_CSTRING(path);
SIGHT_GLOBAL_CLEANUP();
Modified: trunk/sight/native/share/no.c
===================================================================
--- trunk/sight/native/share/no.c 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/native/share/no.c 2007-09-29 06:48:13 UTC (rev 1062)
@@ -207,8 +207,7 @@
{
sight_object_t *no = J2P(instance, sight_object_t *);
jobject object = NULL;
- apr_uint32_t ref_count;
- int valid_global = 0;
+
UNREFERENCED_O;
#ifdef SIGHT_DO_STATS
@@ -216,20 +215,13 @@
#endif
if (!no)
return;
- if (SIGHT_IS_VALID_GLOBAL())
- valid_global = 1;
- ref_count = apr_atomic_read32(&no->references);
- while (ref_count) {
- apr_thread_yield();
- ref_count = apr_atomic_read32(&no->references);
- }
if (no->object) {
object = (*_E)->NewLocalRef(_E, no->object);
(*_E)->DeleteWeakGlobalRef(_E, no->object);
no->object = object;
}
- if (valid_global) {
+ if (SIGHT_IS_VALID_GLOBAL()) {
if (no->pool)
apr_pool_cleanup_kill(no->pool, no, native_object_cleanup);
if (no->object) {
Modified: trunk/sight/native/share/proc.c
===================================================================
--- trunk/sight/native/share/proc.c 2007-09-28 16:05:33 UTC (rev 1061)
+++ trunk/sight/native/share/proc.c 2007-09-29 06:48:13 UTC (rev 1062)
@@ -59,25 +59,36 @@
apr_status_t rv;
UNREFERENCED_O;
+ if (!SIGHT_IS_VALID_GLOBAL()) {
+ throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
+ rv = APR_ENOPOOL;
+ goto cleanup;
+ }
+
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
- return APR_ENOPOOL;
+ rv = APR_ENOPOOL;
+ goto cleanup;
}
p = (sight_runproc_t *)sight_pcalloc(_E, no->pool, sizeof(sight_runproc_t),
THROW_FMARK);
- if (p == NULL)
- return APR_ENOMEM;
+ if (p == NULL) {
+ rv = APR_ENOMEM;
+ goto cleanup;
+ }
if ((rv = apr_procattr_create(&p->attr, no->pool)) != APR_SUCCESS) {
- return rv;
+ goto cleanup;
}
p->exitval = -1;
#ifdef SIGHT_DO_STATS
no->clean = proc_cleanup;
#endif
no->native = p;
-
- return APR_SUCCESS;
+ rv = APR_SUCCESS;
+cleanup:
+ SIGHT_GLOBAL_CLEANUP();
+ return rv;
}
SIGHT_EXPORT_DECLARE(jint, Process, ioset0)(SIGHT_STDARGS,
@@ -299,11 +310,9 @@
UNREFERENCED_STDARGS;
if (!no || !no->native)
return APR_EINVAL;
- SIGHT_NO_IREF(no);
p = (sight_runproc_t *)no->native;
rv = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
(apr_wait_how_e)waithow);
- SIGHT_NO_DREF(no);
return rv;
}
@@ -322,7 +331,7 @@
UNREFERENCED_STDARGS;
if (!no || !no->native)
return APR_EINVAL;
- SIGHT_NO_IREF(no);
+
p = (sight_runproc_t *)no->native;
if (timeout < 0)
how = APR_WAIT;
@@ -330,9 +339,9 @@
how = APR_NOWAIT;
if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
how)) != APR_CHILD_NOTDONE)
- goto done;
+ return rc;
if (timeout < 1)
- goto done;
+ return APR_EINVAL;
timeout_value = timeout * 1000L;
timeout_interval = timeout_value / 64;
do {
@@ -343,8 +352,6 @@
timeout_interval *= 2;
} while (rc == APR_CHILD_NOTDONE);
-done:
- SIGHT_NO_DREF(no);
return rc;
}
@@ -365,11 +372,10 @@
UNREFERENCED_O;
if (!no || !no->native || !progress)
return APR_EINVAL;
- SIGHT_NO_IREF(no);
p = (sight_runproc_t *)no->native;
if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
APR_NOWAIT)) != APR_CHILD_NOTDONE)
- goto done;
+ return rc;
c = (*_E)->GetObjectClass(_E, progress);
cb.name = "progress";
@@ -377,13 +383,11 @@
cb.object = progress;
cb.method = (*_E)->GetMethodID(_E, c, cb.name, cb.msig);
if (!cb.method || (*_E)->ExceptionCheck(_E)) {
- rc = APR_EINVAL;
- goto done;
+ return APR_EINVAL;
}
cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick, NULL);
if ((*_E)->ExceptionCheck(_E)) {
- rc = APR_FROM_OS_ERROR(EINTR);
- goto done;
+ return APR_FROM_OS_ERROR(EINTR);
}
if (timeout > 0) {
@@ -411,14 +415,11 @@
}
cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick++, NULL);
if ((*_E)->ExceptionCheck(_E)) {
- rc = APR_FROM_OS_ERROR(EINTR);
- goto done;
+ return APR_FROM_OS_ERROR(EINTR);
}
} while (rc == APR_CHILD_NOTDONE);
-done:
- SIGHT_NO_DREF(no);
return rc;
}
@@ -469,6 +470,12 @@
UNREFERENCED_O;
+ if (!SIGHT_IS_VALID_GLOBAL()) {
+ throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
+ rv = APR_ENOPOOL;
+ goto cleanup;
+ }
+
if (!no || !no->native) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPROC));
@@ -517,6 +524,7 @@
}
cleanup:
SIGHT_FREE_CSTRING(progname);
+ SIGHT_GLOBAL_CLEANUP();
return rv;
}