33 * <li>
34 * Java code cannot register a handler for signals that are already used
35 * by the Java VM implementation. The <code>Signal.handle</code>
36 * function raises an <code>IllegalArgumentException</code> if such an attempt
37 * is made.
38 * <li>
39 * When <code>Signal.handle</code> is called, the VM internally registers a
40 * special C signal handler. There is no way to force the Java signal handler
41 * to run synchronously before the C signal handler returns. Instead, when the
42 * VM receives a signal, the special C signal handler creates a new thread
43 * (at priority <code>Thread.MAX_PRIORITY</code>) to
44 * run the registered Java signal handler. The C signal handler immediately
45 * returns. Note that because the Java signal handler runs in a newly created
46 * thread, it may not actually be executed until some time after the C signal
47 * handler returns.
48 * </ul>
49 * <p>
50 * Signal objects are created based on their names. For example:
51 * <blockquote><pre>
52 * new Signal("INT");
53 * </blockquote></pre>
54 * constructs a signal object corresponding to <code>SIGINT</code>, which is
55 * typically produced when the user presses <code>Ctrl-C</code> at the command line.
56 * The <code>Signal</code> constructor throws <code>IllegalArgumentException</code>
57 * when it is passed an unknown signal.
58 * <p>
59 * This is an example of how Java code handles <code>SIGINT</code>:
60 * <blockquote><pre>
61 * SignalHandler handler = new SignalHandler () {
62 * public void handle(Signal sig) {
63 * ... // handle SIGINT
64 * }
65 * };
66 * Signal.handle(new Signal("INT"), handler);
67 * </blockquote></pre>
68 *
69 * @author Sheng Liang
70 * @author Bill Shannon
71 * @see sun.misc.SignalHandler
72 * @since 1.2
73 */
74 public final class Signal {
75 private static Hashtable<Signal,SignalHandler> handlers = new Hashtable<>(4);
76 private static Hashtable<Integer,Signal> signals = new Hashtable<>(4);
77
78 private int number;
79 private String name;
80
81 /* Returns the signal number */
82 public int getNumber() {
83 return number;
84 }
85
86 /**
87 * Returns the signal name.
132 /**
133 * Constructs a signal from its name.
134 *
135 * @param name the name of the signal.
136 * @exception IllegalArgumentException unknown signal
137 * @see sun.misc.Signal#getName()
138 */
139 public Signal(String name) {
140 number = findSignal(name);
141 this.name = name;
142 if (number < 0) {
143 throw new IllegalArgumentException("Unknown signal: " + name);
144 }
145 }
146
147 /**
148 * Registers a signal handler.
149 *
150 * @param sig a signal
151 * @param handler the handler to be registered with the given signal.
152 * @result the old handler
153 * @exception IllegalArgumentException the signal is in use by the VM
154 * @see sun.misc.Signal#raise(Signal sig)
155 * @see sun.misc.SignalHandler
156 * @see sun.misc.SignalHandler#SIG_DFL
157 * @see sun.misc.SignalHandler#SIG_IGN
158 */
159 public static synchronized SignalHandler handle(Signal sig,
160 SignalHandler handler)
161 throws IllegalArgumentException {
162 long newH = (handler instanceof NativeSignalHandler) ?
163 ((NativeSignalHandler)handler).getHandler() : 2;
164 long oldH = handle0(sig.number, newH);
165 if (oldH == -1) {
166 throw new IllegalArgumentException
167 ("Signal already used by VM or OS: " + sig);
168 }
169 signals.put(sig.number, sig);
170 synchronized (handlers) {
171 SignalHandler oldHandler = handlers.get(sig);
172 handlers.remove(sig);
|
33 * <li>
34 * Java code cannot register a handler for signals that are already used
35 * by the Java VM implementation. The <code>Signal.handle</code>
36 * function raises an <code>IllegalArgumentException</code> if such an attempt
37 * is made.
38 * <li>
39 * When <code>Signal.handle</code> is called, the VM internally registers a
40 * special C signal handler. There is no way to force the Java signal handler
41 * to run synchronously before the C signal handler returns. Instead, when the
42 * VM receives a signal, the special C signal handler creates a new thread
43 * (at priority <code>Thread.MAX_PRIORITY</code>) to
44 * run the registered Java signal handler. The C signal handler immediately
45 * returns. Note that because the Java signal handler runs in a newly created
46 * thread, it may not actually be executed until some time after the C signal
47 * handler returns.
48 * </ul>
49 * <p>
50 * Signal objects are created based on their names. For example:
51 * <blockquote><pre>
52 * new Signal("INT");
53 * </pre></blockquote>
54 * constructs a signal object corresponding to <code>SIGINT</code>, which is
55 * typically produced when the user presses <code>Ctrl-C</code> at the command line.
56 * The <code>Signal</code> constructor throws <code>IllegalArgumentException</code>
57 * when it is passed an unknown signal.
58 * <p>
59 * This is an example of how Java code handles <code>SIGINT</code>:
60 * <blockquote><pre>
61 * SignalHandler handler = new SignalHandler () {
62 * public void handle(Signal sig) {
63 * ... // handle SIGINT
64 * }
65 * };
66 * Signal.handle(new Signal("INT"), handler);
67 * </pre></blockquote>
68 *
69 * @author Sheng Liang
70 * @author Bill Shannon
71 * @see sun.misc.SignalHandler
72 * @since 1.2
73 */
74 public final class Signal {
75 private static Hashtable<Signal,SignalHandler> handlers = new Hashtable<>(4);
76 private static Hashtable<Integer,Signal> signals = new Hashtable<>(4);
77
78 private int number;
79 private String name;
80
81 /* Returns the signal number */
82 public int getNumber() {
83 return number;
84 }
85
86 /**
87 * Returns the signal name.
132 /**
133 * Constructs a signal from its name.
134 *
135 * @param name the name of the signal.
136 * @exception IllegalArgumentException unknown signal
137 * @see sun.misc.Signal#getName()
138 */
139 public Signal(String name) {
140 number = findSignal(name);
141 this.name = name;
142 if (number < 0) {
143 throw new IllegalArgumentException("Unknown signal: " + name);
144 }
145 }
146
147 /**
148 * Registers a signal handler.
149 *
150 * @param sig a signal
151 * @param handler the handler to be registered with the given signal.
152 * @return the old handler
153 * @exception IllegalArgumentException the signal is in use by the VM
154 * @see sun.misc.Signal#raise(Signal sig)
155 * @see sun.misc.SignalHandler
156 * @see sun.misc.SignalHandler#SIG_DFL
157 * @see sun.misc.SignalHandler#SIG_IGN
158 */
159 public static synchronized SignalHandler handle(Signal sig,
160 SignalHandler handler)
161 throws IllegalArgumentException {
162 long newH = (handler instanceof NativeSignalHandler) ?
163 ((NativeSignalHandler)handler).getHandler() : 2;
164 long oldH = handle0(sig.number, newH);
165 if (oldH == -1) {
166 throw new IllegalArgumentException
167 ("Signal already used by VM or OS: " + sig);
168 }
169 signals.put(sig.number, sig);
170 synchronized (handlers) {
171 SignalHandler oldHandler = handlers.get(sig);
172 handlers.remove(sig);
|