author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:21:27 +0100 | |
changeset 607 | a9a526c51233 |
parent 585 | 33141ab22234 |
child 645 | b9fe149c7ff1 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 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 |
Object subclass:#ProcessorScheduler |
|
159 | 14 |
instanceVariableNames:'quiescentProcessLists scheduler |
15 |
zombie |
|
16 |
activeProcess currentPriority |
|
231 | 17 |
readFdArray readSemaphoreArray readCheckArray |
18 |
writeFdArray writeSemaphoreArray |
|
19 |
timeoutArray timeoutActionArray timeoutProcessArray timeoutSemaphoreArray |
|
302 | 20 |
idleActions anyTimeouts dispatching interruptedProcess |
21 |
useIOInterrupts' |
|
159 | 22 |
classVariableNames:'KnownProcesses KnownProcessIds |
23 |
PureEventDriven |
|
24 |
UserSchedulingPriority |
|
25 |
UserInterruptPriority |
|
26 |
TimingPriority |
|
241 | 27 |
HighestPriority |
369 | 28 |
SchedulingPriority |
29 |
MaxNumberOfProcesses' |
|
159 | 30 |
poolDictionaries:'' |
31 |
category:'Kernel-Processes' |
|
1 | 32 |
! |
33 |
||
34 |
Smalltalk at:#Processor put:nil! |
|
35 |
||
88 | 36 |
!ProcessorScheduler class methodsFor:'documentation'! |
37 |
||
38 |
copyright |
|
39 |
" |
|
40 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 41 |
All Rights Reserved |
88 | 42 |
|
43 |
This software is furnished under a license and may be used |
|
44 |
only in accordance with the terms of that license and with the |
|
45 |
inclusion of the above copyright notice. This software may not |
|
46 |
be provided or otherwise made available to, or used by, any |
|
47 |
other person. No title to or ownership of the software is |
|
48 |
hereby transferred. |
|
49 |
" |
|
50 |
! |
|
51 |
||
52 |
version |
|
585
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
53 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/ProcSched.st,v 1.51 1995-11-20 16:05:23 cg Exp $' |
88 | 54 |
! |
55 |
||
56 |
documentation |
|
57 |
" |
|
58 |
This class has only one instance, which is bound to the global |
|
59 |
'Processor'. It is responsible for scheduling among the smalltalk |
|
60 |
processes (threads; not to confuse with heavy weight unix processes). |
|
93 | 61 |
|
62 |
Scheduling is fully done in smalltalk (the always runnable scheduler- |
|
63 |
process, running at highest priority does this). |
|
231 | 64 |
The main VM primitive to support this is found in threadSwitch, which passes |
93 | 65 |
control to another process (usually selected by the scheduler). |
159 | 66 |
Thus it is possible to modify the schedulers policy and implementation |
67 |
at the smalltalk level. |
|
93 | 68 |
(To answer a frequently asked question: |
159 | 69 |
dont add preemptive round-robin here; this can be implemented without |
93 | 70 |
any need to change the scheduler. See goodies/timeslicing.st for how |
71 |
this is done in a very elegant way). |
|
72 |
||
73 |
Notice: Smalltalk/X can (still) be compiled & configured without |
|
74 |
process support. This non-process mode is called 'pureEventDriven' mode |
|
75 |
and is useful to quickly port ST/X to systems, where these facilities |
|
76 |
are either not needed (server applications), or are difficult to |
|
77 |
implement (threads require some assembler support functions). |
|
78 |
To allow pureEvent mode, kludges are built into some places in the |
|
79 |
system, where either a process is forked, or a timeout is used instead |
|
80 |
(for examples, see ProcessMonitor or MemoryMonitor). |
|
81 |
||
335 | 82 |
This pure-event mode may not be supported in the future |
83 |
(actually, it is no longer maintained in places where was present, so dont |
|
84 |
run the system without Processes). |
|
93 | 85 |
|
369 | 86 |
|
93 | 87 |
class variables: |
88 |
||
159 | 89 |
KnownProcesses <Collection> all known processes |
90 |
KnownProcessIds <Collection> and their IDs |
|
241 | 91 |
|
159 | 92 |
PureEventDriven <Boolean> true, if no process support |
93 |
is available |
|
241 | 94 |
|
159 | 95 |
UserSchedulingPriority <Integer> the priority at which normal |
96 |
user interfaces run |
|
241 | 97 |
|
159 | 98 |
UserInterruptPriority the priority at which user- |
99 |
interrupts (Cntl-C) processing |
|
100 |
takes place. Processes with |
|
101 |
a greater or equal priority are |
|
102 |
not interruptable. |
|
241 | 103 |
|
159 | 104 |
TimingPriority the priority used for timing. |
105 |
Processes with a greater or |
|
106 |
equal priority are not interrupted |
|
107 |
by timers. |
|
241 | 108 |
|
109 |
HighestPriority The highest allowed prio for processes |
|
110 |
||
159 | 111 |
SchedulingPriority The priority of the scheduler (must |
112 |
me higher than any other). |
|
93 | 113 |
|
369 | 114 |
MaxNumberOfProcesses if non-nil, no more than this |
115 |
number of processes are allowed |
|
116 |
(for debugging) |
|
93 | 117 |
|
118 |
most interresting methods: |
|
119 |
||
159 | 120 |
Processor>>suspend: (see also Process>>suspend) |
121 |
Processor>>resume: (see also Process>>resume) |
|
122 |
Processor>>terminate: (see also Process>>terminate) |
|
123 |
Processor>>yield |
|
124 |
Processor>>changePriority:for: (see also Process>>priority: |
|
93 | 125 |
|
159 | 126 |
Processor>>signal:afterSeconds: (see also Delay>>forSeconds:) |
127 |
Processor>>signal:afterMilliseconds: (see also Delay>>forMilliseconds:) |
|
128 |
Processor>>signal:onInput: (see also ExternalStream>>readWait) |
|
129 |
Processor>>signal:onOutput: (see also ExternalStream>>writeWait) |
|
130 |
Processor>>disableSemaphore: |
|
88 | 131 |
" |
132 |
! ! |
|
133 |
||
1 | 134 |
!ProcessorScheduler class methodsFor:'initialization'! |
135 |
||
136 |
initialize |
|
44 | 137 |
"class setup: create the one-and-only instance of myself and |
138 |
setup some priority values." |
|
139 |
||
93 | 140 |
UserSchedulingPriority := 8. |
141 |
UserInterruptPriority := 24. |
|
142 |
TimingPriority := 16. |
|
143 |
SchedulingPriority := 31. |
|
241 | 144 |
HighestPriority := 30. |
93 | 145 |
|
159 | 146 |
Processor isNil ifTrue:[ |
147 |
"create the one and only processor" |
|
1 | 148 |
|
159 | 149 |
Processor := self basicNew initialize. |
10 | 150 |
]. |
77 | 151 |
|
152 |
" |
|
153 |
allow configurations without processes |
|
154 |
" |
|
13 | 155 |
PureEventDriven := self threadsAvailable not. |
156 |
PureEventDriven ifTrue:[ |
|
159 | 157 |
'no process support - running event driven' errorPrintNL |
13 | 158 |
]. |
1 | 159 |
! ! |
160 |
||
10 | 161 |
!ProcessorScheduler class methodsFor:'instance creation'! |
162 |
||
163 |
new |
|
164 |
"there is (currently) only one processor ..." |
|
165 |
||
159 | 166 |
self error:'only one processor is allowed in the system' |
10 | 167 |
! ! |
168 |
||
169 |
!ProcessorScheduler class methodsFor:'instance release'! |
|
170 |
||
171 |
informDispose |
|
159 | 172 |
"some Process has been garbage collected |
335 | 173 |
- terminate the underlying thread. |
174 |
Usually this does not happen; instead, the process terminates itself |
|
175 |
by sending #terminate." |
|
10 | 176 |
|
177 |
|id sz "{ Class: SmallInteger }"| |
|
1 | 178 |
|
10 | 179 |
sz := KnownProcessIds size. |
180 |
1 to:sz do:[:index | |
|
159 | 181 |
(KnownProcesses at:index) isNil ifTrue:[ |
182 |
id := KnownProcessIds at:index. |
|
183 |
id notNil ifTrue:[ |
|
184 |
'PROCESSOR: terminating thread ' errorPrint. |
|
185 |
id errorPrint. |
|
186 |
' (no longer refd)' errorPrintNL. |
|
187 |
||
188 |
self threadDestroy:id. |
|
189 |
KnownProcessIds at:index put:nil. |
|
190 |
] |
|
191 |
] |
|
10 | 192 |
] |
193 |
! ! |
|
194 |
||
195 |
!ProcessorScheduler class methodsFor:'queries'! |
|
196 |
||
197 |
isPureEventDriven |
|
198 |
"this is temporary - (maybe not :-). |
|
199 |
you can run ST/X either with or without processes. |
|
200 |
Without, there is conceptionally a single process handling all |
|
201 |
outside events and timeouts. This has some negative implications |
|
202 |
(Debugger is ugly), but allows a fully portable ST/X without any |
|
203 |
assembler support - i.e. quick portability. |
|
13 | 204 |
The PureEvent flag will automatically be set if the runtime system |
205 |
does not support threads - otherwise, it can be set manually |
|
206 |
(from rc-file). |
|
10 | 207 |
" |
208 |
||
209 |
^ PureEventDriven |
|
210 |
! |
|
211 |
||
212 |
pureEventDriven |
|
13 | 213 |
"turn on pure-event driven mode - no processes, single dispatch loop" |
214 |
||
10 | 215 |
PureEventDriven := true |
216 |
! |
|
217 |
||
218 |
processDriven |
|
13 | 219 |
"turn on process driven mode" |
220 |
||
10 | 221 |
PureEventDriven := false |
93 | 222 |
! |
223 |
||
224 |
knownProcesses |
|
225 |
"return a collection of all (living) processes in the system" |
|
226 |
||
227 |
^ KnownProcesses select:[:p | p notNil] |
|
369 | 228 |
! |
229 |
||
230 |
maxNumberOfProcesses |
|
231 |
"return the limit on the number of processes; |
|
232 |
the default is nil (i.e. unlimited)." |
|
233 |
||
234 |
^ MaxNumberOfProcesses |
|
235 |
! |
|
236 |
||
237 |
maxNumberOfProcesses:aNumber |
|
238 |
"set the limit on the number of processes. |
|
239 |
This helps if you have a program which (by error) creates countless |
|
240 |
subprocesses. Without this limit, you may have a hard time to find |
|
241 |
this error (and repairing it). If nil (the default), the number of |
|
242 |
processes is unlimited." |
|
243 |
||
244 |
MaxNumberOfProcesses := aNumber |
|
10 | 245 |
! ! |
246 |
||
247 |
!ProcessorScheduler class methodsFor:'primitive process primitives'! |
|
248 |
||
13 | 249 |
threadsAvailable |
250 |
"return true, if the runtime system supports threads (i.e. processes); |
|
251 |
false otherwise." |
|
252 |
||
253 |
%{ /* NOCONTEXT */ |
|
254 |
extern OBJ __threadsAvailable(); |
|
255 |
||
256 |
RETURN (__threadsAvailable()); |
|
257 |
%} |
|
258 |
! |
|
259 |
||
93 | 260 |
threadInterrupt:id |
335 | 261 |
"make the process evaluate an interrupt. This sets a flag in the VMs |
262 |
threadSwitcher, to let the process perform a #interrupt when its set to |
|
263 |
run the next time. The process itself can decide how to react on this |
|
264 |
interrupt (currently, it looks for interruptBlocks to evaluate)." |
|
24 | 265 |
|
266 |
%{ /* NOCONTEXT */ |
|
267 |
||
253 | 268 |
if (__isSmallInteger(id)) { |
159 | 269 |
__threadInterrupt(_intVal(id)); |
24 | 270 |
} |
271 |
%} |
|
272 |
! |
|
273 |
||
339 | 274 |
threadCreate:aProcess withId:id |
159 | 275 |
"physical creation of a process. |
10 | 276 |
(warning: low level entry, no administration done). |
339 | 277 |
This may raise an exception, if a VM process could not be created." |
13 | 278 |
|
369 | 279 |
MaxNumberOfProcesses notNil ifTrue:[ |
280 |
KnownProcessIds size >= MaxNumberOfProcesses ifTrue:[ |
|
281 |
(KnownProcessIds count:[:el | el notNil]) >= MaxNumberOfProcesses ifTrue:[ |
|
282 |
" |
|
283 |
the number of processes has reached the (soft) limit. |
|
284 |
This limit prevents runaway programs from creating too many |
|
285 |
processes. If you continue in the debugger, the process will be |
|
286 |
created as usual. If you dont want this, abort or terminate. |
|
287 |
" |
|
288 |
self error:'too many processes'. |
|
289 |
] |
|
290 |
] |
|
291 |
]. |
|
292 |
||
293 |
%{ |
|
1 | 294 |
int tid; |
295 |
extern int __threadCreate(); |
|
296 |
||
339 | 297 |
tid = __threadCreate(aProcess, |
298 |
0 /* stackSize: no longer needed */, |
|
299 |
__isSmallInteger(id) ? _intVal(id) /* assign id */ |
|
300 |
: -1 /* let VM assign one */ ); |
|
301 |
if (tid) { |
|
159 | 302 |
RETURN ( _MKSMALLINT(tid)); |
1 | 303 |
} |
304 |
%} |
|
13 | 305 |
. |
69 | 306 |
" |
307 |
arrive here, if creation of process in VM failed. |
|
159 | 308 |
This may happen, if the VM does not support more processes, |
309 |
or if it ran out of memory, when allocating internal data |
|
310 |
structures. |
|
69 | 311 |
" |
115 | 312 |
^ ObjectMemory allocationFailureSignal raise. |
1 | 313 |
! |
314 |
||
315 |
threadDestroy:id |
|
316 |
"physical destroy other process ... |
|
317 |
(warning: low level entry, no administration done)" |
|
318 |
||
13 | 319 |
%{ /* NOCONTEXT */ |
320 |
||
253 | 321 |
if (__isSmallInteger(id)) { |
159 | 322 |
__threadDestroy(_intVal(id)); |
1 | 323 |
} |
324 |
%} |
|
325 |
! ! |
|
326 |
||
10 | 327 |
!ProcessorScheduler methodsFor:'primitive process primitives'! |
328 |
||
329 |
threadSwitch:aProcess |
|
330 |
"continue execution in aProcess. |
|
331 |
(warning: low level entry, no administration is done here)" |
|
332 |
||
93 | 333 |
|id pri ok oldProcess oldPri p singleStep wasBlocked| |
1 | 334 |
|
159 | 335 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
1 | 336 |
|
93 | 337 |
wasBlocked := OperatingSystem blockInterrupts. |
338 |
||
69 | 339 |
oldProcess := activeProcess. |
340 |
oldPri := currentPriority. |
|
341 |
||
10 | 342 |
id := aProcess id. |
343 |
pri := aProcess priority. |
|
93 | 344 |
singleStep := aProcess isSingleStepping. |
13 | 345 |
aProcess state:#active. |
159 | 346 |
oldProcess setStateTo:#run if:#active. |
1 | 347 |
|
217 | 348 |
" |
349 |
no interrupts now - activeProcess has already been changed |
|
350 |
(dont add any message sends here) |
|
351 |
" |
|
25 | 352 |
activeProcess := aProcess. |
10 | 353 |
currentPriority := pri. |
354 |
%{ |
|
241 | 355 |
extern OBJ ___threadSwitch(); |
69 | 356 |
|
253 | 357 |
if (__isSmallInteger(id)) { |
271 | 358 |
ok = ___threadSwitch(__context, _intVal(id), (singleStep == true) ? 1 : 0); |
253 | 359 |
} else { |
360 |
ok = false; |
|
361 |
} |
|
69 | 362 |
%}. |
217 | 363 |
"time passes spent in some other process ... |
10 | 364 |
... here again" |
365 |
||
217 | 366 |
p := activeProcess. |
367 |
activeProcess := oldProcess. |
|
369 | 368 |
currentPriority := oldProcess priority. |
213 | 369 |
|
69 | 370 |
ok ifFalse:[ |
159 | 371 |
" |
372 |
switch failed for some reason - |
|
373 |
destroy the bad process |
|
374 |
" |
|
375 |
p id ~~ 0 ifTrue:[ |
|
362 | 376 |
'SCHEDULER: problem with process ' errorPrint. |
360 | 377 |
p id errorPrint. |
378 |
p name notNil ifTrue:[ |
|
379 |
' (' errorPrint. p name errorPrint. ')' errorPrint. |
|
380 |
]. |
|
381 |
'; hard-terminate it.' errorPrintNL. |
|
159 | 382 |
p state:#suspended. |
383 |
self terminateNoSignal:p. |
|
384 |
] |
|
69 | 385 |
]. |
10 | 386 |
zombie notNil ifTrue:[ |
159 | 387 |
self class threadDestroy:zombie. |
388 |
zombie := nil |
|
93 | 389 |
]. |
390 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
24 | 391 |
! |
392 |
||
393 |
scheduleForInterrupt:aProcess |
|
217 | 394 |
"make aProcess evaluate its pushed interrupt block(s)" |
24 | 395 |
|
32 | 396 |
|id| |
24 | 397 |
|
398 |
aProcess isNil ifTrue:[^ self]. |
|
25 | 399 |
aProcess == activeProcess ifTrue:[^ self]. |
24 | 400 |
|
401 |
id := aProcess id. |
|
93 | 402 |
self class threadInterrupt:id. |
403 |
" |
|
404 |
and, make the process runnable |
|
405 |
" |
|
326 | 406 |
aProcess state ~~ #stopped ifTrue:[ |
407 |
" |
|
408 |
and, make the process runnable |
|
409 |
" |
|
410 |
aProcess resume |
|
411 |
] |
|
1 | 412 |
! ! |
413 |
||
414 |
!ProcessorScheduler methodsFor:'constants'! |
|
415 |
||
13 | 416 |
lowestPriority |
417 |
"return the lowest priority value" |
|
10 | 418 |
|
419 |
^ 1 "do not change this - its not variable" |
|
1 | 420 |
! |
421 |
||
13 | 422 |
highestPriority |
335 | 423 |
"return the highest priority value (normal) processes can have." |
10 | 424 |
|
93 | 425 |
"must be below schedulingPriority - |
426 |
otherwise scheduler could be blocked ... |
|
427 |
" |
|
241 | 428 |
^ HighestPriority |
10 | 429 |
! |
430 |
||
431 |
schedulingPriority |
|
335 | 432 |
"return the priority at which the scheduler runs." |
10 | 433 |
|
93 | 434 |
"must be above highestPriority - |
435 |
otherwise scheduler could be blocked ... |
|
436 |
" |
|
437 |
^ SchedulingPriority |
|
1 | 438 |
! |
439 |
||
440 |
userInterruptPriority |
|
93 | 441 |
"return the priority, at which the event scheduler runs - i.e. |
335 | 442 |
all processes running at a lower priority are interruptable by Cntl-C |
443 |
or the timer. Processes running at higher prio will not be interrupted." |
|
13 | 444 |
|
93 | 445 |
^ UserInterruptPriority |
1 | 446 |
! |
447 |
||
448 |
timingPriority |
|
13 | 449 |
"return the priority, at which all timing takes place (messageTally, |
450 |
delay etc.)" |
|
10 | 451 |
|
13 | 452 |
^ TimingPriority |
1 | 453 |
! |
454 |
||
455 |
userSchedulingPriority |
|
10 | 456 |
"return the priority, at which all normal user (interactive) processing |
457 |
takes place" |
|
458 |
||
459 |
^ UserSchedulingPriority |
|
1 | 460 |
! |
461 |
||
462 |
userBackgroundPriority |
|
335 | 463 |
"return the priority, at which background user (non-interactive) processing |
464 |
should take place. |
|
465 |
Not currently used - for ST80 compatibility only" |
|
13 | 466 |
|
1 | 467 |
^ 6 |
468 |
! |
|
469 |
||
470 |
systemBackgroundPriority |
|
335 | 471 |
"return the priority, at which background system processing |
472 |
should take place. |
|
473 |
Not currently used - for ST80 compatibility only" |
|
13 | 474 |
|
1 | 475 |
^ 4 |
76 | 476 |
! |
477 |
||
478 |
lowIOPriority |
|
479 |
"not currently used - for ST80 compatibility only" |
|
480 |
||
481 |
^ 2 "claus: is this ok ?" |
|
1 | 482 |
! ! |
483 |
||
484 |
!ProcessorScheduler methodsFor:'private initializing'! |
|
485 |
||
486 |
initialize |
|
487 |
"initialize the one-and-only ProcessorScheduler" |
|
488 |
||
375 | 489 |
|nPrios "{ Class: SmallInteger }" |
490 |
l p| |
|
1 | 491 |
|
159 | 492 |
KnownProcesses isNil ifTrue:[ |
493 |
KnownProcesses := WeakArray new:10. |
|
494 |
KnownProcesses watcher:self class. |
|
495 |
KnownProcessIds := OrderedCollection new. |
|
496 |
]. |
|
497 |
||
335 | 498 |
" |
499 |
create a collection with process lists; accessed using the priority as key |
|
500 |
" |
|
93 | 501 |
nPrios := SchedulingPriority. |
13 | 502 |
quiescentProcessLists := Array new:nPrios. |
375 | 503 |
1 to:nPrios do:[:pri | |
504 |
quiescentProcessLists at:pri put:(LinkedList new) |
|
505 |
]. |
|
1 | 506 |
|
231 | 507 |
readFdArray := Array with:nil. |
508 |
readCheckArray := Array with:nil. |
|
509 |
readSemaphoreArray := Array with:nil. |
|
510 |
writeFdArray := Array with:nil. |
|
511 |
writeSemaphoreArray := Array with:nil. |
|
512 |
timeoutArray := Array with:nil. |
|
513 |
timeoutSemaphoreArray := Array with:nil. |
|
514 |
timeoutActionArray := Array with:nil. |
|
515 |
timeoutProcessArray := Array with:nil. |
|
27 | 516 |
anyTimeouts := false. |
10 | 517 |
dispatching := false. |
302 | 518 |
useIOInterrupts := OperatingSystem supportsIOInterrupts. |
10 | 519 |
|
77 | 520 |
" |
521 |
handcraft the first (dispatcher-) process - this one will never |
|
10 | 522 |
block, but go into a select if there is nothing to do. |
77 | 523 |
Also, it has a prio of max+1 - thus, it comes first when looking |
524 |
for a runnable process. |
|
525 |
" |
|
93 | 526 |
currentPriority := SchedulingPriority. |
527 |
p := Process new. |
|
159 | 528 |
p setId:0 state:#run. |
529 |
p setPriority:currentPriority. |
|
93 | 530 |
p name:'scheduler'. |
1 | 531 |
|
93 | 532 |
scheduler := activeProcess := p. |
1 | 533 |
|
375 | 534 |
(quiescentProcessLists at:currentPriority) add:p. |
1 | 535 |
|
335 | 536 |
" |
537 |
let me handle IO and timer interrupts |
|
538 |
" |
|
2 | 539 |
ObjectMemory ioInterruptHandler:self. |
10 | 540 |
ObjectMemory timerInterruptHandler:self. |
541 |
! |
|
542 |
||
159 | 543 |
reinitialize |
335 | 544 |
"all previous processes (except those marked as restartable) are made dead |
545 |
- each object should reinstall its process(s) upon restart; |
|
546 |
especially, windowgroups have to. |
|
547 |
In contrast to ST-80, restartable processes are restarted at the beginning |
|
548 |
NOT continued where left. This is a consequence of the portable implementation |
|
549 |
of ST/X, since in order to continue a process, we needed to know the |
|
550 |
internals of the machines (and C-compilers) stack layout. |
|
551 |
This was not done, favouring portability for process continuation. |
|
552 |
In praxis, this is not much of a problem, since in almost every case, |
|
553 |
the computation state can be saved in some object, and processing be |
|
554 |
restarted from scratch, reinitializing things from this saved state." |
|
159 | 555 |
|
335 | 556 |
|processesToRestart| |
557 |
||
558 |
" |
|
559 |
lay all processes to rest, collect restartable ones |
|
560 |
" |
|
561 |
processesToRestart := OrderedCollection new. |
|
159 | 562 |
KnownProcesses do:[:p | |
563 |
p notNil ifTrue:[ |
|
564 |
"how, exactly should this be done ?" |
|
10 | 565 |
|
335 | 566 |
p isRestartable == true ifTrue:[ |
339 | 567 |
p nextLink:nil. |
335 | 568 |
processesToRestart add:p |
339 | 569 |
] ifFalse:[ |
570 |
p setId:nil state:#dead |
|
571 |
] |
|
159 | 572 |
]. |
573 |
]. |
|
339 | 574 |
scheduler setId:nil state:#dead. |
159 | 575 |
|
576 |
" |
|
577 |
now, start from scratch |
|
578 |
" |
|
579 |
KnownProcesses := nil. |
|
335 | 580 |
self initialize. |
581 |
||
582 |
" |
|
583 |
... and restart those that can be. |
|
584 |
" |
|
585 |
processesToRestart do:[:p | |
|
337 | 586 |
"/ 'process restart not implemented' errorPrintNL. |
587 |
p restart |
|
335 | 588 |
] |
1 | 589 |
! ! |
590 |
||
591 |
!ProcessorScheduler methodsFor:'private'! |
|
592 |
||
593 |
remember:aProcess |
|
44 | 594 |
"remember aProcess for later disposal (where the underlying |
595 |
system resources have to be freed)." |
|
596 |
||
243 | 597 |
|newShadow oldId wasBlocked |
598 |
oldSize "{ Class: SmallInteger }" |
|
599 |
index "{ Class: SmallInteger }" |
|
600 |
sz "{ Class: SmallInteger }" | |
|
1 | 601 |
|
93 | 602 |
wasBlocked := OperatingSystem blockInterrupts. |
1 | 603 |
index := 1. |
604 |
sz := KnownProcessIds size. |
|
605 |
[index <= sz] whileTrue:[ |
|
159 | 606 |
(KnownProcesses at:index) isNil ifTrue:[ |
607 |
oldId := KnownProcessIds at:index. |
|
608 |
oldId notNil ifTrue:[ |
|
609 |
self class threadDestroy:oldId. |
|
610 |
]. |
|
611 |
KnownProcesses at:index put:aProcess. |
|
612 |
KnownProcessIds at:index put:aProcess id. |
|
613 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
614 |
^ self |
|
615 |
]. |
|
616 |
index := index + 1 |
|
1 | 617 |
]. |
618 |
||
619 |
KnownProcessIds grow:index. |
|
620 |
KnownProcessIds at:index put:aProcess id. |
|
621 |
||
622 |
oldSize := KnownProcesses size. |
|
623 |
(index > oldSize) ifTrue:[ |
|
159 | 624 |
newShadow := WeakArray new:(oldSize * 2). |
625 |
newShadow watcher:self class. |
|
626 |
newShadow replaceFrom:1 with:KnownProcesses. |
|
627 |
KnownProcesses := newShadow |
|
1 | 628 |
]. |
69 | 629 |
KnownProcesses at:index put:aProcess. |
93 | 630 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
10 | 631 |
! |
632 |
||
633 |
unRemember:aProcess |
|
44 | 634 |
"forget aProcess - dispose processing will not consider this one" |
635 |
||
93 | 636 |
|index wasBlocked| |
10 | 637 |
|
93 | 638 |
wasBlocked := OperatingSystem blockInterrupts. |
10 | 639 |
index := KnownProcesses identityIndexOf:aProcess. |
640 |
index ~~ 0 ifTrue:[ |
|
159 | 641 |
KnownProcessIds at:index put:nil. |
642 |
KnownProcesses at:index put:nil. |
|
93 | 643 |
]. |
644 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1 | 645 |
! ! |
646 |
||
647 |
!ProcessorScheduler methodsFor:'process creation'! |
|
648 |
||
339 | 649 |
newProcessFor:aProcess withId:idWant |
650 |
"private entry for Process restart - do not use in your program" |
|
651 |
||
652 |
(self class threadCreate:aProcess withId:idWant) ~~ idWant ifTrue:[ |
|
653 |
^ false |
|
654 |
]. |
|
655 |
||
656 |
aProcess state:#light. "meaning: has no stack yet" |
|
657 |
self remember:aProcess. |
|
658 |
^ true |
|
659 |
! |
|
660 |
||
159 | 661 |
newProcessFor:aProcess |
662 |
"create a physical (VM-) process for aProcess. |
|
663 |
Return true if ok, false if something went wrong. |
|
339 | 664 |
The process is not scheduled; to start it running, |
665 |
it needs a Process>>resume. Once resumed, the process will later |
|
666 |
get control in its #start method." |
|
10 | 667 |
|
159 | 668 |
|id| |
1 | 669 |
|
339 | 670 |
id := self class threadCreate:aProcess withId:nil. |
159 | 671 |
id isNil ifTrue:[^ false]. |
672 |
||
673 |
aProcess setId:id state:#light. "meaning: has no stack yet" |
|
674 |
self remember:aProcess. |
|
675 |
^ true |
|
1 | 676 |
! ! |
677 |
||
678 |
!ProcessorScheduler methodsFor:'scheduling'! |
|
679 |
||
680 |
reschedule |
|
159 | 681 |
"switch to the highest prio runnable process. |
682 |
The scheduler itself is always runnable, so we can do an unconditional switch |
|
683 |
to that one. This method is a historical left-over and will vanish." |
|
1 | 684 |
|
93 | 685 |
^ self threadSwitch:scheduler |
1 | 686 |
! |
687 |
||
688 |
yield |
|
689 |
"move the currently running process to the end of the currentList |
|
690 |
and reschedule to the first in the list, thus switching to the |
|
691 |
next same-prio-process." |
|
692 |
||
93 | 693 |
|l wasBlocked| |
1 | 694 |
|
93 | 695 |
wasBlocked := OperatingSystem blockInterrupts. |
369 | 696 |
|
697 |
" |
|
698 |
debugging consistency check - will be removed later |
|
699 |
" |
|
700 |
activeProcess priority ~~ currentPriority ifTrue:[ |
|
701 |
'oops process changed priority' errorPrintNL. |
|
702 |
currentPriority := activeProcess priority. |
|
703 |
]. |
|
704 |
||
13 | 705 |
l := quiescentProcessLists at:currentPriority. |
93 | 706 |
|
707 |
" |
|
708 |
debugging consistency checks - will be removed later |
|
709 |
" |
|
710 |
l isEmpty ifTrue:[ |
|
159 | 711 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
712 |
'oops - empty runnable list' errorPrintNL. |
|
713 |
^ self |
|
1 | 714 |
]. |
69 | 715 |
|
231 | 716 |
" |
717 |
check if the running process is not the only one |
|
69 | 718 |
" |
231 | 719 |
l size ~~ 1 ifTrue:[ |
241 | 720 |
" |
721 |
bring running process to the end |
|
722 |
" |
|
723 |
l removeFirst. |
|
724 |
l addLast:activeProcess. |
|
69 | 725 |
|
241 | 726 |
" |
727 |
and switch to first in the list |
|
728 |
" |
|
729 |
self threadSwitch:(l first). |
|
231 | 730 |
]. |
93 | 731 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1 | 732 |
! |
733 |
||
734 |
suspend:aProcess |
|
10 | 735 |
"remove the argument, aProcess from the list of runnable processes. |
736 |
If the process is the current one, reschedule." |
|
1 | 737 |
|
181 | 738 |
|pri l p wasBlocked| |
1 | 739 |
|
93 | 740 |
" |
741 |
some debugging stuff |
|
742 |
" |
|
743 |
aProcess isNil ifTrue:[ |
|
159 | 744 |
MiniDebugger enterWithMessage:'nil suspend'. |
745 |
^ self |
|
93 | 746 |
]. |
747 |
aProcess id isNil ifTrue:[ |
|
159 | 748 |
MiniDebugger enterWithMessage:'bad suspend: already dead'. |
749 |
self threadSwitch:scheduler. |
|
750 |
^ self |
|
93 | 751 |
]. |
752 |
aProcess == scheduler ifTrue:[ |
|
339 | 753 |
'scheduler should never be suspended' errorPrintNL. |
217 | 754 |
MiniDebugger enterWithMessage:'scheduler should never be suspended'. |
159 | 755 |
^ self |
93 | 756 |
]. |
69 | 757 |
|
93 | 758 |
wasBlocked := OperatingSystem blockInterrupts. |
13 | 759 |
|
1 | 760 |
pri := aProcess priority. |
93 | 761 |
l := quiescentProcessLists at:pri. |
1 | 762 |
|
181 | 763 |
"notice: this is slightly faster than putting the if-code into |
764 |
the ifAbsent block, because [] is a shared cheap block |
|
765 |
" |
|
766 |
(l remove:aProcess ifAbsent:[]) isNil ifTrue:[ |
|
159 | 767 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
339 | 768 |
'bad suspend: not on run list' errorPrintNL. |
159 | 769 |
"/ MiniDebugger enterWithMessage:'bad suspend: not on run list'. |
770 |
self threadSwitch:scheduler. |
|
771 |
^ self |
|
69 | 772 |
]. |
773 |
||
93 | 774 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1 | 775 |
|
93 | 776 |
" |
777 |
this is a bit of a kludge: allow someone else to |
|
159 | 778 |
set the state to something like #ioWait etc. |
779 |
In this case, do not set to #suspend. |
|
93 | 780 |
All of this to enhance the output of the process monitor ... |
781 |
" |
|
159 | 782 |
aProcess setStateTo:#suspended if:#active or:#run. |
783 |
||
25 | 784 |
(aProcess == activeProcess) ifTrue:[ |
159 | 785 |
"we can immediately switch sometimes" |
375 | 786 |
l notEmpty ifTrue:[ |
159 | 787 |
p := l first |
788 |
] ifFalse:[ |
|
789 |
p := scheduler |
|
790 |
]. |
|
791 |
self threadSwitch:p |
|
69 | 792 |
]. |
1 | 793 |
! |
794 |
||
795 |
resume:aProcess |
|
93 | 796 |
"set aProcess runnable - |
797 |
if its prio is higher than the currently running prio, switch to it." |
|
1 | 798 |
|
93 | 799 |
|l pri wasBlocked| |
1 | 800 |
|
231 | 801 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
13 | 802 |
|
803 |
"ignore, if process is already dead" |
|
804 |
aProcess id isNil ifTrue:[^ self]. |
|
805 |
||
93 | 806 |
wasBlocked := OperatingSystem blockInterrupts. |
69 | 807 |
|
1 | 808 |
pri := aProcess priority. |
809 |
||
13 | 810 |
l := quiescentProcessLists at:pri. |
375 | 811 |
"if already running, ignore" |
812 |
(l identityIndexOf:aProcess) ~~ 0 ifTrue:[ |
|
813 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
814 |
^ self |
|
1 | 815 |
]. |
816 |
l addLast:aProcess. |
|
93 | 817 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1 | 818 |
|
819 |
(pri > currentPriority) ifTrue:[ |
|
159 | 820 |
" |
821 |
its prio is higher; immediately transfer control to it |
|
822 |
" |
|
823 |
self threadSwitch:aProcess |
|
13 | 824 |
] ifFalse:[ |
159 | 825 |
" |
826 |
its prio is lower; it will have to wait for a while ... |
|
827 |
" |
|
828 |
aProcess state:#run |
|
1 | 829 |
] |
830 |
! |
|
831 |
||
93 | 832 |
resumeForSingleSend:aProcess |
833 |
"like resume, but let the process execute a single send only. |
|
308 | 834 |
This will be used by the (new, not yet released) debugger |
835 |
for single stepping." |
|
93 | 836 |
|
231 | 837 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
308 | 838 |
aProcess singleStep:true. |
839 |
self resume:aProcess |
|
1 | 840 |
! |
841 |
||
159 | 842 |
terminateNoSignal:aProcess |
843 |
"hard terminate aProcess without sending the terminate signal, thus |
|
844 |
no unwind blocks or exitAction are performed in the process.. |
|
845 |
If its not the current process, it is simply removed from its list |
|
846 |
and physically destroyed. Otherwise (since we can't take away the chair |
|
847 |
we are sitting on), a switch is forced and the process |
|
848 |
will be physically destroyed by the next running process. |
|
849 |
(see zombie handling)" |
|
1 | 850 |
|
93 | 851 |
|pri id l wasBlocked| |
1 | 852 |
|
853 |
aProcess isNil ifTrue:[^ self]. |
|
854 |
id := aProcess id. |
|
855 |
id isNil ifTrue:[^ self]. "already dead" |
|
856 |
||
159 | 857 |
aProcess setId:nil state:#dead. |
1 | 858 |
|
93 | 859 |
wasBlocked := OperatingSystem blockInterrupts. |
69 | 860 |
|
13 | 861 |
"remove the process from the runnable list" |
69 | 862 |
|
13 | 863 |
pri := aProcess priority. |
864 |
l := quiescentProcessLists at:pri. |
|
375 | 865 |
(l identityIndexOf:aProcess) ~~ 0 ifTrue:[ |
159 | 866 |
l remove:aProcess. |
1 | 867 |
]. |
93 | 868 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1 | 869 |
|
25 | 870 |
aProcess == activeProcess ifTrue:[ |
159 | 871 |
" |
872 |
hard case - its the currently running process |
|
873 |
we must have the next active process destroy this one |
|
874 |
(we cannot destroy the chair we are sitting on ... :-) |
|
875 |
" |
|
876 |
zombie := id. |
|
877 |
self unRemember:aProcess. |
|
878 |
self threadSwitch:scheduler. |
|
879 |
"not reached" |
|
880 |
^ self |
|
10 | 881 |
]. |
13 | 882 |
self class threadDestroy:id. |
10 | 883 |
self unRemember:aProcess. |
884 |
^ self |
|
1 | 885 |
! |
886 |
||
159 | 887 |
terminateActiveNoSignal |
888 |
"hard terminate the active process, without sending any |
|
889 |
terminate signal thus no unwind blocks are evaluated." |
|
890 |
||
891 |
self terminateNoSignal:activeProcess |
|
892 |
! |
|
93 | 893 |
|
159 | 894 |
processTermination |
895 |
"sent by VM if the current process finished its startup block |
|
212 | 896 |
without proper process termination. Lay him to rest now. |
159 | 897 |
This can only happen, if something went wrong in Block>>newProcess, |
898 |
since the block defined there always terminates itself." |
|
899 |
||
900 |
self terminateNoSignal:activeProcess. |
|
93 | 901 |
self threadSwitch:scheduler |
159 | 902 |
! |
903 |
||
904 |
terminate:aProcess |
|
212 | 905 |
"terminate aProcess. This is donen by sending aProcess the terminateSignal, |
159 | 906 |
which will evaluate any unwind blocks and finally do a hard terminate." |
907 |
||
908 |
aProcess terminate |
|
93 | 909 |
! |
910 |
||
69 | 911 |
terminateActive |
212 | 912 |
"terminate the current process (i.e. the running process kills itself). |
159 | 913 |
The active process is sent the terminateSignal so it will evaluate any |
320 | 914 |
unwind blocks and finally do a hard terminate. |
915 |
This is sent for regular termination and by the VM, if the hard-stack limit |
|
916 |
is reached. (i.e. a process did not repair things in a recursionInterrupt and |
|
917 |
continued to grow its stack)" |
|
69 | 918 |
|
159 | 919 |
activeProcess terminate |
69 | 920 |
! |
921 |
||
93 | 922 |
interruptActive |
212 | 923 |
"interrupt the current process" |
93 | 924 |
|
925 |
activeProcess interrupt |
|
926 |
! |
|
927 |
||
115 | 928 |
changePriority:prio for:aProcess |
1 | 929 |
"change the priority of aProcess" |
930 |
||
115 | 931 |
|oldList newList oldPrio newPrio wasBlocked| |
1 | 932 |
|
933 |
oldPrio := aProcess priority. |
|
115 | 934 |
oldPrio == prio ifTrue:[^ self]. |
935 |
||
936 |
" |
|
937 |
check for valid argument |
|
938 |
" |
|
939 |
newPrio := prio. |
|
940 |
newPrio < 1 ifTrue:[ |
|
159 | 941 |
newPrio := 1. |
115 | 942 |
] ifFalse:[ |
213 | 943 |
aProcess == scheduler ifTrue:[^ self]. |
241 | 944 |
newPrio > HighestPriority ifTrue:[ |
945 |
newPrio := HighestPriority |
|
159 | 946 |
] |
115 | 947 |
]. |
2 | 948 |
|
93 | 949 |
wasBlocked := OperatingSystem blockInterrupts. |
69 | 950 |
|
1 | 951 |
aProcess setPriority:newPrio. |
952 |
||
13 | 953 |
oldList := quiescentProcessLists at:oldPrio. |
375 | 954 |
(oldList identityIndexOf:aProcess) == 0 ifTrue:[ |
159 | 955 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
956 |
^ self |
|
69 | 957 |
]. |
958 |
||
959 |
oldList remove:aProcess. |
|
13 | 960 |
|
69 | 961 |
newList := quiescentProcessLists at:newPrio. |
962 |
newList addLast:aProcess. |
|
93 | 963 |
|
69 | 964 |
"if its the current process lowering its prio |
965 |
or another one raising, we have to reschedule" |
|
10 | 966 |
|
69 | 967 |
aProcess == activeProcess ifTrue:[ |
159 | 968 |
currentPriority := newPrio. |
969 |
newPrio < oldPrio ifTrue:[ |
|
970 |
self threadSwitch:scheduler. |
|
971 |
] |
|
69 | 972 |
] ifFalse:[ |
159 | 973 |
newPrio > currentPriority ifTrue:[ |
974 |
self threadSwitch:aProcess. |
|
975 |
] |
|
93 | 976 |
]. |
977 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1 | 978 |
! ! |
979 |
||
980 |
!ProcessorScheduler methodsFor:'accessing'! |
|
981 |
||
982 |
currentPriority |
|
10 | 983 |
"return the priority of the currently running process" |
984 |
||
1 | 985 |
^ currentPriority |
986 |
||
987 |
"Processor currentPriority" |
|
988 |
! |
|
989 |
||
69 | 990 |
activePriority |
991 |
"return the priority of the currently running process. |
|
159 | 992 |
GNU-ST & ST-80 compatibility; this is the same as currentPriority" |
69 | 993 |
|
994 |
^ currentPriority |
|
995 |
! |
|
996 |
||
25 | 997 |
activeProcess |
10 | 998 |
"return the currently running process" |
999 |
||
25 | 1000 |
^ activeProcess |
1 | 1001 |
|
25 | 1002 |
"Processor activeProcess" |
181 | 1003 |
! |
1004 |
||
1005 |
interruptedProcess |
|
1006 |
"returns the process which was interrupted by the active one" |
|
1007 |
||
1008 |
^ interruptedProcess |
|
10 | 1009 |
! ! |
1010 |
||
1011 |
!ProcessorScheduler methodsFor:'queries'! |
|
1 | 1012 |
|
13 | 1013 |
highestPriorityRunnableProcess |
10 | 1014 |
"return the highest prio runnable process" |
1015 |
||
339 | 1016 |
|listArray l p prio "{ Class: SmallInteger }" | |
1 | 1017 |
|
339 | 1018 |
prio := HighestPriority. |
241 | 1019 |
listArray := quiescentProcessLists. |
339 | 1020 |
[prio >= 1] whileTrue:[ |
241 | 1021 |
l := listArray at:prio. |
375 | 1022 |
l notEmpty ifTrue:[ |
1023 |
p := l first. |
|
1024 |
" |
|
1025 |
if it got corrupted somehow ... |
|
1026 |
" |
|
1027 |
p id isNil ifTrue:[ |
|
1028 |
'process with nil id removed' errorPrintNL. |
|
1029 |
l removeFirst. |
|
1030 |
^ nil. |
|
159 | 1031 |
]. |
375 | 1032 |
^ p |
339 | 1033 |
]. |
1034 |
prio := prio - 1 |
|
10 | 1035 |
]. |
1036 |
^ nil |
|
217 | 1037 |
! |
1038 |
||
241 | 1039 |
isSystemProcess:aProcess |
1040 |
"return true if aProcess is a system process, |
|
1041 |
which should not be suspended/terminated etc.." |
|
1042 |
||
1043 |
(self class isPureEventDriven |
|
1044 |
or:[aProcess id == 0 |
|
357 | 1045 |
or:[(Display notNil and:[Display dispatchProcess == aProcess]) |
1046 |
" nameOrId endsWith:'dispatcher' " |
|
1047 |
]]) ifTrue:[ |
|
241 | 1048 |
^ true |
1049 |
]. |
|
1050 |
^ false |
|
1051 |
||
1052 |
" |
|
1053 |
Processor activeProcessIsSystemProcess |
|
1054 |
" |
|
1055 |
! |
|
1056 |
||
217 | 1057 |
activeProcessIsSystemProcess |
1058 |
"return true if the active process is a system process, |
|
1059 |
which should not be suspended." |
|
1060 |
||
241 | 1061 |
^ self isSystemProcess:activeProcess |
217 | 1062 |
|
1063 |
" |
|
1064 |
Processor activeProcessIsSystemProcess |
|
1065 |
" |
|
10 | 1066 |
! ! |
1067 |
||
1068 |
!ProcessorScheduler methodsFor:'dispatching'! |
|
1069 |
||
1070 |
dispatchLoop |
|
93 | 1071 |
"central dispatch loop; the scheduler process is always staying in |
1072 |
this method, looping forever." |
|
10 | 1073 |
|
231 | 1074 |
"avoid confusion if entered twice" |
1075 |
||
10 | 1076 |
dispatching == true ifTrue:[^ self]. |
1077 |
dispatching := true. |
|
93 | 1078 |
|
1079 |
"I made this an extra call to dispatch; this allows recompilation |
|
1080 |
of the dispatch-handling code in the running system. |
|
1081 |
" |
|
10 | 1082 |
[true] whileTrue:[ |
585
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1083 |
AbortSignal handle:[:ex | |
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1084 |
ex return |
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1085 |
] do:[ |
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1086 |
self dispatch |
33141ab22234
catch abortSignal in dispatcher
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1087 |
] |
1 | 1088 |
] |
1089 |
! |
|
1090 |
||
10 | 1091 |
dispatch |
93 | 1092 |
"It handles timeouts and switches to the highest prio runnable process" |
44 | 1093 |
|
13 | 1094 |
|any millis pri p nActions "{ Class: SmallInteger }" | |
10 | 1095 |
|
93 | 1096 |
" |
1097 |
handle all timeout actions |
|
1098 |
" |
|
27 | 1099 |
anyTimeouts ifTrue:[ |
159 | 1100 |
self evaluateTimeouts |
10 | 1101 |
]. |
1102 |
||
231 | 1103 |
"first do a quick check for semaphores using checkActions - this is needed for |
302 | 1104 |
devices like the X-connection, where some events might be in the event |
1105 |
queue. Without these checks, a select might block even though there is work to do |
|
93 | 1106 |
" |
10 | 1107 |
any := false. |
231 | 1108 |
nActions := readCheckArray size. |
10 | 1109 |
1 to:nActions do:[:index | |
374 | 1110 |
|checkBlock sema| |
10 | 1111 |
|
231 | 1112 |
checkBlock := readCheckArray at:index. |
159 | 1113 |
(checkBlock notNil and:[checkBlock value]) ifTrue:[ |
231 | 1114 |
sema := readSemaphoreArray at:index. |
159 | 1115 |
sema notNil ifTrue:[ |
1116 |
sema signalOnce. |
|
1117 |
]. |
|
1118 |
any := true. |
|
1119 |
] |
|
10 | 1120 |
]. |
1121 |
||
231 | 1122 |
"now, someone might be runnable ..." |
10 | 1123 |
|
13 | 1124 |
p := self highestPriorityRunnableProcess. |
10 | 1125 |
p isNil ifTrue:[ |
159 | 1126 |
"no one runnable, hard wait for event or timeout" |
10 | 1127 |
|
159 | 1128 |
self waitForEventOrTimeout. |
1129 |
^ self |
|
10 | 1130 |
]. |
1131 |
||
13 | 1132 |
pri := p priority. |
1133 |
||
302 | 1134 |
" |
1135 |
want to give control to the process p. |
|
10 | 1136 |
If the switched-to processes priority is lower than the |
1137 |
userSchedulingPriority, we have to make certain, that the |
|
1138 |
next input or timer will bring us back for a reschedule. |
|
1139 |
This is done by enabling ioInterrupts for all file descriptors. |
|
302 | 1140 |
If ioInterrupts are not available (OS does not support them), |
1141 |
we schedule a timer interrupt to interrupt us after 1/20s of a second |
|
1142 |
- effectively polling the filedescriptors 20 times a second. |
|
1143 |
(which is bad, since low prio processes will be hurt in performance) |
|
1144 |
Therefore, dont let benchmarks run with low prio ... |
|
1145 |
||
231 | 1146 |
Higher prio processes must be suspended, |
302 | 1147 |
same prio ones must yield or suspend to get back control |
1148 |
" |
|
10 | 1149 |
|
93 | 1150 |
" |
1151 |
uncommenting this will make timeouts interrupt the current process |
|
1152 |
(i.e. as if the interrupt runs at TimingPrio); |
|
1153 |
if left commented, they are handled at UserSchedulingPrio. |
|
13 | 1154 |
this will all change, when timeouts are removed and all is process driven |
231 | 1155 |
(a future version will have a process running to handle a timeout queue) |
13 | 1156 |
" |
1157 |
||
1158 |
" |
|
1159 |
pri < TimingPriority ifTrue:[ |
|
159 | 1160 |
anyTimeouts ifTrue:[ |
1161 |
millis := self timeToNextTimeout. |
|
1162 |
millis == 0 ifTrue:[^ self]. |
|
1163 |
] |
|
13 | 1164 |
]. |
1165 |
" |
|
1166 |
||
93 | 1167 |
" |
1168 |
if the process to run has a lower than UserInterruptPriority, |
|
302 | 1169 |
arrange for an interrupt to occur on I/O. |
231 | 1170 |
This is done by enabling IO-signals (if the OS supports them) |
93 | 1171 |
or by installing a poll-interrupt after 50ms (if the OS does not). |
1172 |
" |
|
1173 |
pri < UserInterruptPriority ifTrue:[ |
|
1174 |
||
1175 |
"comment out this if above is uncommented" |
|
159 | 1176 |
anyTimeouts ifTrue:[ |
1177 |
millis := self timeToNextTimeout. |
|
1178 |
millis == 0 ifTrue:[^ self]. |
|
1179 |
]. |
|
93 | 1180 |
"---" |
10 | 1181 |
|
302 | 1182 |
useIOInterrupts ifTrue:[ |
231 | 1183 |
readFdArray do:[:fd | |
159 | 1184 |
fd notNil ifTrue:[ |
1185 |
OperatingSystem enableIOInterruptsOn:fd |
|
1186 |
]. |
|
1187 |
]. |
|
1188 |
] ifFalse:[ |
|
1189 |
millis notNil ifTrue:[ |
|
1190 |
millis := millis min:50 |
|
1191 |
] ifFalse:[ |
|
1192 |
millis := 50 |
|
1193 |
] |
|
1194 |
] |
|
13 | 1195 |
]. |
10 | 1196 |
|
13 | 1197 |
millis notNil ifTrue:[ |
159 | 1198 |
"schedule a clock interrupt after millis milliseconds" |
1199 |
OperatingSystem enableTimer:millis rounded. |
|
10 | 1200 |
]. |
1201 |
||
231 | 1202 |
" |
1203 |
now let the process run - will come back here by reschedule |
|
1204 |
from ioInterrupt or timerInterrupt ... (running at max+1) |
|
1205 |
" |
|
10 | 1206 |
self threadSwitch:p. |
1207 |
||
1208 |
"... when we arrive here, we are back on stage" |
|
1209 |
||
1210 |
millis notNil ifTrue:[ |
|
159 | 1211 |
OperatingSystem disableTimer. |
1212 |
self checkForInputWithTimeout:0. |
|
10 | 1213 |
] |
1214 |
! ! |
|
1 | 1215 |
|
10 | 1216 |
!ProcessorScheduler methodsFor:'waiting'! |
1217 |
||
1218 |
ioInterrupt |
|
159 | 1219 |
"data arrived while waiting - switch to scheduler process which will decide |
1220 |
what to do now." |
|
10 | 1221 |
|
181 | 1222 |
interruptedProcess := activeProcess. |
93 | 1223 |
self threadSwitch:scheduler |
10 | 1224 |
! |
1225 |
||
1226 |
timerInterrupt |
|
159 | 1227 |
"timer expired while waiting - switch to scheduler process which will decide |
1228 |
what to do now." |
|
10 | 1229 |
|
181 | 1230 |
interruptedProcess := activeProcess. |
93 | 1231 |
self threadSwitch:scheduler |
10 | 1232 |
! |
1233 |
||
1234 |
timeToNextTimeout |
|
1235 |
"return the delta-T (in millis) to next timeout, or nil if |
|
1236 |
there is none" |
|
1237 |
||
231 | 1238 |
|aTime now delta minDelta n "{ Class: SmallInteger }"| |
10 | 1239 |
|
1240 |
"find next timeout. since there are usually not many, just search. |
|
302 | 1241 |
If there were many, the list should be kept sorted ... keeping deltas |
10 | 1242 |
to next (as in Unix kernel)" |
1243 |
||
231 | 1244 |
n := timeoutArray size. |
10 | 1245 |
1 to:n do:[:index | |
231 | 1246 |
aTime := timeoutArray at:index. |
159 | 1247 |
aTime notNil ifTrue:[ |
231 | 1248 |
now isNil ifTrue:[ |
159 | 1249 |
now := OperatingSystem getMillisecondTime. |
231 | 1250 |
]. |
1251 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[^ 0]. |
|
1252 |
delta := OperatingSystem millisecondTimeDeltaBetween:aTime and:now. |
|
1253 |
minDelta isNil ifTrue:[ |
|
1254 |
minDelta := delta |
|
159 | 1255 |
] ifFalse:[ |
231 | 1256 |
minDelta := minDelta min:delta |
159 | 1257 |
] |
1258 |
] |
|
10 | 1259 |
]. |
1260 |
||
1261 |
^ minDelta |
|
1262 |
! |
|
1263 |
||
1264 |
waitForEventOrTimeout |
|
44 | 1265 |
"entered when no process is runnable - wait for either input on |
1266 |
any file descriptors to arrive or a timeout to happen. |
|
1267 |
If it makes sense, do some background garbage collection. |
|
1268 |
The idle actions are a leftover from previous ST/X releases and will |
|
93 | 1269 |
vanish (installing a low-prio process has the same effect)." |
44 | 1270 |
|
271 | 1271 |
|millis doingGC| |
10 | 1272 |
|
1273 |
doingGC := true. |
|
1274 |
[doingGC] whileTrue:[ |
|
159 | 1275 |
anyTimeouts ifTrue:[ |
1276 |
millis := self timeToNextTimeout. |
|
1277 |
(millis notNil and:[millis <= 0]) ifTrue:[ |
|
1278 |
^ self "oops - hurry up checking" |
|
1279 |
]. |
|
1280 |
]. |
|
10 | 1281 |
|
362 | 1282 |
" |
1283 |
if its worth doing, collect a bit of garbage; |
|
1284 |
but not, if a backgroundCollector is active |
|
1285 |
" |
|
1286 |
ObjectMemory backgroundCollectorRunning ifTrue:[ |
|
1287 |
doingGC := false |
|
1288 |
] ifFalse:[ |
|
1289 |
doingGC := ObjectMemory gcStepIfUseful. |
|
1290 |
]. |
|
10 | 1291 |
|
159 | 1292 |
"then do idle actions" |
231 | 1293 |
(idleActions notNil and:[idleActions size ~~ 0]) ifTrue:[ |
159 | 1294 |
idleActions do:[:aBlock | |
1295 |
aBlock value. |
|
1296 |
]. |
|
1297 |
^ self "go back checking" |
|
1298 |
]. |
|
10 | 1299 |
|
159 | 1300 |
doingGC ifTrue:[ |
1301 |
(self checkForInputWithTimeout:0) ifTrue:[ |
|
1302 |
^ self "go back checking" |
|
1303 |
] |
|
1304 |
] |
|
10 | 1305 |
]. |
1306 |
||
49 | 1307 |
(self checkForInputWithTimeout:0) ifTrue:[ |
159 | 1308 |
^ self "go back checking" |
49 | 1309 |
]. |
1310 |
||
231 | 1311 |
"absolutely nothing to do - simply wait" |
10 | 1312 |
|
49 | 1313 |
OperatingSystem supportsSelect ifFalse:[ |
159 | 1314 |
"SCO instant ShitStation has a bug here, |
1315 |
waiting always 1 sec in the select - therefore we delay a bit and |
|
1316 |
return - effectively polling in 50ms cycles |
|
1317 |
" |
|
1318 |
OperatingSystem millisecondDelay:50. |
|
1319 |
^ self |
|
10 | 1320 |
]. |
1321 |
||
1322 |
millis isNil ifTrue:[ |
|
159 | 1323 |
millis := 9999. |
1 | 1324 |
] ifFalse:[ |
159 | 1325 |
millis := millis rounded |
10 | 1326 |
]. |
1327 |
self checkForInputWithTimeout:millis |
|
1328 |
! |
|
1329 |
||
1330 |
checkForInputWithTimeout:millis |
|
93 | 1331 |
"this is called, when there is absolutely nothing to do; |
1332 |
hard wait for either input to arrive or a timeout to occur." |
|
44 | 1333 |
|
10 | 1334 |
|fd index sema action| |
1335 |
||
93 | 1336 |
fd := OperatingSystem |
231 | 1337 |
selectOnAnyReadable:readFdArray |
1338 |
writable:writeFdArray |
|
159 | 1339 |
exception:nil |
1340 |
withTimeOut:millis. |
|
10 | 1341 |
fd notNil ifTrue:[ |
231 | 1342 |
index := readFdArray indexOf:fd. |
159 | 1343 |
index ~~ 0 ifTrue:[ |
231 | 1344 |
sema := readSemaphoreArray at:index. |
159 | 1345 |
sema notNil ifTrue:[ |
1346 |
sema signalOnce. |
|
1347 |
^ true |
|
1348 |
] ifFalse:[ |
|
231 | 1349 |
action := readCheckArray at:index. |
159 | 1350 |
action notNil ifTrue:[ |
1351 |
action value. |
|
1352 |
^ true |
|
1353 |
] |
|
1354 |
] |
|
1355 |
] |
|
10 | 1356 |
]. |
1357 |
^ false |
|
1358 |
! ! |
|
1359 |
||
93 | 1360 |
!ProcessorScheduler methodsFor:'semaphore signalling'! |
10 | 1361 |
|
93 | 1362 |
signal:aSemaphore onInput:aFileDescriptor |
1363 |
"arrange for a semaphore to be triggered when input on aFileDescriptor |
|
1364 |
arrives." |
|
44 | 1365 |
|
93 | 1366 |
self signal:aSemaphore onInput:aFileDescriptor orCheck:nil |
10 | 1367 |
! |
1368 |
||
93 | 1369 |
signal:aSemaphore onInput:aFileDescriptor orCheck:aBlock |
1370 |
"arrange for a semaphore to be triggered when input on aFileDescriptor |
|
1371 |
arrives OR checkblock evaluates to true. |
|
69 | 1372 |
(checkBlock is used for buffered input, where a select may not detect |
1373 |
data already read into a buffer - as in Xlib)" |
|
44 | 1374 |
|
243 | 1375 |
|idx "{ Class: SmallInteger }" |
1376 |
wasBlocked| |
|
10 | 1377 |
|
93 | 1378 |
wasBlocked := OperatingSystem blockInterrupts. |
443 | 1379 |
(readFdArray identityIndexOf:aFileDescriptor startingAt:1) == 0 ifTrue:[ |
1380 |
idx := readFdArray identityIndexOf:nil startingAt:1. |
|
159 | 1381 |
idx ~~ 0 ifTrue:[ |
231 | 1382 |
readFdArray at:idx put:aFileDescriptor. |
1383 |
readSemaphoreArray at:idx put:aSemaphore. |
|
1384 |
readCheckArray at:idx put:aBlock |
|
159 | 1385 |
] ifFalse:[ |
231 | 1386 |
readFdArray := readFdArray copyWith:aFileDescriptor. |
1387 |
readSemaphoreArray := readSemaphoreArray copyWith:aSemaphore. |
|
1388 |
readCheckArray := readCheckArray copyWith:aBlock. |
|
159 | 1389 |
] |
10 | 1390 |
]. |
93 | 1391 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1392 |
! |
|
1393 |
||
1394 |
signal:aSemaphore onOutput:aFileDescriptor |
|
1395 |
"arrange for a semaphore to be triggered when output on aFileDescriptor |
|
1396 |
is possible. (i.e. can be written without blocking)" |
|
1397 |
||
243 | 1398 |
|idx "{ Class: SmallInteger }" |
1399 |
wasBlocked| |
|
93 | 1400 |
|
1401 |
wasBlocked := OperatingSystem blockInterrupts. |
|
443 | 1402 |
(writeFdArray identityIndexOf:aFileDescriptor startingAt:1) == 0 ifTrue:[ |
1403 |
idx := writeFdArray identityIndexOf:nil startingAt:1. |
|
159 | 1404 |
idx ~~ 0 ifTrue:[ |
231 | 1405 |
writeFdArray at:idx put:aFileDescriptor. |
1406 |
writeSemaphoreArray at:idx put:aSemaphore. |
|
159 | 1407 |
] ifFalse:[ |
231 | 1408 |
writeFdArray := writeFdArray copyWith:aFileDescriptor. |
1409 |
writeSemaphoreArray := writeSemaphoreArray copyWith:aSemaphore. |
|
159 | 1410 |
] |
93 | 1411 |
]. |
1412 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1413 |
! |
|
1414 |
||
1415 |
signal:aSemaphore afterSeconds:seconds |
|
1416 |
"arrange for a semaphore to be triggered after some seconds" |
|
1417 |
||
1418 |
self signal:aSemaphore afterMilliseconds:(seconds * 1000) |
|
1419 |
! |
|
1420 |
||
1421 |
signal:aSemaphore afterMilliseconds:millis |
|
1422 |
"arrange for a semaphore to be triggered after some milliseconds" |
|
1423 |
||
1424 |
|now then wasBlocked| |
|
1425 |
||
1426 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1427 |
now := OperatingSystem getMillisecondTime. |
|
1428 |
then := OperatingSystem millisecondTimeAdd:now and:millis rounded. |
|
1429 |
self signal:aSemaphore atMilliseconds:then. |
|
1430 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1431 |
! |
|
1432 |
||
1433 |
signal:aSemaphore atMilliseconds:aMillisecondTime |
|
356 | 1434 |
"arrange for a semaphore to be triggered at a specific millisecond time. |
1435 |
If there is already a pending trigger time, the time is changed." |
|
93 | 1436 |
|
243 | 1437 |
|index "{ Class: SmallInteger }" |
1438 |
wasBlocked| |
|
93 | 1439 |
|
1440 |
wasBlocked := OperatingSystem blockInterrupts. |
|
443 | 1441 |
index := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
93 | 1442 |
index ~~ 0 ifTrue:[ |
231 | 1443 |
timeoutArray at:index put:aMillisecondTime |
93 | 1444 |
] ifFalse:[ |
443 | 1445 |
index := timeoutArray identityIndexOf:nil startingAt:1. |
159 | 1446 |
index ~~ 0 ifTrue:[ |
231 | 1447 |
timeoutSemaphoreArray at:index put:aSemaphore. |
1448 |
timeoutArray at:index put:aMillisecondTime. |
|
1449 |
timeoutActionArray at:index put:nil. |
|
1450 |
timeoutProcessArray at:index put:nil |
|
159 | 1451 |
] ifFalse:[ |
231 | 1452 |
timeoutSemaphoreArray := timeoutSemaphoreArray copyWith:aSemaphore. |
1453 |
timeoutArray := timeoutArray copyWith:aMillisecondTime. |
|
1454 |
timeoutActionArray := timeoutActionArray copyWith:nil. |
|
1455 |
timeoutProcessArray := timeoutProcessArray copyWith:nil |
|
159 | 1456 |
]. |
93 | 1457 |
]. |
356 | 1458 |
|
93 | 1459 |
anyTimeouts := true. |
1460 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
10 | 1461 |
! |
1462 |
||
1463 |
disableSemaphore:aSemaphore |
|
44 | 1464 |
"disable triggering of a semaphore" |
1465 |
||
243 | 1466 |
|idx "{ Class: SmallInteger }" |
1467 |
wasBlocked| |
|
10 | 1468 |
|
93 | 1469 |
wasBlocked := OperatingSystem blockInterrupts. |
443 | 1470 |
idx := readSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
1471 |
[idx ~~ 0] whileTrue:[ |
|
231 | 1472 |
readFdArray at:idx put:nil. |
1473 |
readSemaphoreArray at:idx put:nil. |
|
443 | 1474 |
readCheckArray at:idx put:nil. |
1475 |
idx := readSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
|
159 | 1476 |
]. |
443 | 1477 |
idx := writeSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
1478 |
[idx ~~ 0] whileTrue:[ |
|
231 | 1479 |
writeFdArray at:idx put:nil. |
1480 |
writeSemaphoreArray at:idx put:nil. |
|
443 | 1481 |
idx := writeSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
10 | 1482 |
]. |
443 | 1483 |
idx := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
1484 |
[idx ~~ 0] whileTrue:[ |
|
231 | 1485 |
timeoutArray at:idx put:nil. |
1486 |
timeoutSemaphoreArray at:idx put:nil. |
|
1487 |
timeoutActionArray at:idx put:nil. |
|
1488 |
timeoutProcessArray at:idx put:nil. |
|
443 | 1489 |
idx := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
10 | 1490 |
]. |
93 | 1491 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1492 |
! ! |
|
1493 |
||
1494 |
!ProcessorScheduler methodsFor:'background processing'! |
|
10 | 1495 |
|
93 | 1496 |
addIdleBlock:aBlock |
1497 |
"add the argument, aBlock to the list of idle-actions. |
|
1498 |
Idle blocks are evaluated whenever no other process is runnable, |
|
1499 |
and no events are pending. |
|
1500 |
Use of idle blocks is not recommended, use a low priority processes |
|
159 | 1501 |
instead, which has the same effect. Idle blcoks are still included |
1502 |
to support background actions in pure-event systems, where no processes |
|
1503 |
are available. |
|
93 | 1504 |
Support for idle-blocks may vanish." |
44 | 1505 |
|
93 | 1506 |
|wasBlocked| |
1507 |
||
1508 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1509 |
idleActions isNil ifTrue:[ |
|
159 | 1510 |
idleActions := OrderedCollection new |
93 | 1511 |
]. |
1512 |
idleActions add:aBlock. |
|
1513 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
10 | 1514 |
! |
1515 |
||
93 | 1516 |
removeIdleBlock:aBlock |
1517 |
"remove the argument, aBlock from the list of idle-blocks. |
|
1518 |
Support for idle-blocks may vanish - use low prio processes instead." |
|
44 | 1519 |
|
93 | 1520 |
|wasBlocked| |
10 | 1521 |
|
93 | 1522 |
wasBlocked := OperatingSystem blockInterrupts. |
1523 |
idleActions notNil ifTrue:[ |
|
1524 |
idleActions remove:aBlock |
|
13 | 1525 |
]. |
93 | 1526 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
10 | 1527 |
! ! |
1528 |
||
93 | 1529 |
!ProcessorScheduler methodsFor:'I/O event actions'! |
10 | 1530 |
|
159 | 1531 |
enableIOAction:aBlock onInput:aFileDescriptor |
93 | 1532 |
"half-obsolete event support: arrange for aBlock to be |
1533 |
evaluated when input on aFileDescriptor arrives. |
|
1534 |
This is a leftover support for pure-event systems and may vanish." |
|
44 | 1535 |
|
243 | 1536 |
|idx "{Class: SmallInteger }" |
1537 |
wasBlocked| |
|
10 | 1538 |
|
93 | 1539 |
wasBlocked := OperatingSystem blockInterrupts. |
243 | 1540 |
(readFdArray identityIndexOf:aFileDescriptor startingAt:1) == 0 ifTrue:[ |
443 | 1541 |
idx := readFdArray identityIndexOf:nil startingAt:1. |
159 | 1542 |
idx ~~ 0 ifTrue:[ |
231 | 1543 |
readFdArray at:idx put:aFileDescriptor. |
1544 |
readCheckArray at:idx put:aBlock. |
|
1545 |
readSemaphoreArray at:idx put:nil |
|
159 | 1546 |
] ifFalse:[ |
231 | 1547 |
readFdArray := readFdArray copyWith:aFileDescriptor. |
1548 |
readCheckArray := readCheckArray copyWith:aBlock. |
|
1549 |
readSemaphoreArray := readSemaphoreArray copyWith:nil. |
|
159 | 1550 |
] |
10 | 1551 |
]. |
93 | 1552 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
10 | 1553 |
! |
1554 |
||
13 | 1555 |
disableFd:aFileDescriptor |
93 | 1556 |
"disable block events on aFileDescriptor. |
1557 |
This is a leftover support for pure-event systems and may vanish." |
|
44 | 1558 |
|
243 | 1559 |
|idx "{Class: SmallInteger }" |
1560 |
wasBlocked| |
|
13 | 1561 |
|
93 | 1562 |
wasBlocked := OperatingSystem blockInterrupts. |
443 | 1563 |
idx := readFdArray identityIndexOf:aFileDescriptor startingAt:1. |
13 | 1564 |
idx ~~ 0 ifTrue:[ |
231 | 1565 |
readFdArray at:idx put:nil. |
1566 |
readCheckArray at:idx put:nil. |
|
1567 |
readSemaphoreArray at:idx put:nil |
|
13 | 1568 |
]. |
93 | 1569 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1570 |
! ! |
|
1571 |
||
159 | 1572 |
!ProcessorScheduler methodsFor:'timeout handling'! |
93 | 1573 |
|
1574 |
addTimedBlock:aBlock afterSeconds:delta |
|
1575 |
"add the argument, aBlock to the list of time-scheduled-blocks. |
|
1576 |
to be evaluated after delta seconds. The process which installs this timed |
|
1577 |
block will be interrupted for execution of the block. |
|
1578 |
(if it is running, the interrupt will occur in whatever method it is |
|
159 | 1579 |
executing; if it is suspended, it will be resumed). |
93 | 1580 |
The block will be removed from the timed-block list after evaluation |
1581 |
(i.e. it will trigger only once)." |
|
1582 |
||
1583 |
self addTimedBlock:aBlock for:activeProcess afterMilliseconds:(delta * 1000) rounded |
|
13 | 1584 |
! |
1585 |
||
93 | 1586 |
addTimedBlock:aBlock for:aProcess afterSeconds:delta |
1587 |
"add the argument, aBlock to the list of time-scheduled-blocks. |
|
1588 |
to be evaluated after delta seconds. aProcess will be interrupted for |
|
1589 |
execution of the block. |
|
1590 |
(if it is running, the interrupt will occur in whatever method it is |
|
159 | 1591 |
executing; if it is suspended, it will be resumed). |
1592 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
1593 |
(which is dangerous - the block should not raise any error conditions). |
|
93 | 1594 |
The block will be removed from the timed-block list after evaluation |
1595 |
(i.e. it will trigger only once)." |
|
10 | 1596 |
|
93 | 1597 |
self addTimedBlock:aBlock for:aProcess afterMilliseconds:(delta * 1000) rounded |
1598 |
! |
|
1599 |
||
1600 |
addTimedBlock:aBlock afterMilliseconds:delta |
|
1601 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1602 |
evaluated after delta milliseconds. The process which installs this timed |
|
1603 |
block will be interrupted for execution of the block. |
|
1604 |
(if it is running, the interrupt will occur in whatever method it is |
|
159 | 1605 |
executing; if it is suspended, it will be resumed). |
93 | 1606 |
The block will be removed from the timed-block list after evaluation |
1607 |
(i.e. it will trigger only once)." |
|
1608 |
||
1609 |
^ self addTimedBlock:aBlock for:activeProcess afterMilliseconds:delta |
|
10 | 1610 |
! |
1611 |
||
93 | 1612 |
addTimedBlock:aBlock for:aProcess afterMilliseconds:delta |
1613 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
159 | 1614 |
evaluated after delta milliseconds. The process specified by the argument, |
1615 |
aProcess will be interrupted for execution of the block. |
|
93 | 1616 |
(if it is running, the interrupt will occur in whatever method it is |
159 | 1617 |
executing; if it is suspended, it will be resumed). |
1618 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
1619 |
(which is dangerous - the block should not raise any error conditions). |
|
93 | 1620 |
The block will be removed from the timed-block list after evaluation |
1621 |
(i.e. it will trigger only once)." |
|
10 | 1622 |
|
93 | 1623 |
|now then wasBlocked| |
1624 |
||
1625 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1626 |
now := OperatingSystem getMillisecondTime. |
|
1627 |
then := OperatingSystem millisecondTimeAdd:now and:delta. |
|
1628 |
self addTimedBlock:aBlock for:aProcess atMilliseconds:then. |
|
1629 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1 | 1630 |
! |
1631 |
||
93 | 1632 |
addTimedBlock:aBlock atMilliseconds:aMillisecondTime |
1633 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1634 |
evaluated when the millisecondClock value passes aMillisecondTime. |
|
1635 |
The process which installs this timed block will be interrupted for |
|
1636 |
execution of the block. |
|
1637 |
(if it is running, the interrupt will occur in whatever method it is |
|
159 | 1638 |
executing; if it is suspended, it will be resumed). |
93 | 1639 |
The block will be removed from the timed-block list after evaluation |
1640 |
(i.e. it will trigger only once)." |
|
1641 |
||
1642 |
self addTimedBlock:aBlock for:activeProcess atMilliseconds:aMillisecondTime |
|
1643 |
! |
|
10 | 1644 |
|
93 | 1645 |
addTimedBlock:aBlock for:aProcess atMilliseconds:aMillisecondTime |
1646 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1647 |
evaluated by aProcess when the millisecondClock value passes |
|
356 | 1648 |
aMillisecondTime. |
1649 |
If that block is already in the timeout list, |
|
1650 |
its trigger-time is changed. |
|
1651 |
The process specified by the argument, aProcess will be interrupted |
|
1652 |
for execution of the block. |
|
1653 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
159 | 1654 |
(which is dangerous - the block should not raise any error conditions). |
356 | 1655 |
If the process is active at trigger time, the interrupt will occur in |
1656 |
whatever method it is executing; if suspended at trigger time, it will be |
|
1657 |
resumed. |
|
93 | 1658 |
The block will be removed from the timed-block list after evaluation |
1659 |
(i.e. it will trigger only once)." |
|
10 | 1660 |
|
243 | 1661 |
|index "{ Class: SmallInteger }" |
1662 |
wasBlocked| |
|
10 | 1663 |
|
93 | 1664 |
wasBlocked := OperatingSystem blockInterrupts. |
443 | 1665 |
index := timeoutActionArray identityIndexOf:aBlock startingAt:1. |
10 | 1666 |
index ~~ 0 ifTrue:[ |
231 | 1667 |
timeoutArray at:index put:aMillisecondTime |
10 | 1668 |
] ifFalse:[ |
231 | 1669 |
index := timeoutArray indexOf:nil. |
159 | 1670 |
index ~~ 0 ifTrue:[ |
231 | 1671 |
timeoutArray at:index put:aMillisecondTime. |
1672 |
timeoutActionArray at:index put:aBlock. |
|
1673 |
timeoutSemaphoreArray at:index put:nil. |
|
1674 |
timeoutProcessArray at:index put:aProcess |
|
159 | 1675 |
] ifFalse:[ |
231 | 1676 |
timeoutArray := timeoutArray copyWith:aMillisecondTime. |
1677 |
timeoutActionArray := timeoutActionArray copyWith:aBlock. |
|
1678 |
timeoutSemaphoreArray := timeoutSemaphoreArray copyWith:nil. |
|
1679 |
timeoutProcessArray := timeoutProcessArray copyWith:aProcess. |
|
159 | 1680 |
]. |
10 | 1681 |
]. |
356 | 1682 |
|
27 | 1683 |
anyTimeouts := true. |
93 | 1684 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
10 | 1685 |
! |
1686 |
||
1687 |
removeTimedBlock:aBlock |
|
93 | 1688 |
"remove the argument, aBlock from the list of time-sceduled-blocks." |
10 | 1689 |
|
243 | 1690 |
|index "{ Class: SmallInteger }" |
1691 |
wasBlocked| |
|
1 | 1692 |
|
93 | 1693 |
wasBlocked := OperatingSystem blockInterrupts. |
443 | 1694 |
index := timeoutActionArray identityIndexOf:aBlock startingAt:1. |
1 | 1695 |
(index ~~ 0) ifTrue:[ |
231 | 1696 |
timeoutArray at:index put:nil. |
1697 |
timeoutActionArray at:index put:nil. |
|
1698 |
timeoutSemaphoreArray at:index put:nil. |
|
1699 |
timeoutProcessArray at:index put:nil. |
|
10 | 1700 |
]. |
93 | 1701 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
159 | 1702 |
! |
1703 |
||
1704 |
evaluateTimeouts |
|
1705 |
"walk through timeouts and evaluate blocks or signal semas that need to be .." |
|
1706 |
||
1707 |
|sema now aTime block blocksToEvaluate |
|
1708 |
processes n "{ Class: SmallInteger }"| |
|
1709 |
||
1710 |
anyTimeouts ifFalse:[ ^ self]. |
|
1711 |
||
1712 |
"have to collect the blocks first, then evaluate them. This avoids |
|
1713 |
problems due to newly inserted blocks." |
|
1714 |
||
1715 |
now := OperatingSystem getMillisecondTime. |
|
1716 |
blocksToEvaluate := nil. |
|
231 | 1717 |
n := timeoutArray size. |
159 | 1718 |
anyTimeouts := false. |
1719 |
1 to:n do:[:index | |
|
231 | 1720 |
aTime := timeoutArray at:index. |
159 | 1721 |
aTime notNil ifTrue:[ |
1722 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[ |
|
1723 |
"this one should be triggered" |
|
1724 |
||
231 | 1725 |
sema := timeoutSemaphoreArray at:index. |
159 | 1726 |
sema notNil ifTrue:[ |
1727 |
sema signalOnce. |
|
231 | 1728 |
timeoutSemaphoreArray at:index put:nil |
159 | 1729 |
] ifFalse:[ |
1730 |
"to support pure-events" |
|
231 | 1731 |
block := timeoutActionArray at:index. |
159 | 1732 |
block notNil ifTrue:[ |
1733 |
blocksToEvaluate isNil ifTrue:[ |
|
1734 |
blocksToEvaluate := OrderedCollection new:10. |
|
1735 |
processes := OrderedCollection new:10. |
|
1736 |
]. |
|
1737 |
blocksToEvaluate add:block. |
|
231 | 1738 |
processes add:(timeoutProcessArray at:index). |
1739 |
timeoutActionArray at:index put:nil. |
|
1740 |
timeoutProcessArray at:index put:nil. |
|
159 | 1741 |
] |
1742 |
]. |
|
231 | 1743 |
timeoutArray at:index put:nil. |
159 | 1744 |
] ifTrue:[ |
1745 |
anyTimeouts := true |
|
1746 |
] |
|
1747 |
] |
|
1748 |
]. |
|
1749 |
||
1750 |
blocksToEvaluate notNil ifTrue:[ |
|
1751 |
blocksToEvaluate keysAndValuesDo:[:index :block | |
|
1752 |
|p| |
|
1753 |
||
1754 |
p := processes at:index. |
|
1755 |
(p isNil or:[PureEventDriven]) ifTrue:[ |
|
1756 |
block value |
|
1757 |
] ifFalse:[ |
|
1758 |
p interruptWith:block |
|
1759 |
] |
|
1760 |
] |
|
1761 |
] |
|
1 | 1762 |
! ! |