Function Namespace Signal
gpsee.module.ca.page.signal - Module to expose POSIX signals to JavaScript. Requires SpiderMonkey 1.8.1 or better.
This module recognizes only signals numbered between 0 and 31 and the following signal names:
ABRT ALRM BUS FPE HUP ILL INT KILL PIPE QUIT SEGV TERM USR1 USR2 CHLD CONT STOP TSTP TTIN TTOUThese limits can be modified by changing MAX_OS_SIGNAL in Signal_module.c and editting signal_list.h.
Catching Signals
Signals are caught by registering a signal handler as an event handler named Signal.onSignalName. The signal handler is passed one parameter, the signal number, and has itself as "this". This gives the signal handler the ability to set private variables without polluting the global namespace.
For example, to catch the HUP signal, one might write
Signal.onHUP = function(sig){ print("Caught Signal " + sig) };
Interaction with Thread
This module was designed with GPSEE's Thread module in mind, and as such makes the assumption there exists a 1:1 relationship between threads and contexts in the active runtime. Catching a signal in one context will always cause the signal handler to execute in that same context, regardless of the thread to which the OS signal was delivered. Multiple runtimes in the same process is not supported.
Signal Timing
Signals are caught with SpiderMonkey's operation and context callbacks. This means that signals will be processed only when your program (or thread) branches or terminates. Since most programs either loop or terminate very quickly, signals tend to be processed without undue delay. If for some strange reason (design error?) your program needs received signals processed by a certain point, a one-cycle dummy for-loop at that point will encourage the operation callback to trigger. As of this writing, the following code will trigger the operation callback without triggering a JIT trace or polluting your namespace -- but this is highly version-dependent and subject to change without notice:
(function(){for (var i=0; i < 1; i++);})();
Future versions of SpiderMonkey may also call the operation callback when JS_SuspendRequest() is called. This means that signals may also be caught immediately after certain native function calls without a branch. These native calls would generally be those expected to yield or consume non-trivial, non-JS resources (like IO, mutex spinning, etc).
In a single-threaded program, you can count on raised POSIX signals to interrupt certain system operations (such as System.sleep()), which can in turn cause your JavaScript program to resume running, branch, and handle the signal. This behaviour cannot be counted on in a multi-threaded program, because the OS-level signal will not necessarily be received by the thread that is registered to catch the signal.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Signal()
Module ID gpsee.module.ca.page.signal
|
| Method Attributes | Method Name and Description |
|---|---|
| <inner> |
kill(signal, pid)
Send a signal to a process.
|
| <inner> |
raise(signal)
Raise a signal in the current process.
|
| <inner> |
send(signal, pid)
Send a signal to a process.
|
Author: Wes Garland, PageMail, Inc., wes@page.ca.
- Parameters:
- signal
- Signal number or name as a String (e.g. "HUP" or 1)
- pid
- [optional] Process ID, or process group ID when negative. If pid is unspecified, the signal is raised in the current process.
- Parameters:
- signal
- Signal number or name as a String (e.g. "HUP" or 1)
- Parameters:
- signal
- Signal number or name as a String (e.g. "HUP" or 1)
- pid
- Process ID, or process group ID when negative.