Asynchronous system trap
Asynchronous system trap refers to a mechanism used in several computer operating systems designed by the former Digital Equipment Corporation of Maynard, Massachusetts. The mechanism is a method for executing subroutines outside of the main thread of execution.
Mechanism
Various events within these systems can be optionally signalled back to the user processes via the AST mechanism. These ASTs act like subroutine calls but they are delivered asynchronously, that is, without any regard to the context of the main thread. Because of this, care must be taken:- to ensure that any code that is shared between the main thread and the AST must be designed to be reentrant, and
- any data that is shared must be safe against corruption if modified at any time by the AST. Otherwise, the data must be guarded by blocking ASTs during critical sections.
The following operating systems implement ASTs:
ASTs are roughly analogous to Unix signals. The important differences are:
- There are no "signal codes" assigned to ASTs: instead of assigning a handler to a signal code and raising that code, the AST is specified directly by its address. This allows any number of ASTs to be pending at once.
- ASTs never abort any system call in progress. In fact, it is possible for a process to put itself into a "hibernate" state, or to wait for an event flag by calling e.g. $WAITFR, whereupon it does nothing but wait for ASTs to be delivered. When an AST is delivered, the process is temporarily taken out of the wait to execute the AST. After the AST procedure completes, the call that put the process into hibernation or the event flag wait is made again; in essence, the reason for the wait is re-evaluated. The only way to get out of this loop is to execute a $WAKE or $SETEF system call to satisfy the wait. This can be done by the process itself by invoking $WAKE or $SETEF within the AST, or $SETEF within another process.
So for user mode only, a pair of bit flags was provided at a predefined user-writable memory location. The meanings of these two flags could be construed as "don't deliver any ASTs" and "ASTs have been disabled". Instead of the usual pair of $SETAST calls, the user-mode code would set the first flag before executing the sequence of instructions during which ASTs need to be blocked, and clear it after the sequence. Then it would check the second flag to see if it had become set during this time: if so, then ASTs really have become disabled, and $SETAST should be called to re-enable them. In the most common case, no ASTs would have become pending during this time, so there would be no need to call $SETAST at all.
The kernel AST delivery code, for its part, would check the first flag before trying to deliver a user-mode AST; if it was set, then it would directly set the ASTs-disabled bit in the process control block, and also set the second flag, before returning and leaving the AST undelivered.
The asynchronous procedure call mechanism in the Windows NT family of operating systems is a similar mechanism.