5010
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2886
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2013 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
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 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
4300
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
2886
|
18 |
Object subclass:#ActiveObject
|
|
19 |
instanceVariableNames:'process messageQueue'
|
|
20 |
classVariableNames:'AccessLock'
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Kernel-Processes'
|
|
23 |
!
|
|
24 |
|
|
25 |
Message subclass:#MessageAndResponse
|
|
26 |
instanceVariableNames:'action ok result exception ready'
|
|
27 |
classVariableNames:''
|
|
28 |
poolDictionaries:''
|
|
29 |
privateIn:ActiveObject
|
|
30 |
!
|
|
31 |
|
|
32 |
Lookup subclass:#RedirectingLookup
|
|
33 |
instanceVariableNames:''
|
|
34 |
classVariableNames:'Instance'
|
|
35 |
poolDictionaries:''
|
|
36 |
privateIn:ActiveObject
|
|
37 |
!
|
|
38 |
|
|
39 |
!ActiveObject class methodsFor:'documentation'!
|
|
40 |
|
|
41 |
copyright
|
|
42 |
"
|
|
43 |
COPYRIGHT (c) 2013 by eXept Software AG
|
|
44 |
All Rights Reserved
|
|
45 |
|
|
46 |
This software is furnished under a license and may be used
|
|
47 |
only in accordance with the terms of that license and with the
|
|
48 |
inclusion of the above copyright notice. This software may not
|
|
49 |
be provided or otherwise made available to, or used by, any
|
|
50 |
other person. No title to or ownership of the software is
|
|
51 |
hereby transferred.
|
|
52 |
"
|
|
53 |
!
|
|
54 |
|
|
55 |
documentation
|
|
56 |
"
|
|
57 |
an active object executes incoming messages in a serialized, synchronous manner,
|
|
58 |
enqueuing incoming messages, executing them one after the other, and returning results to the caller.
|
|
59 |
Messages are synchronous: the caller is blocked while I perform my duty.
|
|
60 |
Exceptions during execution are sent back to the caller.
|
|
61 |
This is an abstract framework class, to be subclassed for real workers.
|
|
62 |
|
5392
|
63 |
The following is a not-yet-working experiment, using lookup objects to redirect automatically into a synchronizing
|
2886
|
64 |
method. This does not work yet.
|
|
65 |
|
|
66 |
Notice the use of the lookup object here: all incoming messages from a process other than my worker-process
|
|
67 |
itself are forwarded to the #doesNotUnderstand: method. There is no need to inherit from nil, and subclasses can use
|
|
68 |
any messages they like locally, without them being enqueued.
|
|
69 |
|
|
70 |
For now, define methods which have to be synchronized by defining them as:
|
|
71 |
methodX
|
|
72 |
self synchronized:[
|
|
73 |
...
|
|
74 |
do something
|
|
75 |
...
|
|
76 |
]
|
|
77 |
|
|
78 |
[Author:]
|
|
79 |
Claus Gittinger
|
|
80 |
"
|
|
81 |
!
|
|
82 |
|
|
83 |
examples
|
|
84 |
"
|
|
85 |
normally, one would subclass ActiveObject and put protocol into it;
|
|
86 |
here, for the example, an anon Printer is defined. It is slow printing to the Transcript for a demo.
|
|
87 |
The interesting thing is the error handling which is demonstrated in printWithError:
|
|
88 |
Any exception inside the worker object is returned back and raised in the active-object's client,
|
|
89 |
not in the worker (take a look at the call-chain, when running the example below).
|
|
90 |
|
5030
|
91 |
[exBegin]
|
2886
|
92 |
|workerClass worker|
|
|
93 |
|
|
94 |
workerClass := ActiveObject
|
|
95 |
subclass:#Printer
|
|
96 |
instanceVariableNames:''
|
|
97 |
classVariableNames:''
|
|
98 |
poolDictionaries:''
|
|
99 |
category:nil
|
|
100 |
inEnvironment:nil.
|
|
101 |
|
|
102 |
workerClass compile:'
|
|
103 |
print:aLine
|
|
104 |
self synchronized:[.
|
|
105 |
aLine do:[:ch |
|
|
106 |
Transcript show:ch.
|
|
107 |
Delay waitForSeconds:0.2.
|
|
108 |
].
|
|
109 |
Transcript cr.
|
|
110 |
]
|
|
111 |
'.
|
|
112 |
|
|
113 |
workerClass compile:'
|
|
114 |
printWithError:aLine
|
|
115 |
self synchronized:[.
|
|
116 |
aLine do:[:ch |
|
|
117 |
Transcript show:ch.
|
|
118 |
ch == $l ifTrue:[ self foo ].
|
|
119 |
Delay waitForSeconds:0.2.
|
|
120 |
].
|
|
121 |
Transcript cr.
|
|
122 |
]
|
|
123 |
'.
|
|
124 |
|
|
125 |
worker := workerClass new.
|
|
126 |
'now ready for printing'.
|
|
127 |
worker printWithError:'abcdef'.
|
|
128 |
worker printWithError:'hello world'.
|
5030
|
129 |
[exEnd]
|
2886
|
130 |
"
|
|
131 |
! !
|
|
132 |
|
|
133 |
!ActiveObject class methodsFor:'initialization'!
|
|
134 |
|
|
135 |
initialize
|
|
136 |
AccessLock := Semaphore forMutualExclusion.
|
|
137 |
"/ self lookupObject: RedirectingLookup new
|
|
138 |
self lookupObject: nil
|
|
139 |
! !
|
|
140 |
|
|
141 |
!ActiveObject class methodsFor:'instance creation'!
|
|
142 |
|
|
143 |
new
|
|
144 |
|newObject|
|
|
145 |
|
|
146 |
|
|
147 |
newObject := self basicNew initialize.
|
|
148 |
^ newObject.
|
|
149 |
! !
|
|
150 |
|
|
151 |
!ActiveObject methodsFor:'accessing'!
|
|
152 |
|
|
153 |
process
|
|
154 |
^ process
|
|
155 |
! !
|
|
156 |
|
|
157 |
!ActiveObject methodsFor:'message handling'!
|
|
158 |
|
|
159 |
messageHandlingLoop
|
|
160 |
|here|
|
|
161 |
|
|
162 |
[ true ] whileTrue:[
|
|
163 |
|paket|
|
|
164 |
|
|
165 |
"/ Transcript showCR:'A: await message...'.
|
|
166 |
(messageQueue readWaitWithTimeoutMs:30000) ifTrue:[
|
|
167 |
"/ inactive for 30 seconds - exit
|
|
168 |
AccessLock critical:[
|
|
169 |
messageQueue isEmpty ifTrue:[
|
|
170 |
"/ Transcript showCR:'A: inactive - stop worker'.
|
|
171 |
process := nil.
|
|
172 |
messageQueue := nil.
|
|
173 |
].
|
|
174 |
].
|
4300
|
175 |
"exit the worker process loop"
|
|
176 |
^ self
|
2886
|
177 |
].
|
|
178 |
|
|
179 |
paket := messageQueue next.
|
|
180 |
"/ Transcript showCR:'A: got message: ', paket selector.
|
|
181 |
Exception handle:[:ex |
|
|
182 |
|ex2 c c2|
|
|
183 |
|
|
184 |
"create a copy of the sender chain, up to this frame here
|
|
185 |
(cannot pass the original exception chain, because that one will be unwound,
|
|
186 |
thereby clearing the senders)"
|
|
187 |
|
|
188 |
ex2 := ex shallowCopy.
|
|
189 |
c := ex2 suspendedContext.
|
|
190 |
c2 := c shallowCopy.
|
|
191 |
ex2 suspendedContext: c2.
|
|
192 |
[ c sender ~~ here ] whileTrue:[
|
|
193 |
c2 setSender:(c sender shallowCopy).
|
|
194 |
c2 := c2 sender.
|
|
195 |
c := c sender
|
|
196 |
].
|
|
197 |
paket ok: false.
|
|
198 |
paket exception: ex2.
|
|
199 |
] do:[
|
|
200 |
|rslt|
|
|
201 |
|
|
202 |
here := thisContext.
|
|
203 |
rslt := paket action value. "<- here, an exception may occur"
|
|
204 |
paket ok: true.
|
|
205 |
paket result:rslt.
|
|
206 |
].
|
|
207 |
"/ Transcript showCR:'send reply.'.
|
|
208 |
paket ready signal
|
|
209 |
].
|
|
210 |
!
|
|
211 |
|
|
212 |
sendAction: aBlock
|
|
213 |
|paket sema k|
|
|
214 |
|
|
215 |
process isNil ifTrue:[
|
|
216 |
AccessLock critical:[
|
|
217 |
process isNil ifTrue:[
|
|
218 |
"/ Transcript showCR:'starting worker thread'.
|
|
219 |
messageQueue := SharedQueue new.
|
|
220 |
process := [
|
|
221 |
[
|
|
222 |
self messageHandlingLoop
|
|
223 |
] ensure:[
|
|
224 |
process := nil
|
|
225 |
]
|
|
226 |
] fork.
|
|
227 |
]
|
|
228 |
]
|
|
229 |
].
|
|
230 |
|
|
231 |
paket := MessageAndResponse new.
|
|
232 |
paket action: aBlock.
|
|
233 |
paket ready: (sema := Semaphore new).
|
|
234 |
|
|
235 |
messageQueue nextPut: paket.
|
|
236 |
sema wait.
|
|
237 |
|
|
238 |
"arrive here, when done with message"
|
|
239 |
paket ok ifTrue:[
|
|
240 |
^ paket result
|
|
241 |
].
|
|
242 |
"/ exception
|
|
243 |
|
|
244 |
"/ patch the sender chain by tacking my calling chain to the end of the exception chain (from the worker),
|
|
245 |
"/ so the debugger shows a picture as if the error happened here
|
|
246 |
k := paket exception suspendedContext.
|
|
247 |
[ k sender notNil ] whileTrue:[ k := k sender ].
|
|
248 |
k setSender: thisContext sender sender. "/ hide sendAction and synchronized frames
|
|
249 |
paket exception doRaise. "/ reraise, now in my context
|
|
250 |
!
|
|
251 |
|
|
252 |
sendSelector: selector arguments: arguments
|
|
253 |
|paket sema|
|
|
254 |
|
|
255 |
process isNil ifTrue:[
|
|
256 |
AccessLock critical:[
|
|
257 |
process isNil ifTrue:[
|
|
258 |
"/ Transcript showCR:'starting worker thread'.
|
|
259 |
messageQueue := SharedQueue new.
|
|
260 |
process := [
|
|
261 |
[
|
|
262 |
self messageHandlingLoop
|
|
263 |
] ensure:[
|
|
264 |
process := nil
|
|
265 |
]
|
|
266 |
] fork.
|
|
267 |
]
|
|
268 |
]
|
|
269 |
].
|
|
270 |
|
|
271 |
paket := MessageAndResponse new.
|
|
272 |
paket selector: selector.
|
|
273 |
paket arguments: arguments.
|
|
274 |
paket ready: (sema := Semaphore new).
|
|
275 |
"/ Transcript showCR:'send message...'.
|
|
276 |
messageQueue nextPut: paket.
|
|
277 |
sema wait.
|
|
278 |
"/ Transcript showCR:'got reply...'.
|
|
279 |
"arrive here, when done with message"
|
|
280 |
paket ok ifTrue:[
|
|
281 |
^ paket result
|
|
282 |
].
|
|
283 |
"/ exception
|
|
284 |
paket exception raiseSignal. "/ reraise, now in my context
|
|
285 |
!
|
|
286 |
|
|
287 |
synchronized:aBlock
|
5010
|
288 |
"evaluate aBlock synchronized, i.e. use a monitor for this object;
|
|
289 |
return the value from aBlock"
|
|
290 |
|
2886
|
291 |
Processor activeProcess == process ifTrue:[
|
|
292 |
^ aBlock value
|
|
293 |
].
|
|
294 |
|
|
295 |
^ self sendAction:aBlock.
|
5010
|
296 |
|
|
297 |
"Modified (comment): / 07-06-2019 / 13:51:28 / Claus Gittinger"
|
2886
|
298 |
!
|
|
299 |
|
|
300 |
synchronizedMethod
|
|
301 |
"marks calling methods as being handled via the queue"
|
|
302 |
|
|
303 |
|retVal|
|
|
304 |
|
|
305 |
Processor activeProcess == process ifTrue:[
|
|
306 |
^ self
|
|
307 |
].
|
|
308 |
|
|
309 |
retVal := self sendSelector:(thisContext sender selector) arguments:(thisContext sender args).
|
|
310 |
thisContext sender return:retVal.
|
|
311 |
! !
|
|
312 |
|
|
313 |
!ActiveObject::MessageAndResponse methodsFor:'accessing'!
|
|
314 |
|
|
315 |
action
|
|
316 |
^ action
|
|
317 |
!
|
|
318 |
|
|
319 |
action:something
|
|
320 |
action := something.
|
|
321 |
!
|
|
322 |
|
|
323 |
exception
|
|
324 |
^ exception
|
|
325 |
!
|
|
326 |
|
|
327 |
exception:something
|
|
328 |
exception := something.
|
|
329 |
!
|
|
330 |
|
|
331 |
ok
|
|
332 |
^ ok
|
|
333 |
!
|
|
334 |
|
|
335 |
ok:something
|
|
336 |
ok := something.
|
|
337 |
!
|
|
338 |
|
|
339 |
ready
|
|
340 |
^ ready
|
|
341 |
!
|
|
342 |
|
|
343 |
ready:something
|
|
344 |
ready := something.
|
|
345 |
!
|
|
346 |
|
|
347 |
result
|
|
348 |
^ result
|
|
349 |
!
|
|
350 |
|
|
351 |
result:something
|
|
352 |
result := something.
|
|
353 |
! !
|
|
354 |
|
|
355 |
!ActiveObject::RedirectingLookup class methodsFor:'instance creation'!
|
|
356 |
|
|
357 |
new
|
|
358 |
Instance isNil ifTrue: [Instance := self basicNew].
|
|
359 |
^ Instance
|
|
360 |
! !
|
|
361 |
|
|
362 |
!ActiveObject::RedirectingLookup methodsFor:'lookup'!
|
|
363 |
|
|
364 |
lookupMethodForSelector:selector directedTo:initialSearchClass for:aReceiver withArguments:argArrayOrNil from:sendingContext
|
|
365 |
"redircet everything into the receiver's doesNotUnderstand method"
|
|
366 |
|
|
367 |
Transcript show:'l '; showCR:aReceiver class name.
|
|
368 |
(aReceiver isNil "fake call from lookupMethodFor: - sigh"
|
|
369 |
or:[
|
|
370 |
"insider messages are handled as usual"
|
|
371 |
"to avoid recursive lookup error..."
|
|
372 |
((ActiveObject compiledMethodAt:#process) valueWithReceiver: aReceiver arguments:#()) == Processor activeProcess]
|
|
373 |
) ifTrue:[
|
|
374 |
^ super lookupMethodForSelector:selector directedTo:initialSearchClass for:aReceiver withArguments:argArrayOrNil from:sendingContext
|
|
375 |
].
|
|
376 |
"messages from the outside are forwarded to #doesNotUnderstand:"
|
|
377 |
^ aReceiver compiledMethodAt:#doesNotUnderstand.
|
|
378 |
! !
|
|
379 |
|
|
380 |
!ActiveObject class methodsFor:'documentation'!
|
|
381 |
|
|
382 |
version
|
4300
|
383 |
^ '$Header$'
|
2886
|
384 |
!
|
|
385 |
|
|
386 |
version_CVS
|
4300
|
387 |
^ '$Header$'
|
2886
|
388 |
! !
|
|
389 |
|
|
390 |
|
|
391 |
ActiveObject initialize!
|