author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:28:41 +0100 | |
changeset 530 | 07d0bce293c9 |
parent 399 | c15cfaf3ed4d |
child 532 | 2511c99de912 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Link subclass:#Process |
|
159 | 14 |
instanceVariableNames:'id prio state startBlock name |
15 |
restartable interruptActions |
|
375 | 16 |
exitActions suspendSemaphore |
217 | 17 |
singleStepping emergencySignalHandler' |
302 | 18 |
classVariableNames:'TerminateSignal CoughtSignals' |
159 | 19 |
poolDictionaries:'' |
20 |
category:'Kernel-Processes' |
|
1 | 21 |
! |
22 |
||
88 | 23 |
!Process class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
version |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
40 |
^ '$Header: /cvs/stx/stx/libbasic/Process.st,v 1.34 1995-11-11 15:26:05 cg Exp $' |
88 | 41 |
! |
42 |
||
43 |
documentation |
|
44 |
" |
|
45 |
Instances of Process represent lightweight smalltalk processes |
|
46 |
(i.e. threads). These all run in a shared smalltalk/X address space, |
|
93 | 47 |
and can thus access and communicate via any objects. |
48 |
Do not confuse these with (heavy-weight) unix processes, which are |
|
49 |
created differently, and do NOT run in the same address space. |
|
50 |
Also notice, that heavy-weight process creation takes much longer. |
|
51 |
(see OperatingSystemclass>>fork). |
|
52 |
||
202 | 53 |
Processes are typically created by sending #fork or #forkAt: to a block; |
54 |
the block creates a new process, defines itself as its startBlock, |
|
55 |
and (optionally) tells the Processor about the new process. |
|
56 |
||
93 | 57 |
Scheduling is done by Processor, which is the sole instance of |
88 | 58 |
ProcessorScheduler. |
93 | 59 |
|
202 | 60 |
Processes can be terminated either soft or via a hardTerminate. |
217 | 61 |
|
202 | 62 |
A soft terminate (see Process>>terminate) will raise a TerminationSignal |
217 | 63 |
in the process, which can be handled by the process. |
64 |
If no other handler was specified, the processes own handler |
|
65 |
(see Process>>start) will catch the signal and terminate the process. |
|
66 |
During this signal processing, normal unwind processing takes place, |
|
67 |
this means that with a soft terminate, all valueOnUnwind:/valueNowOrOnUnwind: |
|
68 |
cleanup blocks are evaluated. |
|
202 | 69 |
(so a process which has set up those blocks correctly does not have to |
70 |
care especially about cleanup in case of termination). |
|
217 | 71 |
Other than that, the TerminateSignal can be cought for special cleanup or |
72 |
even to make the process continue execution. |
|
202 | 73 |
|
74 |
A hard terminate (Process>>terminateNoSignal) will NOT do all of the above, |
|
75 |
but quickly (and without any cleanup) terminate the process. |
|
76 |
The debugger offers a quickTerminate option on its popupMenu for |
|
77 |
situations, when soft termination fails. (for example, if some error was |
|
78 |
coded into a handler or unwind block). |
|
79 |
||
80 |
Notice: |
|
81 |
in Smalltalk/X, processes are gone, when an image is restarted; |
|
82 |
this means, that you have to take care of process re-creation yourself. |
|
83 |
Usually, this is done by depending on ObjectMemory, recreating the |
|
375 | 84 |
process(es) when the #returnFromSnapshot-change notifiction arrives. |
202 | 85 |
|
86 |
All views (actually windowGroups) recreate their window process |
|
87 |
on image-restart. You have to do so manually for your own processes. |
|
93 | 88 |
|
89 |
A later version will allow specification of automatic restart, but |
|
217 | 90 |
thats not yet implemented. However, even when implemented, restartable processes |
91 |
will be recreated to restart from the beginning. It will not be possible to |
|
92 |
automatically continue a processes execution where it left off. |
|
93 |
This is a consequence of the portable implementation of ST/X, since in order to |
|
375 | 94 |
implement process continuation, the machines stack had to be preserved and |
95 |
recreated. Although this is possible to do (and actually not too complicated), |
|
96 |
this has not been implemented, since the machines stack layout is highly machine/compiler |
|
97 |
dependent, thus leading to much bigger porting effort of ST/X (which conflicts |
|
98 |
with ST/X's design goal of being highly portable). |
|
217 | 99 |
|
100 |
Process synchronization: |
|
375 | 101 |
Synchronization with cooperating processes is supported as usual, |
102 |
via Semaphores (see Semaphore, Delay, SharedQueue etc.) |
|
103 |
||
104 |
With uncooperative processes, only synchronization on suspend |
|
105 |
and termination is possible: |
|
106 |
any other process can wait for a process to suspend or terminate. |
|
107 |
This waiting is implemented by using suspendSemaphore and exitBlocks |
|
108 |
(where an exitSemaphore is signalled). |
|
109 |
See waitUntilSuspended / waitUntilTerminated. |
|
93 | 110 |
|
111 |
Instance variables: |
|
112 |
||
159 | 113 |
id <SmallInteger> a unique process-id |
202 | 114 |
|
159 | 115 |
prio <SmallInteger> the processes priority |
202 | 116 |
|
159 | 117 |
state <Symbol> the processes state |
118 |
(for process-monitor) |
|
202 | 119 |
|
120 |
startBlock <Block> the startup-block (the one that forked) |
|
121 |
||
159 | 122 |
name <String-or-nil> the processes name (if any) |
123 |
(for process-monitor) |
|
202 | 124 |
|
217 | 125 |
suspendSemaphore <Semaphore> triggered when suspend (if nonNil) |
126 |
||
159 | 127 |
restartable <Boolean> is restartable (not yet implemented) |
202 | 128 |
|
159 | 129 |
interruptActions <Collection> interrupt actions as defined by interruptWith:, |
130 |
performed at interrupt time |
|
202 | 131 |
|
375 | 132 |
exitActions <Collection of Block> |
133 |
additional cleanup actions to perform |
|
134 |
on termination (if nonEmpty) |
|
202 | 135 |
|
159 | 136 |
emergencySignalHandler <Block> can be used for per-process |
137 |
emergency signal handling |
|
138 |
Class variables: |
|
139 |
||
140 |
TerminateSignal <Signal> signal used to terminate processes |
|
141 |
(should not be cought - or at least |
|
142 |
rejected in handlers) |
|
88 | 143 |
" |
213 | 144 |
! |
145 |
||
146 |
examples |
|
147 |
" |
|
148 |
start a background process, computing 1000 factorial 100 times; |
|
149 |
the processes priority is set to not disturb any interactive process. |
|
150 |
Since its prio is higher than the 3D animation demos prio, you will notice, |
|
151 |
that those are suspended while the computation runs. But Interactive views |
|
152 |
(like browsers) will continue to react normal. |
|
153 |
||
154 |
[ |
|
155 |
'starting' printNL. |
|
156 |
100 timesRepeat:[1000 factorial]. |
|
157 |
'done with factorial' printNL. |
|
158 |
] forkAt:(Processor userBackgroundPriority). |
|
159 |
||
160 |
||
161 |
start a background process, computing 1000 factorial 100 times; |
|
162 |
the processes priority is set to not disturb any process. |
|
163 |
The prio is set to 1 (which is the lowest possible) notice that now, |
|
164 |
the 3D animation demos also continue to run. |
|
165 |
||
166 |
[ |
|
167 |
'starting' printNL. |
|
168 |
100 timesRepeat:[1000 factorial]. |
|
169 |
'done with factorial' printNL. |
|
170 |
] forkAt:1. |
|
171 |
||
172 |
||
173 |
start a background process, reading a pipe without blocking other |
|
174 |
processes; |
|
175 |
||
176 |
[ |
|
177 |
|p| |
|
178 |
||
179 |
'starting' printNL. |
|
180 |
p := PipeStream readingFrom:'ls -lR .'. |
|
181 |
[p atEnd] whileFalse:[ |
|
182 |
p readWait. |
|
183 |
p nextLine printNL. |
|
184 |
]. |
|
185 |
p close. |
|
186 |
'done with pipe' printNL. |
|
187 |
] forkAt:1. |
|
188 |
||
189 |
see more examples in doc/coding |
|
190 |
(or search in the browser for senders of fork*) |
|
191 |
" |
|
88 | 192 |
! ! |
1 | 193 |
|
159 | 194 |
!Process class methodsFor:'initialization'! |
195 |
||
196 |
initialize |
|
197 |
TerminateSignal isNil ifTrue:[ |
|
198 |
TerminateSignal := Signal new mayProceed:true. |
|
199 |
TerminateSignal nameClass:self message:#terminateSignal. |
|
200 |
TerminateSignal notifierString:'unhandled process termination'. |
|
302 | 201 |
|
202 |
CoughtSignals := SignalSet with:AbortSignal with:TerminateSignal. |
|
159 | 203 |
] |
204 |
! ! |
|
205 |
||
345 | 206 |
!Process class methodsFor:'Signal constants'! |
159 | 207 |
|
208 |
terminateSignal |
|
202 | 209 |
"return the signal used for process termination" |
210 |
||
159 | 211 |
^ TerminateSignal |
212 |
! ! |
|
213 |
||
214 |
!Process class methodsFor:'instance creation'! |
|
215 |
||
216 |
for:aBlock priority:aPrio |
|
217 |
"create a new (unscheduled) process which will execute aBlock at |
|
218 |
a given priority, once scheduled. The process will start execution once |
|
219 |
it gets a #resume-message." |
|
220 |
||
221 |
^ self new for:aBlock priority:aPrio |
|
222 |
! ! |
|
223 |
||
224 |
!Process methodsFor:'private'! |
|
225 |
||
226 |
for:aBlock priority:aPrio |
|
227 |
"setup the new process - the receiver is not scheduled for |
|
228 |
execution, to get it running, send it #resume" |
|
229 |
||
339 | 230 |
|nm active| |
159 | 231 |
|
232 |
prio := aPrio. |
|
233 |
startBlock := aBlock. |
|
339 | 234 |
restartable := false. |
159 | 235 |
|
236 |
(Processor newProcessFor:self) ifFalse:[ |
|
237 |
"for some reason, the Processor was unable to create |
|
238 |
a VM process for me ...." |
|
239 |
^ nil |
|
240 |
]. |
|
241 |
||
242 |
" |
|
243 |
give me a user-friendly name |
|
244 |
" |
|
339 | 245 |
active := Processor activeProcess. |
246 |
(nm := active name) notNil ifTrue:[ |
|
159 | 247 |
" |
248 |
avoid name inflation |
|
249 |
" |
|
314 | 250 |
(nm endsWith:' sub') ifFalse:[ |
339 | 251 |
nm := nm , ' [' , active id printString , '] sub' |
159 | 252 |
]. |
253 |
name := nm |
|
254 |
] |
|
255 |
! ! |
|
256 |
||
1 | 257 |
!Process methodsFor:'accessing'! |
258 |
||
259 |
state |
|
13 | 260 |
"return a symbol describing the processes state" |
261 |
||
1 | 262 |
^ state |
263 |
! |
|
264 |
||
265 |
state:aSymbol |
|
13 | 266 |
"set the state - only to be used from scheduler" |
267 |
||
1 | 268 |
state := aSymbol |
269 |
! |
|
270 |
||
182 | 271 |
isDead |
272 |
"return true, if the receiver has already terminated" |
|
273 |
||
274 |
^ state == #dead |
|
275 |
! |
|
276 |
||
159 | 277 |
startBlock |
278 |
"return the processes startup-block" |
|
69 | 279 |
|
159 | 280 |
^ startBlock |
1 | 281 |
! |
282 |
||
116 | 283 |
emergencySignalHandler:aOneArgBlock |
284 |
"set the emergencySignalHandler block. |
|
285 |
See Signal>>documentation for more info." |
|
286 |
||
287 |
emergencySignalHandler := aOneArgBlock |
|
288 |
! |
|
289 |
||
290 |
emergencySignalHandler |
|
291 |
"return the emergencySignalHandler block. |
|
292 |
See Signal>>documentation for more info." |
|
293 |
||
294 |
^ emergencySignalHandler |
|
295 |
! |
|
296 |
||
1 | 297 |
priority |
298 |
"return the receivers priority" |
|
299 |
||
300 |
^ prio |
|
301 |
! |
|
302 |
||
303 |
priority:aNumber |
|
304 |
"set my priority" |
|
305 |
||
116 | 306 |
Processor changePriority:aNumber for:self. |
307 |
! |
|
308 |
||
329 | 309 |
isRestartable |
310 |
"return true, iff the receiver is restartable" |
|
311 |
||
312 |
^ restartable |
|
313 |
! |
|
314 |
||
315 |
restartable:aBoolean |
|
316 |
"set/clear, the restartable flag. |
|
317 |
Restartable processes will automatically be restarted by the |
|
318 |
ProcessorScheduler upon image restart. Others have to be restarted |
|
319 |
manually." |
|
320 |
||
337 | 321 |
startBlock isNil ifTrue:[ |
322 |
self error:'cannot be made restartable when already started'. |
|
323 |
^ self |
|
324 |
]. |
|
329 | 325 |
restartable := aBoolean |
326 |
! |
|
327 |
||
116 | 328 |
changePriority:aNumber |
329 |
"same as priority:, but returns the old priority. |
|
330 |
(cannot do this in priority: for ST-80 compatibility)" |
|
331 |
||
332 |
|oldPrio| |
|
333 |
||
334 |
oldPrio := prio. |
|
335 |
Processor changePriority:aNumber for:self. |
|
336 |
^ oldPrio |
|
1 | 337 |
! |
338 |
||
93 | 339 |
isSingleStepping |
340 |
^ singleStepping |
|
341 |
! |
|
342 |
||
343 |
singleStep:aBoolean |
|
344 |
singleStepping := aBoolean |
|
345 |
! |
|
346 |
||
1 | 347 |
id |
10 | 348 |
"return the processes id" |
349 |
||
1 | 350 |
^ id |
351 |
! |
|
352 |
||
13 | 353 |
name |
354 |
"return the processes name" |
|
355 |
||
356 |
^ name |
|
357 |
! |
|
358 |
||
359 |
name:aString |
|
360 |
"set the processes name" |
|
361 |
||
362 |
name := aString |
|
363 |
! |
|
364 |
||
365 |
nameOrId |
|
366 |
"return a string to identify the process - either name or id" |
|
367 |
||
368 |
name notNil ifTrue:[^ name]. |
|
369 |
^ id printString |
|
370 |
! |
|
371 |
||
372 |
exitAction:aBlock |
|
375 | 373 |
"add aBlock to the processes exit actions. |
182 | 374 |
This will be evaluated right before the process dies." |
13 | 375 |
|
375 | 376 |
exitActions isNil ifTrue:[ |
377 |
exitActions := OrderedCollection new |
|
378 |
]. |
|
379 |
exitActions add:aBlock |
|
1 | 380 |
! |
381 |
||
382 |
suspendedContext |
|
182 | 383 |
"return the processes suspended context |
384 |
- this is the context from which a process switch into the scheduler |
|
385 |
or another process occured. |
|
386 |
Typically, only the debugger is interrested in this one." |
|
13 | 387 |
|
388 |
%{ /* NOCONTEXT */ |
|
1 | 389 |
extern OBJ __threadContext(); |
69 | 390 |
OBJ i; |
1 | 391 |
|
252 | 392 |
if (__isSmallInteger(i = _INST(id))) { |
159 | 393 |
RETURN (__threadContext(_intVal(i))); |
13 | 394 |
} |
182 | 395 |
%}. |
69 | 396 |
^ nil |
397 |
! |
|
398 |
||
399 |
maximumStackSize |
|
93 | 400 |
"returns the processes stack limit - i.e. the process will be |
69 | 401 |
interrupted with a recursionSignal-raise, if it ever |
402 |
needs more stack (in bytes) than this number" |
|
403 |
||
404 |
%{ /* NOCONTEXT */ |
|
405 |
extern int __threadMaxStackSize(); |
|
406 |
OBJ i; |
|
13 | 407 |
|
252 | 408 |
if (__isSmallInteger(i = _INST(id))) { |
159 | 409 |
RETURN( _MKSMALLINT(__threadMaxStackSize(_intVal(i))) ); |
69 | 410 |
} |
182 | 411 |
%}. |
69 | 412 |
^ nil |
413 |
! |
|
182 | 414 |
|
69 | 415 |
setMaximumStackSize:limit |
93 | 416 |
"sets the processes stack limit - i.e. the process will be |
69 | 417 |
interrupted with a recursionSignal-raise, if it ever |
418 |
needs more stack (in bytes) than this number. |
|
419 |
Returns the old value." |
|
420 |
||
421 |
%{ /* NOCONTEXT */ |
|
422 |
extern int __threadSetMaxStackSize(); |
|
423 |
OBJ i; |
|
424 |
||
252 | 425 |
if (__isSmallInteger(i = _INST(id)) |
426 |
&& __isSmallInteger(limit) ) { |
|
159 | 427 |
RETURN ( _MKSMALLINT(__threadSetMaxStackSize(_intVal(i), _intVal(limit))) ); |
13 | 428 |
} |
182 | 429 |
%}. |
430 |
^ nil |
|
431 |
! ! |
|
432 |
||
433 |
!Process methodsFor:'monitoring'! |
|
434 |
||
375 | 435 |
vmTrace:aBoolean |
436 |
"turn on/off VM message tracing for the receiver. |
|
399 | 437 |
This is meant for ST/X debugging, and may vanish. |
375 | 438 |
Expect lots of output, once this is turned on." |
439 |
||
440 |
%{ /* NOCONTEXT */ |
|
441 |
OBJ i; |
|
442 |
||
443 |
if (__isSmallInteger(i = _INST(id))) { |
|
444 |
__threadTracing(_intVal(i), aBoolean); |
|
445 |
} |
|
446 |
%}. |
|
447 |
! |
|
448 |
||
182 | 449 |
usedStackSize |
450 |
"Return the processes current stack size. |
|
451 |
This method is for monitoring purposes only - it may vanish." |
|
452 |
||
453 |
%{ /* NOCONTEXT */ |
|
454 |
extern int __threadUsedStackSize(); |
|
455 |
OBJ i; |
|
456 |
||
252 | 457 |
if (__isSmallInteger(i = _INST(id))) { |
182 | 458 |
RETURN( _MKSMALLINT(__threadUsedStackSize(_intVal(i))) ); |
459 |
} |
|
460 |
%}. |
|
461 |
^ nil |
|
462 |
! |
|
463 |
||
464 |
totalStackSize |
|
465 |
"return the processes maximum used stack size. |
|
466 |
This method is for monitoring purposes only - it may vanish." |
|
467 |
||
468 |
%{ /* NOCONTEXT */ |
|
469 |
extern int __threadTotalStackSize(); |
|
470 |
OBJ i; |
|
471 |
||
252 | 472 |
if (__isSmallInteger(i = _INST(id))) { |
182 | 473 |
RETURN( _MKSMALLINT(__threadTotalStackSize(_intVal(i))) ); |
474 |
} |
|
475 |
%}. |
|
476 |
^ nil |
|
477 |
! |
|
478 |
||
479 |
numberOfStackSegments |
|
480 |
"return the processes number of stack segments currently used. |
|
481 |
This method is for monitoring purposes only - it may vanish." |
|
482 |
||
483 |
%{ /* NOCONTEXT */ |
|
484 |
extern int __threadTotalStackSize(); |
|
485 |
OBJ i; |
|
486 |
||
252 | 487 |
if (__isSmallInteger(i = _INST(id))) { |
182 | 488 |
RETURN( _MKSMALLINT(__threadStackSegments(_intVal(i))) ); |
489 |
} |
|
490 |
%}. |
|
13 | 491 |
^ nil |
359 | 492 |
! |
493 |
||
494 |
numberOfStackBoundaryHits |
|
495 |
"internal monitoring only - will vanish" |
|
496 |
||
497 |
%{ /* NOCONTEXT */ |
|
498 |
extern int __threadNumberOfStackBoundaryHits(); |
|
499 |
int n; |
|
500 |
OBJ i; |
|
501 |
||
502 |
if (__isSmallInteger(i = _INST(id))) { |
|
375 | 503 |
n = __threadNumberOfStackBoundaryHits(_intVal(i)); |
359 | 504 |
n &= 0x3FFFFFFF; |
375 | 505 |
RETURN( _MKSMALLINT(n) ); |
359 | 506 |
} |
507 |
%}. |
|
508 |
^ nil |
|
13 | 509 |
! ! |
510 |
||
511 |
!Process methodsFor:'private scheduler access'! |
|
512 |
||
159 | 513 |
setPriority:aNumber |
514 |
"set priority without telling processor - not for public use" |
|
515 |
||
516 |
prio := aNumber |
|
517 |
! |
|
518 |
||
519 |
setStartBlock:aBlock |
|
520 |
"set the receivers startup block" |
|
521 |
||
522 |
startBlock := aBlock |
|
523 |
! |
|
13 | 524 |
|
159 | 525 |
setStateTo:newState if:oldState |
526 |
state == oldState ifTrue:[state := newState] |
|
527 |
! |
|
528 |
||
529 |
setStateTo:newState if:oldState1 or:oldState2 |
|
530 |
(state == oldState1 or:[state == oldState2]) ifTrue:[state := newState] |
|
531 |
! |
|
532 |
||
533 |
setId:idNumber state:stateSymbol |
|
534 |
"set id and state - not for public use" |
|
535 |
||
536 |
id := idNumber. |
|
537 |
state := stateSymbol. |
|
202 | 538 |
! ! |
539 |
||
540 |
!Process methodsFor:'startup '! |
|
116 | 541 |
|
542 |
start |
|
159 | 543 |
"start the process - this is sent by the VM to the process to get |
544 |
the process up and running. |
|
329 | 545 |
Sending #start to the process (instead of directly executing the startBlock) |
546 |
allows more flexible handling of processes, since anything that responds |
|
547 |
to #start can be handled transparently by the VM then ..." |
|
159 | 548 |
|
549 |
|block| |
|
116 | 550 |
|
159 | 551 |
(block := startBlock) notNil ifTrue:[ |
552 |
" |
|
553 |
just for your convenience ... |
|
554 |
" |
|
555 |
name isNil ifTrue:[ |
|
359 | 556 |
name := '(' , block displayString , ')' |
159 | 557 |
]. |
329 | 558 |
restartable ~~ true ifTrue:[startBlock := nil]. |
302 | 559 |
CoughtSignals handle:[:ex | |
159 | 560 |
ex return |
182 | 561 |
] do:block. |
202 | 562 |
|
375 | 563 |
self terminateNoSignal. |
159 | 564 |
] ifFalse:[ |
565 |
"is this artificial restriction useful ?" |
|
566 |
self error:'a process cannot be started twice' |
|
116 | 567 |
] |
337 | 568 |
! |
569 |
||
570 |
restart |
|
571 |
"restart the process from the beginning. |
|
572 |
This is sent by the ProcessorScheduler to all restartable processes." |
|
573 |
||
339 | 574 |
"/ ('restart process ' , id printString) errorPrintNL. |
575 |
||
576 |
(Processor newProcessFor:self withId:id) ifFalse:[ |
|
337 | 577 |
"for some reason, the Processor was unable to create |
578 |
a VM process for me ...." |
|
579 |
||
580 |
('process ' , id printString , ' failed to restart.') errorPrintNL. |
|
581 |
^ nil |
|
582 |
]. |
|
583 |
self resume |
|
1 | 584 |
! ! |
585 |
||
586 |
!Process methodsFor:'suspend / resume'! |
|
587 |
||
326 | 588 |
stop |
589 |
"suspend the receiver process - will continue to run when a resume is sent. |
|
590 |
A stopped process will not be resumed for interrupt processing." |
|
591 |
||
329 | 592 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signalForAll]. |
326 | 593 |
state := #stopped. |
594 |
Processor suspend:self |
|
595 |
! |
|
596 |
||
1 | 597 |
suspend |
326 | 598 |
"suspend the receiver process - will continue to run when a resume is sent. |
599 |
An interrupt will resume the receiver." |
|
13 | 600 |
|
329 | 601 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signalForAll]. |
1 | 602 |
Processor suspend:self |
603 |
! |
|
604 |
||
605 |
resume |
|
13 | 606 |
"resume the receiver process" |
10 | 607 |
|
1 | 608 |
Processor resume:self |
609 |
! |
|
610 |
||
93 | 611 |
resumeForSingleSend |
612 |
"resume the receiver process, but only let it execute a single send." |
|
613 |
||
614 |
Processor resumeForSingleSend:self |
|
615 |
! |
|
616 |
||
1 | 617 |
terminate |
375 | 618 |
"terminate the receiver process. Termination is done by raising |
619 |
the terminateSignal in the receiver process, which can be cought. |
|
620 |
All unwind actions and the exit-actions (if any) |
|
159 | 621 |
will be performed before the process is really terminated." |
10 | 622 |
|
159 | 623 |
Processor activeProcess == self ifTrue:[ |
624 |
Signal noHandlerSignal handle:[:ex | |
|
625 |
ex return. |
|
626 |
] do:[ |
|
627 |
TerminateSignal raise. |
|
628 |
]. |
|
375 | 629 |
self terminateNoSignal. |
159 | 630 |
] ifFalse:[ |
631 |
self interruptWith:[self terminate] |
|
632 |
] |
|
633 |
! |
|
634 |
||
635 |
terminateNoSignal |
|
375 | 636 |
"terminate the receiver process without sending a terminateSignal |
637 |
or performing any unwind-handling. |
|
638 |
However, exit-actions are performed." |
|
639 |
||
640 |
|block| |
|
159 | 641 |
|
375 | 642 |
exitActions notNil ifTrue:[ |
643 |
[exitActions notEmpty] whileTrue:[ |
|
644 |
block := exitActions removeFirst. |
|
645 |
block value. |
|
646 |
] |
|
647 |
]. |
|
329 | 648 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signalForAll]. |
159 | 649 |
Processor terminateNoSignal:self |
1 | 650 |
! ! |
651 |
||
159 | 652 |
!Process methodsFor:'interrupts'! |
24 | 653 |
|
654 |
interruptWith:aBlock |
|
655 |
"interrupt the receiver and make it evaluate aBlock. |
|
656 |
If the receiver is currently suspended, the block will be remembered |
|
93 | 657 |
to be evaluated once the receiver wakes up." |
658 |
||
159 | 659 |
self uninterruptablyDo:[ |
660 |
interruptActions isNil ifTrue:[ |
|
661 |
interruptActions := OrderedCollection with:aBlock. |
|
662 |
] ifFalse:[ |
|
663 |
interruptActions addLast:aBlock. |
|
664 |
]. |
|
93 | 665 |
]. |
666 |
Processor scheduleForInterrupt:self. |
|
667 |
! |
|
24 | 668 |
|
93 | 669 |
interrupt |
159 | 670 |
"evaluate my interrupt-actions |
93 | 671 |
the process will go back to where it got interrupted |
672 |
after doing this. |
|
673 |
" |
|
674 |
|action| |
|
675 |
||
676 |
[interruptActions notNil and:[interruptActions notEmpty]] whileTrue:[ |
|
159 | 677 |
action := interruptActions removeFirst. |
678 |
action value |
|
24 | 679 |
]. |
93 | 680 |
interruptActions := nil |
24 | 681 |
! ! |
159 | 682 |
|
683 |
!Process methodsFor:'special'! |
|
684 |
||
685 |
withPriority:aPrio do:aBlock |
|
686 |
"execute aBlock at another priority. This can be used to perform |
|
687 |
time-consuming operations at a more user-friendly priority, |
|
688 |
or some critical action at a higher priority. Do not use too high |
|
689 |
of a priority to avoid locking up the system (event processing takes place |
|
690 |
at 24)" |
|
691 |
||
692 |
|oldprio| |
|
693 |
||
694 |
oldprio := prio. |
|
695 |
self priority:aPrio. |
|
696 |
||
697 |
^ aBlock valueNowOrOnUnwindDo:[ |
|
698 |
self priority:oldprio |
|
699 |
] |
|
182 | 700 |
|
701 |
" |
|
702 |
Processor activeProcess withPriority:7 do:[3000 factorial] |
|
703 |
" |
|
704 |
"be careful - even ^C wont work until done: |
|
705 |
Processor activeProcess withPriority:25 do:[3000 factorial] |
|
706 |
" |
|
159 | 707 |
! |
708 |
||
709 |
withLowerPriorityDo:aBlock |
|
710 |
"execute aBlock at a lower priority. This can be used to perform |
|
711 |
time-consuming operations at a more user-friendly priority." |
|
712 |
||
713 |
^ self withPriority:(prio - 1) do:aBlock |
|
182 | 714 |
|
715 |
" |
|
716 |
Processor activeProcess withLowerPriorityDo:[3000 factorial] |
|
717 |
" |
|
159 | 718 |
! |
719 |
||
720 |
uninterruptablyDo:aBlock |
|
348 | 721 |
"execute aBlock with interrupts blocked. |
722 |
This does not prevent preemption by a higher priority processes |
|
723 |
if any becomes runnable due to the evaluation of aBlock |
|
724 |
(i.e. if a semaphore is signalled there)." |
|
159 | 725 |
|
726 |
|wasBlocked| |
|
727 |
||
728 |
"we must keep track of blocking-state if this is called nested" |
|
729 |
||
730 |
wasBlocked := OperatingSystem blockInterrupts. |
|
731 |
^ aBlock valueNowOrOnUnwindDo:[ |
|
182 | 732 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
733 |
0 "stc hint" |
|
159 | 734 |
] |
217 | 735 |
! |
736 |
||
737 |
waitUntilTerminated |
|
348 | 738 |
"wait until the receiver is terminated. |
739 |
This method allows another process to wait till the receiver finishes." |
|
329 | 740 |
|
375 | 741 |
|wasBlocked sema| |
217 | 742 |
|
329 | 743 |
wasBlocked := OperatingSystem blockInterrupts. |
375 | 744 |
sema := Semaphore new. |
745 |
self exitAction:[sema signal]. |
|
746 |
sema wait. |
|
329 | 747 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
217 | 748 |
! |
749 |
||
750 |
waitUntilSuspended |
|
348 | 751 |
"wait until the receiver is suspended." |
329 | 752 |
|
753 |
|wasBlocked| |
|
217 | 754 |
|
329 | 755 |
wasBlocked := OperatingSystem blockInterrupts. |
756 |
suspendSemaphore isNil ifTrue:[suspendSemaphore := Semaphore new]. |
|
217 | 757 |
suspendSemaphore wait |
329 | 758 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
159 | 759 |
! ! |
24 | 760 |
|
93 | 761 |
!Process methodsFor:'printing & storing'! |
1 | 762 |
|
59 | 763 |
printOn:aStream |
329 | 764 |
"a little more info in my printed representation" |
765 |
||
85 | 766 |
aStream nextPutAll:state article; |
159 | 767 |
space; |
768 |
nextPutAll:state; |
|
769 |
nextPutAll:' Process ('; |
|
770 |
nextPutAll:self nameOrId; |
|
771 |
nextPutAll:')' |
|
1 | 772 |
! ! |