author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 28 Sep 2011 20:07:49 +0100 | |
branch | jv |
changeset 17865 | 598963c6ff8e |
parent 17846 | 24edc476ac18 |
child 17869 | 9610c6c94e71 |
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 |
||
1033 | 186 |
!Delay class methodsFor:'queries'! |
187 |
||
188 |
millisecondClockValue |
|
189 |
"for ST-80 compatibility" |
|
190 |
||
191 |
^ Time millisecondClockValue |
|
192 |
! ! |
|
193 |
||
873 | 194 |
!Delay class methodsFor:'waiting'! |
195 |
||
11601 | 196 |
waitFor:aTimeDuration |
197 |
"wait for the given timeDuration. |
|
198 |
This is a combined instance creation & wait." |
|
199 |
||
200 |
^ (self forMilliseconds:aTimeDuration getMilliseconds) wait |
|
201 |
||
202 |
||
203 |
" |
|
204 |
Delay waitFor:(TimeDuration seconds:5). |
|
205 |
" |
|
206 |
! |
|
207 |
||
1033 | 208 |
waitForMilliseconds:aNumber |
209 |
"wait for the given number of milliseconds. |
|
210 |
This is a combined instance creation & wait." |
|
211 |
||
212 |
^ (self forMilliseconds:aNumber) wait |
|
8585 | 213 |
|
214 |
||
215 |
" |
|
216 |
|start end| |
|
217 |
||
218 |
start := OperatingSystem getMicrosecondTime. |
|
219 |
Delay waitForMilliseconds:1. |
|
220 |
end := OperatingSystem getMicrosecondTime. |
|
221 |
||
222 |
end - start |
|
223 |
" |
|
1033 | 224 |
! |
225 |
||
873 | 226 |
waitForSeconds:aNumber |
227 |
"wait for the given number of seconds. |
|
228 |
This is a combined instance creation & wait." |
|
229 |
||
230 |
^ (self forSeconds:aNumber) wait |
|
17795 | 231 |
! |
232 |
||
233 |
waitUntil:aTimestamp |
|
234 |
"wait until a given time is reached. |
|
235 |
This is a combined instance creation & wait." |
|
236 |
||
237 |
^ (self until:aTimestamp) wait |
|
238 |
||
239 |
"Created: / 29-07-2010 / 13:51:41 / cg" |
|
9 | 240 |
! ! |
241 |
||
242 |
!Delay methodsFor:'accessing'! |
|
243 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
244 |
delay:numberOfMillis |
10725 | 245 |
"set the millisecond delta and create a new semaphore internally to wait upon" |
92 | 246 |
|
10790 | 247 |
self assert:(numberOfMillis notNil). |
17815 | 248 |
self assert:(numberOfMillis isKindOf:SmallInteger). |
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
249 |
millisecondDelta := numberOfMillis. |
2262 | 250 |
delaySemaphore := Semaphore new name:'delaySema'. |
10582 | 251 |
|
17815 | 252 |
"Modified: / 22-12-2010 / 14:18:00 / cg" |
92 | 253 |
! |
254 |
||
695 | 255 |
delaySemaphore |
256 |
"return the semaphore used to resume the waiting process" |
|
257 |
||
258 |
^ delaySemaphore |
|
259 |
! |
|
260 |
||
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
261 |
resumptionTime:aMillisecondTime |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
262 |
"set the resumption time and create a new semaphore internally to wait upon" |
92 | 263 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
264 |
resumptionTime := aMillisecondTime. |
2262 | 265 |
delaySemaphore := Semaphore new name:'delaySema'. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
266 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
267 |
"Created: 18.4.1997 / 11:56:14 / stefan" |
10582 | 268 |
! |
269 |
||
270 |
setDelayDuration:numberOfMillis |
|
271 |
"set the millisecond delta" |
|
272 |
||
10790 | 273 |
self assert:(numberOfMillis notNil). |
10582 | 274 |
millisecondDelta := numberOfMillis. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
275 |
resumptionTime := nil. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
276 |
! |
10582 | 277 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
278 |
setResumptionTime:aMillisecondTime |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
279 |
"set the resumption time" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
280 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
281 |
resumptionTime := aMillisecondTime. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
282 |
millisecondDelta := nil. |
9 | 283 |
! ! |
284 |
||
285 |
!Delay methodsFor:'delaying'! |
|
286 |
||
287 |
wait |
|
92 | 288 |
"suspend the current process until either the relative time delta |
289 |
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
|
290 |
has been reached (if resumptionTime non-nil)." |
9 | 291 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
292 |
|wasBlocked| |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
293 |
|
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
294 |
wasBlocked := OperatingSystem blockInterrupts. |
1033 | 295 |
[ |
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
296 |
self startup. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
297 |
Processor activeProcess state:#timeWait. |
1033 | 298 |
delaySemaphore wait. |
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
299 |
] ensure:[ |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
300 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
301 |
] |
9 | 302 |
|
88 | 303 |
" |
8566 | 304 |
Transcript showCR:'1'. |
9 | 305 |
(Delay forSeconds:10) wait. |
8566 | 306 |
Transcript showCR:'2'. |
9 | 307 |
" |
1033 | 308 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
309 |
"Modified: 26.2.1997 / 15:21:35 / cg" |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
310 |
"Modified: 18.4.1997 / 11:56:46 / stefan" |
9 | 311 |
! ! |
695 | 312 |
|
7425 | 313 |
!Delay methodsFor:'early signalling'! |
314 |
||
315 |
resume |
|
316 |
"resume the waiter, even if the delay-time has not yet passed." |
|
317 |
||
318 |
[ |
|
319 |
self disable. |
|
320 |
delaySemaphore signalOnce. |
|
321 |
] valueUninterruptably |
|
322 |
||
323 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
324 |
! ! |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
325 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
326 |
!Delay methodsFor:'private'! |
695 | 327 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
328 |
disable |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
329 |
"tell the ProcessorScheduler to forget about signaling my semaphore." |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
330 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
331 |
Processor disableSemaphore:delaySemaphore |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
332 |
! |
695 | 333 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
334 |
startup |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
335 |
"tell the ProcessorScheduler to signal my semaphore after a while" |
695 | 336 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
337 |
millisecondDelta notNil ifTrue:[ |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
338 |
Processor signal:delaySemaphore afterMilliseconds:millisecondDelta. |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
339 |
] ifFalse:[ |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
340 |
Processor signal:delaySemaphore atMilliseconds:resumptionTime. |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
341 |
]. |
695 | 342 |
! ! |
343 |
||
344 |
!Delay class methodsFor:'documentation'! |
|
345 |
||
346 |
version |
|
17865 | 347 |
^ '$Id: Delay.st 10695 2011-09-28 19:07:49Z vranyj1 $' |
17795 | 348 |
! |
349 |
||
350 |
version_CVS |
|
17845 | 351 |
^ '§Header: /cvs/stx/stx/libbasic/Delay.st,v 1.44 2010/12/22 13:18:18 cg Exp §' |
17865 | 352 |
! |
353 |
||
354 |
version_SVN |
|
355 |
^ '$Id: Delay.st 10695 2011-09-28 19:07:49Z vranyj1 $' |
|
17846 | 356 |
! ! |