1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
13
|
14 |
instanceVariableNames:'quiescentProcessLists
|
|
15 |
zombie
|
1
|
16 |
currentProcess currentPriority
|
10
|
17 |
readFds readSemaphores readChecks
|
|
18 |
writeFds writeSemaphores writeChecks
|
|
19 |
timeouts timeoutActions timeoutSemaphores
|
|
20 |
idleActions nTimeouts dispatching'
|
|
21 |
classVariableNames:'KnownProcesses KnownProcessIds DefaultStackSize
|
|
22 |
PureEventDriven
|
13
|
23 |
UserSchedulingPriority TimingPriority'
|
1
|
24 |
poolDictionaries:''
|
|
25 |
category:'Kernel-Processes'
|
|
26 |
!
|
|
27 |
|
|
28 |
ProcessorScheduler comment:'
|
|
29 |
|
|
30 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
31 |
All Rights Reserved
|
|
32 |
|
13
|
33 |
$Header: /cvs/stx/stx/libbasic/ProcessorScheduler.st,v 1.5 1993-12-11 00:54:13 claus Exp $
|
1
|
34 |
'!
|
|
35 |
|
|
36 |
Smalltalk at:#Processor put:nil!
|
|
37 |
|
|
38 |
!ProcessorScheduler class methodsFor:'initialization'!
|
|
39 |
|
|
40 |
initialize
|
|
41 |
KnownProcesses isNil ifTrue:[
|
10
|
42 |
KnownProcesses := WeakArray new:5.
|
1
|
43 |
KnownProcesses watcher:self.
|
|
44 |
KnownProcessIds := OrderedCollection new.
|
|
45 |
|
|
46 |
"want to get informed when returning from snapshot"
|
|
47 |
ObjectMemory addDependent:self
|
|
48 |
].
|
|
49 |
|
10
|
50 |
Processor isNil ifTrue:[
|
|
51 |
"create the one and only processor"
|
1
|
52 |
|
10
|
53 |
Processor := self new.
|
|
54 |
].
|
13
|
55 |
PureEventDriven := self threadsAvailable not.
|
|
56 |
PureEventDriven ifTrue:[
|
|
57 |
'no process support - running event driven' printNewline
|
|
58 |
].
|
10
|
59 |
DefaultStackSize := 100*1024.
|
|
60 |
UserSchedulingPriority := 8.
|
13
|
61 |
TimingPriority := 16.
|
1
|
62 |
!
|
|
63 |
|
|
64 |
update:something
|
|
65 |
something == #returnFromSnapshot ifTrue:[
|
|
66 |
self reinstallProcesses
|
|
67 |
]
|
|
68 |
!
|
|
69 |
|
|
70 |
reinstallProcesses
|
|
71 |
"recreate all processes after a snapShot load"
|
|
72 |
|
10
|
73 |
KnownProcesses do:[:p |
|
1
|
74 |
p notNil ifTrue:[
|
|
75 |
"how, exactly should this be done ?"
|
|
76 |
|
|
77 |
p id ~~ 0 ifTrue:[
|
10
|
78 |
Transcript showCr:'process restart not implemented'
|
1
|
79 |
]
|
|
80 |
]
|
|
81 |
]
|
|
82 |
! !
|
|
83 |
|
10
|
84 |
!ProcessorScheduler class methodsFor:'instance creation'!
|
|
85 |
|
|
86 |
new
|
|
87 |
"there is (currently) only one processor ..."
|
|
88 |
|
|
89 |
Processor isNil ifTrue:[
|
|
90 |
Processor := self basicNew initialize
|
|
91 |
].
|
|
92 |
^ Processor.
|
|
93 |
! !
|
|
94 |
|
|
95 |
!ProcessorScheduler class methodsFor:'instance release'!
|
|
96 |
|
|
97 |
informDispose
|
|
98 |
"some Process has been collected - terminate the underlying thread"
|
|
99 |
|
|
100 |
|id sz "{ Class: SmallInteger }"|
|
1
|
101 |
|
10
|
102 |
sz := KnownProcessIds size.
|
|
103 |
1 to:sz do:[:index |
|
|
104 |
(KnownProcesses at:index) isNil ifTrue:[
|
|
105 |
id := KnownProcessIds at:index.
|
|
106 |
id notNil ifTrue:[
|
|
107 |
Transcript showCr:('terminate thread (no longer refd) ', id printString).
|
|
108 |
self threadDestroy:id.
|
|
109 |
KnownProcessIds at:index put:nil.
|
|
110 |
]
|
|
111 |
]
|
|
112 |
]
|
|
113 |
! !
|
|
114 |
|
|
115 |
!ProcessorScheduler class methodsFor:'queries'!
|
|
116 |
|
|
117 |
isPureEventDriven
|
|
118 |
"this is temporary - (maybe not :-).
|
|
119 |
you can run ST/X either with or without processes.
|
|
120 |
Without, there is conceptionally a single process handling all
|
|
121 |
outside events and timeouts. This has some negative implications
|
|
122 |
(Debugger is ugly), but allows a fully portable ST/X without any
|
|
123 |
assembler support - i.e. quick portability.
|
13
|
124 |
The PureEvent flag will automatically be set if the runtime system
|
|
125 |
does not support threads - otherwise, it can be set manually
|
|
126 |
(from rc-file).
|
10
|
127 |
"
|
|
128 |
|
|
129 |
^ PureEventDriven
|
|
130 |
!
|
|
131 |
|
|
132 |
pureEventDriven
|
13
|
133 |
"turn on pure-event driven mode - no processes, single dispatch loop"
|
|
134 |
|
10
|
135 |
PureEventDriven := true
|
|
136 |
!
|
|
137 |
|
|
138 |
processDriven
|
13
|
139 |
"turn on process driven mode"
|
|
140 |
|
10
|
141 |
PureEventDriven := false
|
|
142 |
! !
|
|
143 |
|
|
144 |
!ProcessorScheduler class methodsFor:'primitive process primitives'!
|
|
145 |
|
13
|
146 |
threadsAvailable
|
|
147 |
"return true, if the runtime system supports threads (i.e. processes);
|
|
148 |
false otherwise."
|
|
149 |
|
|
150 |
%{ /* NOCONTEXT */
|
|
151 |
extern OBJ __threadsAvailable();
|
|
152 |
|
|
153 |
RETURN (__threadsAvailable());
|
|
154 |
%}
|
|
155 |
!
|
|
156 |
|
10
|
157 |
threadCreate:aBlock stack:stackSize
|
1
|
158 |
"physical creation of a process executing aBlock.
|
10
|
159 |
(warning: low level entry, no administration done).
|
|
160 |
This may return nil, if process could not be created."
|
13
|
161 |
|
|
162 |
%{ /* NOCONTEXT */
|
1
|
163 |
int tid;
|
|
164 |
extern int __threadCreate();
|
|
165 |
|
10
|
166 |
if (_isSmallInteger(stackSize)) {
|
|
167 |
tid = __threadCreate(aBlock, _intVal(stackSize));
|
|
168 |
if (tid != 0) {
|
|
169 |
RETURN ( _MKSMALLINT(tid));
|
|
170 |
}
|
1
|
171 |
}
|
|
172 |
%}
|
13
|
173 |
.
|
|
174 |
^ nil
|
1
|
175 |
!
|
|
176 |
|
|
177 |
threadDestroy:id
|
|
178 |
"physical destroy other process ...
|
|
179 |
(warning: low level entry, no administration done)"
|
|
180 |
|
13
|
181 |
%{ /* NOCONTEXT */
|
|
182 |
|
1
|
183 |
if (_isSmallInteger(id)) {
|
|
184 |
__threadDestroy(_intVal(id));
|
|
185 |
}
|
|
186 |
%}
|
|
187 |
! !
|
|
188 |
|
10
|
189 |
!ProcessorScheduler methodsFor:'primitive process primitives'!
|
|
190 |
|
|
191 |
threadSwitch:aProcess
|
|
192 |
"continue execution in aProcess.
|
|
193 |
(warning: low level entry, no administration is done here)"
|
|
194 |
|
|
195 |
|id pri|
|
1
|
196 |
|
10
|
197 |
aProcess isNil ifTrue:[^ self].
|
|
198 |
aProcess == currentProcess ifTrue:[^ self].
|
1
|
199 |
|
10
|
200 |
id := aProcess id.
|
|
201 |
pri := aProcess priority.
|
|
202 |
|
13
|
203 |
aProcess state:#active.
|
1
|
204 |
|
10
|
205 |
"no interrupts now - currentProcess has already been changed
|
|
206 |
(dont add any message sends here)"
|
|
207 |
currentProcess := aProcess.
|
|
208 |
currentPriority := pri.
|
|
209 |
%{
|
|
210 |
__threadSwitch(__context, _intVal(id));
|
|
211 |
%}
|
|
212 |
.
|
|
213 |
"time passes ...
|
|
214 |
... here again"
|
|
215 |
|
|
216 |
zombie notNil ifTrue:[
|
|
217 |
self class threadDestroy:zombie.
|
|
218 |
zombie := nil
|
1
|
219 |
]
|
|
220 |
! !
|
|
221 |
|
|
222 |
!ProcessorScheduler methodsFor:'constants'!
|
|
223 |
|
13
|
224 |
lowestPriority
|
|
225 |
"return the lowest priority value"
|
10
|
226 |
|
|
227 |
^ 1 "do not change this - its not variable"
|
1
|
228 |
!
|
|
229 |
|
13
|
230 |
highestPriority
|
|
231 |
"return the highest priority value processes can have"
|
10
|
232 |
|
13
|
233 |
"must be below schedulingPriority - otherwise scheduler
|
|
234 |
could be blocked ..."
|
|
235 |
^ 30
|
10
|
236 |
!
|
|
237 |
|
|
238 |
schedulingPriority
|
|
239 |
"return the priority at which the scheduler runs"
|
|
240 |
|
13
|
241 |
"must be above highestPriority - otherwise scheduler
|
|
242 |
could be blocked ..."
|
1
|
243 |
^ 31
|
|
244 |
!
|
|
245 |
|
|
246 |
userInterruptPriority
|
13
|
247 |
"not currently used - for ST80 compatibility only"
|
|
248 |
|
1
|
249 |
^ 24
|
|
250 |
!
|
|
251 |
|
|
252 |
timingPriority
|
13
|
253 |
"return the priority, at which all timing takes place (messageTally,
|
|
254 |
delay etc.)"
|
10
|
255 |
|
13
|
256 |
^ TimingPriority
|
1
|
257 |
!
|
|
258 |
|
|
259 |
userSchedulingPriority
|
10
|
260 |
"return the priority, at which all normal user (interactive) processing
|
|
261 |
takes place"
|
|
262 |
|
|
263 |
^ UserSchedulingPriority
|
1
|
264 |
!
|
|
265 |
|
|
266 |
userBackgroundPriority
|
13
|
267 |
"not currently used - for ST80 compatibility only"
|
|
268 |
|
1
|
269 |
^ 6
|
|
270 |
!
|
|
271 |
|
|
272 |
systemBackgroundPriority
|
13
|
273 |
"not currently used - for ST80 compatibility only"
|
|
274 |
|
1
|
275 |
^ 4
|
|
276 |
! !
|
|
277 |
|
|
278 |
!ProcessorScheduler methodsFor:'private initializing'!
|
|
279 |
|
|
280 |
initialize
|
|
281 |
"initialize the one-and-only ProcessorScheduler"
|
|
282 |
|
|
283 |
|nPrios l|
|
|
284 |
|
10
|
285 |
nPrios := self schedulingPriority.
|
13
|
286 |
quiescentProcessLists := Array new:nPrios.
|
1
|
287 |
|
10
|
288 |
readFds := Array with:nil.
|
|
289 |
readChecks := Array with:nil.
|
|
290 |
readSemaphores := Array with:nil.
|
|
291 |
writeFds := Array with:nil.
|
|
292 |
writeChecks := Array with:nil.
|
|
293 |
writeSemaphores := Array with:nil.
|
|
294 |
timeouts := Array with:nil.
|
|
295 |
timeoutSemaphores := Array with:nil.
|
|
296 |
timeoutActions := Array with:nil.
|
|
297 |
nTimeouts := 0.
|
|
298 |
dispatching := false.
|
|
299 |
|
|
300 |
"handcraft the first (dispatcher-) process - this one will never
|
|
301 |
block, but go into a select if there is nothing to do.
|
|
302 |
Also it has a prio of max+1"
|
|
303 |
|
1
|
304 |
currentProcess := Process new.
|
13
|
305 |
currentProcess setId:0.
|
|
306 |
currentProcess name:'scheduler'.
|
|
307 |
currentProcess state:#run.
|
10
|
308 |
currentPriority := self schedulingPriority.
|
1
|
309 |
currentProcess setPriority:currentPriority.
|
|
310 |
|
|
311 |
l := LinkedList new.
|
|
312 |
l add:currentProcess.
|
|
313 |
|
13
|
314 |
quiescentProcessLists at:currentPriority put:l.
|
1
|
315 |
|
2
|
316 |
ObjectMemory ioInterruptHandler:self.
|
10
|
317 |
ObjectMemory timerInterruptHandler:self.
|
|
318 |
!
|
|
319 |
|
|
320 |
reInitialize
|
|
321 |
"all previous stuff is obsolete - each object should reinstall itself
|
|
322 |
upon restart."
|
|
323 |
|
|
324 |
|l|
|
|
325 |
|
|
326 |
KnownProcesses := WeakArray new:5.
|
13
|
327 |
KnownProcesses watcher:self class.
|
10
|
328 |
KnownProcessIds := OrderedCollection new.
|
|
329 |
|
|
330 |
"for now (cannot snapin processes)"
|
|
331 |
|
13
|
332 |
quiescentProcessLists := Array new:self schedulingPriority.
|
10
|
333 |
|
|
334 |
readFds := Array with:nil.
|
|
335 |
readChecks := Array with:nil.
|
|
336 |
readSemaphores := Array with:nil.
|
|
337 |
writeFds := Array with:nil.
|
|
338 |
writeChecks := Array with:nil.
|
|
339 |
writeSemaphores := Array with:nil.
|
|
340 |
dispatching := false.
|
|
341 |
|
|
342 |
"handcraft the first (dispatcher-) process - this one will never
|
|
343 |
block, but go into a select if there is nothing to do.
|
|
344 |
Also it has a prio of max+1"
|
|
345 |
|
|
346 |
currentProcess := Process new.
|
13
|
347 |
currentProcess setId:0.
|
|
348 |
currentProcess name:'scheduler'.
|
|
349 |
currentProcess state:#run.
|
10
|
350 |
currentPriority := self schedulingPriority.
|
|
351 |
currentProcess setPriority:currentPriority.
|
|
352 |
|
|
353 |
l := LinkedList new.
|
|
354 |
l add:currentProcess.
|
|
355 |
|
13
|
356 |
quiescentProcessLists at:currentPriority put:l.
|
10
|
357 |
|
|
358 |
ObjectMemory ioInterruptHandler:self.
|
|
359 |
ObjectMemory timerInterruptHandler:self.
|
1
|
360 |
! !
|
|
361 |
|
|
362 |
!ProcessorScheduler methodsFor:'private'!
|
|
363 |
|
|
364 |
remember:aProcess
|
2
|
365 |
|newShadow oldSize oldId
|
10
|
366 |
index "{ Class: SmallInteger }"
|
|
367 |
sz "{ Class: SmallInteger }" |
|
1
|
368 |
|
|
369 |
index := 1.
|
|
370 |
sz := KnownProcessIds size.
|
|
371 |
[index <= sz] whileTrue:[
|
|
372 |
(KnownProcesses at:index) isNil ifTrue:[
|
2
|
373 |
oldId := KnownProcessIds at:index.
|
1
|
374 |
oldId notNil ifTrue:[
|
10
|
375 |
self class threadDestroy:oldId.
|
1
|
376 |
].
|
|
377 |
KnownProcesses at:index put:aProcess.
|
|
378 |
KnownProcessIds at:index put:aProcess id.
|
|
379 |
^ self
|
|
380 |
].
|
|
381 |
index := index + 1
|
|
382 |
].
|
|
383 |
|
|
384 |
KnownProcessIds grow:index.
|
|
385 |
KnownProcessIds at:index put:aProcess id.
|
|
386 |
|
|
387 |
oldSize := KnownProcesses size.
|
|
388 |
(index > oldSize) ifTrue:[
|
10
|
389 |
newShadow := WeakArray new:(oldSize * 2).
|
13
|
390 |
newShadow watcher:self class.
|
1
|
391 |
newShadow replaceFrom:1 with:KnownProcesses.
|
|
392 |
KnownProcesses := newShadow
|
|
393 |
].
|
|
394 |
KnownProcesses at:index put:aProcess
|
10
|
395 |
!
|
|
396 |
|
|
397 |
unRemember:aProcess
|
|
398 |
|index|
|
|
399 |
|
|
400 |
index := KnownProcesses identityIndexOf:aProcess.
|
|
401 |
index ~~ 0 ifTrue:[
|
|
402 |
KnownProcessIds at:index put:nil.
|
|
403 |
KnownProcesses at:index put:nil.
|
|
404 |
]
|
1
|
405 |
! !
|
|
406 |
|
|
407 |
!ProcessorScheduler methodsFor:'process creation'!
|
|
408 |
|
|
409 |
newProcessFor:aBlock
|
10
|
410 |
"create a new process executing aBlock.
|
|
411 |
Return a process (or nil if fail). The new process is not scheduled.
|
|
412 |
To start it running, it needs a Process>>resume."
|
|
413 |
|
|
414 |
^ self newProcessFor:aBlock stack:DefaultStackSize
|
|
415 |
!
|
|
416 |
|
|
417 |
newProcessFor:aBlock stack:stackSize
|
1
|
418 |
"create a new process executing aBlock. Return a process (or
|
|
419 |
nil if fail). The new process is not scheduled. To start it
|
|
420 |
running, it needs a Process>>resume."
|
|
421 |
|
|
422 |
|id p|
|
|
423 |
|
10
|
424 |
id := self class threadCreate:aBlock stack:stackSize.
|
13
|
425 |
id isNil ifTrue:[
|
|
426 |
self error:'cannot create new Process'.
|
|
427 |
^ nil
|
1
|
428 |
].
|
13
|
429 |
p := Process new.
|
|
430 |
p setId:id.
|
|
431 |
p startBlock:aBlock.
|
|
432 |
p state:#light. "meaning: has no stack yet"
|
|
433 |
p setPriority:currentPriority.
|
|
434 |
self remember:p.
|
1
|
435 |
^ p
|
|
436 |
! !
|
|
437 |
|
|
438 |
!ProcessorScheduler methodsFor:'scheduling'!
|
|
439 |
|
|
440 |
reschedule
|
10
|
441 |
"switch to the highest prio runnable process
|
|
442 |
The scheduler itself is always runnable, so there is always a switch."
|
1
|
443 |
|
2
|
444 |
|l p|
|
1
|
445 |
|
10
|
446 |
(self schedulingPriority) to:1 by:-1 do:[:prio |
|
13
|
447 |
l := quiescentProcessLists at:prio.
|
1
|
448 |
l notNil ifTrue:[
|
10
|
449 |
p := l first.
|
|
450 |
p notNil ifTrue:[
|
13
|
451 |
currentProcess state == #active ifTrue:[
|
|
452 |
currentProcess state:#run.
|
|
453 |
].
|
10
|
454 |
^ self threadSwitch:p
|
|
455 |
].
|
13
|
456 |
quiescentProcessLists at:prio put:nil
|
1
|
457 |
]
|
|
458 |
].
|
10
|
459 |
"no process to run - this cannot happen"
|
1
|
460 |
|
10
|
461 |
self halt
|
1
|
462 |
!
|
|
463 |
|
|
464 |
yield
|
|
465 |
"move the currently running process to the end of the currentList
|
|
466 |
and reschedule to the first in the list, thus switching to the
|
|
467 |
next same-prio-process."
|
|
468 |
|
|
469 |
|l|
|
|
470 |
|
13
|
471 |
l := quiescentProcessLists at:currentPriority.
|
1
|
472 |
l isNil ifTrue:[
|
|
473 |
'oops - nil runnable list' printNewline.
|
|
474 |
^ self
|
|
475 |
].
|
|
476 |
l removeFirst.
|
13
|
477 |
l isEmpty ifTrue:[
|
|
478 |
l addLast:currentProcess.
|
|
479 |
^ self
|
|
480 |
].
|
1
|
481 |
l addLast:currentProcess.
|
13
|
482 |
currentProcess state:#run.
|
10
|
483 |
self threadSwitch:(l first).
|
1
|
484 |
!
|
|
485 |
|
|
486 |
suspend:aProcess
|
10
|
487 |
"remove the argument, aProcess from the list of runnable processes.
|
|
488 |
If the process is the current one, reschedule."
|
1
|
489 |
|
13
|
490 |
|pri l s|
|
1
|
491 |
|
|
492 |
aProcess isNil ifTrue:[self error:'nil suspend'. ^ self].
|
13
|
493 |
aProcess id isNil ifTrue:[self error:'bad suspend: already dead'. ^ self].
|
|
494 |
|
1
|
495 |
pri := aProcess priority.
|
|
496 |
|
13
|
497 |
l := quiescentProcessLists at:pri.
|
|
498 |
l isNil ifTrue:[self error:'bad suspend: not running'. ^ self].
|
1
|
499 |
|
|
500 |
l remove:aProcess ifAbsent:[self error:'bad suspend'. ^ self].
|
10
|
501 |
l isEmpty ifTrue:[
|
13
|
502 |
quiescentProcessLists at:pri put:nil.
|
10
|
503 |
l := nil
|
|
504 |
].
|
1
|
505 |
|
13
|
506 |
s := aProcess state.
|
|
507 |
((s == #active) or:[s == #run]) ifTrue:[
|
|
508 |
aProcess state:#suspended.
|
|
509 |
].
|
1
|
510 |
(aProcess == currentProcess) ifTrue:[
|
10
|
511 |
"can be done a bit faster sometimes"
|
|
512 |
l notNil ifTrue:[
|
|
513 |
self threadSwitch:(l first)
|
|
514 |
] ifFalse:[
|
|
515 |
self reschedule
|
|
516 |
]
|
1
|
517 |
]
|
|
518 |
!
|
|
519 |
|
|
520 |
resume:aProcess
|
|
521 |
"set aProcess runnable - if its prio is higher than the currently running prio,
|
|
522 |
reschedule."
|
|
523 |
|
|
524 |
|l pri|
|
|
525 |
|
|
526 |
aProcess == currentProcess ifTrue:[^ self].
|
|
527 |
aProcess isNil ifTrue:[^ self].
|
13
|
528 |
|
|
529 |
"ignore, if process is already dead"
|
|
530 |
aProcess id isNil ifTrue:[^ self].
|
|
531 |
|
1
|
532 |
pri := aProcess priority.
|
|
533 |
|
13
|
534 |
l := quiescentProcessLists at:pri.
|
1
|
535 |
l isNil ifTrue:[
|
|
536 |
l := LinkedList new.
|
13
|
537 |
quiescentProcessLists at:pri put:l
|
|
538 |
] ifFalse:[
|
|
539 |
"if already running, ignore"
|
|
540 |
(l includes:aProcess) ifTrue:[
|
|
541 |
^ self
|
|
542 |
]
|
1
|
543 |
].
|
|
544 |
l addLast:aProcess.
|
|
545 |
|
|
546 |
(pri > currentPriority) ifTrue:[
|
13
|
547 |
currentProcess state:#run.
|
10
|
548 |
self threadSwitch:aProcess
|
13
|
549 |
] ifFalse:[
|
|
550 |
aProcess state:#suspended
|
1
|
551 |
]
|
|
552 |
!
|
|
553 |
|
|
554 |
processTermination
|
|
555 |
"current process finished its startup block without termination,
|
|
556 |
lay him to rest now"
|
|
557 |
|
2
|
558 |
self terminate:currentProcess.
|
|
559 |
self reschedule
|
1
|
560 |
!
|
|
561 |
|
|
562 |
terminate:aProcess
|
|
563 |
"terminate aProcess. If its not the current process, its simply
|
|
564 |
removed from its list and destroyed. Otherwise, a switch is forced
|
|
565 |
and the process is destroyed by the next running process."
|
|
566 |
|
10
|
567 |
|pri id l state|
|
1
|
568 |
|
|
569 |
aProcess isNil ifTrue:[^ self].
|
|
570 |
id := aProcess id.
|
|
571 |
id isNil ifTrue:[^ self]. "already dead"
|
|
572 |
|
13
|
573 |
aProcess setId:nil.
|
|
574 |
aProcess startBlock:nil.
|
1
|
575 |
|
13
|
576 |
"remove the process from the runnable list"
|
|
577 |
pri := aProcess priority.
|
|
578 |
l := quiescentProcessLists at:pri.
|
|
579 |
(l notNil and:[l includes:aProcess]) ifTrue:[
|
|
580 |
l remove:aProcess.
|
|
581 |
l isEmpty ifTrue:[quiescentProcessLists at:pri put:nil].
|
1
|
582 |
].
|
|
583 |
|
13
|
584 |
aProcess exitAction notNil ifTrue:[
|
|
585 |
aProcess exitAction value.
|
|
586 |
aProcess exitAction:nil
|
|
587 |
].
|
|
588 |
|
|
589 |
aProcess state:#dead.
|
|
590 |
aProcess == currentProcess ifTrue:[
|
1
|
591 |
"hard case - its the currently running process
|
|
592 |
we must have the next active process destroy this one
|
|
593 |
"
|
|
594 |
zombie := id.
|
10
|
595 |
self unRemember:aProcess.
|
|
596 |
self reschedule.
|
|
597 |
^ self
|
|
598 |
].
|
13
|
599 |
self class threadDestroy:id.
|
10
|
600 |
self unRemember:aProcess.
|
|
601 |
^ self
|
1
|
602 |
!
|
|
603 |
|
|
604 |
changePriority:newPrio for:aProcess
|
|
605 |
"change the priority of aProcess"
|
|
606 |
|
|
607 |
|oldList newList oldPrio s|
|
|
608 |
|
|
609 |
oldPrio := aProcess priority.
|
|
610 |
oldPrio == newPrio ifTrue:[^ self].
|
2
|
611 |
|
1
|
612 |
aProcess setPriority:newPrio.
|
|
613 |
|
13
|
614 |
oldList := quiescentProcessLists at:oldPrio.
|
|
615 |
(oldList includes:aProcess) ifTrue:[
|
|
616 |
oldList remove:aProcess.
|
|
617 |
oldList isEmpty ifTrue:[quiescentProcessLists at:oldPrio put:nil].
|
|
618 |
|
|
619 |
newList := quiescentProcessLists at:newPrio.
|
1
|
620 |
newList isNil ifTrue:[
|
|
621 |
newList := LinkedList new.
|
13
|
622 |
quiescentProcessLists at:newPrio put:newList
|
1
|
623 |
].
|
|
624 |
newList addLast:aProcess.
|
10
|
625 |
|
13
|
626 |
"if its the current process lowering its prio
|
|
627 |
or another one raising, we have to reschedule"
|
10
|
628 |
|
|
629 |
aProcess == currentProcess ifTrue:[
|
|
630 |
currentPriority := newPrio.
|
|
631 |
newPrio < oldPrio ifTrue:[
|
|
632 |
self reschedule.
|
|
633 |
]
|
|
634 |
] ifFalse:[
|
2
|
635 |
newPrio > currentPriority ifTrue:[
|
13
|
636 |
currentProcess state:#run.
|
10
|
637 |
self threadSwitch:aProcess.
|
2
|
638 |
]
|
|
639 |
]
|
1
|
640 |
]
|
|
641 |
! !
|
|
642 |
|
|
643 |
!ProcessorScheduler methodsFor:'accessing'!
|
|
644 |
|
|
645 |
currentPriority
|
10
|
646 |
"return the priority of the currently running process"
|
|
647 |
|
1
|
648 |
^ currentPriority
|
|
649 |
|
|
650 |
"Processor currentPriority"
|
|
651 |
!
|
|
652 |
|
|
653 |
currentProcess
|
10
|
654 |
"return the currently running process"
|
|
655 |
|
1
|
656 |
^ currentProcess
|
|
657 |
|
|
658 |
"Processor currentProcess"
|
10
|
659 |
! !
|
|
660 |
|
|
661 |
!ProcessorScheduler methodsFor:'queries'!
|
1
|
662 |
|
13
|
663 |
highestPriorityRunnableProcess
|
10
|
664 |
"return the highest prio runnable process"
|
|
665 |
|
|
666 |
|l p maxPri "{ Class: SmallInteger }" |
|
1
|
667 |
|
13
|
668 |
maxPri := self highestPriority.
|
10
|
669 |
maxPri to:1 by:-1 do:[:prio |
|
13
|
670 |
l := quiescentProcessLists at:prio.
|
10
|
671 |
l notNil ifTrue:[
|
|
672 |
p := l first.
|
|
673 |
p notNil ifTrue:[^ p].
|
13
|
674 |
"in the fly clear out empty lists"
|
|
675 |
quiescentProcessLists at:prio put:nil
|
1
|
676 |
]
|
10
|
677 |
].
|
|
678 |
^ nil
|
|
679 |
! !
|
|
680 |
|
|
681 |
!ProcessorScheduler methodsFor:'dispatching'!
|
|
682 |
|
|
683 |
dispatchLoop
|
|
684 |
"dispatch forever - the main process is running here all the time"
|
|
685 |
|
|
686 |
dispatching == true ifTrue:[^ self].
|
|
687 |
dispatching := true.
|
|
688 |
[true] whileTrue:[
|
|
689 |
self dispatch
|
1
|
690 |
]
|
|
691 |
!
|
|
692 |
|
10
|
693 |
dispatch
|
13
|
694 |
|any millis pri p nActions "{ Class: SmallInteger }" |
|
10
|
695 |
|
|
696 |
"handle all timeout actions"
|
|
697 |
nTimeouts ~~ 0 ifTrue:[
|
|
698 |
self evaluateTimeouts
|
|
699 |
].
|
|
700 |
|
|
701 |
"first do a quick check using checkActions - this is needed for
|
|
702 |
devices like X-connection, where some events might be in the event
|
|
703 |
queue, so a select does not always help"
|
|
704 |
|
|
705 |
any := false.
|
|
706 |
nActions := readChecks size.
|
|
707 |
1 to:nActions do:[:index |
|
|
708 |
|checkBlock sema action|
|
|
709 |
|
|
710 |
checkBlock := readChecks at:index.
|
|
711 |
(checkBlock notNil and:[checkBlock value]) ifTrue:[
|
|
712 |
sema := readSemaphores at:index.
|
|
713 |
sema notNil ifTrue:[
|
|
714 |
sema signalOnce.
|
|
715 |
].
|
|
716 |
any := true.
|
|
717 |
]
|
|
718 |
].
|
|
719 |
|
|
720 |
"now, someone might be runnable:"
|
|
721 |
|
13
|
722 |
p := self highestPriorityRunnableProcess.
|
10
|
723 |
p isNil ifTrue:[
|
|
724 |
"no one runnable, hard wait for event or timeout"
|
|
725 |
|
|
726 |
self waitForEventOrTimeout.
|
|
727 |
^ self
|
|
728 |
].
|
|
729 |
|
13
|
730 |
pri := p priority.
|
|
731 |
|
10
|
732 |
"want to give control to another process p.
|
|
733 |
If the switched-to processes priority is lower than the
|
|
734 |
userSchedulingPriority, we have to make certain, that the
|
|
735 |
next input or timer will bring us back for a reschedule.
|
|
736 |
This is done by enabling ioInterrupts for all file descriptors.
|
|
737 |
If ioInterrupts are not available, we schedule a timer interrupt
|
|
738 |
to interrupt us after 1/20s of a second - effectively polling
|
|
739 |
the filedescriptors. - which is very bad, since low prio processes
|
|
740 |
will be hurt in performance - dont let benchmarks run with low prio ...
|
|
741 |
Higher prio processes must suspend, same prio ones must yield to
|
|
742 |
get back control"
|
|
743 |
|
13
|
744 |
"uncommenting this will make timeouts interrupt the current process
|
|
745 |
(i.e. run at TimingPrio); if commented, they run at UserSchedulingPrio.
|
|
746 |
this will all change, when timeouts are removed and all is process driven
|
|
747 |
"
|
|
748 |
|
|
749 |
"
|
|
750 |
pri < TimingPriority ifTrue:[
|
10
|
751 |
(nTimeouts ~~ 0) ifTrue:[
|
|
752 |
millis := self timeToNextTimeout.
|
13
|
753 |
millis == 0 ifTrue:[^ self].
|
|
754 |
]
|
|
755 |
].
|
|
756 |
"
|
|
757 |
pri < UserSchedulingPriority ifTrue:[
|
|
758 |
|
|
759 |
"comment out this if above is uncommented"
|
|
760 |
(nTimeouts ~~ 0) ifTrue:[
|
|
761 |
millis := self timeToNextTimeout.
|
|
762 |
millis == 0 ifTrue:[^ self].
|
10
|
763 |
].
|
|
764 |
|
|
765 |
OperatingSystem supportsIOInterrupts ifTrue:[
|
|
766 |
readFds do:[:fd |
|
|
767 |
fd notNil ifTrue:[
|
|
768 |
OperatingSystem enableIOInterruptsOn:fd
|
|
769 |
].
|
|
770 |
].
|
|
771 |
] ifFalse:[
|
|
772 |
millis notNil ifTrue:[
|
|
773 |
millis := millis min:50
|
|
774 |
] ifFalse:[
|
|
775 |
millis := 50
|
|
776 |
]
|
13
|
777 |
]
|
|
778 |
].
|
10
|
779 |
|
13
|
780 |
millis notNil ifTrue:[
|
|
781 |
"schedule a clock interrupt"
|
|
782 |
OperatingSystem enableTimer:millis rounded.
|
10
|
783 |
].
|
|
784 |
|
|
785 |
"now let the process run - will come back here by reschedule
|
|
786 |
from ioInterrupt or timerInterrupt ... (running at max+1)"
|
|
787 |
|
13
|
788 |
currentProcess state:#run.
|
10
|
789 |
self threadSwitch:p.
|
|
790 |
|
|
791 |
"... when we arrive here, we are back on stage"
|
|
792 |
|
|
793 |
millis notNil ifTrue:[
|
|
794 |
OperatingSystem disableTimer.
|
|
795 |
self checkForInputWithTimeout:0.
|
|
796 |
]
|
|
797 |
! !
|
1
|
798 |
|
10
|
799 |
!ProcessorScheduler methodsFor:'waiting'!
|
|
800 |
|
|
801 |
ioInterrupt
|
|
802 |
"data arrived while waiting - reschedule to bring dispatcher into play"
|
|
803 |
|
|
804 |
self reschedule
|
|
805 |
!
|
|
806 |
|
|
807 |
timerInterrupt
|
|
808 |
"timer expired while waiting - reschedule to bring dispatcher into play"
|
|
809 |
|
|
810 |
self reschedule
|
|
811 |
!
|
|
812 |
|
|
813 |
timeToNextTimeout
|
|
814 |
"return the delta-T (in millis) to next timeout, or nil if
|
|
815 |
there is none"
|
|
816 |
|
|
817 |
|aTime now minDelta n "{ Class: SmallInteger }"|
|
|
818 |
|
|
819 |
"find next timeout. since there are usually not many, just search.
|
|
820 |
If there where many, the list should be kept sorted ... keeping deltas
|
|
821 |
to next (as in Unix kernel)"
|
|
822 |
|
|
823 |
n := timeouts size.
|
|
824 |
1 to:n do:[:index |
|
|
825 |
aTime := timeouts at:index.
|
|
826 |
aTime notNil ifTrue:[
|
|
827 |
minDelta isNil ifTrue:[
|
|
828 |
now := OperatingSystem getMillisecondTime.
|
|
829 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[^ 0].
|
|
830 |
minDelta := OperatingSystem millisecondTimeDeltaBetween:aTime and:now.
|
|
831 |
] ifFalse:[
|
|
832 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[^ 0].
|
|
833 |
minDelta := minDelta min:(OperatingSystem millisecondTimeDeltaBetween:aTime and:now)
|
|
834 |
]
|
|
835 |
]
|
|
836 |
].
|
|
837 |
|
|
838 |
^ minDelta
|
|
839 |
!
|
|
840 |
|
|
841 |
waitForEventOrTimeout
|
|
842 |
|millis limit doingGC|
|
|
843 |
|
|
844 |
doingGC := true.
|
|
845 |
[doingGC] whileTrue:[
|
|
846 |
(nTimeouts ~~ 0) ifTrue:[
|
|
847 |
millis := self timeToNextTimeout.
|
|
848 |
(millis notNil and:[millis <= 0]) ifTrue:[
|
|
849 |
^ self "oops - hurry up checking"
|
|
850 |
].
|
|
851 |
].
|
|
852 |
|
|
853 |
"if its worth doing, collect a bit of garbage"
|
|
854 |
limit := ObjectMemory incrementalGCLimit.
|
|
855 |
doingGC := limit notNil and:[ObjectMemory oldSpaceAllocatedSinceLastGC > limit].
|
|
856 |
doingGC ifTrue:[
|
|
857 |
ObjectMemory gcStep.
|
|
858 |
].
|
|
859 |
|
|
860 |
"then do idle actions"
|
|
861 |
(idleActions size ~~ 0) ifTrue:[
|
|
862 |
idleActions do:[:aBlock |
|
|
863 |
aBlock value.
|
|
864 |
].
|
|
865 |
^ self "go back checking"
|
|
866 |
].
|
|
867 |
|
|
868 |
doingGC ifTrue:[
|
|
869 |
(self checkForInputWithTimeout:0) ifTrue:[
|
|
870 |
^ self "go back checking"
|
|
871 |
]
|
|
872 |
]
|
|
873 |
].
|
|
874 |
|
|
875 |
"no, really nothing to do - simply wait"
|
|
876 |
|
|
877 |
(OperatingSystem supportsSelect) ifFalse:[
|
|
878 |
"SCO instant ShitStation has a bug here,
|
|
879 |
waiting always 1 sec in the select - therefore we delay a bit and
|
|
880 |
return - effectively polling in 50ms cycles
|
|
881 |
"
|
|
882 |
OperatingSystem millisecondDelay:50.
|
|
883 |
^ self
|
|
884 |
].
|
|
885 |
|
|
886 |
millis isNil ifTrue:[
|
|
887 |
millis := 9999.
|
1
|
888 |
] ifFalse:[
|
10
|
889 |
millis := millis rounded
|
|
890 |
].
|
|
891 |
self checkForInputWithTimeout:millis
|
|
892 |
!
|
|
893 |
|
|
894 |
checkForInputWithTimeout:millis
|
|
895 |
|fd index sema action|
|
|
896 |
|
|
897 |
fd := OperatingSystem selectOnAnyReadable:readFds writable:writeFds error:nil withTimeOut:millis.
|
|
898 |
fd notNil ifTrue:[
|
|
899 |
index := readFds indexOf:fd.
|
|
900 |
index ~~ 0 ifTrue:[
|
|
901 |
sema := readSemaphores at:index.
|
|
902 |
sema notNil ifTrue:[
|
|
903 |
sema signalOnce
|
|
904 |
] ifFalse:[
|
|
905 |
action := readChecks at:index.
|
|
906 |
action notNil ifTrue:[
|
|
907 |
action value.
|
|
908 |
^ true
|
|
909 |
]
|
|
910 |
]
|
|
911 |
]
|
|
912 |
].
|
|
913 |
^ false
|
|
914 |
!
|
|
915 |
|
|
916 |
evaluateTimeouts
|
|
917 |
"walk through timeouts and evaluate blocks or signal semas those that need to be .."
|
|
918 |
|
|
919 |
|now aTime block blocksToEvaluate n "{ Class: SmallInteger }"|
|
|
920 |
|
|
921 |
nTimeouts == 0 ifTrue:[ ^ self].
|
|
922 |
|
|
923 |
"have to collect the blocks first, then evaluate them. This avoids
|
|
924 |
problems due to newly inserted blocks."
|
|
925 |
|
|
926 |
now := OperatingSystem getMillisecondTime.
|
|
927 |
blocksToEvaluate := nil.
|
|
928 |
n := timeouts size.
|
|
929 |
1 to:n do:[:index |
|
|
930 |
aTime := timeouts at:index.
|
|
931 |
aTime notNil ifTrue:[
|
|
932 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[
|
|
933 |
"this one should be triggered"
|
|
934 |
|
|
935 |
(timeoutSemaphores at:index) notNil ifTrue:[
|
|
936 |
(timeoutSemaphores at:index) signalOnce.
|
|
937 |
timeoutSemaphores at:index put:nil
|
|
938 |
] ifFalse:[
|
|
939 |
"to support pure-events"
|
|
940 |
block := timeoutActions at:index.
|
|
941 |
block notNil ifTrue:[
|
|
942 |
blocksToEvaluate isNil ifTrue:[
|
|
943 |
blocksToEvaluate := OrderedCollection with:block
|
|
944 |
] ifFalse:[
|
|
945 |
blocksToEvaluate add:block
|
|
946 |
].
|
|
947 |
timeoutActions at:index put:nil
|
|
948 |
]
|
|
949 |
].
|
|
950 |
timeouts at:index put:nil.
|
|
951 |
nTimeouts := nTimeouts - 1
|
|
952 |
]
|
|
953 |
]
|
|
954 |
].
|
|
955 |
|
|
956 |
blocksToEvaluate notNil ifTrue:[
|
|
957 |
blocksToEvaluate do:[:aBlock |
|
|
958 |
aBlock value
|
|
959 |
]
|
|
960 |
]
|
|
961 |
! !
|
|
962 |
|
|
963 |
!ProcessorScheduler methodsFor:'adding / removing'!
|
|
964 |
|
|
965 |
enableSemaphore:aSemaphore onInput:aFileDescriptor
|
|
966 |
self enableSemaphore:aSemaphore onInput:aFileDescriptor check:nil
|
|
967 |
!
|
|
968 |
|
|
969 |
enableSemaphore:aSemaphore onInput:aFileDescriptor check:aBlock
|
|
970 |
|idx|
|
|
971 |
|
|
972 |
OperatingSystem blockInterrupts.
|
|
973 |
(readFds includes:aFileDescriptor) ifFalse:[
|
|
974 |
idx := readFds indexOf:nil.
|
|
975 |
idx ~~ 0 ifTrue:[
|
|
976 |
readFds at:idx put:aFileDescriptor.
|
|
977 |
readSemaphores at:idx put:aSemaphore.
|
|
978 |
readChecks at:idx put:aBlock
|
1
|
979 |
] ifFalse:[
|
10
|
980 |
readFds := readFds copyWith:aFileDescriptor.
|
|
981 |
readSemaphores := readSemaphores copyWith:aSemaphore.
|
|
982 |
readChecks := readChecks copyWith:aBlock.
|
1
|
983 |
]
|
10
|
984 |
].
|
|
985 |
OperatingSystem unblockInterrupts.
|
|
986 |
!
|
|
987 |
|
|
988 |
disableSemaphore:aSemaphore
|
|
989 |
|idx|
|
|
990 |
|
|
991 |
OperatingSystem blockInterrupts.
|
|
992 |
idx := readSemaphores identityIndexOf:aSemaphore.
|
|
993 |
idx ~~ 0 ifTrue:[
|
|
994 |
readFds at:idx put:nil.
|
|
995 |
readSemaphores at:idx put:nil.
|
|
996 |
readChecks at:idx put:nil
|
|
997 |
].
|
|
998 |
idx := timeoutSemaphores identityIndexOf:aSemaphore.
|
|
999 |
idx ~~ 0 ifTrue:[
|
|
1000 |
timeouts at:idx put:nil.
|
|
1001 |
timeoutSemaphores at:idx put:nil.
|
13
|
1002 |
timeoutActions at:idx put:nil.
|
10
|
1003 |
nTimeouts := nTimeouts - 1.
|
|
1004 |
].
|
|
1005 |
OperatingSystem unblockInterrupts.
|
|
1006 |
!
|
|
1007 |
|
|
1008 |
enableSemaphore:aSemaphore afterSeconds:seconds
|
|
1009 |
self enableSemaphore:aSemaphore afterMilliseconds:(seconds * 1000)
|
|
1010 |
!
|
|
1011 |
|
|
1012 |
enableSemaphore:aSemaphore afterMilliseconds:millis
|
|
1013 |
|now then index|
|
|
1014 |
|
|
1015 |
now := OperatingSystem getMillisecondTime.
|
|
1016 |
then := OperatingSystem millisecondTimeAdd:now and:millis.
|
|
1017 |
|
13
|
1018 |
OperatingSystem blockInterrupts.
|
10
|
1019 |
index := timeoutSemaphores identityIndexOf:aSemaphore.
|
|
1020 |
index ~~ 0 ifTrue:[
|
|
1021 |
timeouts at:index put:then
|
|
1022 |
] ifFalse:[
|
|
1023 |
index := timeouts indexOf:nil.
|
|
1024 |
index ~~ 0 ifTrue:[
|
|
1025 |
timeoutSemaphores at:index put:aSemaphore.
|
|
1026 |
timeouts at:index put:then.
|
13
|
1027 |
timeoutActions at:index put:nil.
|
10
|
1028 |
] ifFalse:[
|
|
1029 |
timeoutSemaphores := timeoutSemaphores copyWith:aSemaphore.
|
|
1030 |
timeouts := timeouts copyWith:then.
|
|
1031 |
timeoutActions := timeoutActions copyWith:nil.
|
|
1032 |
].
|
|
1033 |
nTimeouts := nTimeouts + 1.
|
13
|
1034 |
].
|
|
1035 |
OperatingSystem unblockInterrupts.
|
10
|
1036 |
! !
|
|
1037 |
|
|
1038 |
!ProcessorScheduler methodsFor:'pure event support'!
|
|
1039 |
|
|
1040 |
enableIOAction:aBlock on:aFileDescriptor
|
|
1041 |
|idx|
|
|
1042 |
|
|
1043 |
OperatingSystem blockInterrupts.
|
|
1044 |
(readFds includes:aFileDescriptor) ifFalse:[
|
|
1045 |
idx := readFds indexOf:nil.
|
|
1046 |
idx ~~ 0 ifTrue:[
|
|
1047 |
readFds at:idx put:aFileDescriptor.
|
13
|
1048 |
readChecks at:idx put:aBlock.
|
|
1049 |
readSemaphores at:idx put:nil
|
10
|
1050 |
] ifFalse:[
|
|
1051 |
readFds := readFds copyWith:aFileDescriptor.
|
|
1052 |
readChecks := readChecks copyWith:aBlock.
|
13
|
1053 |
readSemaphores := readSemaphores copyWith:nil.
|
10
|
1054 |
]
|
|
1055 |
].
|
|
1056 |
OperatingSystem unblockInterrupts.
|
|
1057 |
!
|
|
1058 |
|
13
|
1059 |
disableFd:aFileDescriptor
|
|
1060 |
|idx|
|
|
1061 |
|
|
1062 |
OperatingSystem blockInterrupts.
|
|
1063 |
idx := readFds indexOf:aFileDescriptor.
|
|
1064 |
idx ~~ 0 ifTrue:[
|
|
1065 |
readFds at:idx put:nil.
|
|
1066 |
readChecks at:idx put:nil.
|
|
1067 |
readSemaphores at:idx put:nil
|
|
1068 |
].
|
|
1069 |
OperatingSystem unblockInterrupts.
|
|
1070 |
!
|
|
1071 |
|
10
|
1072 |
addIdleBlock:aBlock
|
|
1073 |
"add the argument, aBlock to the list of idle-actions; to be
|
|
1074 |
evaluated whenever no events are pending"
|
|
1075 |
|
13
|
1076 |
OperatingSystem blockInterrupts.
|
10
|
1077 |
idleActions isNil ifTrue:[
|
|
1078 |
idleActions := OrderedCollection new
|
|
1079 |
].
|
13
|
1080 |
idleActions add:aBlock.
|
|
1081 |
OperatingSystem unblockInterrupts.
|
10
|
1082 |
!
|
|
1083 |
|
|
1084 |
removeIdleBlock:aBlock
|
|
1085 |
"remove the argument, aBlock from the list of idle-blocks"
|
|
1086 |
|
13
|
1087 |
OperatingSystem blockInterrupts.
|
10
|
1088 |
idleActions notNil ifTrue:[
|
|
1089 |
idleActions remove:aBlock
|
13
|
1090 |
].
|
|
1091 |
OperatingSystem unblockInterrupts.
|
1
|
1092 |
!
|
|
1093 |
|
10
|
1094 |
addTimedBlock:aBlock after:delta
|
|
1095 |
"add the argument, aBlock to the list of time-sceduled-blocks; to be
|
|
1096 |
evaluated after delta seconds; the block will be removed after beeing
|
|
1097 |
evaluated"
|
|
1098 |
|
|
1099 |
|now then index|
|
|
1100 |
|
|
1101 |
now := OperatingSystem getMillisecondTime.
|
|
1102 |
then := OperatingSystem millisecondTimeAdd:now and:(delta * 1000).
|
|
1103 |
|
|
1104 |
OperatingSystem blockInterrupts.
|
|
1105 |
index := timeoutActions identityIndexOf:aBlock.
|
|
1106 |
index ~~ 0 ifTrue:[
|
|
1107 |
timeouts at:index put:then
|
|
1108 |
] ifFalse:[
|
|
1109 |
index := timeouts indexOf:nil.
|
|
1110 |
index ~~ 0 ifTrue:[
|
|
1111 |
timeoutActions at:index put:aBlock.
|
13
|
1112 |
timeouts at:index put:then.
|
|
1113 |
timeoutSemaphores at:index put:nil
|
10
|
1114 |
] ifFalse:[
|
|
1115 |
timeoutActions := timeoutActions copyWith:aBlock.
|
|
1116 |
timeouts := timeouts copyWith:then.
|
|
1117 |
timeoutSemaphores := timeoutSemaphores copyWith:nil.
|
|
1118 |
].
|
|
1119 |
nTimeouts := nTimeouts + 1.
|
|
1120 |
].
|
|
1121 |
OperatingSystem unblockInterrupts.
|
|
1122 |
!
|
|
1123 |
|
|
1124 |
removeTimedBlock:aBlock
|
|
1125 |
"remove the argument, aBlock from the list of time-sceduled-blocks"
|
|
1126 |
|
1
|
1127 |
|index|
|
|
1128 |
|
10
|
1129 |
OperatingSystem blockInterrupts.
|
|
1130 |
index := timeoutActions identityIndexOf:aBlock.
|
1
|
1131 |
(index ~~ 0) ifTrue:[
|
10
|
1132 |
timeoutActions at:index put:nil.
|
|
1133 |
timeouts at:index put:nil.
|
13
|
1134 |
timeoutSemaphores at:index put:nil.
|
10
|
1135 |
nTimeouts := nTimeouts - 1.
|
|
1136 |
].
|
|
1137 |
OperatingSystem unblockInterrupts.
|
1
|
1138 |
! !
|