2 (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 See the copyright notice in the ACK home directory, in the file "Copyright".
8 Author: Ceriel J.H. Jacobs
13 An implementation of the Modula-2 NEWPROCESS and TRANSFER facilities
14 using the topsize, topsave, and topload facilities.
15 For each coroutine, a proc structure is built. For the main routine,
16 a static space is declared to save its stack. For the other coroutines,
17 the user specifies this space.
25 unsigned size
; /* size of saved stackframe(s) */
26 int (*proc
)(); /* address of coroutine procedure */
27 char *brk
; /* stack break of this coroutine */
30 extern unsigned topsize();
32 static struct proc mainproc
[MAXMAIN
/sizeof(struct proc
) + 1];
34 static struct proc
*curproc
= 0;/* current coroutine */
35 extern char *MainLB
; /* stack break of main routine */
37 _SYSTEM__NEWPROCESS(p
, a
, n
, p1
)
38 int (*p
)(); /* coroutine procedure */
39 struct proc
*a
; /* pointer to area for saved stack-frame */
40 unsigned n
; /* size of this area */
41 struct proc
**p1
; /* where to leave coroutine descriptor,
42 in this implementation the address of
43 the area for saved stack-frame(s) */
45 /* This procedure creates a new coroutine, but does not
46 transfer control to it. The routine "topsize" will compute the
47 stack break, which will be the local base of this routine.
48 Notice that we can do this because we do not need the stack
49 above this point for this coroutine. In Modula-2, coroutines
50 must be level 0 procedures without parameters.
53 unsigned sz
= topsize(&brk
);
55 if (sz
+ sizeof(struct proc
) > n
) {
56 /* not enough space */
63 if (topsave(brk
, a
+1))
64 /* stack frame saved; now just return */
67 /* We get here through the first transfer to the coroutine
69 This also means that curproc is now set to this coroutine.
70 We cannot trust the parameters anymore.
71 Just call the coroutine procedure.
79 _SYSTEM__TRANSFER(a
, b
)
82 /* transfer from one coroutine to another, saving the current
83 descriptor in the space indicated by "a", and transfering to
84 the coroutine in descriptor "b".
89 /* the current coroutine is the main process;
90 initialize a coroutine descriptor for it ...
92 mainproc
[0].brk
= MainLB
;
93 mainproc
[0].size
= sizeof(mainproc
);
94 curproc
= &mainproc
[0];
96 *a
= curproc
; /* save current descriptor in "a" */
98 /* transfer to itself is a no-op */
101 size
= topsize(&(curproc
->brk
));
102 if (size
+ sizeof(struct proc
) > curproc
->size
) {
105 if (topsave(curproc
->brk
, curproc
+1)) {
106 /* stack top saved. Now restore context of target
111 /* we never get here ... */
113 /* but we do get here, when a transfer is done to the coroutine in "a".