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