Hiya.

I've found a new way to invoke the signal, that seems to work better. (for one, I don't get an exception from JRuby when it's called..). So, Tom, could you please try the attached patch+the former signal patch and see if it works on your box?

What I did before was to create a Proc object, and then just call this from the Runnable when the signal occured. This didn't play well with JRuby's other Ruby-threads, so this patch does it a little bit different. I've created a new RubyThread static method, to get a RubyThread without starting it, and then I create one of these, put it in the signal handler class and then start this Thread when the signal occurs. This seem to work fine for me.

Hope it helps.

Regards
 Ola Bini
Index: src/org/jruby/RubyKernel.java
===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyKernel.java,v
retrieving revision 1.53
diff -u -r1.53 RubyKernel.java
--- src/org/jruby/RubyKernel.java       24 May 2006 01:34:03 -0000      1.53
+++ src/org/jruby/RubyKernel.java       17 Jun 2006 11:13:29 -0000
@@ -657,8 +657,42 @@
         throw je;
     }
 
+    private static class JRubyProcObject implements Runnable {
+        private final RubyThread proc;
+        public JRubyProcObject(final RubyThread proc) {
+            this.proc = proc;
+        }
+        public void run() {
+            proc.startThread();
+        }
+    }
+
     public static IRubyObject trap(IRubyObject recv, IRubyObject[] args) {
-        // FIXME: We can probably fake some basic signals, but obviously can't 
do everything. For now, stub.
+        String sigName = null;
+        long sigNum = -1;
+        if(args[0] instanceof RubyNumeric) {
+            sigNum = RubyNumeric.num2long(args[0]);
+        } else {
+            sigName = args[0].toString();
+            if(sigName.startsWith("SIG")) {
+                sigName = sigName.substring("SIG".length());
+            }
+        }
+        RubyThread proc = null;
+        if(args.length>1) {
+            if(args[1] instanceof RubyString) {
+                // Handle cases of IGNORE, SIG_IGN, DEFAULT and SIG_DFL
+            }
+        } else {
+            if (recv.getRuntime().getCurrentContext().isBlockGiven()) {
+                proc = 
RubyThread.notStartThread(recv.getRuntime().getClass("Thread"),new 
IRubyObject[0]);
+            }
+        }
+        if(null != sigName) {
+            recv.getRuntime().getSignalHandler().handleSignal(sigName,new 
JRubyProcObject(proc));
+        } else {
+            recv.getRuntime().getSignalHandler().handleSignal(sigNum,new 
JRubyProcObject(proc));
+        }        
         return recv.getRuntime().getNil();
     }
     
Index: src/org/jruby/RubyThread.java
===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyThread.java,v
retrieving revision 1.23
diff -u -r1.23 RubyThread.java
--- src/org/jruby/RubyThread.java       16 Jun 2006 18:13:26 -0000      1.23
+++ src/org/jruby/RubyThread.java       17 Jun 2006 11:10:59 -0000
@@ -219,6 +219,28 @@
         
         return rubyThread;
     }
+
+    static RubyThread notStartThread(final IRubyObject recv, final 
IRubyObject[] args) {
+        final IRuby runtime = recv.getRuntime();
+        if (!runtime.getCurrentContext().isBlockGiven()) {
+            throw runtime.newThreadError("must be called with a block");
+        }
+        final RubyThread rubyThread = new RubyThread(runtime, (RubyClass) 
recv);
+        rubyThread.callInit(args);
+
+        rubyThread.threadImpl = new NativeThread(rubyThread, args);
+        //        rubyThread.threadImpl.start();
+        
+        // make sure the thread has started before continuing, so it will 
appear "runnable" to the rest of Ruby
+        //        rubyThread.ensureStarted();
+        
+        return rubyThread;
+    }
+
+    void startThread() {
+        threadImpl.start();
+        ensureStarted();
+    }
     
     public void cleanTerminate() {
        try {
_______________________________________________
Jruby-devel mailing list
Jruby-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jruby-devel

Reply via email to