| 1 |
(* Library module defined by the International Standard |
| 2 |
Information technology - programming languages |
| 3 |
BS ISO/IEC 10514-1:1996E Part 1: Modula-2, Base Language. |
| 4 |
|
| 5 |
Copyright ISO/IEC (International Organization for Standardization |
| 6 |
and International Electrotechnical Commission) 1996-2021. |
| 7 |
|
| 8 |
It may be freely copied for the purpose of implementation (see page |
| 9 |
707 of the Information technology - Programming languages Part 1: |
| 10 |
Modula-2, Base Language. BS ISO/IEC 10514-1:1996). *) |
| 11 |
|
| 12 |
DEFINITION MODULE Processes; |
| 13 |
|
| 14 |
(* This module allows concurrent algorithms to be expressed using |
| 15 |
processes. A process is a unit of a program that has the |
| 16 |
potential to run in parallel with other processes. |
| 17 |
*) |
| 18 |
|
| 19 |
IMPORT SYSTEM; |
| 20 |
|
| 21 |
TYPE |
| 22 |
ProcessId; (* Used to identify processes *) |
| 23 |
Parameter = SYSTEM.ADDRESS; (* Used to pass data between processes *) |
| 24 |
Body = PROC; (* Used as the type of a process body *) |
| 25 |
Urgency = INTEGER; (* Used by the internal scheduler *) |
| 26 |
Sources = CARDINAL; (* Used to identify event sources *) |
| 27 |
ProcessesExceptions = (* Exceptions raised by this module *) |
| 28 |
(passiveProgram, processError); |
| 29 |
|
| 30 |
(* The following procedures create processes and switch control between |
| 31 |
them. *) |
| 32 |
|
| 33 |
PROCEDURE Create (procBody: Body; extraSpace: CARDINAL; procUrg: Urgency; |
| 34 |
procParams: Parameter; VAR procId: ProcessId); |
| 35 |
(* Creates a new process with procBody as its body, and with urgency |
| 36 |
and parameters given by procUrg and procParams. At least as |
| 37 |
much workspace (in units of SYSTEM.LOC) as is specified by |
| 38 |
extraSpace is allocated to the process. |
| 39 |
An identity for the new process is returned in procId. |
| 40 |
The process is created in the passive state; it will not run |
| 41 |
until activated. |
| 42 |
*) |
| 43 |
|
| 44 |
PROCEDURE Start (procBody: Body; extraSpace: CARDINAL; procUrg: Urgency; |
| 45 |
procParams: Parameter; VAR procId: ProcessId); |
| 46 |
(* Creates a new process, with parameters as for Create. |
| 47 |
The process is created in the ready state; it is eligible to |
| 48 |
run immediately. |
| 49 |
*) |
| 50 |
|
| 51 |
PROCEDURE StopMe (); |
| 52 |
(* Terminates the calling process. |
| 53 |
The process must not be associated with a source of events. |
| 54 |
*) |
| 55 |
|
| 56 |
PROCEDURE SuspendMe (); |
| 57 |
(* Causes the calling process to enter the passive state. The |
| 58 |
procedure only returns when the calling process is again |
| 59 |
activated by another process. |
| 60 |
*) |
| 61 |
|
| 62 |
PROCEDURE Activate (procId: ProcessId); |
| 63 |
(* Causes the process identified by procId to enter the ready |
| 64 |
state, and thus to become eligible to run again. |
| 65 |
*) |
| 66 |
|
| 67 |
PROCEDURE SuspendMeAndActivate (procId: ProcessId); |
| 68 |
(* Executes an atomic sequence of SuspendMe() and |
| 69 |
Activate(procId). *) |
| 70 |
|
| 71 |
PROCEDURE Switch (procId: ProcessId; VAR info: Parameter); |
| 72 |
(* Causes the calling process to enter the passive state; the |
| 73 |
process identified by procId becomes the currently executing |
| 74 |
process. info is used to pass parameter information from the |
| 75 |
calling to the activated process. On return, info will |
| 76 |
contain information from the process that chooses to switch |
| 77 |
back to this one (or will be NIL if Activate or |
| 78 |
SuspendMeAndActivate are used instead of Switch). |
| 79 |
*) |
| 80 |
|
| 81 |
PROCEDURE Wait (); |
| 82 |
(* Causes the calling process to enter the waiting state. |
| 83 |
The procedure will return when the calling process is |
| 84 |
activated by another process, or when one of its associated |
| 85 |
eventSources has generated an event. |
| 86 |
*) |
| 87 |
|
| 88 |
(* The following procedures allow the association of processes |
| 89 |
with sources of external events. |
| 90 |
*) |
| 91 |
|
| 92 |
PROCEDURE Attach (eventSource: Sources); |
| 93 |
(* Associates the specified eventSource with the calling |
| 94 |
process. *) |
| 95 |
|
| 96 |
PROCEDURE Detach (eventSource: Sources); |
| 97 |
(* Dissociates the specified eventSource from the program. *) |
| 98 |
|
| 99 |
PROCEDURE IsAttached (eventSource: Sources): BOOLEAN; |
| 100 |
(* Returns TRUE if and only if the specified eventSource is |
| 101 |
currently associated with one of the processes of the |
| 102 |
program. |
| 103 |
*) |
| 104 |
|
| 105 |
PROCEDURE Handler (eventSource: Sources): ProcessId; |
| 106 |
(* Returns the identity of the process, if any, that is |
| 107 |
associated with the specified eventSource. |
| 108 |
*) |
| 109 |
|
| 110 |
(* The following procedures allow processes to obtain their |
| 111 |
identity, parameters, and urgency. |
| 112 |
*) |
| 113 |
|
| 114 |
PROCEDURE Me (): ProcessId; |
| 115 |
(* Returns the identity of the calling process (as assigned |
| 116 |
when the process was first created). |
| 117 |
*) |
| 118 |
|
| 119 |
PROCEDURE MyParam (): Parameter; |
| 120 |
(* Returns the value specified as procParams when the calling |
| 121 |
process was created. *) |
| 122 |
|
| 123 |
PROCEDURE UrgencyOf (procId: ProcessId): Urgency; |
| 124 |
(* Returns the urgency established when the process identified |
| 125 |
by procId was first created. |
| 126 |
*) |
| 127 |
|
| 128 |
(* The following procedure provides facilities for exception |
| 129 |
handlers. *) |
| 130 |
|
| 131 |
PROCEDURE ProcessesException (): ProcessesExceptions; |
| 132 |
(* If the current coroutine is in the exceptional execution state |
| 133 |
because of the raising of a language exception, returns the |
| 134 |
corresponding enumeration value, and otherwise raises an |
| 135 |
exception. |
| 136 |
*) |
| 137 |
|
| 138 |
PROCEDURE IsProcessesException (): BOOLEAN; |
| 139 |
(* Returns TRUE if the current coroutine is in the exceptional |
| 140 |
execution state because of the raising of an exception in |
| 141 |
a routine from this module; otherwise returns FALSE. |
| 142 |
*) |
| 143 |
|
| 144 |
(* |
| 145 |
Reschedule - rotates the ready queue and transfers to the process |
| 146 |
with the highest run priority. |
| 147 |
*) |
| 148 |
|
| 149 |
PROCEDURE Reschedule ; |
| 150 |
|
| 151 |
|
| 152 |
(* |
| 153 |
displayProcesses - |
| 154 |
*) |
| 155 |
|
| 156 |
PROCEDURE displayProcesses (message: ARRAY OF CHAR) ; |
| 157 |
|
| 158 |
|
| 159 |
END Processes. |