author | Claus Gittinger <cg@exept.de> |
Mon, 18 Dec 1995 15:18:32 +0100 | |
changeset 786 | 789e2f20de44 |
parent 780 | f90ded2fa9ad |
child 804 | 264d440a67a0 |
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 |
|
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
14 |
instanceVariableNames:'quiescentProcessLists scheduler zombie activeProcess |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
15 |
currentPriority readFdArray readSemaphoreArray readCheckArray |
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
16 |
writeFdArray writeSemaphoreArray timeoutArray timeoutActionArray |
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
17 |
timeoutProcessArray timeoutSemaphoreArray idleActions anyTimeouts |
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
18 |
dispatching interruptedProcess useIOInterrupts' |
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
19 |
classVariableNames:'KnownProcesses KnownProcessIds PureEventDriven |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
20 |
UserSchedulingPriority UserInterruptPriority TimingPriority |
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
21 |
HighestPriority SchedulingPriority MaxNumberOfProcesses' |
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
22 |
poolDictionaries:'' |
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
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 |
|
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
752
diff
changeset
|
104 |
most interesting methods: |
93 | 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. |
780
f90ded2fa9ad
Stefan: da hast Du mir ein Ei ins Nest gelegt ... :-)
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
615 |
useIOInterrupts ifFalse:[ |
786 | 616 |
self checkForInputWithTimeout:0. |
780
f90ded2fa9ad
Stefan: da hast Du mir ein Ei ins Nest gelegt ... :-)
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
617 |
] |
10 | 618 |
] |
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
619 |
|
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
620 |
"Modified: 14.12.1995 / 14:45:13 / stefan" |
699 | 621 |
! |
622 |
||
623 |
dispatchLoop |
|
624 |
"central dispatch loop; the scheduler process is always staying in |
|
625 |
this method, looping forever." |
|
626 |
||
786 | 627 |
|dispatchAction| |
628 |
||
699 | 629 |
"avoid confusion if entered twice" |
630 |
||
631 |
dispatching == true ifTrue:[^ self]. |
|
632 |
dispatching := true. |
|
633 |
||
786 | 634 |
dispatchAction := [self dispatch]. |
635 |
||
636 |
"/ |
|
637 |
"/ I made this an extra call to dispatch; this allows recompilation |
|
638 |
"/ of the dispatch-handling code in the running system. |
|
639 |
"/ |
|
699 | 640 |
[true] whileTrue:[ |
641 |
AbortSignal handle:[:ex | |
|
642 |
ex return |
|
786 | 643 |
] do:dispatchAction |
699 | 644 |
] |
645 |
! ! |
|
646 |
||
647 |
!ProcessorScheduler methodsFor:'primitive process primitives'! |
|
648 |
||
649 |
scheduleForInterrupt:aProcess |
|
650 |
"make aProcess evaluate its pushed interrupt block(s)" |
|
651 |
||
652 |
|id| |
|
653 |
||
654 |
aProcess isNil ifTrue:[^ self]. |
|
655 |
aProcess == activeProcess ifTrue:[^ self]. |
|
656 |
||
657 |
id := aProcess id. |
|
658 |
self class threadInterrupt:id. |
|
659 |
" |
|
660 |
and, make the process runnable |
|
661 |
" |
|
662 |
aProcess state ~~ #stopped ifTrue:[ |
|
663 |
" |
|
664 |
and, make the process runnable |
|
665 |
" |
|
666 |
aProcess resume |
|
667 |
] |
|
668 |
! |
|
669 |
||
670 |
threadSwitch:aProcess |
|
671 |
"continue execution in aProcess. |
|
672 |
(warning: low level entry, no administration is done here)" |
|
673 |
||
674 |
|id pri ok oldProcess oldPri p singleStep wasBlocked| |
|
675 |
||
676 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
|
677 |
||
678 |
wasBlocked := OperatingSystem blockInterrupts. |
|
679 |
||
680 |
oldProcess := activeProcess. |
|
681 |
oldPri := currentPriority. |
|
682 |
||
683 |
id := aProcess id. |
|
684 |
pri := aProcess priority. |
|
685 |
singleStep := aProcess isSingleStepping. |
|
686 |
aProcess state:#active. |
|
687 |
oldProcess setStateTo:#run if:#active. |
|
688 |
||
689 |
" |
|
690 |
no interrupts now - activeProcess has already been changed |
|
691 |
(dont add any message sends here) |
|
692 |
" |
|
693 |
activeProcess := aProcess. |
|
694 |
currentPriority := pri. |
|
695 |
%{ |
|
696 |
extern OBJ ___threadSwitch(); |
|
697 |
||
698 |
if (__isSmallInteger(id)) { |
|
699 |
ok = ___threadSwitch(__context, _intVal(id), (singleStep == true) ? 1 : 0); |
|
700 |
} else { |
|
701 |
ok = false; |
|
702 |
} |
|
703 |
%}. |
|
704 |
"time passes spent in some other process ... |
|
705 |
... here again" |
|
706 |
||
707 |
p := activeProcess. |
|
708 |
activeProcess := oldProcess. |
|
709 |
currentPriority := oldProcess priority. |
|
710 |
||
711 |
ok ifFalse:[ |
|
712 |
" |
|
713 |
switch failed for some reason - |
|
714 |
destroy the bad process |
|
715 |
" |
|
716 |
p id ~~ 0 ifTrue:[ |
|
717 |
'SCHEDULER: problem with process ' errorPrint. |
|
718 |
p id errorPrint. |
|
719 |
p name notNil ifTrue:[ |
|
720 |
' (' errorPrint. p name errorPrint. ')' errorPrint. |
|
721 |
]. |
|
722 |
'; hard-terminate it.' errorPrintNL. |
|
723 |
p state:#suspended. |
|
724 |
self terminateNoSignal:p. |
|
725 |
] |
|
726 |
]. |
|
727 |
zombie notNil ifTrue:[ |
|
728 |
self class threadDestroy:zombie. |
|
729 |
zombie := nil |
|
730 |
]. |
|
731 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
732 |
! ! |
|
733 |
||
734 |
!ProcessorScheduler methodsFor:'private'! |
|
735 |
||
736 |
remember:aProcess |
|
737 |
"remember aProcess for later disposal (where the underlying |
|
738 |
system resources have to be freed)." |
|
739 |
||
740 |
|newShadow oldId wasBlocked |
|
741 |
oldSize "{ Class: SmallInteger }" |
|
742 |
index "{ Class: SmallInteger }" |
|
743 |
sz "{ Class: SmallInteger }" | |
|
744 |
||
745 |
wasBlocked := OperatingSystem blockInterrupts. |
|
746 |
index := 1. |
|
747 |
sz := KnownProcessIds size. |
|
748 |
[index <= sz] whileTrue:[ |
|
749 |
(KnownProcesses at:index) isNil ifTrue:[ |
|
750 |
oldId := KnownProcessIds at:index. |
|
751 |
oldId notNil ifTrue:[ |
|
752 |
self class threadDestroy:oldId. |
|
753 |
]. |
|
754 |
KnownProcesses at:index put:aProcess. |
|
755 |
KnownProcessIds at:index put:aProcess id. |
|
756 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
757 |
^ self |
|
758 |
]. |
|
759 |
index := index + 1 |
|
760 |
]. |
|
761 |
||
762 |
KnownProcessIds grow:index. |
|
763 |
KnownProcessIds at:index put:aProcess id. |
|
764 |
||
765 |
oldSize := KnownProcesses size. |
|
766 |
(index > oldSize) ifTrue:[ |
|
767 |
newShadow := WeakArray new:(oldSize * 2). |
|
768 |
newShadow watcher:self class. |
|
769 |
newShadow replaceFrom:1 with:KnownProcesses. |
|
770 |
KnownProcesses := newShadow |
|
771 |
]. |
|
772 |
KnownProcesses at:index put:aProcess. |
|
773 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
774 |
! |
|
775 |
||
776 |
unRemember:aProcess |
|
777 |
"forget aProcess - dispose processing will not consider this one" |
|
778 |
||
779 |
|index wasBlocked| |
|
780 |
||
781 |
wasBlocked := OperatingSystem blockInterrupts. |
|
782 |
index := KnownProcesses identityIndexOf:aProcess. |
|
783 |
index ~~ 0 ifTrue:[ |
|
784 |
KnownProcessIds at:index put:nil. |
|
785 |
KnownProcesses at:index put:nil. |
|
786 |
]. |
|
787 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
788 |
! ! |
|
789 |
||
790 |
!ProcessorScheduler methodsFor:'private initializing'! |
|
791 |
||
792 |
initialize |
|
793 |
"initialize the one-and-only ProcessorScheduler" |
|
794 |
||
795 |
|nPrios "{ Class: SmallInteger }" |
|
796 |
l p| |
|
797 |
||
798 |
KnownProcesses isNil ifTrue:[ |
|
799 |
KnownProcesses := WeakArray new:10. |
|
800 |
KnownProcesses watcher:self class. |
|
801 |
KnownProcessIds := OrderedCollection new. |
|
802 |
]. |
|
803 |
||
804 |
" |
|
805 |
create a collection with process lists; accessed using the priority as key |
|
806 |
" |
|
807 |
nPrios := SchedulingPriority. |
|
808 |
quiescentProcessLists := Array new:nPrios. |
|
809 |
1 to:nPrios do:[:pri | |
|
810 |
quiescentProcessLists at:pri put:(LinkedList new) |
|
811 |
]. |
|
812 |
||
813 |
readFdArray := Array with:nil. |
|
814 |
readCheckArray := Array with:nil. |
|
815 |
readSemaphoreArray := Array with:nil. |
|
816 |
writeFdArray := Array with:nil. |
|
817 |
writeSemaphoreArray := Array with:nil. |
|
818 |
timeoutArray := Array with:nil. |
|
819 |
timeoutSemaphoreArray := Array with:nil. |
|
820 |
timeoutActionArray := Array with:nil. |
|
821 |
timeoutProcessArray := Array with:nil. |
|
822 |
anyTimeouts := false. |
|
823 |
dispatching := false. |
|
824 |
useIOInterrupts := OperatingSystem supportsIOInterrupts. |
|
825 |
||
826 |
" |
|
827 |
handcraft the first (dispatcher-) process - this one will never |
|
828 |
block, but go into a select if there is nothing to do. |
|
829 |
Also, it has a prio of max+1 - thus, it comes first when looking |
|
830 |
for a runnable process. |
|
831 |
" |
|
832 |
currentPriority := SchedulingPriority. |
|
833 |
p := Process new. |
|
834 |
p setId:0 state:#run. |
|
835 |
p setPriority:currentPriority. |
|
836 |
p name:'scheduler'. |
|
837 |
||
838 |
scheduler := activeProcess := p. |
|
839 |
||
840 |
(quiescentProcessLists at:currentPriority) add:p. |
|
841 |
||
842 |
" |
|
843 |
let me handle IO and timer interrupts |
|
844 |
" |
|
845 |
ObjectMemory ioInterruptHandler:self. |
|
846 |
ObjectMemory timerInterruptHandler:self. |
|
847 |
! |
|
848 |
||
849 |
reinitialize |
|
850 |
"all previous processes (except those marked as restartable) are made dead |
|
851 |
- each object should reinstall its process(s) upon restart; |
|
852 |
especially, windowgroups have to. |
|
853 |
In contrast to ST-80, restartable processes are restarted at the beginning |
|
854 |
NOT continued where left. This is a consequence of the portable implementation |
|
855 |
of ST/X, since in order to continue a process, we needed to know the |
|
856 |
internals of the machines (and C-compilers) stack layout. |
|
857 |
This was not done, favouring portability for process continuation. |
|
858 |
In praxis, this is not much of a problem, since in almost every case, |
|
859 |
the computation state can be saved in some object, and processing be |
|
860 |
restarted from scratch, reinitializing things from this saved state." |
|
861 |
||
862 |
|processesToRestart| |
|
863 |
||
864 |
" |
|
865 |
lay all processes to rest, collect restartable ones |
|
866 |
" |
|
867 |
processesToRestart := OrderedCollection new. |
|
868 |
KnownProcesses do:[:p | |
|
869 |
p notNil ifTrue:[ |
|
870 |
"how, exactly should this be done ?" |
|
871 |
||
872 |
p isRestartable == true ifTrue:[ |
|
873 |
p nextLink:nil. |
|
874 |
processesToRestart add:p |
|
875 |
] ifFalse:[ |
|
876 |
p setId:nil state:#dead |
|
877 |
] |
|
878 |
]. |
|
879 |
]. |
|
880 |
scheduler setId:nil state:#dead. |
|
881 |
||
882 |
" |
|
883 |
now, start from scratch |
|
884 |
" |
|
885 |
KnownProcesses := nil. |
|
886 |
self initialize. |
|
887 |
||
888 |
" |
|
889 |
... and restart those that can be. |
|
890 |
" |
|
891 |
processesToRestart do:[:p | |
|
892 |
"/ 'process restart not implemented' errorPrintNL. |
|
893 |
p restart |
|
894 |
] |
|
895 |
! ! |
|
896 |
||
897 |
!ProcessorScheduler methodsFor:'process creation'! |
|
898 |
||
899 |
newProcessFor:aProcess |
|
900 |
"create a physical (VM-) process for aProcess. |
|
901 |
Return true if ok, false if something went wrong. |
|
902 |
The process is not scheduled; to start it running, |
|
903 |
it needs a Process>>resume. Once resumed, the process will later |
|
904 |
get control in its #start method." |
|
905 |
||
906 |
|id| |
|
907 |
||
908 |
id := self class threadCreate:aProcess withId:nil. |
|
909 |
id isNil ifTrue:[^ false]. |
|
910 |
||
911 |
aProcess setId:id state:#light. "meaning: has no stack yet" |
|
912 |
self remember:aProcess. |
|
913 |
^ true |
|
914 |
! |
|
915 |
||
916 |
newProcessFor:aProcess withId:idWant |
|
917 |
"private entry for Process restart - do not use in your program" |
|
918 |
||
919 |
(self class threadCreate:aProcess withId:idWant) ~~ idWant ifTrue:[ |
|
920 |
^ false |
|
921 |
]. |
|
922 |
||
923 |
aProcess state:#light. "meaning: has no stack yet" |
|
924 |
self remember:aProcess. |
|
925 |
^ true |
|
926 |
! ! |
|
927 |
||
928 |
!ProcessorScheduler methodsFor:'queries'! |
|
929 |
||
930 |
activeProcessIsSystemProcess |
|
931 |
"return true if the active process is a system process, |
|
932 |
which should not be suspended." |
|
933 |
||
934 |
^ self isSystemProcess:activeProcess |
|
935 |
||
936 |
" |
|
937 |
Processor activeProcessIsSystemProcess |
|
938 |
" |
|
939 |
! |
|
940 |
||
941 |
highestPriorityRunnableProcess |
|
942 |
"return the highest prio runnable process" |
|
943 |
||
944 |
|listArray l p prio "{ Class: SmallInteger }" | |
|
945 |
||
946 |
prio := HighestPriority. |
|
947 |
listArray := quiescentProcessLists. |
|
948 |
[prio >= 1] whileTrue:[ |
|
949 |
l := listArray at:prio. |
|
950 |
l notEmpty ifTrue:[ |
|
951 |
p := l first. |
|
952 |
" |
|
953 |
if it got corrupted somehow ... |
|
954 |
" |
|
955 |
p id isNil ifTrue:[ |
|
956 |
'process with nil id removed' errorPrintNL. |
|
957 |
l removeFirst. |
|
958 |
^ nil. |
|
959 |
]. |
|
960 |
^ p |
|
961 |
]. |
|
962 |
prio := prio - 1 |
|
963 |
]. |
|
964 |
^ nil |
|
965 |
! |
|
966 |
||
967 |
isSystemProcess:aProcess |
|
968 |
"return true if aProcess is a system process, |
|
969 |
which should not be suspended/terminated etc.." |
|
970 |
||
971 |
(self class isPureEventDriven |
|
972 |
or:[aProcess id == 0 |
|
973 |
or:[(Display notNil and:[Display dispatchProcess == aProcess]) |
|
974 |
" nameOrId endsWith:'dispatcher' " |
|
975 |
]]) ifTrue:[ |
|
976 |
^ true |
|
977 |
]. |
|
978 |
^ false |
|
979 |
||
980 |
" |
|
981 |
Processor activeProcessIsSystemProcess |
|
982 |
" |
|
983 |
! ! |
|
984 |
||
985 |
!ProcessorScheduler methodsFor:'scheduling'! |
|
986 |
||
987 |
changePriority:prio for:aProcess |
|
988 |
"change the priority of aProcess" |
|
989 |
||
990 |
|oldList newList oldPrio newPrio wasBlocked| |
|
991 |
||
992 |
oldPrio := aProcess priority. |
|
993 |
oldPrio == prio ifTrue:[^ self]. |
|
994 |
||
995 |
" |
|
996 |
check for valid argument |
|
997 |
" |
|
998 |
newPrio := prio. |
|
999 |
newPrio < 1 ifTrue:[ |
|
1000 |
newPrio := 1. |
|
1001 |
] ifFalse:[ |
|
1002 |
aProcess == scheduler ifTrue:[^ self]. |
|
1003 |
newPrio > HighestPriority ifTrue:[ |
|
1004 |
newPrio := HighestPriority |
|
1005 |
] |
|
1006 |
]. |
|
1007 |
||
1008 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1009 |
||
1010 |
aProcess setPriority:newPrio. |
|
1011 |
||
1012 |
oldList := quiescentProcessLists at:oldPrio. |
|
1013 |
(oldList identityIndexOf:aProcess) == 0 ifTrue:[ |
|
1014 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1015 |
^ self |
|
1016 |
]. |
|
1017 |
||
1018 |
oldList remove:aProcess. |
|
1019 |
||
1020 |
newList := quiescentProcessLists at:newPrio. |
|
1021 |
newList addLast:aProcess. |
|
1022 |
||
1023 |
"if its the current process lowering its prio |
|
1024 |
or another one raising, we have to reschedule" |
|
1025 |
||
1026 |
aProcess == activeProcess ifTrue:[ |
|
1027 |
currentPriority := newPrio. |
|
1028 |
newPrio < oldPrio ifTrue:[ |
|
1029 |
self threadSwitch:scheduler. |
|
1030 |
] |
|
1031 |
] ifFalse:[ |
|
1032 |
newPrio > currentPriority ifTrue:[ |
|
1033 |
self threadSwitch:aProcess. |
|
1034 |
] |
|
1035 |
]. |
|
1036 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1037 |
! |
|
1038 |
||
1039 |
interruptActive |
|
1040 |
"interrupt the current process" |
|
1041 |
||
1042 |
activeProcess interrupt |
|
1043 |
! |
|
1044 |
||
1045 |
processTermination |
|
1046 |
"sent by VM if the current process finished its startup block |
|
1047 |
without proper process termination. Lay him to rest now. |
|
1048 |
This can only happen, if something went wrong in Block>>newProcess, |
|
1049 |
since the block defined there always terminates itself." |
|
1050 |
||
1051 |
self terminateNoSignal:activeProcess. |
|
1052 |
self threadSwitch:scheduler |
|
1053 |
! |
|
1054 |
||
1055 |
reschedule |
|
1056 |
"switch to the highest prio runnable process. |
|
1057 |
The scheduler itself is always runnable, so we can do an unconditional switch |
|
1058 |
to that one. This method is a historical left-over and will vanish." |
|
1059 |
||
1060 |
^ self threadSwitch:scheduler |
|
1061 |
! |
|
1062 |
||
1063 |
resume:aProcess |
|
1064 |
"set aProcess runnable - |
|
1065 |
if its prio is higher than the currently running prio, switch to it." |
|
1066 |
||
1067 |
|l pri wasBlocked| |
|
1068 |
||
1069 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
|
1070 |
||
1071 |
"ignore, if process is already dead" |
|
1072 |
aProcess id isNil ifTrue:[^ self]. |
|
1073 |
||
1074 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1075 |
||
1076 |
pri := aProcess priority. |
|
1077 |
||
1078 |
l := quiescentProcessLists at:pri. |
|
1079 |
"if already running, ignore" |
|
1080 |
(l identityIndexOf:aProcess) ~~ 0 ifTrue:[ |
|
1081 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1082 |
^ self |
|
1083 |
]. |
|
1084 |
l addLast:aProcess. |
|
1085 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1086 |
||
1087 |
(pri > currentPriority) ifTrue:[ |
|
1088 |
" |
|
1089 |
its prio is higher; immediately transfer control to it |
|
1090 |
" |
|
1091 |
self threadSwitch:aProcess |
|
1092 |
] ifFalse:[ |
|
1093 |
" |
|
1094 |
its prio is lower; it will have to wait for a while ... |
|
1095 |
" |
|
1096 |
aProcess state:#run |
|
1097 |
] |
|
1098 |
! |
|
1099 |
||
1100 |
resumeForSingleSend:aProcess |
|
1101 |
"like resume, but let the process execute a single send only. |
|
1102 |
This will be used by the (new, not yet released) debugger |
|
1103 |
for single stepping." |
|
1104 |
||
1105 |
(aProcess isNil or:[aProcess == activeProcess]) ifTrue:[^ self]. |
|
1106 |
aProcess singleStep:true. |
|
1107 |
self resume:aProcess |
|
1108 |
! |
|
1109 |
||
1110 |
suspend:aProcess |
|
1111 |
"remove the argument, aProcess from the list of runnable processes. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
1112 |
If the process is the current one, reschedule. |
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
1113 |
This method should only be called by Process>>suspend" |
699 | 1114 |
|
1115 |
|pri l p wasBlocked| |
|
1116 |
||
1117 |
" |
|
1118 |
some debugging stuff |
|
1119 |
" |
|
1120 |
aProcess isNil ifTrue:[ |
|
1121 |
MiniDebugger enterWithMessage:'nil suspend'. |
|
1122 |
^ self |
|
1123 |
]. |
|
1124 |
aProcess id isNil ifTrue:[ |
|
1125 |
MiniDebugger enterWithMessage:'bad suspend: already dead'. |
|
1126 |
self threadSwitch:scheduler. |
|
1127 |
^ self |
|
1128 |
]. |
|
1129 |
aProcess == scheduler ifTrue:[ |
|
1130 |
'scheduler should never be suspended' errorPrintNL. |
|
1131 |
MiniDebugger enterWithMessage:'scheduler should never be suspended'. |
|
1132 |
^ self |
|
1133 |
]. |
|
1134 |
||
1135 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1136 |
||
1137 |
pri := aProcess priority. |
|
1138 |
l := quiescentProcessLists at:pri. |
|
1139 |
||
1140 |
"notice: this is slightly faster than putting the if-code into |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
1141 |
the ifAbsent block, because [] is a shared cheap block, created at compile time |
699 | 1142 |
" |
1143 |
(l remove:aProcess ifAbsent:[]) isNil ifTrue:[ |
|
1144 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1145 |
'bad suspend: not on run list' errorPrintNL. |
|
1146 |
"/ MiniDebugger enterWithMessage:'bad suspend: not on run list'. |
|
1147 |
self threadSwitch:scheduler. |
|
1148 |
^ self |
|
1149 |
]. |
|
1150 |
||
1151 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
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 |
]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
1162 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
750
diff
changeset
|
1163 |
"Modified: 13.12.1995 / 13:32:11 / stefan" |
699 | 1164 |
! |
1165 |
||
1166 |
terminate:aProcess |
|
1167 |
"terminate aProcess. This is donen by sending aProcess the terminateSignal, |
|
1168 |
which will evaluate any unwind blocks and finally do a hard terminate." |
|
1169 |
||
1170 |
aProcess terminate |
|
1171 |
! |
|
1172 |
||
1173 |
terminateActive |
|
1174 |
"terminate the current process (i.e. the running process kills itself). |
|
1175 |
The active process is sent the terminateSignal so it will evaluate any |
|
1176 |
unwind blocks and finally do a hard terminate. |
|
1177 |
This is sent for regular termination and by the VM, if the hard-stack limit |
|
1178 |
is reached. (i.e. a process did not repair things in a recursionInterrupt and |
|
1179 |
continued to grow its stack)" |
|
1180 |
||
1181 |
activeProcess terminate |
|
1182 |
! |
|
1183 |
||
1184 |
terminateActiveNoSignal |
|
1185 |
"hard terminate the active process, without sending any |
|
1186 |
terminate signal thus no unwind blocks are evaluated." |
|
1187 |
||
1188 |
self terminateNoSignal:activeProcess |
|
1189 |
! |
|
1190 |
||
1191 |
terminateNoSignal:aProcess |
|
1192 |
"hard terminate aProcess without sending the terminate signal, thus |
|
1193 |
no unwind blocks or exitAction are performed in the process.. |
|
1194 |
If its not the current process, it is simply removed from its list |
|
1195 |
and physically destroyed. Otherwise (since we can't take away the chair |
|
1196 |
we are sitting on), a switch is forced and the process |
|
1197 |
will be physically destroyed by the next running process. |
|
1198 |
(see zombie handling)" |
|
1199 |
||
1200 |
|pri id l wasBlocked| |
|
1201 |
||
1202 |
aProcess isNil ifTrue:[^ self]. |
|
1203 |
id := aProcess id. |
|
1204 |
id isNil ifTrue:[^ self]. "already dead" |
|
1205 |
||
1206 |
aProcess setId:nil state:#dead. |
|
1207 |
||
1208 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1209 |
||
1210 |
"remove the process from the runnable list" |
|
1211 |
||
1212 |
pri := aProcess priority. |
|
1213 |
l := quiescentProcessLists at:pri. |
|
1214 |
(l identityIndexOf:aProcess) ~~ 0 ifTrue:[ |
|
1215 |
l remove:aProcess. |
|
1216 |
]. |
|
1217 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1218 |
||
1219 |
aProcess == activeProcess ifTrue:[ |
|
1220 |
" |
|
1221 |
hard case - its the currently running process |
|
1222 |
we must have the next active process destroy this one |
|
1223 |
(we cannot destroy the chair we are sitting on ... :-) |
|
1224 |
" |
|
1225 |
zombie := id. |
|
1226 |
self unRemember:aProcess. |
|
1227 |
self threadSwitch:scheduler. |
|
1228 |
"not reached" |
|
1229 |
^ self |
|
1230 |
]. |
|
1231 |
self class threadDestroy:id. |
|
1232 |
self unRemember:aProcess. |
|
1233 |
^ self |
|
1234 |
! |
|
1235 |
||
1236 |
yield |
|
1237 |
"move the currently running process to the end of the currentList |
|
1238 |
and reschedule to the first in the list, thus switching to the |
|
1239 |
next same-prio-process." |
|
1240 |
||
1241 |
|l wasBlocked| |
|
1242 |
||
1243 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1244 |
||
1245 |
" |
|
1246 |
debugging consistency check - will be removed later |
|
1247 |
" |
|
1248 |
activeProcess priority ~~ currentPriority ifTrue:[ |
|
1249 |
'oops process changed priority' errorPrintNL. |
|
1250 |
currentPriority := activeProcess priority. |
|
1251 |
]. |
|
1252 |
||
1253 |
l := quiescentProcessLists at:currentPriority. |
|
1254 |
||
1255 |
" |
|
1256 |
debugging consistency checks - will be removed later |
|
1257 |
" |
|
1258 |
l isEmpty ifTrue:[ |
|
1259 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1260 |
'oops - empty runnable list' errorPrintNL. |
|
1261 |
^ self |
|
1262 |
]. |
|
1263 |
||
1264 |
" |
|
1265 |
check if the running process is not the only one |
|
1266 |
" |
|
1267 |
l size ~~ 1 ifTrue:[ |
|
1268 |
" |
|
1269 |
bring running process to the end |
|
1270 |
" |
|
1271 |
l removeFirst. |
|
1272 |
l addLast:activeProcess. |
|
1273 |
||
1274 |
" |
|
1275 |
and switch to first in the list |
|
1276 |
" |
|
1277 |
self threadSwitch:(l first). |
|
1278 |
]. |
|
1279 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1280 |
! ! |
|
1281 |
||
1282 |
!ProcessorScheduler methodsFor:'semaphore signalling'! |
|
1283 |
||
1284 |
disableSemaphore:aSemaphore |
|
1285 |
"disable triggering of a semaphore" |
|
1286 |
||
1287 |
|idx "{ Class: SmallInteger }" |
|
1288 |
wasBlocked| |
|
1289 |
||
1290 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1291 |
idx := readSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
|
1292 |
[idx ~~ 0] whileTrue:[ |
|
1293 |
readFdArray at:idx put:nil. |
|
1294 |
readSemaphoreArray at:idx put:nil. |
|
1295 |
readCheckArray at:idx put:nil. |
|
1296 |
idx := readSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
|
1297 |
]. |
|
1298 |
idx := writeSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
|
1299 |
[idx ~~ 0] whileTrue:[ |
|
1300 |
writeFdArray at:idx put:nil. |
|
1301 |
writeSemaphoreArray at:idx put:nil. |
|
1302 |
idx := writeSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
|
1303 |
]. |
|
1304 |
idx := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
|
1305 |
[idx ~~ 0] whileTrue:[ |
|
1306 |
timeoutArray at:idx put:nil. |
|
1307 |
timeoutSemaphoreArray at:idx put:nil. |
|
1308 |
timeoutActionArray at:idx put:nil. |
|
1309 |
timeoutProcessArray at:idx put:nil. |
|
1310 |
idx := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:idx. |
|
1311 |
]. |
|
1312 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1313 |
! |
|
1314 |
||
1315 |
signal:aSemaphore afterMilliseconds:millis |
|
1316 |
"arrange for a semaphore to be triggered after some milliseconds" |
|
1317 |
||
1318 |
|now then wasBlocked| |
|
1319 |
||
1320 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1321 |
now := OperatingSystem getMillisecondTime. |
|
1322 |
then := OperatingSystem millisecondTimeAdd:now and:millis rounded. |
|
1323 |
self signal:aSemaphore atMilliseconds:then. |
|
1324 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1325 |
! |
|
1326 |
||
1327 |
signal:aSemaphore afterSeconds:seconds |
|
1328 |
"arrange for a semaphore to be triggered after some seconds" |
|
1329 |
||
1330 |
self signal:aSemaphore afterMilliseconds:(seconds * 1000) |
|
1331 |
! |
|
1332 |
||
1333 |
signal:aSemaphore atMilliseconds:aMillisecondTime |
|
1334 |
"arrange for a semaphore to be triggered at a specific millisecond time. |
|
1335 |
If there is already a pending trigger time, the time is changed." |
|
1336 |
||
1337 |
|index "{ Class: SmallInteger }" |
|
1338 |
wasBlocked| |
|
1339 |
||
1340 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1341 |
index := timeoutSemaphoreArray identityIndexOf:aSemaphore startingAt:1. |
|
1342 |
index ~~ 0 ifTrue:[ |
|
1343 |
timeoutArray at:index put:aMillisecondTime |
|
1344 |
] ifFalse:[ |
|
1345 |
index := timeoutArray identityIndexOf:nil startingAt:1. |
|
1346 |
index ~~ 0 ifTrue:[ |
|
1347 |
timeoutSemaphoreArray at:index put:aSemaphore. |
|
1348 |
timeoutArray at:index put:aMillisecondTime. |
|
1349 |
timeoutActionArray at:index put:nil. |
|
1350 |
timeoutProcessArray at:index put:nil |
|
1351 |
] ifFalse:[ |
|
1352 |
timeoutSemaphoreArray := timeoutSemaphoreArray copyWith:aSemaphore. |
|
1353 |
timeoutArray := timeoutArray copyWith:aMillisecondTime. |
|
1354 |
timeoutActionArray := timeoutActionArray copyWith:nil. |
|
1355 |
timeoutProcessArray := timeoutProcessArray copyWith:nil |
|
1356 |
]. |
|
1357 |
]. |
|
1358 |
||
1359 |
anyTimeouts := true. |
|
1360 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1361 |
! |
|
1362 |
||
1363 |
signal:aSemaphore onInput:aFileDescriptor |
|
1364 |
"arrange for a semaphore to be triggered when input on aFileDescriptor |
|
1365 |
arrives." |
|
1366 |
||
1367 |
self signal:aSemaphore onInput:aFileDescriptor orCheck:nil |
|
1368 |
! |
|
1369 |
||
1370 |
signal:aSemaphore onInput:aFileDescriptor orCheck:aBlock |
|
1371 |
"arrange for a semaphore to be triggered when input on aFileDescriptor |
|
1372 |
arrives OR checkblock evaluates to true. |
|
1373 |
(checkBlock is used for buffered input, where a select may not detect |
|
1374 |
data already read into a buffer - as in Xlib)" |
|
1375 |
||
1376 |
|idx "{ Class: SmallInteger }" |
|
1377 |
wasBlocked| |
|
1378 |
||
1379 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1380 |
(readFdArray identityIndexOf:aFileDescriptor startingAt:1) == 0 ifTrue:[ |
|
1381 |
idx := readFdArray identityIndexOf:nil startingAt:1. |
|
1382 |
idx ~~ 0 ifTrue:[ |
|
1383 |
readFdArray at:idx put:aFileDescriptor. |
|
1384 |
readSemaphoreArray at:idx put:aSemaphore. |
|
1385 |
readCheckArray at:idx put:aBlock |
|
1386 |
] ifFalse:[ |
|
1387 |
readFdArray := readFdArray copyWith:aFileDescriptor. |
|
1388 |
readSemaphoreArray := readSemaphoreArray copyWith:aSemaphore. |
|
1389 |
readCheckArray := readCheckArray copyWith:aBlock. |
|
1390 |
] |
|
1391 |
]. |
|
1392 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1393 |
! |
|
1394 |
||
1395 |
signal:aSemaphore onOutput:aFileDescriptor |
|
1396 |
"arrange for a semaphore to be triggered when output on aFileDescriptor |
|
1397 |
is possible. (i.e. can be written without blocking)" |
|
1398 |
||
1399 |
|idx "{ Class: SmallInteger }" |
|
1400 |
wasBlocked| |
|
1401 |
||
1402 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1403 |
(writeFdArray identityIndexOf:aFileDescriptor startingAt:1) == 0 ifTrue:[ |
|
1404 |
idx := writeFdArray identityIndexOf:nil startingAt:1. |
|
1405 |
idx ~~ 0 ifTrue:[ |
|
1406 |
writeFdArray at:idx put:aFileDescriptor. |
|
1407 |
writeSemaphoreArray at:idx put:aSemaphore. |
|
1408 |
] ifFalse:[ |
|
1409 |
writeFdArray := writeFdArray copyWith:aFileDescriptor. |
|
1410 |
writeSemaphoreArray := writeSemaphoreArray copyWith:aSemaphore. |
|
1411 |
] |
|
1412 |
]. |
|
1413 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1414 |
! ! |
|
1415 |
||
1416 |
!ProcessorScheduler methodsFor:'timeout handling'! |
|
1417 |
||
1418 |
addTimedBlock:aBlock afterMilliseconds:delta |
|
1419 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1420 |
evaluated after delta milliseconds. The process which installs this timed |
|
1421 |
block will be interrupted for execution of the block. |
|
1422 |
(if it is running, the interrupt will occur in whatever method it is |
|
1423 |
executing; if it is suspended, it will be resumed). |
|
1424 |
The block will be removed from the timed-block list after evaluation |
|
1425 |
(i.e. it will trigger only once)." |
|
1426 |
||
1427 |
^ self addTimedBlock:aBlock for:activeProcess afterMilliseconds:delta |
|
1428 |
! |
|
1429 |
||
1430 |
addTimedBlock:aBlock afterSeconds:delta |
|
1431 |
"add the argument, aBlock to the list of time-scheduled-blocks. |
|
1432 |
to be evaluated after delta seconds. The process which installs this timed |
|
1433 |
block will be interrupted for execution of the block. |
|
1434 |
(if it is running, the interrupt will occur in whatever method it is |
|
1435 |
executing; if it is suspended, it will be resumed). |
|
1436 |
The block will be removed from the timed-block list after evaluation |
|
1437 |
(i.e. it will trigger only once)." |
|
1438 |
||
1439 |
self addTimedBlock:aBlock for:activeProcess afterMilliseconds:(delta * 1000) rounded |
|
1440 |
! |
|
1441 |
||
1442 |
addTimedBlock:aBlock atMilliseconds:aMillisecondTime |
|
1443 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1444 |
evaluated when the millisecondClock value passes aMillisecondTime. |
|
1445 |
The process which installs this timed block will be interrupted for |
|
1446 |
execution of the block. |
|
1447 |
(if it is running, the interrupt will occur in whatever method it is |
|
1448 |
executing; if it is suspended, it will be resumed). |
|
1449 |
The block will be removed from the timed-block list after evaluation |
|
1450 |
(i.e. it will trigger only once)." |
|
1451 |
||
1452 |
self addTimedBlock:aBlock for:activeProcess atMilliseconds:aMillisecondTime |
|
1453 |
! |
|
1454 |
||
1455 |
addTimedBlock:aBlock for:aProcess afterMilliseconds:delta |
|
1456 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1457 |
evaluated after delta milliseconds. The process specified by the argument, |
|
1458 |
aProcess will be interrupted for execution of the block. |
|
1459 |
(if it is running, the interrupt will occur in whatever method it is |
|
1460 |
executing; if it is suspended, it will be resumed). |
|
1461 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
1462 |
(which is dangerous - the block should not raise any error conditions). |
|
1463 |
The block will be removed from the timed-block list after evaluation |
|
1464 |
(i.e. it will trigger only once)." |
|
1465 |
||
1466 |
|now then wasBlocked| |
|
1467 |
||
1468 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1469 |
now := OperatingSystem getMillisecondTime. |
|
1470 |
then := OperatingSystem millisecondTimeAdd:now and:delta. |
|
1471 |
self addTimedBlock:aBlock for:aProcess atMilliseconds:then. |
|
1472 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1473 |
! |
|
1474 |
||
1475 |
addTimedBlock:aBlock for:aProcess afterSeconds:delta |
|
1476 |
"add the argument, aBlock to the list of time-scheduled-blocks. |
|
1477 |
to be evaluated after delta seconds. aProcess will be interrupted for |
|
1478 |
execution of the block. |
|
1479 |
(if it is running, the interrupt will occur in whatever method it is |
|
1480 |
executing; if it is suspended, it will be resumed). |
|
1481 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
1482 |
(which is dangerous - the block should not raise any error conditions). |
|
1483 |
The block will be removed from the timed-block list after evaluation |
|
1484 |
(i.e. it will trigger only once)." |
|
1485 |
||
1486 |
self addTimedBlock:aBlock for:aProcess afterMilliseconds:(delta * 1000) rounded |
|
1487 |
! |
|
1488 |
||
1489 |
addTimedBlock:aBlock for:aProcess atMilliseconds:aMillisecondTime |
|
1490 |
"add the argument, aBlock to the list of time-scheduled-blocks; to be |
|
1491 |
evaluated by aProcess when the millisecondClock value passes |
|
1492 |
aMillisecondTime. |
|
1493 |
If that block is already in the timeout list, |
|
1494 |
its trigger-time is changed. |
|
1495 |
The process specified by the argument, aProcess will be interrupted |
|
1496 |
for execution of the block. |
|
1497 |
If aProcess is nil, the block will be evaluated by the scheduler itself |
|
1498 |
(which is dangerous - the block should not raise any error conditions). |
|
1499 |
If the process is active at trigger time, the interrupt will occur in |
|
1500 |
whatever method it is executing; if suspended at trigger time, it will be |
|
1501 |
resumed. |
|
1502 |
The block will be removed from the timed-block list after evaluation |
|
1503 |
(i.e. it will trigger only once)." |
|
1504 |
||
1505 |
|index "{ Class: SmallInteger }" |
|
1506 |
wasBlocked| |
|
1507 |
||
1508 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1509 |
index := timeoutActionArray identityIndexOf:aBlock startingAt:1. |
|
1510 |
index ~~ 0 ifTrue:[ |
|
1511 |
timeoutArray at:index put:aMillisecondTime |
|
1512 |
] ifFalse:[ |
|
1513 |
index := timeoutArray indexOf:nil. |
|
1514 |
index ~~ 0 ifTrue:[ |
|
1515 |
timeoutArray at:index put:aMillisecondTime. |
|
1516 |
timeoutActionArray at:index put:aBlock. |
|
1517 |
timeoutSemaphoreArray at:index put:nil. |
|
1518 |
timeoutProcessArray at:index put:aProcess |
|
1519 |
] ifFalse:[ |
|
1520 |
timeoutArray := timeoutArray copyWith:aMillisecondTime. |
|
1521 |
timeoutActionArray := timeoutActionArray copyWith:aBlock. |
|
1522 |
timeoutSemaphoreArray := timeoutSemaphoreArray copyWith:nil. |
|
1523 |
timeoutProcessArray := timeoutProcessArray copyWith:aProcess. |
|
1524 |
]. |
|
1525 |
]. |
|
1526 |
||
1527 |
anyTimeouts := true. |
|
1528 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1529 |
! |
|
1530 |
||
1531 |
evaluateTimeouts |
|
1532 |
"walk through timeouts and evaluate blocks or signal semas that need to be .." |
|
1533 |
||
1534 |
|sema now aTime block blocksToEvaluate |
|
1535 |
processes n "{ Class: SmallInteger }"| |
|
1536 |
||
1537 |
anyTimeouts ifFalse:[ ^ self]. |
|
1538 |
||
1539 |
"have to collect the blocks first, then evaluate them. This avoids |
|
1540 |
problems due to newly inserted blocks." |
|
1541 |
||
1542 |
now := OperatingSystem getMillisecondTime. |
|
1543 |
blocksToEvaluate := nil. |
|
1544 |
n := timeoutArray size. |
|
1545 |
anyTimeouts := false. |
|
1546 |
1 to:n do:[:index | |
|
1547 |
aTime := timeoutArray at:index. |
|
1548 |
aTime notNil ifTrue:[ |
|
1549 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[ |
|
1550 |
"this one should be triggered" |
|
1551 |
||
1552 |
sema := timeoutSemaphoreArray at:index. |
|
1553 |
sema notNil ifTrue:[ |
|
1554 |
sema signalOnce. |
|
1555 |
timeoutSemaphoreArray at:index put:nil |
|
1556 |
] ifFalse:[ |
|
1557 |
"to support pure-events" |
|
1558 |
block := timeoutActionArray at:index. |
|
1559 |
block notNil ifTrue:[ |
|
1560 |
blocksToEvaluate isNil ifTrue:[ |
|
1561 |
blocksToEvaluate := OrderedCollection new:10. |
|
1562 |
processes := OrderedCollection new:10. |
|
1563 |
]. |
|
1564 |
blocksToEvaluate add:block. |
|
1565 |
processes add:(timeoutProcessArray at:index). |
|
1566 |
timeoutActionArray at:index put:nil. |
|
1567 |
timeoutProcessArray at:index put:nil. |
|
1568 |
] |
|
1569 |
]. |
|
1570 |
timeoutArray at:index put:nil. |
|
1571 |
] ifTrue:[ |
|
1572 |
anyTimeouts := true |
|
1573 |
] |
|
1574 |
] |
|
1575 |
]. |
|
1576 |
||
1577 |
blocksToEvaluate notNil ifTrue:[ |
|
1578 |
blocksToEvaluate keysAndValuesDo:[:index :block | |
|
1579 |
|p| |
|
1580 |
||
1581 |
p := processes at:index. |
|
1582 |
(p isNil or:[p == scheduler or:[PureEventDriven]]) ifTrue:[ |
|
1583 |
block value |
|
1584 |
] ifFalse:[ |
|
1585 |
p interruptWith:block |
|
1586 |
] |
|
1587 |
] |
|
1588 |
] |
|
1589 |
! |
|
1590 |
||
1591 |
removeTimedBlock:aBlock |
|
1592 |
"remove the argument, aBlock from the list of time-sceduled-blocks." |
|
1593 |
||
1594 |
|index "{ Class: SmallInteger }" |
|
1595 |
wasBlocked| |
|
1596 |
||
1597 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1598 |
index := timeoutActionArray identityIndexOf:aBlock startingAt:1. |
|
1599 |
(index ~~ 0) ifTrue:[ |
|
1600 |
timeoutArray at:index put:nil. |
|
1601 |
timeoutActionArray at:index put:nil. |
|
1602 |
timeoutSemaphoreArray at:index put:nil. |
|
1603 |
timeoutProcessArray at:index put:nil. |
|
1604 |
]. |
|
1605 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
10 | 1606 |
! ! |
1 | 1607 |
|
10 | 1608 |
!ProcessorScheduler methodsFor:'waiting'! |
1609 |
||
699 | 1610 |
checkForInputWithTimeout:millis |
1611 |
"this is called, when there is absolutely nothing to do; |
|
1612 |
hard wait for either input to arrive or a timeout to occur." |
|
1613 |
||
1614 |
|fd index sema action| |
|
1615 |
||
1616 |
fd := OperatingSystem |
|
1617 |
selectOnAnyReadable:readFdArray |
|
1618 |
writable:writeFdArray |
|
1619 |
exception:nil |
|
1620 |
withTimeOut:millis. |
|
1621 |
fd notNil ifTrue:[ |
|
1622 |
index := readFdArray indexOf:fd. |
|
1623 |
index ~~ 0 ifTrue:[ |
|
1624 |
sema := readSemaphoreArray at:index. |
|
1625 |
sema notNil ifTrue:[ |
|
1626 |
sema signalOnce. |
|
1627 |
^ true |
|
1628 |
] ifFalse:[ |
|
1629 |
action := readCheckArray at:index. |
|
1630 |
action notNil ifTrue:[ |
|
1631 |
action value. |
|
1632 |
^ true |
|
1633 |
] |
|
1634 |
] |
|
1635 |
] |
|
1636 |
]. |
|
1637 |
^ false |
|
1638 |
! |
|
1639 |
||
10 | 1640 |
ioInterrupt |
159 | 1641 |
"data arrived while waiting - switch to scheduler process which will decide |
1642 |
what to do now." |
|
10 | 1643 |
|
181 | 1644 |
interruptedProcess := activeProcess. |
93 | 1645 |
self threadSwitch:scheduler |
10 | 1646 |
! |
1647 |
||
750
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1648 |
schedulerInterrupt |
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1649 |
"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
|
1650 |
what to do now." |
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1651 |
|
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1652 |
interruptedProcess := activeProcess. |
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1653 |
self threadSwitch:scheduler |
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1654 |
! |
f4ed622893ce
added schedulerInterrupt - for soon to come semaSignal primitive function
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1655 |
|
10 | 1656 |
timeToNextTimeout |
1657 |
"return the delta-T (in millis) to next timeout, or nil if |
|
1658 |
there is none" |
|
1659 |
||
231 | 1660 |
|aTime now delta minDelta n "{ Class: SmallInteger }"| |
10 | 1661 |
|
1662 |
"find next timeout. since there are usually not many, just search. |
|
302 | 1663 |
If there were many, the list should be kept sorted ... keeping deltas |
10 | 1664 |
to next (as in Unix kernel)" |
1665 |
||
231 | 1666 |
n := timeoutArray size. |
10 | 1667 |
1 to:n do:[:index | |
231 | 1668 |
aTime := timeoutArray at:index. |
159 | 1669 |
aTime notNil ifTrue:[ |
231 | 1670 |
now isNil ifTrue:[ |
159 | 1671 |
now := OperatingSystem getMillisecondTime. |
231 | 1672 |
]. |
1673 |
(OperatingSystem millisecondTime:aTime isAfter:now) ifFalse:[^ 0]. |
|
1674 |
delta := OperatingSystem millisecondTimeDeltaBetween:aTime and:now. |
|
1675 |
minDelta isNil ifTrue:[ |
|
1676 |
minDelta := delta |
|
159 | 1677 |
] ifFalse:[ |
231 | 1678 |
minDelta := minDelta min:delta |
159 | 1679 |
] |
1680 |
] |
|
10 | 1681 |
]. |
1682 |
||
1683 |
^ minDelta |
|
1684 |
! |
|
1685 |
||
699 | 1686 |
timerInterrupt |
1687 |
"timer expired while waiting - switch to scheduler process which will decide |
|
1688 |
what to do now." |
|
1689 |
||
1690 |
interruptedProcess := activeProcess. |
|
1691 |
self threadSwitch:scheduler |
|
1692 |
! |
|
1693 |
||
10 | 1694 |
waitForEventOrTimeout |
44 | 1695 |
"entered when no process is runnable - wait for either input on |
1696 |
any file descriptors to arrive or a timeout to happen. |
|
1697 |
If it makes sense, do some background garbage collection. |
|
1698 |
The idle actions are a leftover from previous ST/X releases and will |
|
93 | 1699 |
vanish (installing a low-prio process has the same effect)." |
44 | 1700 |
|
271 | 1701 |
|millis doingGC| |
10 | 1702 |
|
1703 |
doingGC := true. |
|
1704 |
[doingGC] whileTrue:[ |
|
159 | 1705 |
anyTimeouts ifTrue:[ |
1706 |
millis := self timeToNextTimeout. |
|
1707 |
(millis notNil and:[millis <= 0]) ifTrue:[ |
|
1708 |
^ self "oops - hurry up checking" |
|
1709 |
]. |
|
1710 |
]. |
|
10 | 1711 |
|
362 | 1712 |
" |
1713 |
if its worth doing, collect a bit of garbage; |
|
1714 |
but not, if a backgroundCollector is active |
|
1715 |
" |
|
1716 |
ObjectMemory backgroundCollectorRunning ifTrue:[ |
|
1717 |
doingGC := false |
|
1718 |
] ifFalse:[ |
|
1719 |
doingGC := ObjectMemory gcStepIfUseful. |
|
1720 |
]. |
|
10 | 1721 |
|
159 | 1722 |
"then do idle actions" |
231 | 1723 |
(idleActions notNil and:[idleActions size ~~ 0]) ifTrue:[ |
159 | 1724 |
idleActions do:[:aBlock | |
1725 |
aBlock value. |
|
1726 |
]. |
|
1727 |
^ self "go back checking" |
|
1728 |
]. |
|
10 | 1729 |
|
159 | 1730 |
doingGC ifTrue:[ |
1731 |
(self checkForInputWithTimeout:0) ifTrue:[ |
|
1732 |
^ self "go back checking" |
|
1733 |
] |
|
1734 |
] |
|
10 | 1735 |
]. |
1736 |
||
231 | 1737 |
"absolutely nothing to do - simply wait" |
10 | 1738 |
|
49 | 1739 |
OperatingSystem supportsSelect ifFalse:[ |
159 | 1740 |
"SCO instant ShitStation has a bug here, |
1741 |
waiting always 1 sec in the select - therefore we delay a bit and |
|
1742 |
return - effectively polling in 50ms cycles |
|
1743 |
" |
|
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1744 |
(self checkForInputWithTimeout:0) ifTrue:[ |
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1745 |
^ self "go back checking" |
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1746 |
]. |
159 | 1747 |
OperatingSystem millisecondDelay:50. |
1748 |
^ self |
|
10 | 1749 |
]. |
1750 |
||
1751 |
millis isNil ifTrue:[ |
|
159 | 1752 |
millis := 9999. |
1 | 1753 |
] ifFalse:[ |
159 | 1754 |
millis := millis rounded |
10 | 1755 |
]. |
1756 |
self checkForInputWithTimeout:millis |
|
768
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1757 |
|
20434b8239f3
stefans modalBox changes (no more polling)
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1758 |
"Modified: 14.12.1995 / 13:37:46 / stefan" |
10 | 1759 |
! ! |
1760 |
||
699 | 1761 |
!ProcessorScheduler class methodsFor:'documentation'! |
10 | 1762 |
|
699 | 1763 |
version |
786 | 1764 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/ProcSched.st,v 1.59 1995-12-18 14:18:32 cg Exp $' |
1 | 1765 |
! ! |
699 | 1766 |
ProcessorScheduler initialize! |