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