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/17 17:15:43 UTC

svn commit: r816230 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/include/ main/native/os/unix/ main/native/os/win32/ main/native/shared/ test/org/apache/commons/runtime/

Author: mturk
Date: Thu Sep 17 15:15:37 2009
New Revision: 816230

URL: http://svn.apache.org/viewvc?rev=816230&view=rev
Log:
Implement Signal.raise

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SignalAction.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_tlsd.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
    commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSystem.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SignalAction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SignalAction.java?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SignalAction.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SignalAction.java Thu Sep 17 15:15:37 2009
@@ -137,4 +137,14 @@
         else
             return org.handler();
     }
+    public static native int raise0(int signo);
+    public static void raise(Signal signo)
+        throws RuntimeException
+    {
+        int rc = raise0(signo.valueOf());
+        if (rc != Status.OK) {
+            throw new RuntimeException(Status.describe(rc));
+        }
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_tlsd.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_tlsd.h?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_tlsd.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_tlsd.h Thu Sep 17 15:15:37 2009
@@ -49,8 +49,9 @@
 
 struct acr_thread_local_t {
     ACR_RING_HEAD(tlsd_data_t, acr_tlsd_data_t)  data_ring;
-    JNIEnv     *env;
-    int         jvm_attached;
+    JNIEnv         *env;
+    unsigned int    id;
+    int             jvm_attached;
 #if defined(DEBUG)
 #if defined(WIN32)
     /* SEH requires no special TLSD data

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/main.c Thu Sep 17 15:15:37 2009
@@ -45,8 +45,8 @@
 
 mode_t acr_default_umask;
 mode_t acr_default_perms;
-
-static acr_thread_local_t _null_tlsd = { {NULL, NULL}, NULL, 0 };
+static volatile unsigned int acr_thread_id_cur = 0;
+static acr_thread_local_t _null_tlsd = { {NULL, NULL}, NULL, 0, 0 };
 static void acr_thread_key_destructor(void *data)
 {
     acr_tlsd_data_t    *p;
@@ -123,6 +123,9 @@
             return &_null_tlsd;
         }
         else {
+            /* ###: The increment should be atomic operation
+             */
+            tlsd->id = ++acr_thread_id_cur;
             ACR_RING_INIT(&tlsd->data_ring, acr_tlsd_data_t, link);
             pthread_setspecific(acr_thread_key, tlsd);
             return tlsd;
@@ -182,7 +185,7 @@
             char tn[32];
             JavaVMAttachArgs aa;
 
-            sprintf(tn, "ACR Native Thread %u", V2U(pthread_self()));
+            sprintf(tn, "ACR Native Thread %u", tlsd->id);
             aa.version = JNI_VERSION_1_4;
             aa.name    = tn;
             aa.group   = NULL;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Thu Sep 17 15:15:37 2009
@@ -54,6 +54,7 @@
 PSID acr_ownerusr_sid = NULL;
 PSID acr_ownergrp_sid = NULL;
 
+static volatile long acr_thread_id_cur = 0;
 static acr_thread_local_t _null_tlsd;
 static void acr_thread_key_destructor(void *data)
 {
@@ -478,8 +479,8 @@
                                JNI_VERSION_1_4) == JNI_EDETACHED) {
             char tn[32];
             JavaVMAttachArgs aa;
-
-            sprintf(tn, "ACR Native Thread %u", GetCurrentThreadId());
+            tlsd->id = (unsigned int)InterlockedIncrement(&acr_thread_id_cur);
+            sprintf(tn, "ACR Native Thread %u", tlsd->id);
             aa.version = JNI_VERSION_1_4;
             aa.name    = tn;
             aa.group   = NULL;

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c Thu Sep 17 15:15:37 2009
@@ -240,14 +240,22 @@
     }
 }
 
+ACR_JNI_EXPORT_DECLARE(jint, SignalAction, raise0)(ACR_JNISTDARGS, jint sig)
+{
+    int signum = 0;
+    if (sig > 0 && sig < ACR_NUMSIG)
+        signum = _sig_translate[sig];
+    else
+        return ACR_EINVAL;
+    return ACR_RaiseSignal(NULL, signum, -1);
+}
+
 ACR_JNI_EXPORT_DECLARE(jint, SignalAction, signal0)(ACR_JNISTDARGS, jint sig,
                                                     jint mode, jobject callback)
 {
     acr_sigfunc_t *handler;
     int signum = 0;
 
-    fprintf(stdout, "Setting signal %d for %d\n", signum, getpid());
-    fflush(stdout);
     if (sig > 0 && sig < ACR_NUMSIG)
         signum = _sig_translate[sig];
     else
@@ -258,8 +266,6 @@
         ACR_CallbackFree(_E, _callbacks[signum]);
     }
     _callbacks[signum] = NULL;
-    fprintf(stdout, "Setting signal mode %d\n", mode);
-    fflush(stdout);
     switch (mode) {
         case ACR_JSIG_IGN:
             handler = SIG_IGN;
@@ -277,8 +283,6 @@
             handler = SIG_ERR;
         break;
     }
