66 lines
2.1 KiB
Plaintext
66 lines
2.1 KiB
Plaintext
DEFINITION MODULE Processes;
|
||
(*
|
||
(pseudo-) concurrent programming with SEND/WAIT
|
||
|
||
|
||
From the book 'Programming in Modula-2' by Prof. N. Wirth.
|
||
*)
|
||
|
||
EXPORT QUALIFIED SIGNAL, SEND, WAIT,
|
||
StartProcess, Awaited, Init;
|
||
|
||
|
||
TYPE SIGNAL;
|
||
(* SIGNAL's are the means of synchronization between processes. *)
|
||
|
||
|
||
PROCEDURE StartProcess (P: PROC; n: CARDINAL);
|
||
(*- Start up a new process.
|
||
in: P top-level procedure that will execute in this process.
|
||
n number of bytes of workspace to be allocated to it.
|
||
|
||
Allocates (from Storage) a workspace of n bytes, and creates a process
|
||
executing procedure P in that workspace. Control is given to the new process.
|
||
|
||
Caution: The caller must ensure that the workspace size is sufficient for P.
|
||
Errors: StartProcess may fail due to insufficient memory.
|
||
*)
|
||
|
||
PROCEDURE SEND (VAR s: SIGNAL);
|
||
(*- Send a signal
|
||
in: s the signal to be sent. [Must have been Init'd]
|
||
out: s the signal with one less process waiting for it.
|
||
|
||
If no process is waiting for s, SEND has precisely no effect. Otherwise,
|
||
some process which is waiting for s is given control and allowed to
|
||
continue from WAIT.
|
||
*)
|
||
|
||
PROCEDURE WAIT (VAR s: SIGNAL);
|
||
(*- Wait for some other process to send a signal.
|
||
in: s the signal to wait for. [Must have been Init'd]
|
||
|
||
The current process waits for the signal s. At some later time, a
|
||
SEND(s) by some other process can cause this process to return from WAIT.
|
||
|
||
Errors: If all other processes are waiting, WAIT terminates the program.
|
||
*)
|
||
|
||
PROCEDURE Awaited (s:SIGNAL): BOOLEAN;
|
||
(*- Test whether any process is waiting for a signal.
|
||
in: s the signal of interest. [Must have been Init'd]
|
||
out: TRUE if and only if at least one process is waiting for s.
|
||
*)
|
||
|
||
PROCEDURE Init (VAR s: SIGNAL);
|
||
(*- Initialize a SIGNAL object.
|
||
in: s the signal to be initialized
|
||
out: s the initialized signal (ready to be used as above)
|
||
|
||
An object of type SIGNAL must be initialized with this procedure before
|
||
it can be used with any of the other operations. After Init(S), Awaited(S)
|
||
is FALSE.
|
||
*)
|
||
|
||
END Processes.
|
||
|