author | Claus Gittinger <cg@exept.de> |
Fri, 29 Aug 2003 21:23:45 +0200 | |
changeset 7590 | e318df34c74e |
parent 7425 | de40123f867c |
child 8254 | 73b6109ab982 |
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 |
|
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 | 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 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Processes' |
|
9 | 20 |
! |
21 |
||
88 | 22 |
!Delay class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
202 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
92 | 40 |
Instances of Delay are used to suspend the execution of a process |
41 |
(i.e. thread) for some time interval. |
|
42 |
Delays can be created either for some time-interval (seconds or milliseconds), |
|
43 |
or for delaying until a specific time has reached. |
|
44 |
Once created, a delay is waited upon with Delay>>wait. |
|
45 |
||
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 | 48 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds) |
49 |
||
275 | 50 |
Warning: currently, the implementation does not support delays longer than |
51 |
a system specific maximum - future versions may remove this limitation. |
|
52 |
For now, do not use delays longer than the value returned by |
|
777 | 53 |
OperatingSystem maximumMillisecondTimeDelta |
275 | 54 |
|
55 |
Also notice: the clock resolution of the operatingSystem is usually limited |
|
56 |
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will |
|
1273 | 57 |
delay for at least this minimum time interval. See examples. |
58 |
||
59 |
[see also:] |
|
60 |
Semaphore Process ProcessorScheduler |
|
1348 | 61 |
Time AbsoluteTime OperatingSystem |
62 |
(Using delay in ``Working with time, delays and interrupts'': programming/timing.html#DELAY) |
|
1294 | 63 |
|
64 |
[author:] |
|
65 |
Claus Gittinger |
|
1273 | 66 |
" |
67 |
! |
|
68 |
||
69 |
examples |
|
70 |
" |
|
92 | 71 |
Check your systems resolution with: |
778 | 72 |
(make certain, that no other timed processes are running in the background when doing this) |
1316 | 73 |
[exBegin] |
777 | 74 |
|d t1 t2 res| |
275 | 75 |
|
777 | 76 |
Processor activeProcess priority:24. |
77 |
t1 := Time millisecondClockValue. |
|
78 |
d := Delay forMilliseconds:1. |
|
79 |
100 timesRepeat:[d wait]. |
|
80 |
t2 := Time millisecondClockValue. |
|
81 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100. |
|
1422 | 82 |
Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'. |
777 | 83 |
Processor activeProcess priority:8. |
1316 | 84 |
[exEnd] |
92 | 85 |
|
1273 | 86 |
|
92 | 87 |
examples: |
88 |
||
1316 | 89 |
delaying for some time-delta: |
90 |
(notice: you cannot use this without time-errors in a loop, |
|
2179 | 91 |
since the errors will accumulate; after 10 runs through the loop, |
1316 | 92 |
more than 5 seconds have passed) |
92 | 93 |
|
1316 | 94 |
|d| |
95 |
d := Delay forMilliseconds:500. |
|
96 |
10 timesRepeat:[d wait] |
|
92 | 97 |
|
1316 | 98 |
prove: |
99 |
[exBegin] |
|
100 |
|d t1 t2 deltaT| |
|
101 |
d := Delay forMilliseconds:500. |
|
102 |
t1 := Time millisecondClockValue. |
|
2178 | 103 |
20 timesRepeat:[ |
1316 | 104 |
d wait |
105 |
]. |
|
106 |
t2 := Time millisecondClockValue. |
|
107 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 108 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 109 |
[exEnd] |
92 | 110 |
|
1316 | 111 |
delaying until a specific time is reached: |
112 |
(this can be used to fix the above problem) |
|
113 |
[exBegin] |
|
114 |
|now then t1 t2 deltaT| |
|
92 | 115 |
|
1316 | 116 |
t1 := Time millisecondClockValue. |
117 |
now := Time millisecondClockValue. |
|
2178 | 118 |
20 timesRepeat:[ |
119 |
then := OperatingSystem millisecondTimeAdd:now and:500. |
|
1316 | 120 |
(Delay untilMilliseconds:then) wait. |
121 |
now := then |
|
122 |
]. |
|
123 |
t2 := Time millisecondClockValue. |
|
124 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 125 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 126 |
[exEnd] |
92 | 127 |
|
1316 | 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 | 130 |
[exBegin] |
131 |
|d now then t1 t2 deltaT| |
|
92 | 132 |
|
1316 | 133 |
t1 := Time millisecondClockValue. |
134 |
now := Time millisecondClockValue. |
|
135 |
d := Delay new. |
|
136 |
10 timesRepeat:[ |
|
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 | 139 |
d wait. |
140 |
now := then |
|
141 |
]. |
|
142 |
t2 := Time millisecondClockValue. |
|
143 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
1422 | 144 |
Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds' |
1316 | 145 |
[exEnd] |
88 | 146 |
" |
147 |
! ! |
|
148 |
||
9 | 149 |
!Delay class methodsFor:'instance creation'! |
150 |
||
38 | 151 |
forMilliseconds:aNumber |
152 |
"return a new Delay object for delaying aNumber milliseconds" |
|
153 |
||
92 | 154 |
^ self new delay:aNumber |
9 | 155 |
! |
156 |
||
38 | 157 |
forSeconds:aNumber |
158 |
"return a new Delay object for delaying aNumber seconds" |
|
159 |
||
92 | 160 |
^ self new delay:(aNumber * 1000) rounded |
161 |
! |
|
162 |
||
275 | 163 |
until:anAbsoluteTime |
164 |
"return a new Delay object, that will delay the active process |
|
165 |
until the system has reached the time represented by the argument. |
|
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 | 170 |
|
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
171 |
|numberOfMilliseconds| |
275 | 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 | 175 |
! |
176 |
||
177 |
untilMilliseconds:aMillisecondTime |
|
178 |
"return a new Delay object, that will delay the active process |
|
179 |
until the systems millisecond time has reached aMillisecondTime. |
|
180 |
" |
|
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 | 185 |
! ! |
186 |
||
1033 | 187 |
!Delay class methodsFor:'queries'! |
188 |
||
189 |
millisecondClockValue |
|
190 |
"for ST-80 compatibility" |
|
191 |
||
192 |
^ Time millisecondClockValue |
|
193 |
! ! |
|
194 |
||
873 | 195 |
!Delay class methodsFor:'waiting'! |
196 |
||
1033 | 197 |
waitForMilliseconds:aNumber |
198 |
"wait for the given number of milliseconds. |
|
199 |
This is a combined instance creation & wait." |
|
200 |
||
201 |
^ (self forMilliseconds:aNumber) wait |
|
202 |
! |
|
203 |
||
873 | 204 |
waitForSeconds:aNumber |
205 |
"wait for the given number of seconds. |
|
206 |
This is a combined instance creation & wait." |
|
207 |
||
208 |
^ (self forSeconds:aNumber) wait |
|
9 | 209 |
! ! |
210 |
||
211 |
!Delay methodsFor:'accessing'! |
|
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 | 214 |
"set the millisecond delta" |
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 | 217 |
delaySemaphore := Semaphore new name:'delaySema'. |
92 | 218 |
! |
219 |
||
695 | 220 |
delaySemaphore |
221 |
"return the semaphore used to resume the waiting process" |
|
222 |
||
223 |
^ delaySemaphore |
|
224 |
! |
|
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 | 228 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
229 |
resumptionTime := aMillisecondTime. |
2262 | 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 | 233 |
! ! |
234 |
||
235 |
!Delay methodsFor:'delaying'! |
|
236 |
||
237 |
wait |
|
92 | 238 |
"suspend the current process until either the relative time delta |
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 | 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 | 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 | 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 | 252 |
|
88 | 253 |
" |
254 |
'1' printNewline. |
|
9 | 255 |
(Delay forSeconds:10) wait. |
256 |
'2' printNewline |
|
257 |
" |
|
1033 | 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 | 261 |
! ! |
695 | 262 |
|
7425 | 263 |
!Delay methodsFor:'early signalling'! |
264 |
||
265 |
resume |
|
266 |
"resume the waiter, even if the delay-time has not yet passed." |
|
267 |
||
268 |
[ |
|
269 |
self disable. |
|
270 |
delaySemaphore signalOnce. |
|
271 |
] valueUninterruptably |
|
272 |
||
273 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
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 | 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 | 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 | 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 | 292 |
! ! |
293 |
||
294 |
!Delay class methodsFor:'documentation'! |
|
295 |
||
296 |
version |
|
7590 | 297 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.34 2003-08-29 19:23:34 cg Exp $' |
695 | 298 |
! ! |