author | Stefan Vogel <sv@exept.de> |
Mon, 22 Jun 2015 11:33:37 +0200 | |
branch | expecco_2_7_5_branch |
changeset 18499 | b132ac7c9d6a |
parent 16404 | ebb89de51a93 |
child 18120 | e3a375d5f6a8 |
child 18996 | d27c313ccf06 |
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 |
||
16404 | 151 |
for:aTimeDuration |
152 |
"return a new Delay object for delaying aNumber seconds" |
|
153 |
||
154 |
^ self new delay:aTimeDuration getMilliseconds. |
|
155 |
||
156 |
" |
|
157 |
Delay for:10 seconds |
|
158 |
" |
|
159 |
! |
|
160 |
||
38 | 161 |
forMilliseconds:aNumber |
162 |
"return a new Delay object for delaying aNumber milliseconds" |
|
163 |
||
92 | 164 |
^ self new delay:aNumber |
9 | 165 |
! |
166 |
||
38 | 167 |
forSeconds:aNumber |
168 |
"return a new Delay object for delaying aNumber seconds" |
|
169 |
||
92 | 170 |
^ self new delay:(aNumber * 1000) rounded |
171 |
! |
|
172 |
||
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
173 |
until:aTimeStamp |
275 | 174 |
"return a new Delay object, that will delay the active process |
15630 | 175 |
until the system has reached the time represented by the argument." |
275 | 176 |
|
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
177 |
|numberOfMilliseconds| |
275 | 178 |
|
16351 | 179 |
"/ numberOfMilliseconds := aTimeStamp getMilliseconds - Timestamp now getMilliseconds. |
180 |
numberOfMilliseconds := aTimeStamp millisecondDeltaFrom:Timestamp now. |
|
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
181 |
^ self new delay:numberOfMilliseconds |
695 | 182 |
! |
183 |
||
184 |
untilMilliseconds:aMillisecondTime |
|
185 |
"return a new Delay object, that will delay the active process |
|
15630 | 186 |
until the systems millisecond time has reached aMillisecondTime." |
695 | 187 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
188 |
^ self new resumptionTime:aMillisecondTime |
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
189 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
190 |
"Modified: 18.4.1997 / 11:57:53 / stefan" |
92 | 191 |
! ! |
192 |
||
1033 | 193 |
!Delay class methodsFor:'queries'! |
194 |
||
195 |
millisecondClockValue |
|
196 |
"for ST-80 compatibility" |
|
197 |
||
198 |
^ Time millisecondClockValue |
|
199 |
! ! |
|
200 |
||
873 | 201 |
!Delay class methodsFor:'waiting'! |
202 |
||
11601 | 203 |
waitFor:aTimeDuration |
204 |
"wait for the given timeDuration. |
|
205 |
This is a combined instance creation & wait." |
|
206 |
||
207 |
^ (self forMilliseconds:aTimeDuration getMilliseconds) wait |
|
208 |
||
209 |
||
210 |
" |
|
211 |
Delay waitFor:(TimeDuration seconds:5). |
|
212 |
" |
|
213 |
! |
|
214 |
||
1033 | 215 |
waitForMilliseconds:aNumber |
216 |
"wait for the given number of milliseconds. |
|
217 |
This is a combined instance creation & wait." |
|
218 |
||
219 |
^ (self forMilliseconds:aNumber) wait |
|
8585 | 220 |
|
221 |
||
222 |
" |
|
223 |
|start end| |
|
224 |
||
225 |
start := OperatingSystem getMicrosecondTime. |
|
226 |
Delay waitForMilliseconds:1. |
|
227 |
end := OperatingSystem getMicrosecondTime. |
|
228 |
||
229 |
end - start |
|
230 |
" |
|
1033 | 231 |
! |
232 |
||
873 | 233 |
waitForSeconds:aNumber |
234 |
"wait for the given number of seconds. |
|
235 |
This is a combined instance creation & wait." |
|
236 |
||
237 |
^ (self forSeconds:aNumber) wait |
|
12982 | 238 |
! |
239 |
||
240 |
waitUntil:aTimestamp |
|
241 |
"wait until a given time is reached. |
|
242 |
This is a combined instance creation & wait." |
|
243 |
||
244 |
^ (self until:aTimestamp) wait |
|
245 |
||
246 |
"Created: / 29-07-2010 / 13:51:41 / cg" |
|
9 | 247 |
! ! |
248 |
||
249 |
!Delay methodsFor:'accessing'! |
|
250 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
251 |
delay:numberOfMillis |
10725 | 252 |
"set the millisecond delta and create a new semaphore internally to wait upon" |
92 | 253 |
|
10790 | 254 |
self assert:(numberOfMillis notNil). |
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
255 |
millisecondDelta := numberOfMillis. |
2262 | 256 |
delaySemaphore := Semaphore new name:'delaySema'. |
10582 | 257 |
|
13185 | 258 |
"Modified: / 22-12-2010 / 14:18:00 / cg" |
92 | 259 |
! |
260 |
||
695 | 261 |
delaySemaphore |
262 |
"return the semaphore used to resume the waiting process" |
|
263 |
||
264 |
^ delaySemaphore |
|
265 |
! |
|
266 |
||
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
267 |
resumptionTime:aMillisecondTime |
11898 | 268 |
"set the resumption time and create a new semaphore internally to wait upon" |
92 | 269 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
270 |
resumptionTime := aMillisecondTime. |
2262 | 271 |
delaySemaphore := Semaphore new name:'delaySema'. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
272 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
273 |
"Created: 18.4.1997 / 11:56:14 / stefan" |
10582 | 274 |
! |
275 |
||
276 |
setDelayDuration:numberOfMillis |
|
277 |
"set the millisecond delta" |
|
278 |
||
10790 | 279 |
self assert:(numberOfMillis notNil). |
10582 | 280 |
millisecondDelta := numberOfMillis. |
11898 | 281 |
resumptionTime := nil. |
282 |
! |
|
10582 | 283 |
|
11898 | 284 |
setResumptionTime:aMillisecondTime |
285 |
"set the resumption time" |
|
286 |
||
287 |
resumptionTime := aMillisecondTime. |
|
288 |
millisecondDelta := nil. |
|
9 | 289 |
! ! |
290 |
||
291 |
!Delay methodsFor:'delaying'! |
|
292 |
||
293 |
wait |
|
92 | 294 |
"suspend the current process until either the relative time delta |
295 |
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
|
296 |
has been reached (if resumptionTime non-nil)." |
9 | 297 |
|
13802 | 298 |
|wasBlocked currentDelta dueTime now then| |
299 |
||
300 |
isInterrupted := false. |
|
301 |
||
302 |
millisecondDelta notNil ifTrue:[ |
|
303 |
now := OperatingSystem getMillisecondTime. |
|
304 |
currentDelta := millisecondDelta rounded. |
|
305 |
currentDelta > 16r0fffffff ifTrue:[ |
|
306 |
"NOTE: the microsecondTime is increasing monotonically, |
|
307 |
while millisecondTime is wrapping at 16r1fffffff. |
|
308 |
So use the microsecondTime to check when we are finished" |
|
309 |
dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000). |
|
310 |
currentDelta := 16r0fffffff. |
|
311 |
]. |
|
312 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta . |
|
313 |
] ifFalse:[ |
|
314 |
then := resumptionTime. |
|
315 |
]. |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
316 |
|
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
317 |
wasBlocked := OperatingSystem blockInterrupts. |
1033 | 318 |
[ |
13802 | 319 |
[ |
320 |
Processor signal:delaySemaphore atMilliseconds:then. |
|
15553 | 321 |
Processor activeProcess setStateTo:#timeWait if:#active. |
13802 | 322 |
delaySemaphore wait. |
16288 | 323 |
|
324 |
dueTime notNil |
|
13802 | 325 |
and:[isInterrupted not |
16288 | 326 |
and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0 |
327 |
and:[ |
|
13802 | 328 |
currentDelta := (currentDelta // 1000) min:16r0fffffff. |
329 |
now := OperatingSystem getMillisecondTime. |
|
330 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta. |
|
16288 | 331 |
true.]]] |
332 |
] whileTrue. |
|
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
333 |
] ensure:[ |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
334 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
335 |
] |
9 | 336 |
|
88 | 337 |
" |
8566 | 338 |
Transcript showCR:'1'. |
9 | 339 |
(Delay forSeconds:10) wait. |
8566 | 340 |
Transcript showCR:'2'. |
9 | 341 |
" |
1033 | 342 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
343 |
"Modified: 26.2.1997 / 15:21:35 / cg" |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
344 |
"Modified: 18.4.1997 / 11:56:46 / stefan" |
9 | 345 |
! ! |
695 | 346 |
|
7425 | 347 |
!Delay methodsFor:'early signalling'! |
348 |
||
349 |
resume |
|
350 |
"resume the waiter, even if the delay-time has not yet passed." |
|
351 |
||
352 |
[ |
|
13802 | 353 |
isInterrupted := true. |
7425 | 354 |
self disable. |
355 |
delaySemaphore signalOnce. |
|
356 |
] valueUninterruptably |
|
357 |
||
358 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
359 |
! ! |
|
7424
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 |
!Delay methodsFor:'private'! |
695 | 362 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
363 |
disable |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
364 |
"tell the ProcessorScheduler to forget about signaling my semaphore." |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
365 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
366 |
Processor disableSemaphore:delaySemaphore |
695 | 367 |
! ! |
368 |
||
369 |
!Delay class methodsFor:'documentation'! |
|
370 |
||
371 |
version |
|
16404 | 372 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.51 2014-05-07 14:30:39 stefan Exp $' |
12982 | 373 |
! |
374 |
||
375 |
version_CVS |
|
16404 | 376 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.51 2014-05-07 14:30:39 stefan Exp $' |
695 | 377 |
! ! |
15552 | 378 |