Permalink
Browse files

Make tweaks suggested by @wmeissner.

* Use HashMap + synchronized block for signal handlers.
* Bind native signal function with intptr_t return type.
  • Loading branch information...
1 parent 877559d commit 8f95d59dcd3bb2a0bb583af29a6f6b95517118be @headius headius committed Dec 6, 2012
Showing with 18 additions and 14 deletions.
  1. +16 −13 src/main/java/jnr/posix/BaseNativePOSIX.java
  2. +2 −1 src/main/java/jnr/posix/LibC.java
View
29 src/main/java/jnr/posix/BaseNativePOSIX.java
@@ -19,7 +19,8 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Map;
import jnr.constants.platform.Signal;
abstract class BaseNativePOSIX extends NativePOSIX implements POSIX {
@@ -29,7 +30,7 @@
protected final POSIXHandler handler;
protected final JavaLibCHelper helper;
- private final Hashtable<Signal, SignalHandler> signalHandlers = new Hashtable();
+ protected final Map<Signal, SignalHandler> signalHandlers = new HashMap();
BaseNativePOSIX(String libraryName, LibCProvider libcProvider, POSIXHandler handler) {
this.handler = handler;
@@ -223,19 +224,21 @@ public int kill(int pid, int signal) {
}
public SignalHandler signal(Signal sig, final SignalHandler handler) {
- SignalHandler old = signalHandlers.get(sig);
-
- int result = libc().signal(sig.intValue(), new LibC.LibCSignalHandler() {
- public void signal(int sig) {
- handler.handle(sig);
+ synchronized (signalHandlers) {
+ SignalHandler old = signalHandlers.get(sig);
+
+ long result = libc().signal(sig.intValue(), new LibC.LibCSignalHandler() {
+ public void signal(int sig) {
+ handler.handle(sig);
+ }
+ });
+
+ if (result != -1) {
+ signalHandlers.put(sig, handler);
}
- });
-
- if (result != -1) {
- signalHandlers.put(sig, handler);
+
+ return old;
}
-
- return old;
}
public int lchmod(String filename, int mode) {
View
3 src/main/java/jnr/posix/LibC.java
@@ -41,6 +41,7 @@
import java.nio.ByteBuffer;
import jnr.ffi.annotations.Delegate;
+import jnr.ffi.types.intptr_t;
public interface LibC {
int chmod(CharSequence filename, int mode);
@@ -79,7 +80,7 @@
public interface LibCSignalHandler {
@Delegate void signal(int sig);
}
- int signal(int sig, LibCSignalHandler handler);
+ @intptr_t long signal(int sig, LibCSignalHandler handler);
int lchmod(CharSequence filename, int mode);
int lchown(CharSequence filename, int user, int group);
int link(CharSequence oldpath, CharSequence newpath);

0 comments on commit 8f95d59

Please sign in to comment.