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-- |
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 |
" |
|
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 | 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 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
18 |
category:'Kernel-Processes' |
|
9 | 19 |
! |
20 |
||
88 | 21 |
!Delay class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
202 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
92 | 39 |
Instances of Delay are used to suspend the execution of a process |
40 |
(i.e. thread) for some time interval. |
|
41 |
Delays can be created either for some time-interval (seconds or milliseconds), |
|
42 |
or for delaying until a specific time has reached. |
|
43 |
Once created, a delay is waited upon with Delay>>wait. |
|
44 |
||
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 | 47 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds) |
48 |
||
275 | 49 |
Warning: currently, the implementation does not support delays longer than |
50 |
a system specific maximum - future versions may remove this limitation. |
|
51 |
For now, do not use delays longer than the value returned by |
|
777 | 52 |
OperatingSystem maximumMillisecondTimeDelta |
275 | 53 |
|
54 |
Also notice: the clock resolution of the operatingSystem is usually limited |
|
55 |
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will |
|
1273 | 56 |
delay for at least this minimum time interval. See examples. |
57 |
||
58 |
[see also:] |
|
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 | 61 |
(Using delay in ``Working with time, delays and interrupts'': programming/timing.html#DELAY) |
1294 | 62 |
|
63 |
[author:] |
|
64 |
Claus Gittinger |
|
1273 | 65 |
" |
66 |
! |
|
67 |
||
68 |
examples |
|
69 |
" |
|
92 | 70 |
Check your systems resolution with: |
778 | 71 |
(make certain, that no other timed processes are running in the background when doing this) |
1316 | 72 |
[exBegin] |
777 | 73 |
|d t1 t2 res| |
275 | 74 |
|
777 | 75 |
Processor activeProcess priority:24. |
76 |
t1 := Time millisecondClockValue. |
|
77 |
d := Delay forMilliseconds:1. |
|
78 |
100 timesRepeat:[d wait]. |
|
79 |
t2 := Time millisecondClockValue. |
|
80 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100. |
|
1422 | 81 |
Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'. |
777 | 82 |
Processor activeProcess priority:8. |
1316 | 83 |
[exEnd] |
92 | 84 |
|
1273 | 85 |
|
92 | 86 |
examples: |
87 |
||
1316 | 88 |
delaying for some time-delta: |
89 |
(notice: you cannot use this without time-errors in a loop, |
|
2179 | 90 |
since the errors will accumulate; after 10 runs through the loop, |
1316 | 91 |
more than 5 seconds have passed) |
92 | 92 |
|
1316 | 93 |
|d| |
94 |
d := Delay forMilliseconds:500. |
|
95 |
10 timesRepeat:[d wait] |
|
92 | 96 |
|
1316 | 97 |
prove: |
98 |
[exBegin] |
|
99 |
|d t1 t2 deltaT| |
|
100 |
d := Delay forMilliseconds:500. |
|
101 |
t1 := Time millisecondClockValue. |
|
2178 | 102 |
20 timesRepeat:[ |
1316 | 103 |
d wait |
104 |
]. |
|
105 |
t2 := Time millisecondClockValue. |
|
106 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 107 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 108 |
[exEnd] |
92 | 109 |
|
1316 | 110 |
delaying until a specific time is reached: |
111 |
(this can be used to fix the above problem) |
|
112 |
[exBegin] |
|
113 |
|now then t1 t2 deltaT| |
|
92 | 114 |
|
1316 | 115 |
t1 := Time millisecondClockValue. |
116 |
now := Time millisecondClockValue. |
|
2178 | 117 |
20 timesRepeat:[ |
118 |
then := OperatingSystem millisecondTimeAdd:now and:500. |
|
1316 | 119 |
(Delay untilMilliseconds:then) wait. |
120 |
now := then |
|
121 |
]. |
|
122 |
t2 := Time millisecondClockValue. |
|
123 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 124 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 125 |
[exEnd] |
92 | 126 |
|
1316 | 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 | 129 |
[exBegin] |
130 |
|d now then t1 t2 deltaT| |
|
92 | 131 |
|
1316 | 132 |
t1 := Time millisecondClockValue. |
133 |
now := Time millisecondClockValue. |
|
134 |
d := Delay new. |
|
135 |
10 timesRepeat:[ |
|
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 | 138 |
d wait. |
139 |
now := then |
|
140 |
]. |
|
141 |
t2 := Time millisecondClockValue. |
|
142 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
1422 | 143 |
Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds' |
1316 | 144 |
[exEnd] |
88 | 145 |
" |
146 |
! ! |
|
147 |
||
9 | 148 |
!Delay class methodsFor:'instance creation'! |
149 |
||
38 | 150 |
forMilliseconds:aNumber |
151 |
"return a new Delay object for delaying aNumber milliseconds" |
|
152 |
||
92 | 153 |
^ self new delay:aNumber |
9 | 154 |
! |
155 |
||
38 | 156 |
forSeconds:aNumber |
157 |
"return a new Delay object for delaying aNumber seconds" |
|
158 |
||
92 | 159 |
^ self new delay:(aNumber * 1000) rounded |
160 |
! |
|
161 |
||
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
162 |
until:aTimeStamp |
275 | 163 |
"return a new Delay object, that will delay the active process |
164 |
until the system has reached the time represented by the argument. |
|
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 | 169 |
|
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
170 |
|numberOfMilliseconds| |
275 | 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 | 174 |
! |
175 |
||
176 |
untilMilliseconds:aMillisecondTime |
|
177 |
"return a new Delay object, that will delay the active process |
|
178 |
until the systems millisecond time has reached aMillisecondTime. |
|
179 |
" |
|
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 | 184 |
! ! |
185 |
||
10582 | 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 |
|
8585 | 202 |
|
203 |
||
204 |
" |
|
205 |
|start end| |
|
206 |
||
207 |
start := OperatingSystem getMicrosecondTime. |
|
208 |
Delay waitForMilliseconds:1. |
|
209 |
end := OperatingSystem getMicrosecondTime. |
|
210 |
||
211 |
end - start |
|
212 |
" |
|
1033 | 213 |
! |
214 |
||
873 | 215 |
waitForSeconds:aNumber |
216 |
"wait for the given number of seconds. |
|
217 |
This is a combined instance creation & wait." |
|
218 |
||
219 |
^ (self forSeconds:aNumber) wait |
|
9 | 220 |
! ! |
221 |
||
222 |
!Delay methodsFor:'accessing'! |
|
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 | 225 |
"set the millisecond delta and create a new semapjore internally to wait upon" |
92 | 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 | 228 |
delaySemaphore := Semaphore new name:'delaySema'. |
10582 | 229 |
|
230 |
"Modified: / 31-05-2007 / 15:03:47 / cg" |
|
92 | 231 |
! |
232 |
||
695 | 233 |
delaySemaphore |
234 |
"return the semaphore used to resume the waiting process" |
|
235 |
||
236 |
^ delaySemaphore |
|
237 |
! |
|
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 | 241 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
242 |
resumptionTime := aMillisecondTime. |
2262 | 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 | 246 |
! |
247 |
||
248 |
setDelayDuration:numberOfMillis |
|
249 |
"set the millisecond delta" |
|
250 |
||
251 |
millisecondDelta := numberOfMillis. |
|
252 |
||
253 |
"Created: / 31-05-2007 / 15:03:26 / cg" |
|
9 | 254 |
! ! |
255 |
||
256 |
!Delay methodsFor:'delaying'! |
|
257 |
||
258 |
wait |
|
92 | 259 |
"suspend the current process until either the relative time delta |
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 | 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 | 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 | 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 | 273 |
|
88 | 274 |
" |
8566 | 275 |
Transcript showCR:'1'. |
9 | 276 |
(Delay forSeconds:10) wait. |
8566 | 277 |
Transcript showCR:'2'. |
9 | 278 |
" |
1033 | 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 | 282 |
! ! |
695 | 283 |
|
7425 | 284 |
!Delay methodsFor:'early signalling'! |
285 |
||
286 |
resume |
|
287 |
"resume the waiter, even if the delay-time has not yet passed." |
|
288 |
||
289 |
[ |
|
290 |
self disable. |
|
291 |
delaySemaphore signalOnce. |
|
292 |
] valueUninterruptably |
|
293 |
||
294 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
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 | 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 | 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 | 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 | 313 |
! ! |
314 |
||
315 |
!Delay class methodsFor:'documentation'! |
|
316 |
||
317 |
version |
|
10582 | 318 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.38 2007-05-31 13:43:26 cg Exp $' |
695 | 319 |
! ! |