author | vromero |
Mon, 08 Oct 2018 06:52:41 -0700 | |
changeset 52038 | 957de5be48bc |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
2 |
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.misc; |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
27 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
28 |
import java.util.Objects; |
2 | 29 |
|
30 |
/** |
|
31 |
* This class provides ANSI/ISO C signal support. A Java program can register |
|
32 |
* signal handlers for the current process. There are two restrictions: |
|
33 |
* <ul> |
|
34 |
* <li> |
|
35 |
* Java code cannot register a handler for signals that are already used |
|
36 |
* by the Java VM implementation. The <code>Signal.handle</code> |
|
37 |
* function raises an <code>IllegalArgumentException</code> if such an attempt |
|
38 |
* is made. |
|
39 |
* <li> |
|
40 |
* When <code>Signal.handle</code> is called, the VM internally registers a |
|
41 |
* special C signal handler. There is no way to force the Java signal handler |
|
42 |
* to run synchronously before the C signal handler returns. Instead, when the |
|
43 |
* VM receives a signal, the special C signal handler creates a new thread |
|
44 |
* (at priority <code>Thread.MAX_PRIORITY</code>) to |
|
45 |
* run the registered Java signal handler. The C signal handler immediately |
|
46 |
* returns. Note that because the Java signal handler runs in a newly created |
|
47 |
* thread, it may not actually be executed until some time after the C signal |
|
48 |
* handler returns. |
|
49 |
* </ul> |
|
50 |
* <p> |
|
51 |
* Signal objects are created based on their names. For example: |
|
52 |
* <blockquote><pre> |
|
53 |
* new Signal("INT"); |
|
30655 | 54 |
* </pre></blockquote> |
2 | 55 |
* constructs a signal object corresponding to <code>SIGINT</code>, which is |
56 |
* typically produced when the user presses <code>Ctrl-C</code> at the command line. |
|
57 |
* The <code>Signal</code> constructor throws <code>IllegalArgumentException</code> |
|
58 |
* when it is passed an unknown signal. |
|
59 |
* <p> |
|
60 |
* This is an example of how Java code handles <code>SIGINT</code>: |
|
61 |
* <blockquote><pre> |
|
62 |
* SignalHandler handler = new SignalHandler () { |
|
63 |
* public void handle(Signal sig) { |
|
64 |
* ... // handle SIGINT |
|
65 |
* } |
|
66 |
* }; |
|
67 |
* Signal.handle(new Signal("INT"), handler); |
|
30655 | 68 |
* </pre></blockquote> |
2 | 69 |
* |
70 |
* @author Sheng Liang |
|
71 |
* @author Bill Shannon |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
72 |
* @see sun.misc.SignalHandler |
2 | 73 |
* @since 1.2 |
74 |
*/ |
|
75 |
public final class Signal { |
|
76 |
||
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
77 |
// Delegate to jdk.internal.misc.Signal. |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
78 |
private final jdk.internal.misc.Signal iSignal; |
2 | 79 |
|
80 |
/* Returns the signal number */ |
|
81 |
public int getNumber() { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
82 |
return iSignal.getNumber(); |
2 | 83 |
} |
84 |
||
85 |
/** |
|
86 |
* Returns the signal name. |
|
87 |
* |
|
88 |
* @return the name of the signal. |
|
89 |
* @see sun.misc.Signal#Signal(String name) |
|
90 |
*/ |
|
91 |
public String getName() { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
92 |
return iSignal.getName(); |
2 | 93 |
} |
94 |
||
95 |
/** |
|
96 |
* Compares the equality of two <code>Signal</code> objects. |
|
97 |
* |
|
98 |
* @param other the object to compare with. |
|
99 |
* @return whether two <code>Signal</code> objects are equal. |
|
100 |
*/ |
|
101 |
public boolean equals(Object other) { |
|
102 |
if (this == other) { |
|
103 |
return true; |
|
104 |
} |
|
105 |
if (other == null || !(other instanceof Signal)) { |
|
106 |
return false; |
|
107 |
} |
|
108 |
Signal other1 = (Signal)other; |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
109 |
return iSignal.equals(other1.iSignal); |
2 | 110 |
} |
111 |
||
112 |
/** |
|
113 |
* Returns a hashcode for this Signal. |
|
114 |
* |
|
115 |
* @return a hash code value for this object. |
|
116 |
*/ |
|
117 |
public int hashCode() { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
118 |
return getNumber(); |
2 | 119 |
} |
120 |
||
121 |
/** |
|
122 |
* Returns a string representation of this signal. For example, "SIGINT" |
|
123 |
* for an object constructed using <code>new Signal ("INT")</code>. |
|
124 |
* |
|
125 |
* @return a string representation of the signal |
|
126 |
*/ |
|
127 |
public String toString() { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
128 |
return iSignal.toString(); |
2 | 129 |
} |
130 |
||
131 |
/** |
|
132 |
* Constructs a signal from its name. |
|
133 |
* |
|
134 |
* @param name the name of the signal. |
|
135 |
* @exception IllegalArgumentException unknown signal |
|
136 |
* @see sun.misc.Signal#getName() |
|
137 |
*/ |
|
138 |
public Signal(String name) { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
139 |
iSignal = new jdk.internal.misc.Signal(name); |
2 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Registers a signal handler. |
|
144 |
* |
|
145 |
* @param sig a signal |
|
146 |
* @param handler the handler to be registered with the given signal. |
|
30655 | 147 |
* @return the old handler |
2 | 148 |
* @exception IllegalArgumentException the signal is in use by the VM |
149 |
* @see sun.misc.Signal#raise(Signal sig) |
|
150 |
* @see sun.misc.SignalHandler |
|
151 |
* @see sun.misc.SignalHandler#SIG_DFL |
|
152 |
* @see sun.misc.SignalHandler#SIG_IGN |
|
153 |
*/ |
|
154 |
public static synchronized SignalHandler handle(Signal sig, |
|
155 |
SignalHandler handler) |
|
156 |
throws IllegalArgumentException { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
157 |
jdk.internal.misc.Signal.Handler oldHandler = jdk.internal.misc.Signal.handle(sig.iSignal, |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
158 |
InternalMiscHandler.of(sig, handler)); |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
159 |
return SunMiscHandler.of(sig.iSignal, oldHandler); |
2 | 160 |
} |
161 |
||
162 |
/** |
|
163 |
* Raises a signal in the current process. |
|
164 |
* |
|
165 |
* @param sig a signal |
|
166 |
* @see sun.misc.Signal#handle(Signal sig, SignalHandler handler) |
|
167 |
*/ |
|
168 |
public static void raise(Signal sig) throws IllegalArgumentException { |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
169 |
jdk.internal.misc.Signal.raise(sig.iSignal); |
2 | 170 |
} |
171 |
||
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
172 |
/* |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
173 |
* Wrapper class to proxy a SignalHandler to a jdk.internal.misc.Signal.Handler. |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
174 |
*/ |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
175 |
static final class InternalMiscHandler implements jdk.internal.misc.Signal.Handler { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
176 |
private final SignalHandler handler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
177 |
private final Signal signal; |
2 | 178 |
|
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
179 |
static jdk.internal.misc.Signal.Handler of(Signal signal, SignalHandler handler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
180 |
if (handler == SignalHandler.SIG_DFL) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
181 |
return jdk.internal.misc.Signal.Handler.SIG_DFL; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
182 |
} else if (handler == SignalHandler.SIG_IGN) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
183 |
return jdk.internal.misc.Signal.Handler.SIG_IGN; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
184 |
} else if (handler instanceof SunMiscHandler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
185 |
return ((SunMiscHandler)handler).iHandler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
186 |
} else { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
187 |
return new InternalMiscHandler(signal, handler); |
2 | 188 |
} |
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
189 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
190 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
191 |
private InternalMiscHandler(Signal signal, SignalHandler handler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
192 |
this.handler = handler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
193 |
this.signal = signal; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
194 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
195 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
196 |
@Override |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
197 |
public void handle(jdk.internal.misc.Signal ignore) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
198 |
handler.handle(signal); |
2 | 199 |
} |
200 |
} |
|
201 |
||
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
202 |
/* |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
203 |
* Wrapper class to proxy a jdk.internal.misc.Signal.Handler to a SignalHandler. |
2 | 204 |
*/ |
36009
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
205 |
static final class SunMiscHandler implements SignalHandler { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
206 |
private final jdk.internal.misc.Signal iSignal; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
207 |
private final jdk.internal.misc.Signal.Handler iHandler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
208 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
209 |
static SignalHandler of(jdk.internal.misc.Signal signal, jdk.internal.misc.Signal.Handler handler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
210 |
if (handler == jdk.internal.misc.Signal.Handler.SIG_DFL) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
211 |
return SignalHandler.SIG_DFL; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
212 |
} else if (handler == jdk.internal.misc.Signal.Handler.SIG_IGN) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
213 |
return SignalHandler.SIG_IGN; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
214 |
} else if (handler instanceof InternalMiscHandler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
215 |
return ((InternalMiscHandler) handler).handler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
216 |
} else { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
217 |
return new SunMiscHandler(signal, handler); |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
218 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
219 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
220 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
221 |
SunMiscHandler(jdk.internal.misc.Signal iSignal, jdk.internal.misc.Signal.Handler iHandler) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
222 |
this.iSignal = iSignal; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
223 |
this.iHandler = iHandler; |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
224 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
225 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
226 |
@Override |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
227 |
public void handle(Signal sig) { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
228 |
iHandler.handle(iSignal); |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
229 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
230 |
|
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
231 |
public String toString() { |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
232 |
return iHandler.toString(); |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
233 |
} |
2dbdf909ac81
8149750: Decouple sun.misc.Signal from the base module
rriggs
parents:
34716
diff
changeset
|
234 |
} |
2 | 235 |
} |