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