Delay.st
author Claus Gittinger <cg@exept.de>
Wed, 18 Jun 2003 15:40:19 +0200
changeset 7425 de40123f867c
parent 7424 d68d1ccc2135
child 7590 e318df34c74e
permissions -rw-r--r--
checkin from browser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     1
"
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     4
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     5
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     6
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     8
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
     9
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    10
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    11
"
9
b07612967dfa Initial revision
claus
parents:
diff changeset
    12
6423
e43f6e61bfaa #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 3917
diff changeset
    13
"{ Package: 'stx:libbasic' }"
e43f6e61bfaa #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 3917
diff changeset
    14
9
b07612967dfa Initial revision
claus
parents:
diff changeset
    15
Object subclass:#Delay
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
    16
	instanceVariableNames:'millisecondDelta resumptionTime delaySemaphore'
777
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    17
	classVariableNames:''
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    18
	poolDictionaries:''
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    19
	category:'Kernel-Processes'
9
b07612967dfa Initial revision
claus
parents:
diff changeset
    20
!
b07612967dfa Initial revision
claus
parents:
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    22
!Delay class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    23
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    26
 COPYRIGHT (c) 1993 by Claus Gittinger
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 92
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    37
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    38
documentation
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
    39
"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    40
    Instances of Delay are used to suspend the execution of a process 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    41
    (i.e. thread) for some time interval. 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    42
    Delays can be created either for some time-interval (seconds or milliseconds), 
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    43
    or for delaying until a specific time has reached.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    44
    Once created, a delay is waited upon with Delay>>wait.
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    45
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    46
    Notice: due to delays (both within unix AND within Smalltalk itself,
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
    47
    the resumption time will ALWAYS be after the actual delay time.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    48
    (i.e. a Delay for n-millis will actually suspend for more than n milliseconds)
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    49
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    50
    Warning: currently, the implementation does not support delays longer than
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    51
    a system specific maximum - future versions may remove this limitation.
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    52
    For now, do not use delays longer than the value returned by
777
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    53
        OperatingSystem maximumMillisecondTimeDelta
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    54
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    55
    Also notice: the clock resolution of the operatingSystem is usually limited
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    56
    (1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    57
    delay for at least this minimum time interval. See examples.
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    58
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    59
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    60
        Semaphore Process ProcessorScheduler
1348
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    61
        Time AbsoluteTime OperatingSystem
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    62
        (Using delay in ``Working with time, delays and interrupts'': programming/timing.html#DELAY)
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1282
diff changeset
    63
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1282
diff changeset
    64
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1282
diff changeset
    65
        Claus Gittinger
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    66
"
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    67
!
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    68
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    69
examples 
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    70
"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    71
    Check your systems resolution with:
778
afbb3ebde874 commentary
Claus Gittinger <cg@exept.de>
parents: 777
diff changeset
    72
    (make certain, that no other timed processes are running in the background when doing this)
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    73
                                                                        [exBegin]
777
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    74
        |d t1 t2 res|
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
    75
777
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    76
        Processor activeProcess priority:24.
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    77
        t1 := Time millisecondClockValue.
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    78
        d := Delay forMilliseconds:1.
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    79
        100 timesRepeat:[d wait].
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    80
        t2 := Time millisecondClockValue.
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    81
        res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1348
diff changeset
    82
        Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'.
777
452133016f54 commentary
Claus Gittinger <cg@exept.de>
parents: 695
diff changeset
    83
        Processor activeProcess priority:8.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    84
                                                                        [exEnd]
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    85
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
    86
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    87
    examples:
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    88
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    89
    delaying for some time-delta:
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    90
    (notice: you cannot use this without time-errors in a loop,
2179
3f633442dc34 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2178
diff changeset
    91
     since the errors will accumulate; after 10 runs through the loop,
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    92
     more than 5 seconds have passed)
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    93
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    94
            |d|
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    95
            d := Delay forMilliseconds:500.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    96
            10 timesRepeat:[d wait]
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    97
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    98
    prove:
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    99
                                                                    [exBegin]
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   100
            |d t1 t2 deltaT|
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   101
            d := Delay forMilliseconds:500.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   102
            t1 := Time millisecondClockValue.
2178
e255c5cd6dc7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   103
            20 timesRepeat:[
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   104
                d wait
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   105
            ].
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   106
            t2 := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   107
            deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
2178
e255c5cd6dc7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   108
            Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   109
                                                                    [exEnd]
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   110
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   111
    delaying until a specific time is reached:
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   112
    (this can be used to fix the above problem)
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   113
                                                                    [exBegin]
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   114
            |now then t1 t2 deltaT|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   115
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   116
            t1 := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   117
            now := Time millisecondClockValue.
2178
e255c5cd6dc7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   118
            20 timesRepeat:[
e255c5cd6dc7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   119
                then := OperatingSystem millisecondTimeAdd:now and:500.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   120
                (Delay untilMilliseconds:then) wait.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   121
                now := then
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   122
            ].
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   123
            t2 := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   124
            deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
2178
e255c5cd6dc7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   125
            Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   126
                                                                    [exEnd]
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   127
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   128
    instead of recreating new delays all over, 
2422
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   129
    you can also reuse it (but that does not make a big difference ;-):
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   130
                                                                    [exBegin]
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   131
            |d now then t1 t2 deltaT|
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   132
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   133
            t1 := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   134
            now := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   135
            d := Delay new.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   136
            10 timesRepeat:[
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   137
                then := OperatingSystem millisecondTimeAdd:now and:1000.
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   138
                d resumptionTime:then.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   139
                d wait.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   140
                now := then
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   141
            ].
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   142
            t2 := Time millisecondClockValue.
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   143
            deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1348
diff changeset
   144
            Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds'
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   145
                                                                    [exEnd]
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
   146
"
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
   147
! !
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
   148
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   149
!Delay class methodsFor:'instance creation'!
b07612967dfa Initial revision
claus
parents:
diff changeset
   150
38
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   151
forMilliseconds:aNumber
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   152
    "return a new Delay object for delaying aNumber milliseconds"
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   153
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   154
    ^ self new delay:aNumber
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   155
!
b07612967dfa Initial revision
claus
parents:
diff changeset
   156
38
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   157
forSeconds:aNumber
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   158
    "return a new Delay object for delaying aNumber seconds"
454b1b94a48e *** empty log message ***
claus
parents: 25
diff changeset
   159
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   160
    ^ self new delay:(aNumber * 1000) rounded
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   161
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   162
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   163
until:anAbsoluteTime
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   164
    "return a new Delay object, that will delay the active process
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   165
     until the system has reached the time represented by the argument.
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   166
     BUG:
6934
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   167
        due to the limited range of the millisecondTimer, this can 
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   168
        (currently) not be used for long delays. The maximum supported
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   169
        delay is returned by OperatingSystem>>maximumMillisecondTimeDelta."
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   170
6934
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   171
    |numberOfMilliseconds|
275
a76029ddaa98 *** empty log message ***
claus
parents: 202
diff changeset
   172
6934
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   173
    numberOfMilliseconds := anAbsoluteTime getMilliseconds - AbsoluteTime now getMilliseconds.
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   174
    ^ self new delay:numberOfMilliseconds
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   175
!
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   176
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   177
untilMilliseconds:aMillisecondTime
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   178
    "return a new Delay object, that will delay the active process
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   179
     until the systems millisecond time has reached aMillisecondTime.
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   180
    "
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   181
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   182
    ^ self new resumptionTime:aMillisecondTime
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   183
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   184
    "Modified: 18.4.1997 / 11:57:53 / stefan"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   185
! !
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   186
1033
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   187
!Delay class methodsFor:'queries'!
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   188
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   189
millisecondClockValue
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   190
    "for ST-80 compatibility"
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   191
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   192
    ^ Time millisecondClockValue
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   193
! !
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   194
873
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   195
!Delay class methodsFor:'waiting'!
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   196
1033
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   197
waitForMilliseconds:aNumber
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   198
    "wait for the given number of milliseconds.
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   199
     This is a combined instance creation & wait."
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   200
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   201
    ^ (self forMilliseconds:aNumber) wait
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   202
!
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   203
873
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   204
waitForSeconds:aNumber
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   205
    "wait for the given number of seconds.
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   206
     This is a combined instance creation & wait."
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   207
b899dd803470 added combined create & wait
Claus Gittinger <cg@exept.de>
parents: 778
diff changeset
   208
    ^ (self forSeconds:aNumber) wait
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   209
! !
b07612967dfa Initial revision
claus
parents:
diff changeset
   210
b07612967dfa Initial revision
claus
parents:
diff changeset
   211
!Delay methodsFor:'accessing'!
b07612967dfa Initial revision
claus
parents:
diff changeset
   212
6934
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   213
delay:numberOfMillis
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   214
    "set the millisecond delta"
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   215
6934
7f47cfcbe755 oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents: 6423
diff changeset
   216
    millisecondDelta := numberOfMillis.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   217
    delaySemaphore := Semaphore new name:'delaySema'.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   218
!
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   219
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   220
delaySemaphore
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   221
    "return the semaphore used to resume the waiting process"
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   222
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   223
    ^ delaySemaphore
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   224
!
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   225
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   226
resumptionTime:aMillisecondTime
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   227
    "set the resumption time"
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   228
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   229
    resumptionTime := aMillisecondTime.
2262
4c4d810f006f semaphore names
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   230
    delaySemaphore := Semaphore new name:'delaySema'.
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   231
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   232
    "Created: 18.4.1997 / 11:56:14 / stefan"
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   233
! !
b07612967dfa Initial revision
claus
parents:
diff changeset
   234
b07612967dfa Initial revision
claus
parents:
diff changeset
   235
!Delay methodsFor:'delaying'!
b07612967dfa Initial revision
claus
parents:
diff changeset
   236
b07612967dfa Initial revision
claus
parents:
diff changeset
   237
wait
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   238
    "suspend the current process until either the relative time delta
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
   239
     has passed (if millisecondDelta is non-nil), or the absolute millisecondTime
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   240
     has been reached (if resumptionTime non-nil)."
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   241
2422
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   242
    |wasBlocked|
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   243
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   244
    wasBlocked := OperatingSystem blockInterrupts.
1033
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   245
    [
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   246
        self startup.
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   247
        Processor activeProcess state:#timeWait.
1033
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   248
        delaySemaphore wait.
6423
e43f6e61bfaa #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 3917
diff changeset
   249
    ] ensure:[
2422
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   250
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   251
    ]
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   252
88
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
   253
    "
81dacba7a63a *** empty log message ***
claus
parents: 38
diff changeset
   254
     '1' printNewline.
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   255
     (Delay forSeconds:10) wait.
b07612967dfa Initial revision
claus
parents:
diff changeset
   256
     '2' printNewline
b07612967dfa Initial revision
claus
parents:
diff changeset
   257
    "
1033
244530001e84 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 873
diff changeset
   258
2422
a319ee1bf54b open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
   259
    "Modified: 26.2.1997 / 15:21:35 / cg"
2556
4e58fce26d69 Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents: 2422
diff changeset
   260
    "Modified: 18.4.1997 / 11:56:46 / stefan"
9
b07612967dfa Initial revision
claus
parents:
diff changeset
   261
! !
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   262
7425
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   263
!Delay methodsFor:'early signalling'!
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   264
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   265
resume
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   266
    "resume the waiter, even if the delay-time has not yet passed."
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   267
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   268
    [
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   269
        self disable.
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   270
        delaySemaphore signalOnce.
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   271
    ] valueUninterruptably
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   272
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   273
    "Modified: / 9.11.1998 / 20:56:43 / cg"
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   274
! !
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   275
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   276
!Delay methodsFor:'private'!
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   277
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   278
disable
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   279
    "tell the ProcessorScheduler to forget about signaling my semaphore."
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   280
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   281
    Processor disableSemaphore:delaySemaphore
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   282
!
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   283
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   284
startup
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   285
    "tell the ProcessorScheduler to signal my semaphore after a while"
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   286
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   287
    millisecondDelta notNil ifTrue:[
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   288
        Processor signal:delaySemaphore afterMilliseconds:millisecondDelta.
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   289
    ] ifFalse:[
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   290
        Processor signal:delaySemaphore atMilliseconds:resumptionTime.
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   291
    ].
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   292
! !
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   293
7424
d68d1ccc2135 refactored for compatibility
Claus Gittinger <cg@exept.de>
parents: 6934
diff changeset
   294
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   295
!Delay class methodsFor:'documentation'!
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   296
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   297
version
7425
de40123f867c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7424
diff changeset
   298
    ^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.33 2003-06-18 13:40:19 cg Exp $'
695
20ec350f92ca checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   299
! !