| 1 |
(* concurrentstore.mod a concurrent test for ALLOCATE/DEALLOCATE. |
| 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 concurrentstore ; |
| 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 Processes IMPORT Wait, Attach, Create, ProcessId, Urgency, Activate ; |
| 35 |
FROM RTint IMPORT InitTimeVector, ReArmTimeVector, IncludeVector, ExcludeVector ; |
| 36 |
FROM COROUTINES IMPORT TurnInterrupts, PROTECTION ; |
| 37 |
|
| 38 |
|
| 39 |
CONST |
| 40 |
maxProcesses = 5 ; |
| 41 |
maxStorageItems = 10 ; |
| 42 |
stackSpace = 1 * 1024 * 1024 ; |
| 43 |
maxTests = 10 ; |
| 44 |
|
| 45 |
VAR |
| 46 |
processes: ARRAY [1..maxProcesses] OF ProcessId ; |
| 47 |
heap : ARRAY [1..maxProcesses] OF ARRAY [1..maxStorageItems] OF ADDRESS ; |
| 48 |
|
| 49 |
|
| 50 |
(* |
| 51 |
createThreads - |
| 52 |
*) |
| 53 |
|
| 54 |
PROCEDURE createThreads ; |
| 55 |
VAR |
| 56 |
t: INTEGER ; |
| 57 |
BEGIN |
| 58 |
FOR t := 1 TO maxProcesses DO |
| 59 |
Create (stressAllocation, stackSpace, -1, NIL, processes[t]) |
| 60 |
END |
| 61 |
END createThreads ; |
| 62 |
|
| 63 |
|
| 64 |
(* |
| 65 |
stressAllocation - |
| 66 |
*) |
| 67 |
|
| 68 |
PROCEDURE stressAllocation ; |
| 69 |
VAR |
| 70 |
looping, |
| 71 |
count, |
| 72 |
a, tid : CARDINAL ; |
| 73 |
BEGIN |
| 74 |
tid := Find (Me ()) ; |
| 75 |
count := 0 ; |
| 76 |
looping := 0 ; |
| 77 |
LOOP |
| 78 |
FOR a := 1 TO maxStorageItems DO |
| 79 |
ALLOCATE (heap[tid][a], a) |
| 80 |
END ; |
| 81 |
FOR a := 1 TO maxStorageItems DO |
| 82 |
DEALLOCATE (heap[tid][a], a) |
| 83 |
END ; |
| 84 |
INC (count) ; |
| 85 |
(* Reschedule ; *) |
| 86 |
IF count = maxTests |
| 87 |
THEN |
| 88 |
printf ("process %d completed %d allocate/deallocates\n", |
| 89 |
tid, count * maxTests) ; |
| 90 |
count := 0 ; |
| 91 |
INC (looping) ; |
| 92 |
IF looping = 10 |
| 93 |
THEN |
| 94 |
printf ("this thread is complete\n") ; |
| 95 |
SuspendMe |
| 96 |
END |
| 97 |
END |
| 98 |
END |
| 99 |
END stressAllocation ; |
| 100 |
|
| 101 |
|
| 102 |
(* |
| 103 |
Find - |
| 104 |
*) |
| 105 |
|
| 106 |
PROCEDURE Find (id: ProcessId) : CARDINAL ; |
| 107 |
VAR |
| 108 |
c: CARDINAL ; |
| 109 |
BEGIN |
| 110 |
FOR c := 1 TO maxProcesses DO |
| 111 |
IF processes[c] = id |
| 112 |
THEN |
| 113 |
RETURN c |
| 114 |
END |
| 115 |
END ; |
| 116 |
HALT |
| 117 |
END Find ; |
| 118 |
|
| 119 |
|
| 120 |
(* |
| 121 |
timedWait - |
| 122 |
*) |
| 123 |
|
| 124 |
PROCEDURE timedWait ; |
| 125 |
VAR |
| 126 |
old: PROTECTION ; |
| 127 |
vec: CARDINAL ; |
| 128 |
BEGIN |
| 129 |
printf ("timedWait for 5 seconds\n"); |
| 130 |
old := TurnInterrupts (MAX (PROTECTION)) ; |
| 131 |
vec := InitTimeVector (5, 5, MAX (PROTECTION)) ; |
| 132 |
Attach (vec) ; (* attach vector to this process. *) |
| 133 |
IncludeVector (vec) ; |
| 134 |
(* ReArmTimeVector (vec, 3, 3) ; (* 10 seconds. *) *) |
| 135 |
printf ("main process is now going Wait for 5 seconds\n"); |
| 136 |
Wait ; |
| 137 |
printf ("yes, 5 seconds has elapsed\n"); |
| 138 |
END timedWait ; |
| 139 |
|
| 140 |
|
| 141 |
PROCEDURE parallelRun ; |
| 142 |
VAR |
| 143 |
c: CARDINAL ; |
| 144 |
BEGIN |
| 145 |
printf ("initPreemptive\n") ; |
| 146 |
initPreemptive (2, 0) ; |
| 147 |
printf ("after initPreemptive\n") ; |
| 148 |
|
| 149 |
FOR c := 1 TO maxProcesses DO |
| 150 |
Activate (processes[c]) |
| 151 |
END ; |
| 152 |
timedWait ; |
| 153 |
printf ("all complete\n") ; |
| 154 |
END parallelRun ; |
| 155 |
|
| 156 |
|
| 157 |
BEGIN |
| 158 |
printf ("starting concurrentstore test\n") ; |
| 159 |
createThreads ; |
| 160 |
printf ("running all threads\n") ; |
| 161 |
parallelRun |
| 162 |
END concurrentstore. |