1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
"======================================================================
|
| Test process operations
|
|
======================================================================"
"======================================================================
|
| Copyright (C) 1999, 2002, 2003, 2007, 2008 Free Software Foundation.
| Written by Paolo Bonzini
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING. If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
======================================================================"
Process extend [
executeUntilTermination [
self isTerminated ifTrue: [ ^self ].
self isActive ifFalse: [ self resume ].
[ self isTerminated ] whileFalse: [ Processor yield ]
]
ensureTermination [
self terminate; executeUntilTermination
]
]
"Test resuming/terminating a process"
Eval [
p := [ 'inside p' printNl ] newProcess name: 'test 1'; yourself.
p printNl.
p executeUntilTermination.
p printNl
]
"Test Process suspend/resume"
Eval [
goOn := false.
p := [
'inside p' printNl.
goOn := true.
p suspend.
'suspension finished' printNl ] newProcess name: 'test 2'; yourself.
p printNl.
p resume.
[ goOn ] whileFalse: [ Processor yield ].
p printNl.
p executeUntilTermination.
p printNl
]
"Test processes yielding control to each other without suspending themselves"
Eval [
goOn := false.
p := [
'inside p' printNl.
goOn := true.
Processor yield.
'yielded back to p' printNl ] newProcess name: 'test 3'; yourself.
p printNl.
p resume.
[ goOn ] whileFalse: [ Processor yield ].
p printNl.
p executeUntilTermination.
p printNl
]
"Test simple wait on a semaphore"
Eval [
s := Semaphore new.
p := [
'inside p' printNl.
s wait.
'wait finished' printNl ] newProcess name: 'test 4'; yourself.
p printNl.
p resume.
[ s size = 0 ] whileTrue: [ Processor yield ].
p printNl.
s signal.
p printNl
]
"Now test process interrupts"
Eval [
s := Semaphore new.
([ [ false ] whileFalse: [ Processor yield ] ]
forkAt: Processor userBackgroundPriority)
name: 'background';
queueInterrupt: [ (p := Processor activeProcess) printNl. s signal ].
s wait.
p printNl.
p ensureTermination.
p printNl
]
"Now interrupt a sleeping process"
Eval [
s := Semaphore new.
([ 'should go back to sleep' printNl ] newProcess)
priority: Processor userInterruptPriority;
name: 'interrupted';
queueInterrupt: [ (p := Processor activeProcess) printNl. s signal ].
s wait.
p printNl.
p ensureTermination.
p printNl
]
"Resume a process and check that it is removed from the semaphore"
Eval [
| p1 p2 s p1ok p2ok |
s := Semaphore new.
p1 := [ [ s wait ] ensure: [ p1ok := true ] ] fork.
p2 := [ [ s wait ] ensure: [ p2ok := true ] ] fork.
[ s size = 2 ] whileFalse: [ Processor yield ].
p2 resume.
s signal.
p1 ensureTermination.
^p1ok & p2ok & s size = 0
]
Eval [
| p1 p2 s p1ok p2ok |
s := Semaphore new.
p1 := [ [ s wait ] ensure: [ p1ok := true ] ] fork.
p2 := [ [ s wait ] ensure: [ p2ok := true ] ] fork.
[ s size = 2 ] whileFalse: [ Processor yield ].
p1 resume.
s signal.
p2 ensureTermination.
^p1ok & p2ok & s size = 0
]
"Terminate a process and check that #ensure: blocks are evaluated"
Eval [
dummy := Semaphore new.
s := Semaphore new.
p1 := [ [ dummy wait ] ensure: [ s signal ] ] fork.
p2 := [ [ dummy wait ] ensure: [ s signal ] ] fork.
p1 ensureTermination.
p2 ensureTermination.
s wait.
s wait.
^s size = 0
]
Eval [
dummy := Semaphore new.
s := Semaphore new.
p1 := [
[
Processor activeProcess priority: Processor userBackgroundPriority.
dummy wait
] ensure: [ s signal ]
] fork.
p2 := [
[
Processor activeProcess priority: Processor userBackgroundPriority.
dummy wait
] ensure: [ s signal ]
] fork.
p1 ensureTermination.
p2 ensureTermination.
s wait.
s wait.
^s size = 0
]
Eval [
"A semaphore that has just left the wait in Semaphore>>critical:
should signal the associated semaphore before leaving."
| s p |
s := Semaphore new.
p := [s critical:[]] forkAt: Processor activePriority - 1.
"Wait until p entered the critical section"
[p isWaiting] whileFalse: [Processor yield].
"Now that p entered it, signal the semaphore. p now 'owns' the semaphore
but since we are running at higher priority than p it will not get to do
anything."
s signal.
p ensureTermination.
^s signals = 1
]
Eval [
"A process that has entered the wait in Semaphore>>critical:,
but never obtains the semaphore, should leave it without
signaling the semaphore."
| s p |
s := Semaphore new.
p := [s critical:[]. 'a' printNl] fork.
[p isWaiting] whileFalse: [Processor yield].
p ensureTermination.
^s signals = 0
]
"Test that processes with the same priority are executed fairly. See
http://permalink.gmane.org/gmane.comp.lang.smalltalk.squeak.general/122772
for a proposed patch to Squeak that would break this testcase.
The two producer processes would ping-pong control to each other,
and the delay won't even be started."
Eval [
| queue stop s |
queue := SharedQueue new.
stop := false.
s := Semaphore new.
[ s signal.
[ stop ] whileFalse: [ queue nextPut: true. Processor yield ] ] fork.
s wait.
[ (Delay forMilliseconds: 500) wait. stop := true ] fork.
[ stop ] whileFalse: [ queue nextPut: false. Processor yield ].
]
"Test ProcessEnvironment and ProcessVariable"
Eval [
"Value defaults to nil"
b := Processor processEnvironment associationAt: #a.
b value printNl.
"#at:put: affects #value"
Processor processEnvironment at: #a put: 1.
b value printNl.
"and #value: affects #at:"
b value: 2.
(Processor processEnvironment at: #a) printNl.
s := Semaphore new.
[
"Value defaults to nil here too."
b value printNl.
"Requesting value has not created the variable."
Processor processEnvironment at: #a ifAbsentPut: [3].
b value printNl.
s signal
] fork.
s wait.
"The variable exists here..."
Processor processEnvironment at: #a ifAbsentPut: [4].
"... and its value is still 2."
(Processor processEnvironment at: #a) printNl.
b value printNl
]
"Test that CallinProcesses can be terminated softly"
Eval [
[ [ Processor activeProcess terminate ] ensure: [ '... ' display ] ]
on: SystemExceptions.ProcessBeingTerminated
do: [ :sig | 'nothing should follow' display. sig pass ].
'failed' displayNl
]
|