eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Delay':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Delay


Inheritance:

   Object
   |
   +--Delay

Package:
stx:libbasic
Category:
Kernel-Processes
Version:
rev: 1.64 date: 2022/02/23 08:05:12
user: cg
file: Delay.st directory: libbasic
module: stx stc-classLibrary: libbasic

Description:


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.

copyright

COPYRIGHT (c) 1993 by Claus Gittinger All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

Compatibility-Squeak
o  forDuration: aTimeDuration
return a new Delay object for delaying aTimeDuration

Usage example(s):

      Delay forDuration:10 seconds

Compatibility-VW
o  forMicroseconds: aNumber
( an extension from the stx:libcompat package )
return a new Delay object for delaying aNumber microseconds;
Be aware, that most operating systems do not support such small
times in their scheduler, and often the time is rounded up
to milliseconds (actually, for now, it is done always here).

o  untilMicroseconds: aMicrosecondTime
( an extension from the stx:libcompat package )
for compatibility only...
Do not use

instance creation
o  for: aTimeDurationOrNumberOfSeconds
return a new Delay object for delaying aTimeDuration

Usage example(s):

      Delay for:10 seconds
      Delay for:2.5

o  forMilliseconds: aNumber
return a new Delay object for delaying aNumber milliseconds

o  forSeconds: secondsOrTimeDuration
return a new Delay object for delaying secondsOrTimeDuration seconds

o  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

o  untilMilliseconds: aMillisecondTime
return a new Delay object, that will delay the active process
until the system's millisecond time has reached aMillisecondTime.

queries
o  millisecondClockValue
for ST-80 compatibility

waiting
o  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.

o  waitForMilliseconds: aNumber
wait for the given number of milliseconds.
This is a combined instance creation & wait.

Usage example(s):

        |start end|

        start := OperatingSystem getMicrosecondTime.
        Delay waitForMilliseconds:1.
        end := OperatingSystem getMicrosecondTime.

        end - start

o  waitForSeconds: aNumber
wait for the given number of seconds.
This is a combined instance creation & wait.

o  waitUntil: aTimestamp
wait until a given time is reached.
This is a combined instance creation & wait.


Instance protocol:

accessing
o  delay: numberOfMillis
set the millisecond delta and create a new semaphore internally to wait upon

o  delaySemaphore
return the semaphore used to resume the waiting process

o  resumptionTime: aMillisecondTime
set the resumption time and create a new semaphore internally to wait upon

o  setDelayDuration: numberOfMillis
set the millisecond delta

o  setResumptionTime: aMillisecondTime
set the resumption time

delaying
o  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
o  resume
resume the waiter, even if the delay-time has not yet passed.

printing
o  printOn: aStream
(Delay forSeconds:2) printString
(Delay until:(Timestamp now + 10 seconds)) printString

private
o  disable
tell the ProcessorScheduler to forget about signaling my semaphore.


Examples:


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'


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Tue, 10 Dec 2024 08:14:18 GMT