| 1 |
(* contimer.mod a basic thread test for round robin scheduling. |
| 2 |
|
| 3 |
Copyright (C) 2020 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
MODULE contimer ; |
| 28 |
|
| 29 |
FROM Storage IMPORT ALLOCATE, DEALLOCATE ; |
| 30 |
FROM Processes IMPORT Create, Start, StopMe, SuspendMe, Activate, SuspendMeAndActivate, ProcessId, Me, Reschedule ; |
| 31 |
FROM libc IMPORT printf, exit ; |
| 32 |
FROM SYSTEM IMPORT ADDRESS ; |
| 33 |
FROM Preemptive IMPORT initPreemptive ; |
| 34 |
FROM COROUTINES IMPORT LISTEN, PROTECTION ; |
| 35 |
|
| 36 |
CONST |
| 37 |
maxProcesses = 10 ; |
| 38 |
stackSpace = 1 * 1024 * 1024 ; |
| 39 |
|
| 40 |
VAR |
| 41 |
processes: ARRAY [1..maxProcesses] OF ProcessId ; |
| 42 |
|
| 43 |
|
| 44 |
(* |
| 45 |
createThreads - |
| 46 |
*) |
| 47 |
|
| 48 |
PROCEDURE createThreads ; |
| 49 |
VAR |
| 50 |
t: INTEGER ; |
| 51 |
BEGIN |
| 52 |
FOR t := 1 TO maxProcesses DO |
| 53 |
Create (simpleProcess, stackSpace, -1, NIL, processes[t]) |
| 54 |
END |
| 55 |
END createThreads ; |
| 56 |
|
| 57 |
|
| 58 |
(* |
| 59 |
simpleProcess - |
| 60 |
*) |
| 61 |
|
| 62 |
PROCEDURE simpleProcess ; |
| 63 |
VAR |
| 64 |
count: CARDINAL ; |
| 65 |
BEGIN |
| 66 |
count := 0 ; |
| 67 |
LOOP |
| 68 |
printf ("%x: hello world\n", Me ()) ; |
| 69 |
(* LISTEN (MIN (PROTECTION)) ; *) |
| 70 |
Reschedule ; |
| 71 |
INC (count) ; |
| 72 |
IF count = 1000 |
| 73 |
THEN |
| 74 |
printf ("all done - finishing\n"); |
| 75 |
exit (0) |
| 76 |
END |
| 77 |
END |
| 78 |
END simpleProcess ; |
| 79 |
|
| 80 |
|
| 81 |
PROCEDURE parallelRun ; |
| 82 |
VAR |
| 83 |
c: CARDINAL ; |
| 84 |
BEGIN |
| 85 |
printf ("initPreemptive\n") ; |
| 86 |
initPreemptive (1, 0) ; |
| 87 |
printf ("after initPreemptive\n") ; |
| 88 |
FOR c := 1 TO maxProcesses DO |
| 89 |
Activate (processes[c]) |
| 90 |
END ; |
| 91 |
printf ("all complete\n") ; |
| 92 |
SuspendMe |
| 93 |
END parallelRun ; |
| 94 |
|
| 95 |
|
| 96 |
BEGIN |
| 97 |
printf ("starting contimer test\n") ; |
| 98 |
createThreads ; |
| 99 |
printf ("running all threads\n") ; |
| 100 |
parallelRun |
| 101 |
END contimer. |