author | Patrik Svestka <patrik.svestka@gmail.com> |
Tue, 09 Apr 2019 11:34:04 +0200 | |
branch | jv |
changeset 24093 | 0f94f6c8c9d4 |
parent 20206 | 51652e7f46dd |
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 |
|
18996 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
9 | 16 |
Object subclass:#Delay |
13802 | 17 |
instanceVariableNames:'millisecondDelta resumptionTime delaySemaphore isInterrupted' |
777 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Processes' |
|
9 | 21 |
! |
22 |
||
88 | 23 |
!Delay class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
202 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
20197 | 41 |
Instances of Delay are used to suspend the execution of a process |
42 |
(i.e. thread) for some time interval. |
|
43 |
Delays can be created either for some time-interval (seconds or milliseconds), |
|
92 | 44 |
or for delaying until a specific time has reached. |
45 |
Once created, a delay is waited upon with Delay>>wait. |
|
46 |
||
15552 | 47 |
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
|
48 |
the resumption time will ALWAYS be after the actual delay time. |
92 | 49 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds) |
50 |
||
20197 | 51 |
Warning: |
15552 | 52 |
currently, the implementation does not support delays longer than |
53 |
a system specific maximum - future versions may remove this limitation. |
|
54 |
For now, do not use delays longer than the value returned by |
|
777 | 55 |
OperatingSystem maximumMillisecondTimeDelta |
275 | 56 |
|
15552 | 57 |
Also notice: the clock resolution of the operating system is usually limited |
275 | 58 |
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will |
1273 | 59 |
delay for at least this minimum time interval. See examples. |
60 |
||
61 |
[see also:] |
|
62 |
Semaphore Process ProcessorScheduler |
|
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
63 |
Time Timestamp OperatingSystem |
1348 | 64 |
(Using delay in ``Working with time, delays and interrupts'': programming/timing.html#DELAY) |
1294 | 65 |
|
66 |
[author:] |
|
67 |
Claus Gittinger |
|
1273 | 68 |
" |
69 |
! |
|
70 |
||
20197 | 71 |
examples |
1273 | 72 |
" |
92 | 73 |
Check your systems resolution with: |
778 | 74 |
(make certain, that no other timed processes are running in the background when doing this) |
1316 | 75 |
[exBegin] |
777 | 76 |
|d t1 t2 res| |
275 | 77 |
|
777 | 78 |
Processor activeProcess priority:24. |
79 |
t1 := Time millisecondClockValue. |
|
80 |
d := Delay forMilliseconds:1. |
|
81 |
100 timesRepeat:[d wait]. |
|
82 |
t2 := Time millisecondClockValue. |
|
83 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100. |
|
1422 | 84 |
Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'. |
777 | 85 |
Processor activeProcess priority:8. |
1316 | 86 |
[exEnd] |
92 | 87 |
|
1273 | 88 |
|
92 | 89 |
examples: |
90 |
||
1316 | 91 |
delaying for some time-delta: |
92 |
(notice: you cannot use this without time-errors in a loop, |
|
2179 | 93 |
since the errors will accumulate; after 10 runs through the loop, |
1316 | 94 |
more than 5 seconds have passed) |
92 | 95 |
|
1316 | 96 |
|d| |
97 |
d := Delay forMilliseconds:500. |
|
98 |
10 timesRepeat:[d wait] |
|
92 | 99 |
|
1316 | 100 |
prove: |
101 |
[exBegin] |
|
102 |
|d t1 t2 deltaT| |
|
103 |
d := Delay forMilliseconds:500. |
|
104 |
t1 := Time millisecondClockValue. |
|
2178 | 105 |
20 timesRepeat:[ |
1316 | 106 |
d wait |
107 |
]. |
|
108 |
t2 := Time millisecondClockValue. |
|
109 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 110 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 111 |
[exEnd] |
92 | 112 |
|
1316 | 113 |
delaying until a specific time is reached: |
114 |
(this can be used to fix the above problem) |
|
115 |
[exBegin] |
|
116 |
|now then t1 t2 deltaT| |
|
92 | 117 |
|
1316 | 118 |
t1 := Time millisecondClockValue. |
119 |
now := Time millisecondClockValue. |
|
2178 | 120 |
20 timesRepeat:[ |
121 |
then := OperatingSystem millisecondTimeAdd:now and:500. |
|
1316 | 122 |
(Delay untilMilliseconds:then) wait. |
123 |
now := then |
|
124 |
]. |
|
125 |
t2 := Time millisecondClockValue. |
|
126 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
2178 | 127 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds' |
1316 | 128 |
[exEnd] |
92 | 129 |
|
1316 | 130 |
instead of recreating new delays all over, |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
131 |
you can also reuse it (but that does not make a big difference ;-): |
1316 | 132 |
[exBegin] |
133 |
|d now then t1 t2 deltaT| |
|
92 | 134 |
|
1316 | 135 |
t1 := Time millisecondClockValue. |
136 |
now := Time millisecondClockValue. |
|
137 |
d := Delay new. |
|
138 |
10 timesRepeat:[ |
|
139 |
then := OperatingSystem millisecondTimeAdd:now and:1000. |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
140 |
d resumptionTime:then. |
1316 | 141 |
d wait. |
142 |
now := then |
|
143 |
]. |
|
144 |
t2 := Time millisecondClockValue. |
|
145 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
1422 | 146 |
Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds' |
1316 | 147 |
[exEnd] |
88 | 148 |
" |
149 |
! ! |
|
150 |
||
9 | 151 |
!Delay class methodsFor:'instance creation'! |
152 |
||
16404 | 153 |
for:aTimeDuration |
20197 | 154 |
"return a new Delay object for delaying aTimeDuration" |
16404 | 155 |
|
20197 | 156 |
^ self new delay:aTimeDuration getMilliseconds |
16404 | 157 |
|
158 |
" |
|
159 |
Delay for:10 seconds |
|
160 |
" |
|
161 |
! |
|
162 |
||
38 | 163 |
forMilliseconds:aNumber |
164 |
"return a new Delay object for delaying aNumber milliseconds" |
|
165 |
||
92 | 166 |
^ self new delay:aNumber |
9 | 167 |
! |
168 |
||
38 | 169 |
forSeconds:aNumber |
170 |
"return a new Delay object for delaying aNumber seconds" |
|
171 |
||
92 | 172 |
^ self new delay:(aNumber * 1000) rounded |
173 |
! |
|
174 |
||
8254
73b6109ab982
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
7590
diff
changeset
|
175 |
until:aTimeStamp |
275 | 176 |
"return a new Delay object, that will delay the active process |
15630 | 177 |
until the system has reached the time represented by the argument." |
275 | 178 |
|
18996 | 179 |
^ self new delay:(aTimeStamp millisecondDeltaFrom:Timestamp now) |
275 | 180 |
|
18996 | 181 |
" |
182 |
(self until:(Timestamp now + 1 seconds)) wait |
|
183 |
(self until:(Timestamp now - 30 seconds)) wait |
|
184 |
" |
|
695 | 185 |
! |
186 |
||
187 |
untilMilliseconds:aMillisecondTime |
|
188 |
"return a new Delay object, that will delay the active process |
|
15630 | 189 |
until the systems millisecond time has reached aMillisecondTime." |
695 | 190 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
191 |
^ self new resumptionTime:aMillisecondTime |
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
192 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
193 |
"Modified: 18.4.1997 / 11:57:53 / stefan" |
92 | 194 |
! ! |
195 |
||
1033 | 196 |
!Delay class methodsFor:'queries'! |
197 |
||
198 |
millisecondClockValue |
|
199 |
"for ST-80 compatibility" |
|
200 |
||
201 |
^ Time millisecondClockValue |
|
202 |
! ! |
|
203 |
||
873 | 204 |
!Delay class methodsFor:'waiting'! |
205 |
||
11601 | 206 |
waitFor:aTimeDuration |
207 |
"wait for the given timeDuration. |
|
208 |
This is a combined instance creation & wait." |
|
209 |
||
210 |
^ (self forMilliseconds:aTimeDuration getMilliseconds) wait |
|
211 |
||
212 |
||
213 |
" |
|
214 |
Delay waitFor:(TimeDuration seconds:5). |
|
215 |
" |
|
216 |
! |
|
217 |
||
1033 | 218 |
waitForMilliseconds:aNumber |
219 |
"wait for the given number of milliseconds. |
|
220 |
This is a combined instance creation & wait." |
|
221 |
||
222 |
^ (self forMilliseconds:aNumber) wait |
|
8585 | 223 |
|
224 |
||
225 |
" |
|
226 |
|start end| |
|
227 |
||
228 |
start := OperatingSystem getMicrosecondTime. |
|
229 |
Delay waitForMilliseconds:1. |
|
230 |
end := OperatingSystem getMicrosecondTime. |
|
231 |
||
232 |
end - start |
|
233 |
" |
|
1033 | 234 |
! |
235 |
||
873 | 236 |
waitForSeconds:aNumber |
237 |
"wait for the given number of seconds. |
|
238 |
This is a combined instance creation & wait." |
|
239 |
||
240 |
^ (self forSeconds:aNumber) wait |
|
12982 | 241 |
! |
242 |
||
243 |
waitUntil:aTimestamp |
|
244 |
"wait until a given time is reached. |
|
245 |
This is a combined instance creation & wait." |
|
246 |
||
247 |
^ (self until:aTimestamp) wait |
|
248 |
||
249 |
"Created: / 29-07-2010 / 13:51:41 / cg" |
|
9 | 250 |
! ! |
251 |
||
252 |
!Delay methodsFor:'accessing'! |
|
253 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
254 |
delay:numberOfMillis |
10725 | 255 |
"set the millisecond delta and create a new semaphore internally to wait upon" |
92 | 256 |
|
10790 | 257 |
self assert:(numberOfMillis notNil). |
18996 | 258 |
numberOfMillis < 0 ifTrue:[ |
259 |
millisecondDelta := 0. |
|
260 |
] ifFalse:[ |
|
261 |
millisecondDelta := numberOfMillis. |
|
262 |
]. |
|
263 |
||
2262 | 264 |
delaySemaphore := Semaphore new name:'delaySema'. |
10582 | 265 |
|
13185 | 266 |
"Modified: / 22-12-2010 / 14:18:00 / cg" |
92 | 267 |
! |
268 |
||
695 | 269 |
delaySemaphore |
270 |
"return the semaphore used to resume the waiting process" |
|
271 |
||
272 |
^ delaySemaphore |
|
273 |
! |
|
274 |
||
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
275 |
resumptionTime:aMillisecondTime |
11898 | 276 |
"set the resumption time and create a new semaphore internally to wait upon" |
92 | 277 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
278 |
resumptionTime := aMillisecondTime. |
2262 | 279 |
delaySemaphore := Semaphore new name:'delaySema'. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
280 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
281 |
"Created: 18.4.1997 / 11:56:14 / stefan" |
10582 | 282 |
! |
283 |
||
284 |
setDelayDuration:numberOfMillis |
|
285 |
"set the millisecond delta" |
|
286 |
||
10790 | 287 |
self assert:(numberOfMillis notNil). |
18996 | 288 |
numberOfMillis < 0 ifTrue:[ |
289 |
millisecondDelta := 0. |
|
290 |
] ifFalse:[ |
|
291 |
millisecondDelta := numberOfMillis. |
|
292 |
]. |
|
11898 | 293 |
resumptionTime := nil. |
294 |
! |
|
10582 | 295 |
|
11898 | 296 |
setResumptionTime:aMillisecondTime |
297 |
"set the resumption time" |
|
298 |
||
299 |
resumptionTime := aMillisecondTime. |
|
300 |
millisecondDelta := nil. |
|
9 | 301 |
! ! |
302 |
||
303 |
!Delay methodsFor:'delaying'! |
|
304 |
||
305 |
wait |
|
92 | 306 |
"suspend the current process until either the relative time delta |
307 |
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
|
308 |
has been reached (if resumptionTime non-nil)." |
9 | 309 |
|
13802 | 310 |
|wasBlocked currentDelta dueTime now then| |
311 |
||
312 |
isInterrupted := false. |
|
313 |
||
314 |
millisecondDelta notNil ifTrue:[ |
|
18996 | 315 |
currentDelta := millisecondDelta rounded. |
316 |
currentDelta <= 0 ifTrue:[ |
|
317 |
^ self. |
|
318 |
]. |
|
13802 | 319 |
now := OperatingSystem getMillisecondTime. |
320 |
currentDelta > 16r0fffffff ifTrue:[ |
|
321 |
"NOTE: the microsecondTime is increasing monotonically, |
|
322 |
while millisecondTime is wrapping at 16r1fffffff. |
|
323 |
So use the microsecondTime to check when we are finished" |
|
324 |
dueTime := OperatingSystem getMicrosecondTime + (currentDelta * 1000). |
|
325 |
currentDelta := 16r0fffffff. |
|
326 |
]. |
|
327 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta . |
|
328 |
] ifFalse:[ |
|
329 |
then := resumptionTime. |
|
330 |
]. |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
331 |
|
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
332 |
wasBlocked := OperatingSystem blockInterrupts. |
1033 | 333 |
[ |
13802 | 334 |
[ |
335 |
Processor signal:delaySemaphore atMilliseconds:then. |
|
15553 | 336 |
Processor activeProcess setStateTo:#timeWait if:#active. |
13802 | 337 |
delaySemaphore wait. |
16288 | 338 |
|
339 |
dueTime notNil |
|
13802 | 340 |
and:[isInterrupted not |
16288 | 341 |
and:[(currentDelta := dueTime - OperatingSystem getMicrosecondTime) > 0 |
342 |
and:[ |
|
13802 | 343 |
currentDelta := (currentDelta // 1000) min:16r0fffffff. |
344 |
now := OperatingSystem getMillisecondTime. |
|
345 |
then := OperatingSystem millisecondTimeAdd:now and:currentDelta. |
|
16288 | 346 |
true.]]] |
347 |
] whileTrue. |
|
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
348 |
] ensure:[ |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
349 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
350 |
] |
9 | 351 |
|
88 | 352 |
" |
8566 | 353 |
Transcript showCR:'1'. |
9 | 354 |
(Delay forSeconds:10) wait. |
8566 | 355 |
Transcript showCR:'2'. |
9 | 356 |
" |
1033 | 357 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
358 |
"Modified: 26.2.1997 / 15:21:35 / cg" |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
359 |
"Modified: 18.4.1997 / 11:56:46 / stefan" |
9 | 360 |
! ! |
695 | 361 |
|
7425 | 362 |
!Delay methodsFor:'early signalling'! |
363 |
||
364 |
resume |
|
365 |
"resume the waiter, even if the delay-time has not yet passed." |
|
366 |
||
367 |
[ |
|
13802 | 368 |
isInterrupted := true. |
7425 | 369 |
self disable. |
370 |
delaySemaphore signalOnce. |
|
371 |
] valueUninterruptably |
|
372 |
||
373 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
374 |
! ! |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
375 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
376 |
!Delay methodsFor:'private'! |
695 | 377 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
378 |
disable |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
379 |
"tell the ProcessorScheduler to forget about signaling my semaphore." |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
380 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
381 |
Processor disableSemaphore:delaySemaphore |
695 | 382 |
! ! |
383 |
||
384 |
!Delay class methodsFor:'documentation'! |
|
385 |
||
386 |
version |
|
18996 | 387 |
^ '$Header$' |
12982 | 388 |
! |
389 |
||
390 |
version_CVS |
|
18996 | 391 |
^ '$Header$' |
695 | 392 |
! ! |
15552 | 393 |