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