Debian Codegraph @

gcc/testsuite/gm2/isocoroutines/run/pass/coroutine.mod

raw
1 (* Copyright (C) 2010 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with gm2; see the file COPYING. If not, write to the Free
16 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA. *)
18
19 MODULE coroutine ;
20
21 FROM SYSTEM IMPORT ADDRESS ;
22 FROM COROUTINES IMPORT COROUTINE, NEWCOROUTINE, PROTECTION, TRANSFER ;
23 FROM Storage IMPORT ALLOCATE ;
24 FROM libc IMPORT printf ;
25
26 CONST
27 Workspace = 32 * 1024 * 1024 ;
28
29
30 PROCEDURE first ;
31 VAR
32 x: CARDINAL ;
33 BEGIN
34 x := 0 ;
35 LOOP
36 printf ("c1 is alive and well\n") ;
37 IF x=1000
38 THEN
39 printf ("finished!\n") ;
40 TRANSFER (c1, mainc)
41 ELSE
42 TRANSFER (c1, c2)
43 END ;
44 INC (x)
45 END
46 END first ;
47
48
49 PROCEDURE second ;
50 BEGIN
51 LOOP
52 printf ("c2 is alive and well\n") ;
53 TRANSFER (c2, c1)
54 END
55 END second ;
56
57
58 VAR
59 mainc,
60 c1, c2: COROUTINE ;
61 w1, w2: ADDRESS ;
62 BEGIN
63 ALLOCATE (w1, Workspace) ;
64 NEWCOROUTINE (first, w1, Workspace, c1) ;
65 ALLOCATE (w2, Workspace) ;
66 NEWCOROUTINE (second, w2, Workspace, c2) ;
67 printf ("first context switch to c1\n") ;
68 TRANSFER (mainc, c1) ;
69 printf ("back to mainc and all done\n\n")
70 END coroutine.