You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/12 17:22:57 UTC
svn commit: r814169 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/Callback.java
main/native/include/acr_callback.h main/native/shared/callback.c
test/org/apache/commons/runtime/TestCallback.java
Author: mturk
Date: Sat Sep 12 15:22:53 2009
New Revision: 814169
URL: http://svn.apache.org/viewvc?rev=814169&view=rev
Log:
Allow native handler callback for creating the callback objects
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Callback.java
commons/sandbox/runtime/trunk/src/main/native/include/acr_callback.h
commons/sandbox/runtime/trunk/src/main/native/shared/callback.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestCallback.java
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Callback.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Callback.java?rev=814169&r1=814168&r2=814169&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Callback.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Callback.java Sat Sep 12 15:22:53 2009
@@ -37,20 +37,21 @@
public interface Callback
{
/**
- * Callback method executed from Native code.
+ * Handler method executed from Native code.
* <p>
- * If {@code callback} returns {@code zero} the operation continues.
+ * If {@code handler} returns {@code zero} the operation continues.
* If negative value is returned the operation breaks. If positive
* value is returned the native part will decide what to do with this
* value and it depends on the implementation of the callback itself.
* </p>
*
- * @param opaque Data passed to the callback at register time.
+ * @param data Data passed to the callback at register time or
+ * newly created object by the native.
* @param status Native provided status call that informs about
* the callback reason.
* @return Continuation state that defines
* if the operation should continue or not.
*/
- public int callback(Object opaque, int status);
+ public int handler(Object data, int status);
}
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_callback.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_callback.h?rev=814169&r1=814168&r2=814169&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_callback.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_callback.h Sat Sep 12 15:22:53 2009
@@ -33,7 +33,9 @@
typedef enum {
ACR_CALLBACK_NORMAL,
- ACR_CALLBACK_SYNC
+ ACR_CALLBACK_SYNC /** Define when the callback for the same object
+ * can be called from multiple threads.
+ */
} acr_callback_type_e;
typedef struct acr_callback_t acr_callback_t;
@@ -43,7 +45,7 @@
* The handler function must check for data validity.
*/
typedef int (acr_callback_handler_fn_t)(JNIEnv *, acr_callback_t *,
- void *, int);
+ void **, int);
struct acr_callback_t {
size_t size; /* Length of 'this' structure */
@@ -77,10 +79,12 @@
* @param refd Java Object representing additional data passed to the callback.
* @param size Additional context size.
* @param type Callback type.
+ * @param handler Callback function pointer.
*/
ACR_DECLARE(acr_callback_t *) ACR_CallbackAttach(JNIEnv *env, jobject obj,
jobject refd, size_t size,
- acr_callback_type_e type);
+ acr_callback_type_e type,
+ acr_callback_handler_fn_t *handler);
/**
* Free the memory associated with this Callback.
* @param env Current JNI environment
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/callback.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/callback.c?rev=814169&r1=814168&r2=814169&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/callback.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/callback.c Sat Sep 12 15:22:53 2009
@@ -40,7 +40,7 @@
J_DECLARE_M_ID(0000) = {
NULL,
- "callback",
+ "handler",
"(Ljava/lang/Object;I)I"
};
@@ -82,7 +82,8 @@
ACR_DECLARE(acr_callback_t *) ACR_CallbackAttach(ACR_JNISTDARGS, jobject refd,
size_t size,
- acr_callback_type_e type)
+ acr_callback_type_e type,
+ acr_callback_handler_fn_t *handler)
{
acr_callback_t *cb = ACR_Calloc(_E, THROW_NMARK,
@@ -100,6 +101,7 @@
}
}
cb->type = type;
+ cb->handler = handler;
cb->size = sizeof(acr_callback_t) + size;
if (size)
cb->ctx = &cb->context[0];
@@ -135,6 +137,11 @@
if (cb == NULL) {
return ACR_EINVAL;
}
+ if (_E == NULL && cb->thiz) {
+ /* Get JNIEnv only if needed
+ */
+ _E = ACR_GetJNIEnv();
+ }
if (cb->handler) {
/* This is native callback
*/
@@ -146,7 +153,7 @@
}
/* Execute the handler
*/
- *rv = (*cb->handler)(_E, cb, ctx, val);
+ *rv = (*cb->handler)(_E, cb, &ctx, val);
if (cb->type == ACR_CALLBACK_SYNC) {
}
rc = ACR_SUCCESS;
@@ -154,8 +161,6 @@
if (cb->thiz == NULL) {
return rc;
}
- if (_E == NULL)
- _E = ACR_GetJNIEnv();
if (IS_VALID_HANDLE(_E)) {
jobject d = NULL;
jobject o = (*_E)->NewLocalRef(_E, cb->thiz);
@@ -198,11 +203,11 @@
}
#ifdef ACR_ENABLE_TEST
-ACR_JNI_EXPORT_DECLARE(int, TestCallback, test00)(ACR_JNISTDARGS, jobject o, jint d)
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test00)(ACR_JNISTDARGS, jobject o)
{
acr_callback_t *cb;
- if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC))) {
+ if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC, NULL))) {
int i;
fputc('\n', stdout);
for (i = 0; i < 10; i++) {
@@ -228,9 +233,14 @@
}
static int test_handler(JNIEnv *_E, acr_callback_t *cb,
- void *ctx, int val)
+ void **ctx, int val)
{
- return val;
+ if (val) {
+ char buf[32];
+ sprintf(buf, "Native object %d", val);
+ *ctx = (*_E)->NewStringUTF(_E, buf);
+ }
+ return 0;
}
ACR_JNI_EXPORT_DECLARE(int, TestCallback, test01)(ACR_JNISTDARGS, jint d)
@@ -244,14 +254,73 @@
int rv;
int rc;
+ rc = ACR_CallbackRun(NULL, cb, NULL, 0, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun01 failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "[native] CallbackRun01 (%d) return %d\n", i, rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test02)(ACR_JNISTDARGS, jobject o,
+ jobject d)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackAttach(_E, o, d, 0, ACR_CALLBACK_SYNC, NULL))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 10; i++) {
+ int rv;
+ int rc;
+
rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
if (rc) {
- fprintf(stderr, "CallbackRun failed with %d\n", rc);
+ fprintf(stderr, "CallbackRun02 failed with %d\n", rc);
+ fflush(stderr);
+ break;
+ }
+ else {
+ fprintf(stdout, "[native] CallbackRun02 (%d) return %d\n", i, rv);
+ fflush(stdout);
+ if (rv < 0)
+ break;
+ }
+ }
+ return ACR_CallbackFree(NULL, cb);
+ }
+ return 1;
+}
+
+ACR_JNI_EXPORT_DECLARE(int, TestCallback, test03)(ACR_JNISTDARGS, jobject o)
+{
+ acr_callback_t *cb;
+
+ if ((cb = ACR_CallbackAttach(_E, o, NULL, 0, ACR_CALLBACK_SYNC, test_handler))) {
+ int i;
+ fputc('\n', stdout);
+ for (i = 0; i < 10; i++) {
+ int rv;
+ int rc;
+
+ rc = ACR_CallbackRun(NULL, cb, NULL, i, &rv);
+ if (rc) {
+ fprintf(stderr, "CallbackRun03 failed with %d\n", rc);
fflush(stderr);
break;
}
else {
- fprintf(stdout, "[native] CallbackRun (%d) return %d\n", i, rv);
+ fprintf(stdout, "[native] CallbackRun03 (%d) return %d\n", i, rv);
fflush(stdout);
if (rv < 0)
break;
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestCallback.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestCallback.java?rev=814169&r1=814168&r2=814169&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestCallback.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestCallback.java Sat Sep 12 15:22:53 2009
@@ -25,8 +25,10 @@
public class TestCallback extends TestCase
{
- private static native int test00(Object o, int d);
+ private static native int test00(Object o);
private static native int test01(int d);
+ private static native int test02(Object o, Object d);
+ private static native int test03(Object o);
public static Test suite() {
TestSuite suite = new TestSuite(TestCallback.class);
@@ -49,11 +51,16 @@
public MyCallback() {
counter = 0;
}
- public int callback(Object o, int val) {
+ public int handler(Object o, int val) {
counter++;
if (o != null) {
- MyData d = (MyData)o;
- d.myMethod();
+ if (o instanceof MyData) {
+ MyData d = (MyData)o;
+ d.myMethod();
+ }
+ else if (o instanceof String) {
+ System.out.println((String)o);
+ }
}
if (counter > 5)
return -1;
@@ -71,7 +78,7 @@
throws Exception
{
MyCallback cb = new MyCallback();
- int rc = test00(cb, 0);
+ int rc = test00(cb);
assertEquals("Callback result", 0, rc);
}
@@ -82,5 +89,23 @@
assertEquals("Callback result", 0, rc);
}
+ public void testDataCallback()
+ throws Exception
+ {
+ MyData dp = new MyData();
+ MyCallback cb = new MyCallback();
+ int rc = test02(cb, dp);
+ assertEquals("Callback result", 0, rc);
+ assertEquals("Callback calls", cb.counter, dp.val);
+ }
+
+ public void testNativeDataCallback()
+ throws Exception
+ {
+ MyCallback cb = new MyCallback();
+ int rc = test03(cb);
+ assertEquals("Callback result", 0, rc);
+ }
+
}