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