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.
5 Copyright ISO/IEC (International Organization for Standardization
6 and International Electrotechnical Commission) 1996-2021.
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). *)
12 DEFINITION MODULE COROUTINES
;
14 (* Facilities for coroutines and the handling of interrupts *)
20 UnassignedPriority
= 0 ;
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] ;
29 PROCEDURE NEWCOROUTINE (procBody
: PROC;
30 workspace
: SYSTEM.ADDRESS
;
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.
39 The optarg, initProtection, may contain a single parameter which
40 specifies the initial protection level of the coroutine.
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.
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.
57 PROCEDURE ATTACH (source
: INTERRUPTSOURCE
);
58 (* Associates the specified source of interrupts with the calling
61 PROCEDURE DETACH (source
: INTERRUPTSOURCE
);
62 (* Dissociates the specified source of interrupts from the calling
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.
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) =
76 PROCEDURE CURRENT (): COROUTINE
;
77 (* Returns the identity of the calling coroutine. *)
79 PROCEDURE LISTEN (p
: PROTECTION
);
80 (* Momentarily changes the protection of the calling coroutine to
83 PROCEDURE PROT (): PROTECTION
;
84 (* Returns the protection of the calling coroutine. *)
88 TurnInterrupts - switches processor interrupts to the protection
89 level, to. It returns the old value.
92 PROCEDURE TurnInterrupts (to
: PROTECTION
) : PROTECTION
;
96 ListenLoop - should be called instead of users writing:
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).
109 PROCEDURE ListenLoop
;