| 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 COROUTINES; |
| 13 |
|
| 14 |
(* Facilities for coroutines and the handling of interrupts *) |
| 15 |
|
| 16 |
IMPORT SYSTEM ; |
| 17 |
|
| 18 |
|
| 19 |
CONST |
| 20 |
UnassignedPriority = 0 ; |
| 21 |
|
| 22 |
TYPE |
| 23 |
COROUTINE ; (* Values of this type are created dynamically by NEWCOROUTINE |
| 24 |
and identify the coroutine in subsequent operations *) |
| 25 |
INTERRUPTSOURCE = CARDINAL ; |
| 26 |
PROTECTION = [UnassignedPriority..7] ; |
| 27 |
|
| 28 |
|
| 29 |
PROCEDURE NEWCOROUTINE (procBody: PROC; |
| 30 |
workspace: SYSTEM.ADDRESS; |
| 31 |
size: CARDINAL; |
| 32 |
VAR cr: COROUTINE; |
| 33 |
[initProtection: PROTECTION = UnassignedPriority]); |
| 34 |
(* Creates a new coroutine whose body is given by procBody, and |
| 35 |
returns the identity of the coroutine in cr. workspace is a |
| 36 |
pointer to the work space allocated to the coroutine; size |
| 37 |
specifies the size of this workspace in terms of SYSTEM.LOC. |
| 38 |
|
| 39 |
The optarg, initProtection, may contain a single parameter which |
| 40 |
specifies the initial protection level of the coroutine. |
| 41 |
*) |
| 42 |
|
| 43 |
PROCEDURE TRANSFER (VAR from: COROUTINE; to: COROUTINE); |
| 44 |
(* Returns the identity of the calling coroutine in from, and |
| 45 |
transfers control to the coroutine specified by to. |
| 46 |
*) |
| 47 |
|
| 48 |
PROCEDURE IOTRANSFER (VAR from: COROUTINE; to: COROUTINE); |
| 49 |
(* Returns the identity of the calling coroutine in from and |
| 50 |
transfers control to the coroutine specified by to. On |
| 51 |
occurrence of an interrupt, associated with the caller, control |
| 52 |
is transferred back to the caller, and the identity of the |
| 53 |
interrupted coroutine is returned in from. The calling coroutine |
| 54 |
must be associated with a source of interrupts. |
| 55 |
*) |
| 56 |
|
| 57 |
PROCEDURE ATTACH (source: INTERRUPTSOURCE); |
| 58 |
(* Associates the specified source of interrupts with the calling |
| 59 |
coroutine. *) |
| 60 |
|
| 61 |
PROCEDURE DETACH (source: INTERRUPTSOURCE); |
| 62 |
(* Dissociates the specified source of interrupts from the calling |
| 63 |
coroutine. *) |
| 64 |
|
| 65 |
PROCEDURE IsATTACHED (source: INTERRUPTSOURCE): BOOLEAN; |
| 66 |
(* Returns TRUE if and only if the specified source of interrupts is |
| 67 |
currently associated with a coroutine; otherwise returns FALSE. |
| 68 |
*) |
| 69 |
|
| 70 |
PROCEDURE HANDLER (source: INTERRUPTSOURCE): COROUTINE; |
| 71 |
(* Returns the coroutine, if any, that is associated with the source |
| 72 |
of interrupts. The result is undefined if IsATTACHED(source) = |
| 73 |
FALSE. |
| 74 |
*) |
| 75 |
|
| 76 |
PROCEDURE CURRENT (): COROUTINE; |
| 77 |
(* Returns the identity of the calling coroutine. *) |
| 78 |
|
| 79 |
PROCEDURE LISTEN (p: PROTECTION); |
| 80 |
(* Momentarily changes the protection of the calling coroutine to |
| 81 |
p. *) |
| 82 |
|
| 83 |
PROCEDURE PROT (): PROTECTION; |
| 84 |
(* Returns the protection of the calling coroutine. *) |
| 85 |
|
| 86 |
|
| 87 |
(* |
| 88 |
TurnInterrupts - switches processor interrupts to the protection |
| 89 |
level, to. It returns the old value. |
| 90 |
*) |
| 91 |
|
| 92 |
PROCEDURE TurnInterrupts (to: PROTECTION) : PROTECTION ; |
| 93 |
|
| 94 |
|
| 95 |
(* |
| 96 |
ListenLoop - should be called instead of users writing: |
| 97 |
|
| 98 |
LOOP |
| 99 |
LISTEN |
| 100 |
END |
| 101 |
|
| 102 |
It performs the same function but yields |
| 103 |
control back to the underlying operating system. |
| 104 |
It also checks for deadlock. |
| 105 |
Note that this function does return when an interrupt occurs. |
| 106 |
(File descriptor becomes ready or time event expires). |
| 107 |
*) |
| 108 |
|
| 109 |
PROCEDURE ListenLoop ; |
| 110 |
|
| 111 |
|
| 112 |
END COROUTINES. |