-    fprintf(stdout, "Inited signal %d for %d\n", signum, getpid());
-    fflush(stdout);
     if (ACR_Signal(signum, handler) == SIG_ERR)
         return ACR_GET_OS_ERROR();
     else

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Thu Sep 17 15:15:37 2009
@@ -49,7 +49,16 @@
         suite.addTest(TestMemoryMap.suite());
         suite.addTest(TestCallback.suite());
         suite.addTest(TestObserver.suite());
-        suite.addTest(TestSystem.suite());
+
+        // On Sun JVM/Linux this gives wired results.
+        // On first call the exception is thrown, but then
+        // the JVM becomes unstable.
+        // Seems that ExceptionClear is making a problem.
+        // However the same stuf works on windows and with
+        // IBM VM, so it might be some bug.
+        //
+        // suite.addTest(TestSystem.suite());
+        //
         suite.addTest(TestSignal.suite());
         return suite;
     }

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java Thu Sep 17 15:15:37 2009
@@ -26,6 +26,7 @@
 public class TestSignal extends TestCase
 {
 
+    private static int counter = 0;
     public static Test suite() {
         TestSuite suite = new TestSuite(TestSignal.class);
         return suite;
@@ -37,7 +38,8 @@
         System.loadLibrary("acr");
     }
 
-    private static final class Handler implements SignalHandler {
+    private static final class Handler implements SignalHandler
+    {
         public Handler()
         {
             // Nothing
@@ -45,25 +47,65 @@
         public void handler(Signal signo)
             throws Exception
         {
-            System.out.println();
-            System.out.println("Received signal(" + signo + ") continuing...");
-            System.out.println("Signal executed in " + Thread.currentThread().getName() + " tread.");
+            counter++;
+            System.out.println("Received signal(" + signo + ") in " +
+                               Thread.currentThread().getName());
         }
     }
 
     public void testSignal()
         throws Exception
     {
+        counter = 0;
         Handler h = new Handler();
         SignalAction.handler(Signal.SIGINT, h);
-        System.out.println("Handler initialized in " + Thread.currentThread().getName() + " tread.");
-        for (int i = 0; i < 10; i++) {
-        try {
-            Thread.sleep(1000);
-        } catch (Throwable e) {
-            // Ignore
+        System.out.println("Handler initialized in " + Thread.currentThread().getName());
+        for (int i = 0; i < 2; i++) {
+            try {
+                Thread.sleep(1000);
+            } catch (Throwable e) {
+                // Ignore
+            }
+            if (i == 0)
+                SignalAction.raise(Signal.SIGINT);
         }
+        assertEquals("Signaled" , 1, counter);
+    }
+
+    class MySignalTester extends Thread
+    {
+        Signal signo;
+        public MySignalTester(Signal signo)
+        {
+            this.signo = signo;
         }
+        public void run() {
+            Handler h = new Handler();
+            SignalAction.handler(signo, h);
+            System.out.println();
+            System.out.println("Handler initialized in " + Thread.currentThread().getName());
+            for (int i = 0; i < 2; i++) {
+                try {
+                    Thread.sleep(500);
+                } catch (Throwable e) {
+                // Ignore
+                }
+                SignalAction.raise(signo);
+            }
+        }
+    }
+    public void testSignalThreaded()
+        throws Exception
+    {
+        System.out.println();
+        counter = 0;
+        MySignalTester h1 = new MySignalTester(Signal.SIGINT);
+        MySignalTester h2 = new MySignalTester(Signal.SIGHUP);
+        h1.start();
+        h2.start();
+        h1.join();
+        h2.join();
+        assertEquals("Signaled" , 4, counter);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSystem.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSystem.java?rev=816230&r1=816229&r2=816230&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSystem.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSystem.java Thu Sep 17 15:15:37 2009
@@ -30,6 +30,7 @@
     private static native int    test00(int s);
     private static native String test01(String k);
 
+    private MySecurityManager mgr = new MySecurityManager();
     public static Test suite() {
         TestSuite suite = new TestSuite(TestSystem.class);
         return suite;
@@ -67,13 +68,28 @@
         throws Exception
     {
         int rc = 0;
-        System.setSecurityManager(new MySecurityManager());
+        SecurityManager org = System.getSecurityManager();
+        System.setSecurityManager(mgr);
         rc = test00(11);
-        /* Result is EACCES cause out tmp security manager
+        /* Result is EACCES cause our tmp security manager
          * throws the exception
          */
         assertEquals("Call result", Status.EACCES, rc);
-        System.setSecurityManager(null);
+        System.setSecurityManager(org);
+    }
+
+    public void testSystemExitAgain()
+        throws Exception
+    {
+        int rc = 0;
+        SecurityManager org = System.getSecurityManager();
+        System.setSecurityManager(mgr);
+        rc = test00(11);
+        /* Result is EACCES cause our tmp security manager
+         * throws the exception
+         */
+        assertEquals("Call result", Status.EACCES, rc);
+        System.setSecurityManager(org);
     }