author | Claus Gittinger <cg@exept.de> |
Thu, 25 Jul 2013 11:35:36 +0200 | |
changeset 15553 | 8e0b933a9d68 |
parent 15552 | 9a800a2e1e81 |
child 15630 | 9e0f47e44106 |
child 18079 | 7b5afc0ad3d5 |
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 |
13802 | 15 |
instanceVariableNames:'millisecondDelta resumptionTime delaySemaphore isInterrupted' |
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 |
||
15552 | 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 |
||
15552 | 49 |
Warning: |
50 |
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 |
|
15552 | 55 |
Also notice: the clock resolution of the operating system is usually limited |
275 | 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 |
|
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
61 |
Time Timestamp OperatingSystem |
1348 | 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 |
||
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
163 |
until:aTimeStamp |
275 | 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 |
|
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
173 |
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
|
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 |
||
11601 | 197 |
waitFor:aTimeDuration |
198 |
"wait for the given timeDuration. |
|
199 |
This is a combined instance creation & wait." |
|
200 |
||
201 |
^ (self forMilliseconds:aTimeDuration getMilliseconds) wait |
|
202 |
||
203 |
||
204 |
" |
|
205 |
Delay waitFor:(TimeDuration seconds:5). |
|
206 |
" |
|
207 |
! |
|
208 |
||
1033 | 209 |
waitForMilliseconds:aNumber |
210 |
"wait for the given number of milliseconds. |
|
211 |
This is a combined instance creation & wait." |
|
212 |
||
213 |
^ (self forMilliseconds:aNumber) wait |
|
8585 | 214 |
|
215 |
||
216 |
" |
|
217 |
|start end| |
|
218 |
||
219 |
start := OperatingSystem getMicrosecondTime. |
|
220 |
Delay waitForMilliseconds:1. |
|
221 |
end := OperatingSystem getMicrosecondTime. |
|
222 |
||
223 |
end - start |
|
224 |
" |
|
1033 | 225 |
! |
226 |
||
873 | 227 |
waitForSeconds:aNumber |
228 |
"wait for the given number of seconds. |
|
229 |
This is a combined instance creation & wait." |
|
230 |
||
231 |
^ (self forSeconds:aNumber) wait |
|
12982 | 232 |
! |
233 |
||
234 |
waitUntil:aTimestamp |
|
235 |
"wait until a given time is reached. |
|
236 |
This is a combined instance creation & wait." |
|
237 |
||
238 |
^ (self until:aTimestamp) wait |
|
239 |
||
240 |
"Created: / 29-07-2010 / 13:51:41 / cg" |
|
9 | 241 |
! ! |
242 |
||
243 |
!Delay methodsFor:'accessing'! |
|
244 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
245 |
delay:numberOfMillis |
10725 | 246 |
"set the millisecond delta and create a new semaphore internally to wait upon" |
92 | 247 |
|
10790 | 248 |
self assert:(numberOfMillis notNil). |
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 |
|
13185 | 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 |
11898 | 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. |
11898 | 275 |
resumptionTime := nil. |
276 |
! |
|
10582 | 277 |
|
11898 | 278 |
setResumptionTime:aMillisecondTime |
279 |
"set the resumption time" |
|
280 |
||
281 |
resumptionTime := aMillisecondTime. |
|
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 |
|
13802 | 292 |
|wasBlocked currentDelta dueTime now then| |
293 |
||
294 |
isInterrupted := false. |
|
295 |
||
296 |
millisecondDelta notNil ifTrue:[ |
|
297 |
now := OperatingSystem getMillisecondTime. |
|
298 |
currentDelta := millisecondDelta rounded. |
|
299 |
currentDelta > 16r0fffffff ifTrue:[ |
|
300 |
"NOTE: the microsecondTime is increasing monotonically, |
|
301 |
while millisecondTime is wrapping at 16r1fffffff. |
|
302 |
So use the microsecondTime to check when we are finished" |
|
303 |
dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000). |
|
304 |
currentDelta := 16r0fffffff. |
|
305 |
]. |
|
306 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta . |
|
307 |
] ifFalse:[ |
|
308 |
then := resumptionTime. |
|
309 |
]. |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
310 |
|
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
311 |
wasBlocked := OperatingSystem blockInterrupts. |
1033 | 312 |
[ |
13802 | 313 |
[ |
314 |
Processor signal:delaySemaphore atMilliseconds:then. |
|
15553 | 315 |
Processor activeProcess setStateTo:#timeWait if:#active. |
13802 | 316 |
delaySemaphore wait. |
317 |
] doWhile:[ |
|
318 |
(dueTime notNil |
|
319 |
and:[isInterrupted not |
|
320 |
and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0]] |
|
321 |
) ifTrue:[ |
|
322 |
currentDelta := (currentDelta // 1000) min:16r0fffffff. |
|
323 |
now := OperatingSystem getMillisecondTime. |
|
324 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta. |
|
325 |
true. |
|
326 |
] ifFalse:[ false ] |
|
327 |
]. |
|
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
328 |
] ensure:[ |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
329 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
330 |
] |
9 | 331 |
|
88 | 332 |
" |
8566 | 333 |
Transcript showCR:'1'. |
9 | 334 |
(Delay forSeconds:10) wait. |
8566 | 335 |
Transcript showCR:'2'. |
9 | 336 |
" |
1033 | 337 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
338 |
"Modified: 26.2.1997 / 15:21:35 / cg" |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
339 |
"Modified: 18.4.1997 / 11:56:46 / stefan" |
9 | 340 |
! ! |
695 | 341 |
|
7425 | 342 |
!Delay methodsFor:'early signalling'! |
343 |
||
344 |
resume |
|
345 |
"resume the waiter, even if the delay-time has not yet passed." |
|
346 |
||
347 |
[ |
|
13802 | 348 |
isInterrupted := true. |
7425 | 349 |
self disable. |
350 |
delaySemaphore signalOnce. |
|
351 |
] valueUninterruptably |
|
352 |
||
353 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
354 |
! ! |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
355 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
356 |
!Delay methodsFor:'private'! |
695 | 357 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
358 |
disable |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
359 |
"tell the ProcessorScheduler to forget about signaling my semaphore." |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
360 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
361 |
Processor disableSemaphore:delaySemaphore |
695 | 362 |
! ! |
363 |
||
364 |
!Delay class methodsFor:'documentation'! |
|
365 |
||
366 |
version |
|
15553 | 367 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.47 2013-07-25 09:35:36 cg Exp $' |
12982 | 368 |
! |
369 |
||
370 |
version_CVS |
|
15553 | 371 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.47 2013-07-25 09:35:36 cg Exp $' |
695 | 372 |
! ! |
15552 | 373 |