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