|
Class: Delay
Object
|
+--Delay
- Package:
- stx:libbasic
- Category:
- Kernel-Processes
- Version:
- rev:
1.57
date: 2019/05/27 19:55:52
- user: stefan
- file: Delay.st directory: libbasic
- module: stx stc-classLibrary: libbasic
- Author:
- Claus Gittinger
Instances of Delay are used to suspend the execution of a process
(i.e. thread) for some time interval.
Delays can be created either for some time-interval (seconds or milliseconds),
or for delaying until a specific time has reached.
Once created, a delay is waited upon with Delay>>wait.
Notice: due to delays (both within Unix AND within Smalltalk itself,
the resumption time will ALWAYS be after the actual delay time.
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds)
Warning:
currently, the implementation does not support delays longer than
a system specific maximum - future versions may remove this limitation.
For now, do not use delays longer than the value returned by
OperatingSystem maximumMillisecondTimeDelta
Also notice: the clock resolution of the operating system is usually limited
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will
delay for at least this minimum time interval. See examples.
Semaphore
Process
ProcessorScheduler
Time
Timestamp
OperatingSystem
[Using delay in ``Working with time, delays and interrupts'']
instance creation
-
for: aTimeDuration
-
return a new Delay object for delaying aTimeDuration
usage example(s):
Delay for:10 seconds
Delay for:2.5
|
-
forMilliseconds: aNumber
-
return a new Delay object for delaying aNumber milliseconds
-
forSeconds: secondsOrTimeDuration
-
return a new Delay object for delaying secondsOrTimeDuration seconds
-
until: aTimeStamp
-
return a new Delay object, that will delay the active process
until the system has reached the time represented by the argument.
usage example(s):
(self until:(Timestamp now + 1 seconds)) wait
(self until:(Timestamp now - 30 seconds)) wait
(self until:Date tomorrow asTimestamp) wait
|
-
untilMilliseconds: aMillisecondTime
-
return a new Delay object, that will delay the active process
until the systems millisecond time has reached aMillisecondTime.
queries
-
millisecondClockValue
-
for ST-80 compatibility
waiting
-
waitFor: aTimeDuration
-
wait for the given timeDuration.
This is a combined instance creation & wait.
usage example(s):
Delay waitFor:(TimeDuration seconds:5).
Delay waitFor:2.5.
|
-
waitForMilliseconds: aNumber
-
wait for the given number of milliseconds.
This is a combined instance creation & wait.
-
waitForSeconds: aNumber
-
wait for the given number of seconds.
This is a combined instance creation & wait.
-
waitUntil: aTimestamp
-
wait until a given time is reached.
This is a combined instance creation & wait.
accessing
-
delay: numberOfMillis
-
set the millisecond delta and create a new semaphore internally to wait upon
-
delaySemaphore
-
return the semaphore used to resume the waiting process
-
resumptionTime: aMillisecondTime
-
set the resumption time and create a new semaphore internally to wait upon
-
setDelayDuration: numberOfMillis
-
set the millisecond delta
-
setResumptionTime: aMillisecondTime
-
set the resumption time
delaying
-
wait
-
suspend the current process until either the relative time delta
has passed (if millisecondDelta is non-nil), or the absolute millisecondTime
has been reached (if resumptionTime non-nil).
usage example(s):
Transcript showCR:'1'.
(Delay forSeconds:10) wait.
Transcript showCR:'2'.
|
early signalling
-
resume
-
resume the waiter, even if the delay-time has not yet passed.
private
-
disable
-
tell the ProcessorScheduler to forget about signaling my semaphore.
Check your systems resolution with:
(make certain, that no other timed processes are running in the background when doing this)
|d t1 t2 res|
Processor activeProcess priority:24.
t1 := Time millisecondClockValue.
d := Delay forMilliseconds:1.
100 timesRepeat:[d wait].
t2 := Time millisecondClockValue.
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'.
Processor activeProcess priority:8.
|
examples:
delaying for some time-delta:
(notice: you cannot use this without time-errors in a loop,
since the errors will accumulate; after 10 runs through the loop,
more than 5 seconds have passed)
|d|
d := Delay forMilliseconds:500.
10 timesRepeat:[d wait]
prove:
|d t1 t2 deltaT|
d := Delay forMilliseconds:500.
t1 := Time millisecondClockValue.
20 timesRepeat:[
d wait
].
t2 := Time millisecondClockValue.
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
|
delaying until a specific time is reached:
(this can be used to fix the above problem)
|now then t1 t2 deltaT|
t1 := Time millisecondClockValue.
now := Time millisecondClockValue.
20 timesRepeat:[
then := OperatingSystem millisecondTimeAdd:now and:500.
(Delay untilMilliseconds:then) wait.
now := then
].
t2 := Time millisecondClockValue.
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
|
instead of recreating new delays all over,
you can also reuse it (but that does not make a big difference ;-):
|d now then t1 t2 deltaT|
t1 := Time millisecondClockValue.
now := Time millisecondClockValue.
d := Delay new.
10 timesRepeat:[
then := OperatingSystem millisecondTimeAdd:now and:1000.
d resumptionTime:then.
d wait.
now := then
].
t2 := Time millisecondClockValue.
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds'
|
|