author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:16:37 +0100 | |
changeset 605 | 8b17f96bf05a |
parent 528 | a083413dfbe8 |
child 695 | 20ec350f92ca |
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 |
|
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 |
version |
|
528
a083413dfbe8
converted version methods from comment-only to returning-a-string
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.11 1995-11-11 14:28:18 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
92 | 42 |
Instances of Delay are used to suspend the execution of a process |
43 |
(i.e. thread) for some time interval. |
|
44 |
Delays can be created either for some time-interval (seconds or milliseconds), |
|
45 |
or for delaying until a specific time has reached. |
|
46 |
Once created, a delay is waited upon with Delay>>wait. |
|
47 |
||
48 |
Notice: due to delays (both within unix AND within Smalltalk itself, |
|
49 |
the resumtion time will ALWAYS be after the actual delay time. |
|
50 |
(i.e. a Delay for n-millis will actually suspend for more than n milliseconds) |
|
51 |
||
275 | 52 |
Warning: currently, the implementation does not support delays longer than |
53 |
a system specific maximum - future versions may remove this limitation. |
|
54 |
For now, do not use delays longer than the value returned by |
|
55 |
OperatingSystem maximumMillisecondTimeDelta |
|
56 |
||
57 |
Also notice: the clock resolution of the operatingSystem is usually limited |
|
58 |
(1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will |
|
92 | 59 |
delay for at least this minimum time interval. |
60 |
||
61 |
Check your systems resolution with: |
|
62 |
||
275 | 63 |
|d t1 t2 res| |
64 |
||
65 |
Processor activeProcess priority:24. |
|
66 |
t1 := Time millisecondClockValue. |
|
67 |
d := Delay forMilliseconds:1. |
|
68 |
100 timesRepeat:[d wait]. |
|
69 |
t2 := Time millisecondClockValue. |
|
70 |
res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100. |
|
71 |
Transcript show:'minimum delta is about '; show:res; showCr:' milliseconds'. |
|
72 |
Processor activeProcess priority:8. |
|
202 | 73 |
" |
74 |
! |
|
92 | 75 |
|
202 | 76 |
examples |
77 |
" |
|
92 | 78 |
examples: |
79 |
||
202 | 80 |
delaying for some time-delta: |
81 |
(notice: you cannot use this without time-errors in a loop, |
|
82 |
since the errors will accumulate; after 5 runs through the loop, |
|
83 |
more than 5 seconds have passed) |
|
92 | 84 |
|
202 | 85 |
|d| |
86 |
d := Delay forMilliseconds:500. |
|
87 |
10 timesRepeat:[d wait] |
|
92 | 88 |
|
202 | 89 |
prove: |
90 |
|d t1 t2 deltaT| |
|
91 |
d := Delay forMilliseconds:500. |
|
92 |
t1 := Time millisecondClockValue. |
|
93 |
10 timesRepeat:[ |
|
94 |
d wait |
|
95 |
]. |
|
96 |
t2 := Time millisecondClockValue. |
|
97 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
98 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds' |
|
92 | 99 |
|
202 | 100 |
delaying until a specific time is reached: |
101 |
(this can be used to fix the above problem) |
|
92 | 102 |
|
202 | 103 |
|now then t1 t2 deltaT| |
92 | 104 |
|
202 | 105 |
t1 := Time millisecondClockValue. |
106 |
now := Time millisecondClockValue. |
|
107 |
10 timesRepeat:[ |
|
108 |
then := OperatingSystem millisecondTimeAdd:now and:1000. |
|
109 |
(Delay untilMilliseconds:then) wait. |
|
110 |
now := then |
|
111 |
]. |
|
112 |
t2 := Time millisecondClockValue. |
|
113 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
114 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds' |
|
92 | 115 |
|
202 | 116 |
instead of recreating new delays, you can also reuse it: |
92 | 117 |
|
202 | 118 |
|d now then t1 t2 deltaT| |
92 | 119 |
|
202 | 120 |
t1 := Time millisecondClockValue. |
121 |
now := Time millisecondClockValue. |
|
122 |
d := Delay new. |
|
123 |
10 timesRepeat:[ |
|
124 |
then := OperatingSystem millisecondTimeAdd:now and:1000. |
|
125 |
d resumtionTime:then. |
|
126 |
d wait. |
|
127 |
now := then |
|
128 |
]. |
|
129 |
t2 := Time millisecondClockValue. |
|
130 |
deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1. |
|
131 |
Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds' |
|
88 | 132 |
" |
133 |
! ! |
|
134 |
||
9 | 135 |
!Delay class methodsFor:'instance creation'! |
136 |
||
38 | 137 |
forMilliseconds:aNumber |
138 |
"return a new Delay object for delaying aNumber milliseconds" |
|
139 |
||
92 | 140 |
^ self new delay:aNumber |
9 | 141 |
! |
142 |
||
38 | 143 |
forSeconds:aNumber |
144 |
"return a new Delay object for delaying aNumber seconds" |
|
145 |
||
92 | 146 |
^ self new delay:(aNumber * 1000) rounded |
147 |
! |
|
148 |
||
149 |
untilMilliseconds:aMillisecondTime |
|
150 |
"return a new Delay object, that will delay the active process |
|
151 |
until the systems millisecond time has reached aMillisecondTime. |
|
152 |
" |
|
153 |
||
154 |
^ self new resumtionTime:aMillisecondTime |
|
275 | 155 |
! |
156 |
||
157 |
until:anAbsoluteTime |
|
158 |
"return a new Delay object, that will delay the active process |
|
159 |
until the system has reached the time represented by the argument. |
|
160 |
BUG: |
|
161 |
due to the limited range of the millisecondTimer, this can |
|
162 |
(currently) not be used for long delays. The maximum supported |
|
163 |
delay is returned by OperatingSystem>>maximumMillisecondTimeDelta." |
|
164 |
||
165 |
|numberOfSeconds| |
|
166 |
||
167 |
numberOfSeconds := anAbsoluteTime getSeconds - AbsoluteTime now getSeconds. |
|
168 |
^ self new delay:numberOfSeconds * 1000 |
|
92 | 169 |
! ! |
170 |
||
171 |
!Delay class methodsFor:'queries'! |
|
172 |
||
173 |
millisecondClockValue |
|
174 |
"for ST-80 compatibility" |
|
175 |
||
176 |
^ Time millisecondClockValue |
|
9 | 177 |
! ! |
178 |
||
179 |
!Delay methodsFor:'accessing'! |
|
180 |
||
92 | 181 |
delay:aNumber |
182 |
"set the millisecond delta" |
|
183 |
||
184 |
millisecondDelta := aNumber. |
|
185 |
delaySemaphore := Semaphore new. |
|
186 |
! |
|
187 |
||
188 |
resumtionTime:aMillisecondTime |
|
189 |
"set the resumtion time" |
|
190 |
||
191 |
resumtionTime := aMillisecondTime. |
|
192 |
delaySemaphore := Semaphore new. |
|
193 |
! |
|
38 | 194 |
|
92 | 195 |
delaySemaphore |
196 |
"return the semaphore used to resume the waiting process" |
|
197 |
||
198 |
^ delaySemaphore |
|
199 |
! ! |
|
200 |
||
201 |
!Delay methodsFor:'early signalling'! |
|
202 |
||
203 |
resume |
|
204 |
"resume the waiter, even if the delay-time has not yet passed." |
|
205 |
||
206 |
|wasBlocked| |
|
207 |
||
208 |
wasBlocked := OperatingSystem blockInterrupts. |
|
209 |
Processor disableSemaphore:delaySemaphore. |
|
210 |
delaySemaphore signal. |
|
211 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
9 | 212 |
! ! |
213 |
||
214 |
!Delay methodsFor:'delaying'! |
|
215 |
||
216 |
wait |
|
92 | 217 |
"suspend the current process until either the relative time delta |
218 |
has passed (if millisecondDelta is non-nil), or the absolute millisecondTime |
|
219 |
has been reached (if resumtionTime non-nil)." |
|
9 | 220 |
|
92 | 221 |
|wasBlocked| |
9 | 222 |
|
25 | 223 |
Processor activeProcess state:#timeWait. |
92 | 224 |
wasBlocked := OperatingSystem blockInterrupts. |
225 |
millisecondDelta notNil ifTrue:[ |
|
202 | 226 |
Processor signal:delaySemaphore afterMilliseconds:millisecondDelta. |
92 | 227 |
] ifFalse:[ |
202 | 228 |
Processor signal:delaySemaphore atMilliseconds:resumtionTime. |
92 | 229 |
]. |
230 |
delaySemaphore wait. |
|
231 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
9 | 232 |
|
88 | 233 |
" |
234 |
'1' printNewline. |
|
9 | 235 |
(Delay forSeconds:10) wait. |
236 |
'2' printNewline |
|
237 |
" |
|
238 |
! ! |