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
|
202
|
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
|
|
51 |
OperatingSystem maximumMillisecondTimeDelta
|
|
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
|
92
|
55 |
delay for at least this minimum time interval.
|
|
56 |
|
|
57 |
Check your systems resolution with:
|
|
58 |
|
275
|
59 |
|d t1 t2 res|
|
|
60 |
|
|
61 |
Processor activeProcess priority:24.
|
|
62 |
t1 := Time millisecondClockValue.
|
|
63 |
d := Delay forMilliseconds:1.
|
|
64 |
100 timesRepeat:[d wait].
|
|
65 |
t2 := Time millisecondClockValue.
|
|
66 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
|
|
67 |
Transcript show:'minimum delta is about '; show:res; showCr:' milliseconds'.
|
|
68 |
Processor activeProcess priority:8.
|
202
|
69 |
"
|
|
70 |
!
|
92
|
71 |
|
202
|
72 |
examples
|
|
73 |
"
|
92
|
74 |
examples:
|
|
75 |
|
202
|
76 |
delaying for some time-delta:
|
|
77 |
(notice: you cannot use this without time-errors in a loop,
|
|
78 |
since the errors will accumulate; after 5 runs through the loop,
|
|
79 |
more than 5 seconds have passed)
|
92
|
80 |
|
202
|
81 |
|d|
|
|
82 |
d := Delay forMilliseconds:500.
|
|
83 |
10 timesRepeat:[d wait]
|
92
|
84 |
|
202
|
85 |
prove:
|
|
86 |
|d t1 t2 deltaT|
|
|
87 |
d := Delay forMilliseconds:500.
|
|
88 |
t1 := Time millisecondClockValue.
|
|
89 |
10 timesRepeat:[
|
|
90 |
d wait
|
|
91 |
].
|
|
92 |
t2 := Time millisecondClockValue.
|
|
93 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
|
94 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
|
92
|
95 |
|
202
|
96 |
delaying until a specific time is reached:
|
|
97 |
(this can be used to fix the above problem)
|
92
|
98 |
|
202
|
99 |
|now then t1 t2 deltaT|
|
92
|
100 |
|
202
|
101 |
t1 := Time millisecondClockValue.
|
|
102 |
now := Time millisecondClockValue.
|
|
103 |
10 timesRepeat:[
|
|
104 |
then := OperatingSystem millisecondTimeAdd:now and:1000.
|
|
105 |
(Delay untilMilliseconds:then) wait.
|
|
106 |
now := then
|
|
107 |
].
|
|
108 |
t2 := Time millisecondClockValue.
|
|
109 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
|
110 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
|
92
|
111 |
|
202
|
112 |
instead of recreating new delays, you can also reuse it:
|
92
|
113 |
|
202
|
114 |
|d now then t1 t2 deltaT|
|
92
|
115 |
|
202
|
116 |
t1 := Time millisecondClockValue.
|
|
117 |
now := Time millisecondClockValue.
|
|
118 |
d := Delay new.
|
|
119 |
10 timesRepeat:[
|
|
120 |
then := OperatingSystem millisecondTimeAdd:now and:1000.
|
|
121 |
d resumtionTime:then.
|
|
122 |
d wait.
|
|
123 |
now := then
|
|
124 |
].
|
|
125 |
t2 := Time millisecondClockValue.
|
|
126 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
|
|
127 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
|
88
|
128 |
"
|
|
129 |
! !
|
|
130 |
|
9
|
131 |
!Delay class methodsFor:'instance creation'!
|
|
132 |
|
38
|
133 |
forMilliseconds:aNumber
|
|
134 |
"return a new Delay object for delaying aNumber milliseconds"
|
|
135 |
|
92
|
136 |
^ self new delay:aNumber
|
9
|
137 |
!
|
|
138 |
|
38
|
139 |
forSeconds:aNumber
|
|
140 |
"return a new Delay object for delaying aNumber seconds"
|
|
141 |
|
92
|
142 |
^ self new delay:(aNumber * 1000) rounded
|
|
143 |
!
|
|
144 |
|
275
|
145 |
until:anAbsoluteTime
|
|
146 |
"return a new Delay object, that will delay the active process
|
|
147 |
until the system has reached the time represented by the argument.
|
|
148 |
BUG:
|
|
149 |
due to the limited range of the millisecondTimer, this can
|
|
150 |
(currently) not be used for long delays. The maximum supported
|
|
151 |
delay is returned by OperatingSystem>>maximumMillisecondTimeDelta."
|
|
152 |
|
|
153 |
|numberOfSeconds|
|
|
154 |
|
|
155 |
numberOfSeconds := anAbsoluteTime getSeconds - AbsoluteTime now getSeconds.
|
|
156 |
^ self new delay:numberOfSeconds * 1000
|
695
|
157 |
!
|
|
158 |
|
|
159 |
untilMilliseconds:aMillisecondTime
|
|
160 |
"return a new Delay object, that will delay the active process
|
|
161 |
until the systems millisecond time has reached aMillisecondTime.
|
|
162 |
"
|
|
163 |
|
|
164 |
^ self new resumtionTime:aMillisecondTime
|
92
|
165 |
! !
|
|
166 |
|
|
167 |
!Delay class methodsFor:'queries'!
|
|
168 |
|
|
169 |
millisecondClockValue
|
|
170 |
"for ST-80 compatibility"
|
|
171 |
|
|
172 |
^ Time millisecondClockValue
|
9
|
173 |
! !
|
|
174 |
|
|
175 |
!Delay methodsFor:'accessing'!
|
|
176 |
|
92
|
177 |
delay:aNumber
|
|
178 |
"set the millisecond delta"
|
|
179 |
|
|
180 |
millisecondDelta := aNumber.
|
|
181 |
delaySemaphore := Semaphore new.
|
|
182 |
!
|
|
183 |
|
695
|
184 |
delaySemaphore
|
|
185 |
"return the semaphore used to resume the waiting process"
|
|
186 |
|
|
187 |
^ delaySemaphore
|
|
188 |
!
|
|
189 |
|
92
|
190 |
resumtionTime:aMillisecondTime
|
|
191 |
"set the resumtion time"
|
|
192 |
|
|
193 |
resumtionTime := aMillisecondTime.
|
|
194 |
delaySemaphore := Semaphore new.
|
9
|
195 |
! !
|
|
196 |
|
|
197 |
!Delay methodsFor:'delaying'!
|
|
198 |
|
|
199 |
wait
|
92
|
200 |
"suspend the current process until either the relative time delta
|
|
201 |
has passed (if millisecondDelta is non-nil), or the absolute millisecondTime
|
|
202 |
has been reached (if resumtionTime non-nil)."
|
9
|
203 |
|
92
|
204 |
|wasBlocked|
|
9
|
205 |
|
25
|
206 |
Processor activeProcess state:#timeWait.
|
92
|
207 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
208 |
millisecondDelta notNil ifTrue:[
|
202
|
209 |
Processor signal:delaySemaphore afterMilliseconds:millisecondDelta.
|
92
|
210 |
] ifFalse:[
|
202
|
211 |
Processor signal:delaySemaphore atMilliseconds:resumtionTime.
|
92
|
212 |
].
|
|
213 |
delaySemaphore wait.
|
|
214 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
9
|
215 |
|
88
|
216 |
"
|
|
217 |
'1' printNewline.
|
9
|
218 |
(Delay forSeconds:10) wait.
|
|
219 |
'2' printNewline
|
|
220 |
"
|
|
221 |
! !
|
695
|
222 |
|
|
223 |
!Delay methodsFor:'early signalling'!
|
|
224 |
|
|
225 |
resume
|
|
226 |
"resume the waiter, even if the delay-time has not yet passed."
|
|
227 |
|
|
228 |
|wasBlocked|
|
|
229 |
|
|
230 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
231 |
Processor disableSemaphore:delaySemaphore.
|
|
232 |
delaySemaphore signal.
|
|
233 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
234 |
! !
|
|
235 |
|
|
236 |
!Delay class methodsFor:'documentation'!
|
|
237 |
|
|
238 |
version
|
|
239 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.12 1995-12-07 19:42:47 cg Exp $'
|
|
240 |
! !
|