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 |
|
|
13 |
Object subclass:#Delay
|
777
|
14 |
instanceVariableNames:'millisecondDelta resumtionTime delaySemaphore'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Processes'
|
9
|
18 |
!
|
|
19 |
|
88
|
20 |
!Delay class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
202
|
25 |
All Rights Reserved
|
88
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
92
|
38 |
Instances of Delay are used to suspend the execution of a process
|
|
39 |
(i.e. thread) for some time interval.
|
|
40 |
Delays can be created either for some time-interval (seconds or milliseconds),
|
|
41 |
or for delaying until a specific time has reached.
|
|
42 |
Once created, a delay is waited upon with Delay>>wait.
|
|
43 |
|
|
44 |
Notice: due to delays (both within unix AND within Smalltalk itself,
|
|
45 |
the resumtion time will ALWAYS be after the actual delay time.
|
|
46 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds)
|
|
47 |
|
275
|
48 |
Warning: currently, the implementation does not support delays longer than
|
|
49 |
a system specific maximum - future versions may remove this limitation.
|
|
50 |
For now, do not use delays longer than the value returned by
|
777
|
51 |
OperatingSystem maximumMillisecondTimeDelta
|
275
|
52 |
|
|
53 |
Also notice: the clock resolution of the operatingSystem is usually limited
|
|
54 |
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will
|
1273
|
55 |
delay for at least this minimum time interval. See examples.
|
|
56 |
|
|
57 |
[see also:]
|
|
58 |
Semaphore Process ProcessorScheduler
|
1348
|
59 |
Time AbsoluteTime OperatingSystem
|
|
60 |
(Using delay in ``Working with time, delays and interrupts'': programming/timing.html#DELAY)
|
1294
|
61 |
|
|
62 |
[author:]
|
|
63 |
Claus Gittinger
|
1273
|
64 |
"
|
|
65 |
!
|
|
66 |
|
|
67 |
examples
|
|
68 |
"
|
92
|
69 |
Check your systems resolution with:
|
778
|
70 |
(make certain, that no other timed processes are running in the background when doing this)
|
1316
|
71 |
[exBegin]
|
777
|
72 |
|d t1 t2 res|
|
275
|
73 |
|
777
|
74 |
Processor activeProcess priority:24.
|
|
75 |
t1 := Time millisecondClockValue.
|
|
76 |
d := Delay forMilliseconds:1.
|
|
77 |
100 timesRepeat:[d wait].
|
|
78 |
t2 := Time millisecondClockValue.
|
|
79 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
|
1422
|
80 |
Transcript show:'minimum delta is about '; show:res; showCR:' milliseconds'.
|
777
|
81 |
Processor activeProcess priority:8.
|
1316
|
82 |
[exEnd]
|
92
|
83 |
|
1273
|
84 |
|
92
|
85 |
examples:
|
|
86 |
|
1316
|
87 |
delaying for some time-delta:
|
|
88 |
(notice: you cannot use this without time-errors in a loop,
|
2179
|
89 |
since the errors will accumulate; after 10 runs through the loop,
|
1316
|
90 |
more than 5 seconds have passed)
|
92
|
91 |
|
1316
|
92 |
|d|
|
|
93 |
d := Delay forMilliseconds:500.
|
|
94 |
10 timesRepeat:[d wait]
|
92
|
95 |
|
1316
|
96 |
prove:
|
|
97 |
[exBegin]
|
|
98 |
|d t1 t2 deltaT|
|
|
99 |
d := Delay forMilliseconds:500.
|
|
100 |
t1 := Time millisecondClockValue.
|
2178
|
101 |
20 timesRepeat:[
|
1316
|
102 |
d wait
|
|
103 |
].
|
|
104 |
t2 := Time millisecondClockValue.
|
|
105 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
2178
|
106 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
|
1316
|
107 |
[exEnd]
|
92
|
108 |
|
1316
|
109 |
delaying until a specific time is reached:
|
|
110 |
(this can be used to fix the above problem)
|
|
111 |
[exBegin]
|
|
112 |
|now then t1 t2 deltaT|
|
92
|
113 |
|
1316
|
114 |
t1 := Time millisecondClockValue.
|
|
115 |
now := Time millisecondClockValue.
|
2178
|
116 |
20 timesRepeat:[
|
|
117 |
then := OperatingSystem millisecondTimeAdd:now and:500.
|
1316
|
118 |
(Delay untilMilliseconds:then) wait.
|
|
119 |
now := then
|
|
120 |
].
|
|
121 |
t2 := Time millisecondClockValue.
|
|
122 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
2178
|
123 |
Transcript show:'average delay: '; show:deltaT // 20; showCR:' milliseconds'
|
1316
|
124 |
[exEnd]
|
92
|
125 |
|
1316
|
126 |
instead of recreating new delays all over,
|
2178
|
127 |
you can also reuse it (but that does not make a bit difference ;-):
|
1316
|
128 |
[exBegin]
|
|
129 |
|d now then t1 t2 deltaT|
|
92
|
130 |
|
1316
|
131 |
t1 := Time millisecondClockValue.
|
|
132 |
now := Time millisecondClockValue.
|
|
133 |
d := Delay new.
|
|
134 |
10 timesRepeat:[
|
|
135 |
then := OperatingSystem millisecondTimeAdd:now and:1000.
|
|
136 |
d resumtionTime:then.
|
|
137 |
d wait.
|
|
138 |
now := then
|
|
139 |
].
|
|
140 |
t2 := Time millisecondClockValue.
|
|
141 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
1422
|
142 |
Transcript show:'average delay: '; show:deltaT // 10; showCR:' milliseconds'
|
1316
|
143 |
[exEnd]
|
88
|
144 |
"
|
|
145 |
! !
|
|
146 |
|
9
|
147 |
!Delay class methodsFor:'instance creation'!
|
|
148 |
|
38
|
149 |
forMilliseconds:aNumber
|
|
150 |
"return a new Delay object for delaying aNumber milliseconds"
|
|
151 |
|
92
|
152 |
^ self new delay:aNumber
|
9
|
153 |
!
|
|
154 |
|
38
|
155 |
forSeconds:aNumber
|
|
156 |
"return a new Delay object for delaying aNumber seconds"
|
|
157 |
|
92
|
158 |
^ self new delay:(aNumber * 1000) rounded
|
|
159 |
!
|
|
160 |
|
275
|
161 |
until:anAbsoluteTime
|
|
162 |
"return a new Delay object, that will delay the active process
|
|
163 |
until the system has reached the time represented by the argument.
|
|
164 |
BUG:
|
|
165 |
due to the limited range of the millisecondTimer, this can
|
|
166 |
(currently) not be used for long delays. The maximum supported
|
|
167 |
delay is returned by OperatingSystem>>maximumMillisecondTimeDelta."
|
|
168 |
|
|
169 |
|numberOfSeconds|
|
|
170 |
|
|
171 |
numberOfSeconds := anAbsoluteTime getSeconds - AbsoluteTime now getSeconds.
|
|
172 |
^ self new delay:numberOfSeconds * 1000
|
695
|
173 |
!
|
|
174 |
|
|
175 |
untilMilliseconds:aMillisecondTime
|
|
176 |
"return a new Delay object, that will delay the active process
|
|
177 |
until the systems millisecond time has reached aMillisecondTime.
|
|
178 |
"
|
|
179 |
|
|
180 |
^ self new resumtionTime:aMillisecondTime
|
92
|
181 |
! !
|
|
182 |
|
1033
|
183 |
!Delay class methodsFor:'queries'!
|
|
184 |
|
|
185 |
millisecondClockValue
|
|
186 |
"for ST-80 compatibility"
|
|
187 |
|
|
188 |
^ Time millisecondClockValue
|
|
189 |
! !
|
|
190 |
|
873
|
191 |
!Delay class methodsFor:'waiting'!
|
|
192 |
|
1033
|
193 |
waitForMilliseconds:aNumber
|
|
194 |
"wait for the given number of milliseconds.
|
|
195 |
This is a combined instance creation & wait."
|
|
196 |
|
|
197 |
^ (self forMilliseconds:aNumber) wait
|
|
198 |
!
|
|
199 |
|
873
|
200 |
waitForSeconds:aNumber
|
|
201 |
"wait for the given number of seconds.
|
|
202 |
This is a combined instance creation & wait."
|
|
203 |
|
|
204 |
^ (self forSeconds:aNumber) wait
|
9
|
205 |
! !
|
|
206 |
|
|
207 |
!Delay methodsFor:'accessing'!
|
|
208 |
|
92
|
209 |
delay:aNumber
|
|
210 |
"set the millisecond delta"
|
|
211 |
|
|
212 |
millisecondDelta := aNumber.
|
|
213 |
delaySemaphore := Semaphore new.
|
|
214 |
!
|
|
215 |
|
695
|
216 |
delaySemaphore
|
|
217 |
"return the semaphore used to resume the waiting process"
|
|
218 |
|
|
219 |
^ delaySemaphore
|
|
220 |
!
|
|
221 |
|
92
|
222 |
resumtionTime:aMillisecondTime
|
|
223 |
"set the resumtion time"
|
|
224 |
|
|
225 |
resumtionTime := aMillisecondTime.
|
|
226 |
delaySemaphore := Semaphore new.
|
9
|
227 |
! !
|
|
228 |
|
|
229 |
!Delay methodsFor:'delaying'!
|
|
230 |
|
|
231 |
wait
|
92
|
232 |
"suspend the current process until either the relative time delta
|
|
233 |
has passed (if millisecondDelta is non-nil), or the absolute millisecondTime
|
|
234 |
has been reached (if resumtionTime non-nil)."
|
9
|
235 |
|
25
|
236 |
Processor activeProcess state:#timeWait.
|
1033
|
237 |
[
|
|
238 |
millisecondDelta notNil ifTrue:[
|
|
239 |
Processor signal:delaySemaphore afterMilliseconds:millisecondDelta.
|
|
240 |
] ifFalse:[
|
|
241 |
Processor signal:delaySemaphore atMilliseconds:resumtionTime.
|
|
242 |
].
|
|
243 |
delaySemaphore wait.
|
|
244 |
] valueUninterruptably
|
9
|
245 |
|
88
|
246 |
"
|
|
247 |
'1' printNewline.
|
9
|
248 |
(Delay forSeconds:10) wait.
|
|
249 |
'2' printNewline
|
|
250 |
"
|
1033
|
251 |
|
|
252 |
"Modified: 28.2.1996 / 21:39:26 / cg"
|
9
|
253 |
! !
|
695
|
254 |
|
|
255 |
!Delay methodsFor:'early signalling'!
|
|
256 |
|
|
257 |
resume
|
|
258 |
"resume the waiter, even if the delay-time has not yet passed."
|
|
259 |
|
1035
|
260 |
[
|
|
261 |
Processor disableSemaphore:delaySemaphore.
|
|
262 |
delaySemaphore signal.
|
|
263 |
] valueUninterruptably
|
695
|
264 |
|
1035
|
265 |
"Modified: 28.2.1996 / 21:40:27 / cg"
|
695
|
266 |
! !
|
|
267 |
|
|
268 |
!Delay class methodsFor:'documentation'!
|
|
269 |
|
|
270 |
version
|
2179
|
271 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.25 1997-01-16 13:15:26 cg Exp $'
|
695
|
272 |
! !
|