author | Stefan Vogel <sv@exept.de> |
Wed, 07 Jun 2006 17:45:06 +0200 | |
changeset 9364 | beef5f0aa8bb |
parent 8585 | 47c2cef4692d |
child 10582 | 00d5691a2b72 |
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 |
" |
|
9 | 12 |
|
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
14 |
|
9 | 15 |
Object subclass:#Delay |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
16 |
instanceVariableNames:'millisecondDelta resumptionTime delaySemaphore' |
777 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Processes' |
|
9 | 20 |
! |
21 |
||
88 | 22 |
!Delay class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
202 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
92 | 40 |
Instances of Delay are used to suspend the execution of a process |
41 |
(i.e. thread) for some time interval. |
|
42 |
Delays can be created either for some time-interval (seconds or milliseconds), |
|
43 |
or for delaying until a specific time has reached. |
|
44 |
Once created, a delay is waited upon with Delay>>wait. |
|
45 |
||
46 |
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
|
47 |
the resumption time will ALWAYS be after the actual delay time. |
92 | 48 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds) |
49 |
||
275 | 50 |
Warning: 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 |
|
55 |
Also notice: the clock resolution of the operatingSystem is usually limited |
|
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 |
||
1033 | 197 |
waitForMilliseconds:aNumber |
198 |
"wait for the given number of milliseconds. |
|
199 |
This is a combined instance creation & wait." |
|
200 |
||
201 |
^ (self forMilliseconds:aNumber) wait |
|
8585 | 202 |
|
203 |
||
204 |
" |
|
205 |
|start end| |
|
206 |
||
207 |
start := OperatingSystem getMicrosecondTime. |
|
208 |
Delay waitForMilliseconds:1. |
|
209 |
end := OperatingSystem getMicrosecondTime. |
|
210 |
||
211 |
end - start |
|
212 |
" |
|
1033 | 213 |
! |
214 |
||
873 | 215 |
waitForSeconds:aNumber |
216 |
"wait for the given number of seconds. |
|
217 |
This is a combined instance creation & wait." |
|
218 |
||
219 |
^ (self forSeconds:aNumber) wait |
|
9 | 220 |
! ! |
221 |
||
222 |
!Delay methodsFor:'accessing'! |
|
223 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
224 |
delay:numberOfMillis |
92 | 225 |
"set the millisecond delta" |
226 |
||
6934
7f47cfcbe755
oops - until: was wrong (setting seconds instead of millis)
Claus Gittinger <cg@exept.de>
parents:
6423
diff
changeset
|
227 |
millisecondDelta := numberOfMillis. |
2262 | 228 |
delaySemaphore := Semaphore new name:'delaySema'. |
92 | 229 |
! |
230 |
||
695 | 231 |
delaySemaphore |
232 |
"return the semaphore used to resume the waiting process" |
|
233 |
||
234 |
^ delaySemaphore |
|
235 |
! |
|
236 |
||
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
237 |
resumptionTime:aMillisecondTime |
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
238 |
"set the resumption time" |
92 | 239 |
|
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
240 |
resumptionTime := aMillisecondTime. |
2262 | 241 |
delaySemaphore := Semaphore new name:'delaySema'. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
242 |
|
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
243 |
"Created: 18.4.1997 / 11:56:14 / stefan" |
9 | 244 |
! ! |
245 |
||
246 |
!Delay methodsFor:'delaying'! |
|
247 |
||
248 |
wait |
|
92 | 249 |
"suspend the current process until either the relative time delta |
250 |
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
|
251 |
has been reached (if resumptionTime non-nil)." |
9 | 252 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
253 |
|wasBlocked| |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
254 |
|
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
255 |
wasBlocked := OperatingSystem blockInterrupts. |
1033 | 256 |
[ |
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
257 |
self startup. |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
258 |
Processor activeProcess state:#timeWait. |
1033 | 259 |
delaySemaphore wait. |
6423
e43f6e61bfaa
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3917
diff
changeset
|
260 |
] ensure:[ |
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
261 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
262 |
] |
9 | 263 |
|
88 | 264 |
" |
8566 | 265 |
Transcript showCR:'1'. |
9 | 266 |
(Delay forSeconds:10) wait. |
8566 | 267 |
Transcript showCR:'2'. |
9 | 268 |
" |
1033 | 269 |
|
2422
a319ee1bf54b
open coded valueUninterruptably
Claus Gittinger <cg@exept.de>
parents:
2262
diff
changeset
|
270 |
"Modified: 26.2.1997 / 15:21:35 / cg" |
2556
4e58fce26d69
Fix spelling (resumtion --> resumption).
Stefan Vogel <sv@exept.de>
parents:
2422
diff
changeset
|
271 |
"Modified: 18.4.1997 / 11:56:46 / stefan" |
9 | 272 |
! ! |
695 | 273 |
|
7425 | 274 |
!Delay methodsFor:'early signalling'! |
275 |
||
276 |
resume |
|
277 |
"resume the waiter, even if the delay-time has not yet passed." |
|
278 |
||
279 |
[ |
|
280 |
self disable. |
|
281 |
delaySemaphore signalOnce. |
|
282 |
] valueUninterruptably |
|
283 |
||
284 |
"Modified: / 9.11.1998 / 20:56:43 / cg" |
|
285 |
! ! |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
286 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
287 |
!Delay methodsFor:'private'! |
695 | 288 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
289 |
disable |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
290 |
"tell the ProcessorScheduler to forget about signaling my semaphore." |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
291 |
|
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
292 |
Processor disableSemaphore:delaySemaphore |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
293 |
! |
695 | 294 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
295 |
startup |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
296 |
"tell the ProcessorScheduler to signal my semaphore after a while" |
695 | 297 |
|
7424
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
298 |
millisecondDelta notNil ifTrue:[ |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
299 |
Processor signal:delaySemaphore afterMilliseconds:millisecondDelta. |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
300 |
] ifFalse:[ |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
301 |
Processor signal:delaySemaphore atMilliseconds:resumptionTime. |
d68d1ccc2135
refactored for compatibility
Claus Gittinger <cg@exept.de>
parents:
6934
diff
changeset
|
302 |
]. |
695 | 303 |
! ! |
304 |
||
305 |
!Delay class methodsFor:'documentation'! |
|
306 |
||
307 |
version |
|
8585 | 308 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.37 2004-09-22 14:35:10 stefan Exp $' |
695 | 309 |
! ! |