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