Author: mladen.turk(a)jboss.com
Date: 2007-10-01 03:58:39 -0400 (Mon, 01 Oct 2007)
New Revision: 1065
Modified:
trunk/sight/java/org/jboss/sight/NativeObject.java
trunk/sight/java/org/jboss/sight/Process.java
trunk/sight/native/include/sight_local.h
trunk/sight/native/include/sight_types.h
trunk/sight/native/share/dir.c
trunk/sight/native/share/file.c
trunk/sight/native/share/finfo.c
trunk/sight/native/share/no.c
trunk/sight/native/share/pool.c
trunk/sight/native/share/proc.c
Log:
Use APR atomics in more consistent way so we can have paralel GC
Modified: trunk/sight/java/org/jboss/sight/NativeObject.java
===================================================================
--- trunk/sight/java/org/jboss/sight/NativeObject.java 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/java/org/jboss/sight/NativeObject.java 2007-10-01 07:58:39 UTC (rev 1065)
@@ -24,7 +24,6 @@
*/
package org.jboss.sight;
-import java.util.concurrent.locks.ReentrantLock;
/**
* Native Object abstract class
*
@@ -49,30 +48,16 @@
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
@@ -83,7 +68,6 @@
protected NativeObject(long parent)
throws OutOfMemoryError
{
- jniMutex = new ReentrantLock();
POOL = 0;
INSTANCE = alloc();
init0(this, INSTANCE, parent);
@@ -94,15 +78,9 @@
*/
public void destroy()
{
- jniMutex.lock();
- try {
- free0(INSTANCE);
- }
- finally {
- INSTANCE = 0;
- POOL = 0;
- jniMutex.unlock();
- }
+ free0(INSTANCE);
+ INSTANCE = 0;
+ POOL = 0;
}
/**
Modified: trunk/sight/java/org/jboss/sight/Process.java
===================================================================
--- trunk/sight/java/org/jboss/sight/Process.java 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/java/org/jboss/sight/Process.java 2007-10-01 07:58:39 UTC (rev 1065)
@@ -436,13 +436,7 @@
*/
public int wait(WaitHow waithow)
{
- nativeLock();
- try {
- return wait0(INSTANCE, waithow.valueOf());
- }
- finally {
- nativeUnlock();
- }
+ return wait0(INSTANCE, waithow.valueOf());
}
/**
@@ -463,13 +457,7 @@
*/
public int waitFor()
{
- nativeLock();
- try {
- return wait0(INSTANCE, 0);
- }
- finally {
- nativeUnlock();
- }
+ return wait0(INSTANCE, 0);
}
/**
@@ -494,13 +482,7 @@
*/
public int waitFor(long timeout)
{
- nativeLock();
- try {
- return wait1(INSTANCE, 0, timeout);
- }
- finally {
- nativeUnlock();
- }
+ return wait1(INSTANCE, 0, timeout);
}
/**
@@ -525,13 +507,7 @@
*/
public int waitFor(IProgressNotificationCallback progress, long timeout)
{
- nativeLock();
- try {
- return wait2(INSTANCE, progress, timeout);
- }
- finally {
- nativeUnlock();
- }
+ return wait2(INSTANCE, progress, timeout);
}
/**
@@ -554,13 +530,7 @@
*/
public int waitFor(IProgressNotificationCallback progress)
{
- nativeLock();
- try {
- return wait2(INSTANCE, progress, 0);
- }
- finally {
- nativeUnlock();
- }
+ return wait2(INSTANCE, progress, 0);
}
@@ -631,20 +601,14 @@
public int exec(String progname, String [] args, String [] env)
throws NullPointerException, OperatingSystemException
{
- 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;
+ 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));
}
- finally {
- nativeUnlock();
- }
+ return rv;
}
/**
Modified: trunk/sight/native/include/sight_local.h
===================================================================
--- trunk/sight/native/include/sight_local.h 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/include/sight_local.h 2007-10-01 07:58:39 UTC (rev 1065)
@@ -382,7 +382,18 @@
#define SIGHT_GLOBAL_CLEANUP() apr_atomic_dec32(sight_global_atomic)
#endif
+#define SIGHT_GLOBAL_TRY if (apr_atomic_inc32(sight_global_atomic))
+#define SIGHT_GLOBAL_END() apr_atomic_dec32(sight_global_atomic)
+#define SIGHT_GLOBAL_BRK() apr_atomic_dec32(sight_global_atomic)
+#define SIGHT_LOCAL_TRY(N) { apr_uint32_t _SGATOM =
apr_atomic_inc32(sight_global_atomic); \
+ if ((N)) apr_atomic_inc32(&(N)->refcount);
\
+ if ((N))
+
+#define SIGHT_LOCAL_END(N) apr_atomic_dec32(&(N)->refcount); }
apr_atomic_dec32(sight_global_atomic)
+#define SIGHT_LOCAL_BRK(N) apr_atomic_dec32(&(N)->refcount);
apr_atomic_dec32(sight_global_atomic)
+
+
/* Statistics counters */
extern volatile apr_uint64_t sight_cnt_native_alloc;
extern volatile apr_uint64_t sight_cnt_native_create;
Modified: trunk/sight/native/include/sight_types.h
===================================================================
--- trunk/sight/native/include/sight_types.h 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/include/sight_types.h 2007-10-01 07:58:39 UTC (rev 1065)
@@ -110,6 +110,7 @@
/* org.jboss.sight.NativeObject instance */
struct sight_object_t {
+ volatile apr_uint32_t refcount;
apr_pool_t *pool;
void *native;
void *opaque;
Modified: trunk/sight/native/share/dir.c
===================================================================
--- trunk/sight/native/share/dir.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/dir.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -50,34 +50,29 @@
jstring name)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- apr_status_t rv;
+ apr_status_t rv = APR_ENOPOOL;
apr_dir_t *d = NULL;
SIGHT_ALLOC_CSTRING(name);
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));
rv = APR_ENOPOOL;
goto cleanup;
}
- if ((rv = apr_dir_open(&d, J2S(name), no->pool)) != APR_SUCCESS) {
- throwAprException(_E, rv);
- goto cleanup;
- }
- no->clean = dir_cleanup;
- no->native = d;
-
+ SIGHT_LOCAL_TRY(no) {
+ if ((rv = apr_dir_open(&d, J2S(name), no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ }
+ else {
+ no->clean = dir_cleanup;
+ no->native = d;
+ }
+ } SIGHT_LOCAL_END(no);
cleanup:
SIGHT_FREE_CSTRING(name);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -121,40 +116,33 @@
jobject fi = NULL;
apr_status_t rc;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return NULL;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
- SIGHT_GLOBAL_CLEANUP();
return NULL;
}
if (!no->native) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOFILE));
- SIGHT_GLOBAL_CLEANUP();
return NULL;
}
else
d = (apr_dir_t *)no->native;
- if (!(fi = sight_new_finfo_class(_E, _O))) {
- SIGHT_GLOBAL_CLEANUP();
- return NULL;
- }
- rc = apr_dir_read(&info, wanted, d);
- if (rc == APR_SUCCESS || APR_STATUS_IS_INCOMPLETE(rc)) {
- sight_finfo_fill(_E, fi, &info);
- }
- else {
- (*_E)->DeleteLocalRef(_E, fi);
- /* Do not throw exceptions */
- fi = NULL;
- }
-
- SIGHT_GLOBAL_CLEANUP();
+ SIGHT_LOCAL_TRY(no) {
+ if (!(fi = sight_new_finfo_class(_E, _O))) {
+ SIGHT_LOCAL_BRK(no);
+ return NULL;
+ }
+ rc = apr_dir_read(&info, wanted, d);
+ if (rc == APR_SUCCESS || APR_STATUS_IS_INCOMPLETE(rc)) {
+ sight_finfo_fill(_E, fi, &info);
+ }
+ else {
+ (*_E)->DeleteLocalRef(_E, fi);
+ /* Do not throw exceptions */
+ fi = NULL;
+ }
+ } SIGHT_LOCAL_END(no);
return fi;
}
@@ -163,33 +151,28 @@
jint perms,
jboolean recursive)
{
- apr_status_t rv;
+ apr_status_t rv = APR_ENOPOOL;
apr_pool_t *p = NULL;
apr_fileperms_t perm = (apr_fileperms_t)perms;
SIGHT_ALLOC_CSTRING(name);
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- rv = APR_ENOPOOL;
- goto cleanup;
- }
- if ((rv = sight_create_pool(&p, sight_temp_pool)) != APR_SUCCESS) {
- throwAprException(_E, rv);
- goto cleanup;
- }
-
- if (recursive)
- rv = apr_dir_make_recursive(J2S(name), perm, p);
- else
- rv = apr_dir_make(J2S(name), perm, p);
-
+ SIGHT_GLOBAL_TRY {
+ if ((rv = sight_create_pool(&p, sight_temp_pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ SIGHT_GLOBAL_BRK();
+ goto cleanup;
+ }
+ if (recursive)
+ rv = apr_dir_make_recursive(J2S(name), perm, p);
+ else
+ rv = apr_dir_make(J2S(name), perm, p);
+ if (p)
+ apr_pool_destroy(p);
+ } SIGHT_GLOBAL_END();
cleanup:
- if (p)
- apr_pool_destroy(p);
SIGHT_FREE_CSTRING(name);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -263,10 +246,6 @@
SIGHT_ALLOC_CSTRING(path);
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- goto cleanup;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
@@ -279,32 +258,29 @@
}
else
d = (apr_dir_t *)no->native;
- 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_LOCAL_TRY(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_LOCAL_END(no);
+
cleanup:
SIGHT_FREE_CSTRING(path);
- SIGHT_GLOBAL_CLEANUP();
return (jlong)len;
}
SIGHT_EXPORT_DECLARE(jint, Directory, remove0)(SIGHT_STDARGS,
jstring name)
{
- apr_status_t rv;
+ apr_status_t rv = APR_ENOPOOL;
SIGHT_ALLOC_CSTRING(name);
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- rv = APR_ENOPOOL;
- goto cleanup;
+ SIGHT_GLOBAL_TRY {
+ rv = apr_dir_remove(J2S(name), NULL);
}
- rv = apr_dir_remove(J2S(name), NULL);
-cleanup:
SIGHT_FREE_CSTRING(name);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
Modified: trunk/sight/native/share/file.c
===================================================================
--- trunk/sight/native/share/file.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/file.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -52,28 +52,24 @@
SIGHT_ALLOC_CSTRING(name);
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return APR_ENOPOOL;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
rv = APR_ENOPOOL;
goto cleanup;
}
- if ((rv = apr_file_open(&f, J2S(name), fflag,
- (apr_fileperms_t)perm, no->pool)) != APR_SUCCESS) {
- throwAprException(_E, rv);
- goto cleanup;
- }
- no->clean = file_cleanup;
- no->native = f;
-
+ SIGHT_LOCAL_TRY(no) {
+ if ((rv = apr_file_open(&f, J2S(name), fflag,
+ (apr_fileperms_t)perm, no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ }
+ else {
+ no->clean = file_cleanup;
+ no->native = f;
+ }
+ } SIGHT_LOCAL_END(no);
cleanup:
SIGHT_FREE_CSTRING(name);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -101,17 +97,12 @@
jint flag)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- apr_status_t rv;
+ apr_status_t rv = APR_ENOPOOL;
apr_file_t *f = NULL;
apr_int32_t fflag = (apr_int32_t)flag | APR_FOPEN_NOCLEANUP;
char *ctempl = NULL;
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return APR_ENOPOOL;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
@@ -122,17 +113,18 @@
rv = APR_ENOMEM;
goto cleanup;
}
- if ((rv = apr_file_mktemp(&f, ctempl,
- fflag, no->pool)) != APR_SUCCESS) {
- throwAprException(_E, rv);
- goto cleanup;
- }
- no->clean = file_cleanup;
- no->native = f;
-
+ SIGHT_LOCAL_TRY(no) {
+ if ((rv = apr_file_mktemp(&f, ctempl,
+ fflag, no->pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ }
+ else {
+ no->clean = file_cleanup;
+ no->native = f;
+ }
+ } SIGHT_LOCAL_END(no);
cleanup:
SIGHT_FREE(ctempl);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -141,40 +133,35 @@
jint which)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- apr_status_t rv;
+ apr_status_t rv = APR_ENOPOOL;
apr_file_t *f = NULL;
UNREFERENCED_O;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return APR_ENOPOOL;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
- SIGHT_GLOBAL_CLEANUP();
return APR_ENOPOOL;
}
- switch (which) {
- case 0:
- rv = apr_file_open_stdin(&f, no->pool);
- break;
- case 1:
- rv = apr_file_open_stdout(&f, no->pool);
- break;
- default:
- rv = apr_file_open_stderr(&f, no->pool);
- break;
+ SIGHT_LOCAL_TRY(no) {
+ switch (which) {
+ case 0:
+ rv = apr_file_open_stdin(&f, no->pool);
+ break;
+ case 1:
+ rv = apr_file_open_stdout(&f, no->pool);
+ break;
+ default:
+ rv = apr_file_open_stderr(&f, no->pool);
+ break;
- }
- if (rv == APR_SUCCESS) {
- no->clean = NULL;
- no->native = f;
- }
- else
- throwAprException(_E, rv);
- SIGHT_GLOBAL_CLEANUP();
+ }
+ if (rv == APR_SUCCESS) {
+ no->clean = NULL;
+ no->native = f;
+ }
+ else
+ throwAprException(_E, rv);
+ } SIGHT_LOCAL_END(no);
return rv;
}
@@ -193,22 +180,23 @@
throwIOException(_E, sight_strerror(SIGHT_ENOFILE));
return 0;
}
- switch (where) {
- case 1:
- w = APR_CUR;
- break;
- case 2:
- w = APR_END;
- break;
- default:
- w = APR_SET;
- break;
- }
-
- if ((rv = apr_file_seek((apr_file_t *)no->native,
- w, &pos)) != APR_SUCCESS) {
- throwAprIOException(_E, rv);
- }
+ SIGHT_LOCAL_TRY(no) {
+ switch (where) {
+ case 1:
+ w = APR_CUR;
+ break;
+ case 2:
+ w = APR_END;
+ break;
+ default:
+ w = APR_SET;
+ break;
+ }
+ if ((rv = apr_file_seek((apr_file_t *)no->native,
+ w, &pos)) != APR_SUCCESS) {
+ throwAprIOException(_E, rv);
+ }
+ } SIGHT_LOCAL_END(no);
return (jlong)pos;
}
@@ -554,40 +542,31 @@
jobject fi = NULL;
apr_status_t rc;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return NULL;
- }
if (!no || !no->pool) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOPOOL));
- SIGHT_GLOBAL_CLEANUP();
return NULL;
}
if (!no->native) {
throwNullPointerException(_E, THROW_FMARK,
sight_strerror(SIGHT_ENOFILE));
- SIGHT_GLOBAL_CLEANUP();
return NULL;
}
else
f = (apr_file_t *)no->native;
- if (!(fi = sight_new_finfo_class(_E, _O))) {
- SIGHT_GLOBAL_CLEANUP();
- return NULL;
- }
- rc = apr_file_info_get(&info, wanted, f);
- if (rc == APR_SUCCESS || APR_STATUS_IS_INCOMPLETE(rc)) {
- sight_finfo_fill(_E, fi, &info);
- }
- else {
- (*_E)->DeleteLocalRef(_E, fi);
- /* Do not throw exceptions */
- fi = NULL;
- throwAprException(_E, rc);
- }
-
- SIGHT_GLOBAL_CLEANUP();
+ SIGHT_LOCAL_TRY(no) {
+ if ((fi = sight_new_finfo_class(_E, _O))) {
+ rc = apr_file_info_get(&info, wanted, f);
+ if (rc == APR_SUCCESS || APR_STATUS_IS_INCOMPLETE(rc)) {
+ sight_finfo_fill(_E, fi, &info);
+ }
+ else {
+ (*_E)->DeleteLocalRef(_E, fi);
+ /* Do not throw exceptions */
+ fi = NULL;
+ throwAprException(_E, rc);
+ }
+ }
+ } SIGHT_LOCAL_END(no);
return fi;
}
Modified: trunk/sight/native/share/finfo.c
===================================================================
--- trunk/sight/native/share/finfo.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/finfo.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -201,31 +201,27 @@
jint wanted)
{
apr_pool_t *p = NULL;
- apr_status_t rv = APR_SUCCESS;
+ apr_status_t rv = APR_ENOPOOL;
apr_finfo_t info;
SIGHT_ALLOC_CSTRING(name);
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- return APR_ENOPOOL;
- }
- if ((rv = sight_create_pool(&p, sight_temp_pool)) != APR_SUCCESS) {
- throwAprException(_E, rv);
- goto cleanup;
- }
- memset(&info, 0, sizeof(apr_finfo_t));
- rv = apr_stat(&info, J2S(name), wanted, p);
- if (rv == APR_SUCCESS || rv == APR_INCOMPLETE) {
- sight_finfo_fill(_E, thiz, &info);
- }
- else {
- throwAprException(_E, rv);
- }
-cleanup:
- if (p)
- apr_pool_destroy(p);
+ SIGHT_GLOBAL_TRY {
+ if ((rv = sight_create_pool(&p, sight_temp_pool)) != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ }
+ else {
+ memset(&info, 0, sizeof(apr_finfo_t));
+ rv = apr_stat(&info, J2S(name), wanted, p);
+ if (rv == APR_SUCCESS || rv == APR_INCOMPLETE) {
+ sight_finfo_fill(_E, thiz, &info);
+ }
+ else {
+ throwAprException(_E, rv);
+ }
+ apr_pool_destroy(p);
+ }
+ } SIGHT_GLOBAL_END();
+
SIGHT_FREE_CSTRING(name);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
Modified: trunk/sight/native/share/no.c
===================================================================
--- trunk/sight/native/share/no.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/no.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -146,30 +146,26 @@
#endif
if (!no)
return;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- SIGHT_GLOBAL_CLEANUP();
- return;
- }
+ SIGHT_GLOBAL_TRY {
+ if (!pp)
+ pp = sight_global_pool;
- if (!pp)
- pp = sight_global_pool;
+ if ((rc = sight_create_pool(&pool, pp)) != APR_SUCCESS) {
+ throwAprMemoryException(_E, THROW_FMARK, rc);
+ SIGHT_GLOBAL_END();
+ return;
+ }
- if ((rc = sight_create_pool(&pool, pp)) != APR_SUCCESS) {
- throwAprMemoryException(_E, THROW_FMARK, rc);
- SIGHT_GLOBAL_CLEANUP();
- return;
- }
+ c = (*_E)->GetObjectClass(_E, thiz);
+ no->destroy = (*_E)->GetMethodID(_E, c, "onDestroy",
"()V");
+ no->object = (*_E)->NewWeakGlobalRef(_E, thiz);
+ no->pool = pool;
+ apr_pool_cleanup_register(pool, (const void *)no,
+ native_object_cleanup,
+ apr_pool_cleanup_null);
+ SET_IFIELD_J(0001, no->object, P2J(pool));
- c = (*_E)->GetObjectClass(_E, thiz);
- no->destroy = (*_E)->GetMethodID(_E, c, "onDestroy",
"()V");
- no->object = (*_E)->NewWeakGlobalRef(_E, thiz);
- no->pool = pool;
- apr_pool_cleanup_register(pool, (const void *)no,
- native_object_cleanup,
- apr_pool_cleanup_null);
- SET_IFIELD_J(0001, no->object, P2J(pool));
- SIGHT_GLOBAL_CLEANUP();
+ } SIGHT_GLOBAL_END();
}
SIGHT_EXPORT_DECLARE(void, NativeObject, cbset0)(SIGHT_STDARGS,
@@ -183,23 +179,19 @@
if (!no)
return;
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- SIGHT_GLOBAL_CLEANUP();
- return;
- }
- if (no->cb.object) {
- (*_E)->DeleteGlobalRef(_E, no->cb.object);
- no->cb.object = NULL;
- no->cb.method = NULL;
- }
- if (callback) {
- c = (*_E)->GetObjectClass(_E, callback);
- no->cb.object = (*_E)->NewGlobalRef(_E, callback);
- no->cb.method = (*_E)->GetMethodID(_E, c, no->cb.name, no->cb.msig);
- DBPRINTF("NativeObject_cbset0: %s/%s", no->cb.name,
no->cb.msig);
- }
- SIGHT_GLOBAL_CLEANUP();
+ SIGHT_GLOBAL_TRY {
+ if (no->cb.object) {
+ (*_E)->DeleteGlobalRef(_E, no->cb.object);
+ no->cb.object = NULL;
+ no->cb.method = NULL;
+ }
+ if (callback) {
+ c = (*_E)->GetObjectClass(_E, callback);
+ no->cb.object = (*_E)->NewGlobalRef(_E, callback);
+ no->cb.method = (*_E)->GetMethodID(_E, c, no->cb.name,
no->cb.msig);
+ DBPRINTF("NativeObject_cbset0: %s/%s", no->cb.name,
no->cb.msig);
+ }
+ } SIGHT_GLOBAL_END();
}
SIGHT_EXPORT_DECLARE(void, NativeObject, free0)(SIGHT_STDARGS,
@@ -207,6 +199,7 @@
{
sight_object_t *no = J2P(instance, sight_object_t *);
jobject object = NULL;
+ apr_uint32_t refcount;
UNREFERENCED_O;
@@ -215,13 +208,18 @@
#endif
if (!no)
return;
-
if (no->object) {
object = (*_E)->NewLocalRef(_E, no->object);
(*_E)->DeleteWeakGlobalRef(_E, no->object);
no->object = object;
}
- if (SIGHT_IS_VALID_GLOBAL()) {
+
+ SIGHT_GLOBAL_TRY {
+ refcount = apr_atomic_read32(&no->refcount);
+ while (refcount) {
+ apr_thread_yield();
+ refcount = apr_atomic_read32(&no->refcount);
+ }
if (no->pool)
apr_pool_cleanup_kill(no->pool, no, native_object_cleanup);
if (no->object) {
@@ -235,7 +233,7 @@
(*no->clean)(POOL_DESTROY, no);
if (no->pool)
apr_pool_destroy(no->pool);
- }
+ } SIGHT_GLOBAL_END();
if (object) {
(*_E)->DeleteLocalRef(_E, object);
}
@@ -243,34 +241,38 @@
(*_E)->DeleteGlobalRef(_E, no->cb.object);
}
free(no);
- SIGHT_GLOBAL_CLEANUP();
}
SIGHT_EXPORT_DECLARE(void, NativeObject, clear0)(SIGHT_STDARGS,
jlong instance)
{
sight_object_t *no = J2P(instance, sight_object_t *);
+ apr_uint32_t refcount;
UNREFERENCED_O;
#ifdef SIGHT_DO_STATS
sight_cnt_native_clear++;
#endif
- if (!SIGHT_IS_VALID_GLOBAL()) {
- SIGHT_GLOBAL_CLEANUP();
+ if (!no)
return;
- }
+ SIGHT_GLOBAL_TRY {
+ refcount = apr_atomic_read32(&no->refcount);
+ while (refcount) {
+ apr_thread_yield();
+ refcount = apr_atomic_read32(&no->refcount);
+ }
- if (no && no->pool) {
- apr_pool_cleanup_kill(no->pool, no, native_object_cleanup);
- if (no->clean)
- (*no->clean)(POOL_CLEAR, no);
- apr_pool_clear(no->pool);
- apr_pool_cleanup_register(no->pool, (const void *)no,
- native_object_cleanup,
- apr_pool_cleanup_null);
- no->opaque = NULL;
- no->native = NULL;
- }
- SIGHT_GLOBAL_CLEANUP();
+ if (no->pool) {
+ apr_pool_cleanup_kill(no->pool, no, native_object_cleanup);
+ if (no->clean)
+ (*no->clean)(POOL_CLEAR, no);
+ apr_pool_clear(no->pool);
+ apr_pool_cleanup_register(no->pool, (const void *)no,
+ native_object_cleanup,
+ apr_pool_cleanup_null);
+ no->opaque = NULL;
+ no->native = NULL;
+ }
+ } SIGHT_GLOBAL_END();
}
Modified: trunk/sight/native/share/pool.c
===================================================================
--- trunk/sight/native/share/pool.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/pool.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -61,7 +61,7 @@
jobject obj)
{
sight_object_t *no = J2P(instance, sight_object_t *);
- sight_callback_t *cb;
+ sight_callback_t *cb = NULL;
jclass c;
UNREFERENCED_O;
@@ -70,29 +70,22 @@
sight_strerror(SIGHT_ENOPOOL));
return 0;
}
- if (!SIGHT_IS_VALID_GLOBAL()) {
- throwAprMemoryException(_E, THROW_FMARK, APR_ENOPOOL);
- SIGHT_GLOBAL_CLEANUP();
+ if (!(cb = (sight_callback_t *)sight_calloc(_E,
+ sizeof(sight_callback_t),
+ THROW_FMARK))) {
return 0;
}
- cb = (sight_callback_t *)calloc(1, sizeof(sight_callback_t));
- if (cb == NULL) {
- throwAprMemoryException(_E, THROW_FMARK,
- apr_get_os_error());
- SIGHT_GLOBAL_CLEANUP();
- return 0;
- }
- c = (*_E)->GetObjectClass(_E, obj);
- cb->name = "cleanup";
- cb->msig = "()I";
- cb->object = (*_E)->NewGlobalRef(_E, obj);
- cb->method = (*_E)->GetMethodID(_E, c, cb->name, cb->msig);
+ SIGHT_LOCAL_TRY(no) {
+ c = (*_E)->GetObjectClass(_E, obj);
+ cb->name = "cleanup";
+ cb->msig = "()I";
+ cb->object = (*_E)->NewGlobalRef(_E, obj);
+ cb->method = (*_E)->GetMethodID(_E, c, cb->name, cb->msig);
- apr_pool_cleanup_register(no->pool, (const void *)cb,
- callback_object_cleanup,
- apr_pool_cleanup_null);
-
- SIGHT_GLOBAL_CLEANUP();
+ apr_pool_cleanup_register(no->pool, (const void *)cb,
+ callback_object_cleanup,
+ apr_pool_cleanup_null);
+ } SIGHT_LOCAL_END(no);
return P2J(cb);
}
@@ -114,10 +107,9 @@
sight_strerror(SIGHT_ENULL));
return;
}
- if (SIGHT_IS_VALID_GLOBAL()) {
+ SIGHT_LOCAL_TRY(no) {
apr_pool_cleanup_kill(no->pool, cb, callback_object_cleanup);
- SIGHT_GLOBAL_CLEANUP();
- }
+ } SIGHT_LOCAL_END(no);
if (cb->object)
(*_E)->DeleteGlobalRef(_E, cb->object);
free(cb);
Modified: trunk/sight/native/share/proc.c
===================================================================
--- trunk/sight/native/share/proc.c 2007-10-01 07:10:19 UTC (rev 1064)
+++ trunk/sight/native/share/proc.c 2007-10-01 07:58:39 UTC (rev 1065)
@@ -59,11 +59,6 @@
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,
@@ -71,23 +66,26 @@
rv = APR_ENOPOOL;
goto cleanup;
}
- p = (sight_runproc_t *)sight_pcalloc(_E, no->pool, sizeof(sight_runproc_t),
- THROW_FMARK);
- if (p == NULL) {
- rv = APR_ENOMEM;
- goto cleanup;
- }
- if ((rv = apr_procattr_create(&p->attr, no->pool)) != APR_SUCCESS) {
- goto cleanup;
- }
- p->exitval = -1;
+ SIGHT_LOCAL_TRY(no) {
+ p = (sight_runproc_t *)sight_pcalloc(_E, no->pool, sizeof(sight_runproc_t),
+ THROW_FMARK);
+ if (p == NULL) {
+ rv = APR_ENOMEM;
+ SIGHT_LOCAL_BRK(no);
+ goto cleanup;
+ }
+ if ((rv = apr_procattr_create(&p->attr, no->pool)) != APR_SUCCESS) {
+ SIGHT_LOCAL_BRK(no);
+ goto cleanup;
+ }
+ p->exitval = -1;
#ifdef SIGHT_DO_STATS
- no->clean = proc_cleanup;
+ no->clean = proc_cleanup;
#endif
- no->native = p;
- rv = APR_SUCCESS;
+ no->native = p;
+ rv = APR_SUCCESS;
+ } SIGHT_LOCAL_END(no);
cleanup:
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -310,9 +308,11 @@
UNREFERENCED_STDARGS;
if (!no || !no->native)
return APR_EINVAL;
- p = (sight_runproc_t *)no->native;
- rv = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
- (apr_wait_how_e)waithow);
+ SIGHT_LOCAL_TRY(no) {
+ p = (sight_runproc_t *)no->native;
+ rv = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
+ (apr_wait_how_e)waithow);
+ } SIGHT_LOCAL_END(no);
return rv;
}
@@ -324,7 +324,7 @@
{
sight_object_t *no = J2P(instance, sight_object_t *);
sight_runproc_t *p;
- apr_status_t rc;
+ apr_status_t rc = APR_ENOPOOL;
apr_interval_time_t timeout_value, timeout_interval;
apr_wait_how_e how = (apr_wait_how_e)waithow;
@@ -332,26 +332,31 @@
if (!no || !no->native)
return APR_EINVAL;
- p = (sight_runproc_t *)no->native;
- if (timeout < 0)
- how = APR_WAIT;
- else
- how = APR_NOWAIT;
- if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
- how)) != APR_CHILD_NOTDONE)
- return rc;
- if (timeout < 1)
- return APR_EINVAL;
- timeout_value = timeout * 1000L;
- timeout_interval = timeout_value / 64;
- do {
- apr_sleep(timeout_interval);
- rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
APR_NOWAIT);
- if (timeout_interval >= timeout_value)
- break;
- timeout_interval *= 2;
- } while (rc == APR_CHILD_NOTDONE);
-
+ SIGHT_LOCAL_TRY(no) {
+ p = (sight_runproc_t *)no->native;
+ if (timeout < 0)
+ how = APR_WAIT;
+ else
+ how = APR_NOWAIT;
+ if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
+ how)) != APR_CHILD_NOTDONE) {
+ SIGHT_LOCAL_BRK(no);
+ return rc;
+ }
+ if (timeout < 1) {
+ SIGHT_LOCAL_BRK(no);
+ return APR_EINVAL;
+ }
+ timeout_value = timeout * 1000L;
+ timeout_interval = timeout_value / 64;
+ do {
+ apr_sleep(timeout_interval);
+ rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
APR_NOWAIT);
+ if (timeout_interval >= timeout_value)
+ break;
+ timeout_interval *= 2;
+ } while (rc == APR_CHILD_NOTDONE);
+ } SIGHT_LOCAL_END(no);
return rc;
}
@@ -372,54 +377,59 @@
UNREFERENCED_O;
if (!no || !no->native || !progress)
return APR_EINVAL;
- p = (sight_runproc_t *)no->native;
- if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
- APR_NOWAIT)) != APR_CHILD_NOTDONE)
- return rc;
-
- c = (*_E)->GetObjectClass(_E, progress);
- cb.name = "progress";
- cb.msig = "(I)I";
- cb.object = progress;
- cb.method = (*_E)->GetMethodID(_E, c, cb.name, cb.msig);
- if (!cb.method || (*_E)->ExceptionCheck(_E)) {
- return APR_EINVAL;
- }
- cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick, NULL);
- if ((*_E)->ExceptionCheck(_E)) {
- return APR_FROM_OS_ERROR(EINTR);
- }
-
- if (timeout > 0) {
- timeout_value = timeout * 1000L;
- timeout_interval = timeout_value / 100;
- }
- else {
- /* Defaults to 100 ms */
- timeout_interval = 100000L;
- }
- do {
- if (cres < 0) {
- /* Broken by the callback */
- break;
+ SIGHT_LOCAL_TRY(no) {
+ p = (sight_runproc_t *)no->native;
+ if ((rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
+ APR_NOWAIT)) != APR_CHILD_NOTDONE) {
+ SIGHT_LOCAL_BRK(no);
+ return rc;
}
- else if (cres > 0) {
- timeout_interval = cres * 1000L;
+ c = (*_E)->GetObjectClass(_E, progress);
+ cb.name = "progress";
+ cb.msig = "(I)I";
+ cb.object = progress;
+ cb.method = (*_E)->GetMethodID(_E, c, cb.name, cb.msig);
+ if (!cb.method || (*_E)->ExceptionCheck(_E)) {
+ SIGHT_LOCAL_BRK(no);
+ return APR_EINVAL;
}
- apr_sleep(timeout_interval);
- rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
APR_NOWAIT);
- if (timeout > 0) {
- if (timeout_interval >= timeout_value)
- break;
- timeout_value -= timeout_interval;
- }
- cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick++, NULL);
+ cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick, NULL);
if ((*_E)->ExceptionCheck(_E)) {
+ SIGHT_LOCAL_BRK(no);
return APR_FROM_OS_ERROR(EINTR);
}
- } while (rc == APR_CHILD_NOTDONE);
+ if (timeout > 0) {
+ timeout_value = timeout * 1000L;
+ timeout_interval = timeout_value / 100;
+ }
+ else {
+ /* Defaults to 100 ms */
+ timeout_interval = 100000L;
+ }
+ do {
+ if (cres < 0) {
+ /* Broken by the callback */
+ break;
+ }
+ else if (cres > 0) {
+ timeout_interval = cres * 1000L;
+ }
+ apr_sleep(timeout_interval);
+ rc = apr_proc_wait(&p->p, &p->exitval, &p->exitwhy,
APR_NOWAIT);
+ if (timeout > 0) {
+ if (timeout_interval >= timeout_value)
+ break;
+ timeout_value -= timeout_interval;
+ }
+ cres = (*_E)->CallIntMethod(_E, cb.object, cb.method, tick++, NULL);
+ if ((*_E)->ExceptionCheck(_E)) {
+ SIGHT_LOCAL_BRK(no);
+ return APR_FROM_OS_ERROR(EINTR);
+ }
+ } while (rc == APR_CHILD_NOTDONE);
+ } SIGHT_LOCAL_END(no);
return rc;
}
@@ -470,61 +480,59 @@
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));
rv = APR_ENOPOOL;
goto cleanup;
}
- if (args)
- largs = (*_E)->GetArrayLength(_E, args);
- if (env)
- lenv = (*_E)->GetArrayLength(_E, env);
- if (largs > MAX_ARGS_SIZE || lenv > MAX_ENV_SIZE) {
- rv = APR_EINVAL;
- goto cleanup;
- }
- if (largs) {
- for (i = 0; i < largs; i++) {
- jstring str = (*_E)->GetObjectArrayElement(_E, args, i);
- if (!str || (*_E)->ExceptionCheck(_E)) {
- rv = APR_EINVAL;
- goto cleanup;
- }
- sargs[i] = sight_pstrdupj(no->pool, _E, str);
- (*_E)->DeleteLocalRef(_E, str);
+ SIGHT_LOCAL_TRY(no) {
+ if (args)
+ largs = (*_E)->GetArrayLength(_E, args);
+ if (env)
+ lenv = (*_E)->GetArrayLength(_E, env);
+ if (largs > MAX_ARGS_SIZE || lenv > MAX_ENV_SIZE) {
+ rv = APR_EINVAL;
+ SIGHT_LOCAL_BRK(no);
+ goto cleanup;
}
- sargs[i] = NULL;
- pargs = (const char * const *)&sargs[0];
- }
- if (lenv) {
- for (i = 0; i < lenv; i++) {
- jstring str = (*_E)->GetObjectArrayElement(_E, env, i);
- if (!str || (*_E)->ExceptionCheck(_E)) {
- rv = APR_EINVAL;
- goto cleanup;
- }
- senv[i] = sight_pstrdupj(no->pool, _E, str);
- (*_E)->DeleteLocalRef(_E, str);
+ if (largs) {
+ for (i = 0; i < largs; i++) {
+ jstring str = (*_E)->GetObjectArrayElement(_E, args, i);
+ if (!str || (*_E)->ExceptionCheck(_E)) {
+ rv = APR_EINVAL;
+ SIGHT_LOCAL_BRK(no);
+ goto cleanup;
+ }
+ sargs[i] = sight_pstrdupj(no->pool, _E, str);
+ (*_E)->DeleteLocalRef(_E, str);
+ }
+ sargs[i] = NULL;
+ pargs = (const char * const *)&sargs[0];
}
- senv[i] = NULL;
- penv = (const char * const *)&senv[0];
- }
- p = (sight_runproc_t *)no->native;
- rv = apr_proc_create(&p->p, J2S(progname), pargs,
- penv, p->attr, no->pool);
- if (rv != APR_SUCCESS) {
- throwAprException(_E, rv);
- }
+ if (lenv) {
+ for (i = 0; i < lenv; i++) {
+ jstring str = (*_E)->GetObjectArrayElement(_E, env, i);
+ if (!str || (*_E)->ExceptionCheck(_E)) {
+ rv = APR_EINVAL;
+ SIGHT_LOCAL_BRK(no);
+ goto cleanup;
+ }
+ senv[i] = sight_pstrdupj(no->pool, _E, str);
+ (*_E)->DeleteLocalRef(_E, str);
+ }
+ senv[i] = NULL;
+ penv = (const char * const *)&senv[0];
+ }
+ p = (sight_runproc_t *)no->native;
+ rv = apr_proc_create(&p->p, J2S(progname), pargs,
+ penv, p->attr, no->pool);
+ if (rv != APR_SUCCESS) {
+ throwAprException(_E, rv);
+ }
+ } SIGHT_LOCAL_END(no);
cleanup:
SIGHT_FREE_CSTRING(progname);
- SIGHT_GLOBAL_CLEANUP();
return rv;
}
@@ -566,24 +574,27 @@
{
sight_object_t *no = J2P(instance, sight_object_t *);
sight_runproc_t *p;
+ apr_status_t rv = APR_EINVAL;
UNREFERENCED_O;
if (!no || !no->native) {
return APR_EINVAL;
}
- p = (sight_runproc_t *)no->native;
- switch (which) {
- case 0:
- return apr_file_pipe_timeout_set(p->p.in, timeout * 1000L);
- break;
- case 1:
- return apr_file_pipe_timeout_set(p->p.out, timeout * 1000L);
- break;
- case 2:
- return apr_file_pipe_timeout_set(p->p.err, timeout * 1000L);
- break;
- }
- return APR_EINVAL;
+ SIGHT_LOCAL_TRY(no) {
+ p = (sight_runproc_t *)no->native;
+ switch (which) {
+ case 0:
+ rv = apr_file_pipe_timeout_set(p->p.in, timeout * 1000L);
+ break;
+ case 1:
+ rv = apr_file_pipe_timeout_set(p->p.out, timeout * 1000L);
+ break;
+ case 2:
+ rv = apr_file_pipe_timeout_set(p->p.err, timeout * 1000L);
+ break;
+ }
+ } SIGHT_LOCAL_END(no);
+ return rv;
}
SIGHT_EXPORT_DECLARE(void, Process, notes0)(SIGHT_STDARGS,