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'
|
159
|
18 |
classVariableNames:'TerminateSignal'
|
|
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 |
|
252
|
27 |
$Header: /cvs/stx/stx/libbasic/Process.st,v 1.20 1995-02-15 10:24:43 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 |
"
|
252
|
48 |
$Header: /cvs/stx/stx/libbasic/Process.st,v 1.20 1995-02-15 10:24:43 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
|
|
103 |
implement this feature, the machines stack had to be preserved and recreated.
|
|
104 |
Although this is possible theoretically, this has not been implemented, since
|
|
105 |
the machines stack layout is highly machine/compiler dependent,
|
|
106 |
|
|
107 |
|
|
108 |
Process synchronization:
|
|
109 |
any other process can wait for a process to suspend or terminate. This
|
|
110 |
is implemented by using suspendSemaphore and exitSemaphore, which are
|
|
111 |
signalled when these events occur (see waitUntilSuspended/waitUntilTerminated).
|
93
|
112 |
|
|
113 |
Instance variables:
|
|
114 |
|
159
|
115 |
id <SmallInteger> a unique process-id
|
202
|
116 |
|
159
|
117 |
prio <SmallInteger> the processes priority
|
202
|
118 |
|
159
|
119 |
state <Symbol> the processes state
|
|
120 |
(for process-monitor)
|
202
|
121 |
|
|
122 |
startBlock <Block> the startup-block (the one that forked)
|
|
123 |
|
159
|
124 |
name <String-or-nil> the processes name (if any)
|
|
125 |
(for process-monitor)
|
202
|
126 |
|
217
|
127 |
suspendSemaphore <Semaphore> triggered when suspend (if nonNil)
|
|
128 |
|
|
129 |
exitSemaphore <Semaphore> triggered when terminated (if nonNil)
|
|
130 |
|
159
|
131 |
restartable <Boolean> is restartable (not yet implemented)
|
202
|
132 |
|
159
|
133 |
interruptActions <Collection> interrupt actions as defined by interruptWith:,
|
|
134 |
performed at interrupt time
|
202
|
135 |
|
217
|
136 |
exitAction <Block> additional cleanup action to perform
|
|
137 |
on termination (if nonNil)
|
202
|
138 |
|
159
|
139 |
emergencySignalHandler <Block> can be used for per-process
|
|
140 |
emergency signal handling
|
|
141 |
Class variables:
|
|
142 |
|
|
143 |
TerminateSignal <Signal> signal used to terminate processes
|
|
144 |
(should not be cought - or at least
|
|
145 |
rejected in handlers)
|
88
|
146 |
"
|
213
|
147 |
!
|
|
148 |
|
|
149 |
examples
|
|
150 |
"
|
|
151 |
start a background process, computing 1000 factorial 100 times;
|
|
152 |
the processes priority is set to not disturb any interactive process.
|
|
153 |
Since its prio is higher than the 3D animation demos prio, you will notice,
|
|
154 |
that those are suspended while the computation runs. But Interactive views
|
|
155 |
(like browsers) will continue to react normal.
|
|
156 |
|
|
157 |
[
|
|
158 |
'starting' printNL.
|
|
159 |
100 timesRepeat:[1000 factorial].
|
|
160 |
'done with factorial' printNL.
|
|
161 |
] forkAt:(Processor userBackgroundPriority).
|
|
162 |
|
|
163 |
|
|
164 |
start a background process, computing 1000 factorial 100 times;
|
|
165 |
the processes priority is set to not disturb any process.
|
|
166 |
The prio is set to 1 (which is the lowest possible) notice that now,
|
|
167 |
the 3D animation demos also continue to run.
|
|
168 |
|
|
169 |
[
|
|
170 |
'starting' printNL.
|
|
171 |
100 timesRepeat:[1000 factorial].
|
|
172 |
'done with factorial' printNL.
|
|
173 |
] forkAt:1.
|
|
174 |
|
|
175 |
|
|
176 |
start a background process, reading a pipe without blocking other
|
|
177 |
processes;
|
|
178 |
|
|
179 |
[
|
|
180 |
|p|
|
|
181 |
|
|
182 |
'starting' printNL.
|
|
183 |
p := PipeStream readingFrom:'ls -lR .'.
|
|
184 |
[p atEnd] whileFalse:[
|
|
185 |
p readWait.
|
|
186 |
p nextLine printNL.
|
|
187 |
].
|
|
188 |
p close.
|
|
189 |
'done with pipe' printNL.
|
|
190 |
] forkAt:1.
|
|
191 |
|
|
192 |
see more examples in doc/coding
|
|
193 |
(or search in the browser for senders of fork*)
|
|
194 |
"
|
88
|
195 |
! !
|
1
|
196 |
|
159
|
197 |
!Process class methodsFor:'initialization'!
|
|
198 |
|
|
199 |
initialize
|
|
200 |
TerminateSignal isNil ifTrue:[
|
|
201 |
TerminateSignal := Signal new mayProceed:true.
|
|
202 |
TerminateSignal nameClass:self message:#terminateSignal.
|
|
203 |
TerminateSignal notifierString:'unhandled process termination'.
|
|
204 |
]
|
|
205 |
! !
|
|
206 |
|
|
207 |
!Process class methodsFor:'signal access'!
|
|
208 |
|
|
209 |
terminateSignal
|
202
|
210 |
"return the signal used for process termination"
|
|
211 |
|
159
|
212 |
^ TerminateSignal
|
|
213 |
! !
|
|
214 |
|
|
215 |
!Process class methodsFor:'instance creation'!
|
|
216 |
|
|
217 |
for:aBlock priority:aPrio
|
|
218 |
"create a new (unscheduled) process which will execute aBlock at
|
|
219 |
a given priority, once scheduled. The process will start execution once
|
|
220 |
it gets a #resume-message."
|
|
221 |
|
|
222 |
^ self new for:aBlock priority:aPrio
|
|
223 |
! !
|
|
224 |
|
|
225 |
!Process methodsFor:'private'!
|
|
226 |
|
|
227 |
for:aBlock priority:aPrio
|
|
228 |
"setup the new process - the receiver is not scheduled for
|
|
229 |
execution, to get it running, send it #resume"
|
|
230 |
|
|
231 |
|nm|
|
|
232 |
|
|
233 |
prio := aPrio.
|
|
234 |
startBlock := aBlock.
|
|
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 |
"
|
|
245 |
(nm := Processor activeProcess name) notNil ifTrue:[
|
|
246 |
"
|
|
247 |
avoid name inflation
|
|
248 |
"
|
|
249 |
(nm endsWith:'(sub)') ifFalse:[
|
|
250 |
nm := nm , ' (sub)'
|
|
251 |
].
|
|
252 |
name := nm
|
|
253 |
]
|
|
254 |
! !
|
|
255 |
|
1
|
256 |
!Process methodsFor:'accessing'!
|
|
257 |
|
|
258 |
state
|
13
|
259 |
"return a symbol describing the processes state"
|
|
260 |
|
1
|
261 |
^ state
|
|
262 |
!
|
|
263 |
|
|
264 |
state:aSymbol
|
13
|
265 |
"set the state - only to be used from scheduler"
|
|
266 |
|
1
|
267 |
state := aSymbol
|
|
268 |
!
|
|
269 |
|
182
|
270 |
isDead
|
|
271 |
"return true, if the receiver has already terminated"
|
|
272 |
|
|
273 |
^ state == #dead
|
|
274 |
!
|
|
275 |
|
159
|
276 |
startBlock
|
|
277 |
"return the processes startup-block"
|
69
|
278 |
|
159
|
279 |
^ startBlock
|
1
|
280 |
!
|
|
281 |
|
116
|
282 |
emergencySignalHandler:aOneArgBlock
|
|
283 |
"set the emergencySignalHandler block.
|
|
284 |
See Signal>>documentation for more info."
|
|
285 |
|
|
286 |
emergencySignalHandler := aOneArgBlock
|
|
287 |
!
|
|
288 |
|
|
289 |
emergencySignalHandler
|
|
290 |
"return the emergencySignalHandler block.
|
|
291 |
See Signal>>documentation for more info."
|
|
292 |
|
|
293 |
^ emergencySignalHandler
|
|
294 |
!
|
|
295 |
|
1
|
296 |
priority
|
|
297 |
"return the receivers priority"
|
|
298 |
|
|
299 |
^ prio
|
|
300 |
!
|
|
301 |
|
|
302 |
priority:aNumber
|
|
303 |
"set my priority"
|
|
304 |
|
116
|
305 |
Processor changePriority:aNumber for:self.
|
|
306 |
!
|
|
307 |
|
|
308 |
changePriority:aNumber
|
|
309 |
"same as priority:, but returns the old priority.
|
|
310 |
(cannot do this in priority: for ST-80 compatibility)"
|
|
311 |
|
|
312 |
|oldPrio|
|
|
313 |
|
|
314 |
oldPrio := prio.
|
|
315 |
Processor changePriority:aNumber for:self.
|
|
316 |
^ oldPrio
|
1
|
317 |
!
|
|
318 |
|
93
|
319 |
isSingleStepping
|
|
320 |
^ singleStepping
|
|
321 |
!
|
|
322 |
|
|
323 |
singleStep:aBoolean
|
|
324 |
singleStepping := aBoolean
|
|
325 |
!
|
|
326 |
|
1
|
327 |
id
|
10
|
328 |
"return the processes id"
|
|
329 |
|
1
|
330 |
^ id
|
|
331 |
!
|
|
332 |
|
13
|
333 |
name
|
|
334 |
"return the processes name"
|
|
335 |
|
|
336 |
^ name
|
|
337 |
!
|
|
338 |
|
|
339 |
name:aString
|
|
340 |
"set the processes name"
|
|
341 |
|
|
342 |
name := aString
|
|
343 |
!
|
|
344 |
|
|
345 |
nameOrId
|
|
346 |
"return a string to identify the process - either name or id"
|
|
347 |
|
|
348 |
name notNil ifTrue:[^ name].
|
|
349 |
^ id printString
|
|
350 |
!
|
|
351 |
|
|
352 |
exitAction
|
|
353 |
"return the processes exit action"
|
|
354 |
|
|
355 |
^ exitAction
|
|
356 |
!
|
|
357 |
|
|
358 |
exitAction:aBlock
|
182
|
359 |
"set the processes exit action to aBlock.
|
|
360 |
This will be evaluated right before the process dies."
|
13
|
361 |
|
|
362 |
exitAction := aBlock
|
1
|
363 |
!
|
|
364 |
|
|
365 |
suspendedContext
|
182
|
366 |
"return the processes suspended context
|
|
367 |
- this is the context from which a process switch into the scheduler
|
|
368 |
or another process occured.
|
|
369 |
Typically, only the debugger is interrested in this one."
|
13
|
370 |
|
|
371 |
%{ /* NOCONTEXT */
|
1
|
372 |
extern OBJ __threadContext();
|
69
|
373 |
OBJ i;
|
1
|
374 |
|
252
|
375 |
if (__isSmallInteger(i = _INST(id))) {
|
159
|
376 |
RETURN (__threadContext(_intVal(i)));
|
13
|
377 |
}
|
182
|
378 |
%}.
|
69
|
379 |
^ nil
|
|
380 |
!
|
|
381 |
|
|
382 |
maximumStackSize
|
93
|
383 |
"returns the processes stack limit - i.e. the process will be
|
69
|
384 |
interrupted with a recursionSignal-raise, if it ever
|
|
385 |
needs more stack (in bytes) than this number"
|
|
386 |
|
|
387 |
%{ /* NOCONTEXT */
|
|
388 |
extern int __threadMaxStackSize();
|
|
389 |
OBJ i;
|
13
|
390 |
|
252
|
391 |
if (__isSmallInteger(i = _INST(id))) {
|
159
|
392 |
RETURN( _MKSMALLINT(__threadMaxStackSize(_intVal(i))) );
|
69
|
393 |
}
|
182
|
394 |
%}.
|
69
|
395 |
^ nil
|
|
396 |
!
|
182
|
397 |
|
69
|
398 |
setMaximumStackSize:limit
|
93
|
399 |
"sets the processes stack limit - i.e. the process will be
|
69
|
400 |
interrupted with a recursionSignal-raise, if it ever
|
|
401 |
needs more stack (in bytes) than this number.
|
|
402 |
Returns the old value."
|
|
403 |
|
|
404 |
%{ /* NOCONTEXT */
|
|
405 |
extern int __threadSetMaxStackSize();
|
|
406 |
OBJ i;
|
|
407 |
|
252
|
408 |
if (__isSmallInteger(i = _INST(id))
|
|
409 |
&& __isSmallInteger(limit) ) {
|
159
|
410 |
RETURN ( _MKSMALLINT(__threadSetMaxStackSize(_intVal(i), _intVal(limit))) );
|
13
|
411 |
}
|
182
|
412 |
%}.
|
|
413 |
^ nil
|
|
414 |
! !
|
|
415 |
|
|
416 |
!Process methodsFor:'monitoring'!
|
|
417 |
|
|
418 |
usedStackSize
|
|
419 |
"Return the processes current stack size.
|
|
420 |
This method is for monitoring purposes only - it may vanish."
|
|
421 |
|
|
422 |
%{ /* NOCONTEXT */
|
|
423 |
extern int __threadUsedStackSize();
|
|
424 |
OBJ i;
|
|
425 |
|
252
|
426 |
if (__isSmallInteger(i = _INST(id))) {
|
182
|
427 |
RETURN( _MKSMALLINT(__threadUsedStackSize(_intVal(i))) );
|
|
428 |
}
|
|
429 |
%}.
|
|
430 |
^ nil
|
|
431 |
!
|
|
432 |
|
|
433 |
totalStackSize
|
|
434 |
"return the processes maximum used stack size.
|
|
435 |
This method is for monitoring purposes only - it may vanish."
|
|
436 |
|
|
437 |
%{ /* NOCONTEXT */
|
|
438 |
extern int __threadTotalStackSize();
|
|
439 |
OBJ i;
|
|
440 |
|
252
|
441 |
if (__isSmallInteger(i = _INST(id))) {
|
182
|
442 |
RETURN( _MKSMALLINT(__threadTotalStackSize(_intVal(i))) );
|
|
443 |
}
|
|
444 |
%}.
|
|
445 |
^ nil
|
|
446 |
!
|
|
447 |
|
|
448 |
numberOfStackSegments
|
|
449 |
"return the processes number of stack segments currently used.
|
|
450 |
This method is for monitoring purposes only - it may vanish."
|
|
451 |
|
|
452 |
%{ /* NOCONTEXT */
|
|
453 |
extern int __threadTotalStackSize();
|
|
454 |
OBJ i;
|
|
455 |
|
252
|
456 |
if (__isSmallInteger(i = _INST(id))) {
|
182
|
457 |
RETURN( _MKSMALLINT(__threadStackSegments(_intVal(i))) );
|
|
458 |
}
|
|
459 |
%}.
|
13
|
460 |
^ nil
|
|
461 |
! !
|
|
462 |
|
|
463 |
!Process methodsFor:'private scheduler access'!
|
|
464 |
|
159
|
465 |
setPriority:aNumber
|
|
466 |
"set priority without telling processor - not for public use"
|
|
467 |
|
|
468 |
prio := aNumber
|
|
469 |
!
|
|
470 |
|
|
471 |
setStartBlock:aBlock
|
|
472 |
"set the receivers startup block"
|
|
473 |
|
|
474 |
startBlock := aBlock
|
|
475 |
!
|
13
|
476 |
|
159
|
477 |
setStateTo:newState if:oldState
|
|
478 |
state == oldState ifTrue:[state := newState]
|
|
479 |
!
|
|
480 |
|
|
481 |
setStateTo:newState if:oldState1 or:oldState2
|
|
482 |
(state == oldState1 or:[state == oldState2]) ifTrue:[state := newState]
|
|
483 |
!
|
|
484 |
|
|
485 |
setId:idNumber state:stateSymbol
|
|
486 |
"set id and state - not for public use"
|
|
487 |
|
|
488 |
id := idNumber.
|
|
489 |
state := stateSymbol.
|
202
|
490 |
! !
|
|
491 |
|
|
492 |
!Process methodsFor:'startup '!
|
116
|
493 |
|
|
494 |
start
|
159
|
495 |
"start the process - this is sent by the VM to the process to get
|
|
496 |
the process up and running.
|
|
497 |
Sending start to the process allows more flexible handling
|
|
498 |
of processes, since anything that responds to #start can be handled
|
|
499 |
by the VM then ..."
|
|
500 |
|
|
501 |
|block|
|
116
|
502 |
|
159
|
503 |
(block := startBlock) notNil ifTrue:[
|
|
504 |
"
|
|
505 |
just for your convenience ...
|
|
506 |
"
|
|
507 |
name isNil ifTrue:[
|
|
508 |
name := '(' , startBlock displayString , ')'
|
|
509 |
].
|
|
510 |
startBlock := nil.
|
217
|
511 |
(SignalSet with:TerminateSignal with:AbortSignal)
|
202
|
512 |
handle:[:ex |
|
159
|
513 |
ex return
|
182
|
514 |
] do:block.
|
202
|
515 |
|
159
|
516 |
(block := exitAction) notNil ifTrue:[
|
|
517 |
exitAction := nil.
|
|
518 |
block value.
|
|
519 |
].
|
217
|
520 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signal].
|
|
521 |
exitSemaphore notNil ifTrue:[exitSemaphore signal].
|
159
|
522 |
Processor terminateActiveNoSignal
|
|
523 |
] ifFalse:[
|
|
524 |
"is this artificial restriction useful ?"
|
|
525 |
self error:'a process cannot be started twice'
|
116
|
526 |
]
|
1
|
527 |
! !
|
|
528 |
|
|
529 |
!Process methodsFor:'suspend / resume'!
|
|
530 |
|
|
531 |
suspend
|
13
|
532 |
"suspend the receiver process - will continue to run when a resume is sent"
|
|
533 |
|
217
|
534 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signal].
|
1
|
535 |
Processor suspend:self
|
|
536 |
!
|
|
537 |
|
|
538 |
resume
|
13
|
539 |
"resume the receiver process"
|
10
|
540 |
|
1
|
541 |
Processor resume:self
|
|
542 |
!
|
|
543 |
|
93
|
544 |
resumeForSingleSend
|
|
545 |
"resume the receiver process, but only let it execute a single send."
|
|
546 |
|
|
547 |
Processor resumeForSingleSend:self
|
|
548 |
!
|
|
549 |
|
1
|
550 |
terminate
|
159
|
551 |
"terminate the receiver process. All unwind actions and the exit-action (if any)
|
|
552 |
will be performed before the process is really terminated."
|
10
|
553 |
|
159
|
554 |
Processor activeProcess == self ifTrue:[
|
|
555 |
Signal noHandlerSignal handle:[:ex |
|
|
556 |
ex return.
|
|
557 |
] do:[
|
|
558 |
TerminateSignal raise.
|
|
559 |
].
|
217
|
560 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signal].
|
|
561 |
exitSemaphore notNil ifTrue:[exitSemaphore signal].
|
159
|
562 |
Processor terminateNoSignal:self
|
|
563 |
] ifFalse:[
|
|
564 |
self interruptWith:[self terminate]
|
|
565 |
]
|
|
566 |
!
|
|
567 |
|
|
568 |
terminateNoSignal
|
|
569 |
"terminate the receiver process without performing any unwind- or exit-actions"
|
|
570 |
|
217
|
571 |
suspendSemaphore notNil ifTrue:[suspendSemaphore signal].
|
|
572 |
exitSemaphore notNil ifTrue:[exitSemaphore signal].
|
159
|
573 |
Processor terminateNoSignal:self
|
1
|
574 |
! !
|
|
575 |
|
159
|
576 |
!Process methodsFor:'interrupts'!
|
24
|
577 |
|
|
578 |
interruptWith:aBlock
|
|
579 |
"interrupt the receiver and make it evaluate aBlock.
|
|
580 |
If the receiver is currently suspended, the block will be remembered
|
93
|
581 |
to be evaluated once the receiver wakes up."
|
|
582 |
|
159
|
583 |
self uninterruptablyDo:[
|
|
584 |
interruptActions isNil ifTrue:[
|
|
585 |
interruptActions := OrderedCollection with:aBlock.
|
|
586 |
] ifFalse:[
|
|
587 |
interruptActions addLast:aBlock.
|
|
588 |
].
|
93
|
589 |
].
|
|
590 |
Processor scheduleForInterrupt:self.
|
|
591 |
!
|
24
|
592 |
|
93
|
593 |
interrupt
|
159
|
594 |
"evaluate my interrupt-actions
|
93
|
595 |
the process will go back to where it got interrupted
|
|
596 |
after doing this.
|
|
597 |
"
|
|
598 |
|action|
|
|
599 |
|
|
600 |
[interruptActions notNil and:[interruptActions notEmpty]] whileTrue:[
|
159
|
601 |
action := interruptActions removeFirst.
|
|
602 |
action value
|
24
|
603 |
].
|
93
|
604 |
interruptActions := nil
|
24
|
605 |
! !
|
159
|
606 |
|
|
607 |
!Process methodsFor:'special'!
|
|
608 |
|
|
609 |
withPriority:aPrio do:aBlock
|
|
610 |
"execute aBlock at another priority. This can be used to perform
|
|
611 |
time-consuming operations at a more user-friendly priority,
|
|
612 |
or some critical action at a higher priority. Do not use too high
|
|
613 |
of a priority to avoid locking up the system (event processing takes place
|
|
614 |
at 24)"
|
|
615 |
|
|
616 |
|oldprio|
|
|
617 |
|
|
618 |
oldprio := prio.
|
|
619 |
self priority:aPrio.
|
|
620 |
|
|
621 |
^ aBlock valueNowOrOnUnwindDo:[
|
|
622 |
self priority:oldprio
|
|
623 |
]
|
182
|
624 |
|
|
625 |
"
|
|
626 |
Processor activeProcess withPriority:7 do:[3000 factorial]
|
|
627 |
"
|
|
628 |
"be careful - even ^C wont work until done:
|
|
629 |
Processor activeProcess withPriority:25 do:[3000 factorial]
|
|
630 |
"
|
159
|
631 |
!
|
|
632 |
|
|
633 |
withLowerPriorityDo:aBlock
|
|
634 |
"execute aBlock at a lower priority. This can be used to perform
|
|
635 |
time-consuming operations at a more user-friendly priority."
|
|
636 |
|
|
637 |
^ self withPriority:(prio - 1) do:aBlock
|
182
|
638 |
|
|
639 |
"
|
|
640 |
Processor activeProcess withLowerPriorityDo:[3000 factorial]
|
|
641 |
"
|
159
|
642 |
!
|
|
643 |
|
|
644 |
uninterruptablyDo:aBlock
|
|
645 |
"execute aBlock with interrupts blocked. This does not prevent
|
|
646 |
preemption by higher priority processes."
|
|
647 |
|
|
648 |
|wasBlocked|
|
|
649 |
|
|
650 |
"we must keep track of blocking-state if this is called nested"
|
|
651 |
|
|
652 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
653 |
^ aBlock valueNowOrOnUnwindDo:[
|
182
|
654 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
655 |
0 "stc hint"
|
159
|
656 |
]
|
217
|
657 |
!
|
|
658 |
|
|
659 |
waitUntilTerminated
|
|
660 |
"set the softSuspend flag of the receiver and wait until it
|
|
661 |
suspends. The receviers process must execute checkForSoftSuspend
|
|
662 |
periodically for this rendevous to work."
|
|
663 |
|
|
664 |
exitSemaphore := Semaphore new.
|
|
665 |
exitSemaphore wait
|
|
666 |
!
|
|
667 |
|
|
668 |
waitUntilSuspended
|
|
669 |
"set the softSuspend flag of the receiver and wait until it
|
|
670 |
suspends. The receviers process must execute checkForSoftSuspend
|
|
671 |
periodically for this rendevous to work."
|
|
672 |
|
|
673 |
suspendSemaphore := Semaphore new.
|
|
674 |
suspendSemaphore wait
|
159
|
675 |
! !
|
24
|
676 |
|
93
|
677 |
!Process methodsFor:'printing & storing'!
|
1
|
678 |
|
59
|
679 |
printOn:aStream
|
85
|
680 |
aStream nextPutAll:state article;
|
159
|
681 |
space;
|
|
682 |
nextPutAll:state;
|
|
683 |
nextPutAll:' Process (';
|
|
684 |
nextPutAll:self nameOrId;
|
|
685 |
nextPutAll:')'
|
1
|
686 |
! !
|