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