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