at:tutorial:actors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:actors [2007/07/18 11:30] – elisag | at:tutorial:actors [2008/11/04 10:11] – added tvcutsem | ||
---|---|---|---|
Line 26: | Line 26: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
Line 70: | Line 70: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
Line 112: | Line 112: | ||
}; | }; | ||
}; | }; | ||
- | >>< | + | >>< |
</ | </ | ||
Line 138: | Line 138: | ||
<note warning> | <note warning> | ||
- | A word of warning: isolates | + | Isolates |
< | < | ||
def ==(other) { | def ==(other) { | ||
Line 144: | Line 144: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | On a related note, it is good practice to consider isolates as // | ||
</ | </ | ||
Line 169: | Line 171: | ||
>>4 | >>4 | ||
</ | </ | ||
+ | |||
+ | < | ||
+ | Since AmbientTalk 2.12 the interpreter is smart enough to figure out the lexically free variables of an isolate itself. If no variables are explicitly listed, the interpreter will try to figure out which lexically free variables it should implicitly copy. Unlike explicitly listed variables (like '' | ||
+ | </ | ||
===== Futures ===== | ===== Futures ===== | ||
Line 196: | Line 202: | ||
The first statement imports the futures module into the current lexical scope. This enables you as a developer to use some additional language constructs exported by the futures module, as will be explained later. The second statement enables the futures behaviour, causing any asynchronous message send to return a future rather than '' | The first statement imports the futures module into the current lexical scope. This enables you as a developer to use some additional language constructs exported by the futures module, as will be explained later. The second statement enables the futures behaviour, causing any asynchronous message send to return a future rather than '' | ||
+ | |||
+ | More information pertaining to the API of the futures language module can be found in the [[: | ||
==== Working with Unresolved Futures ==== | ==== Working with Unresolved Futures ==== | ||
Line 201: | Line 209: | ||
We have described a future as a placeholder for the return value of an asynchronous message send which is eventually // | We have described a future as a placeholder for the return value of an asynchronous message send which is eventually // | ||
- | Blocking a thread on a future can be a major source of deadlocks, like any form of blocking, of course. In the actor paradigm where communication between actors should remain strictly asynchronous, | + | Blocking a thread on a future can be a major source of deadlocks, like any form of blocking. In the actor paradigm where communication between actors should remain strictly asynchronous, |
The solution proposed in the [[http:// | The solution proposed in the [[http:// | ||
+ | |||
+ | As an example of a pipeline of message sends, consider the following code: | ||
+ | |||
+ | < | ||
+ | def booleanFuture := remoteObject< | ||
+ | booleanFuture< | ||
+ | ... | ||
+ | } ifFalse: { | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In this example, the message '' | ||
==== Working with Resolved Futures ==== | ==== Working with Resolved Futures ==== | ||
Line 271: | Line 292: | ||
</ | </ | ||
- | Finally, it is useful to know that '' | + | Finally, it is useful to know that '' |
< | < | ||
Line 304: | Line 325: | ||
< | < | ||
+ | // to create an explicit future: | ||
def [future, resolver] := makeFuture(); | def [future, resolver] := makeFuture(); | ||
- | consumer< | + | |
- | def val := /* calculate useful value */ | + | // to explicitly resolve a future |
resolver.resolve(val); | resolver.resolve(val); | ||
</ | </ | ||
Line 313: | Line 335: | ||
The resolver also defines a '' | The resolver also defines a '' | ||
+ | |||
+ | As an example of such conditional synchronization, | ||
+ | |||
+ | < | ||
+ | def makeDatingService() { | ||
+ | def people := []; // a list of Questionnaire objects | ||
+ | object: { | ||
+ | def match(lonelyHeart) { | ||
+ | // if an ideal mate is found in the list, | ||
+ | // | ||
+ | // otherwise | ||
+ | // | ||
+ | // the lonelyHeart later, when an ideal made | ||
+ | // has registered with the dating service | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Let us assume that a person is simply identified by a name and its sex: | ||
+ | < | ||
+ | def makePerson(nam, | ||
+ | object: { | ||
+ | def name := nam; | ||
+ | def sex := sx; | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The dating service has a little database stored as a simple list. This list does not contain person objects but rather Questionnaire objects. The questionnaire contains the logic necessary to match people. We will assume for the sake of the example that 2 people match if they are of the opposite sex. In addition, a questionnaire object can keep track of an " | ||
+ | |||
+ | < | ||
+ | def makeQuestionnaire(p) { | ||
+ | def idealPersonResolver; | ||
+ | object: { | ||
+ | def person := p; | ||
+ | def matches(otherQ) { otherQ.person.sex != p.sex }; | ||
+ | def wait() { | ||
+ | def [future, resolver] := makeFuture(); | ||
+ | idealPersonResolver := resolver; | ||
+ | future | ||
+ | }; | ||
+ | def notify(name) { idealPersonResolver.resolve(name) }; | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | When a questionnaire is asked to '' | ||
+ | |||
+ | Armed with these abstractions, | ||
+ | |||
+ | < | ||
+ | def makeDatingService() { | ||
+ | def people := []; // a list of Questionnaire objects | ||
+ | object: { | ||
+ | def match(lonelyHeart) { | ||
+ | def lonelyHeartQ := makeQuestionnaire(lonelyHeart); | ||
+ | { |return| | ||
+ | people.each: | ||
+ | // an ideal mate was found | ||
+ | if: (idealMateQ.matches(lonelyHeartQ).and: | ||
+ | idealMateQ.notify(lonelyHeart.name); | ||
+ | // remove the person from the database | ||
+ | people := people.filter: | ||
+ | return(idealMateQ.person.name) // notify lonelyHeart | ||
+ | } | ||
+ | }; | ||
+ | // no ideal mate was found, store its questionnaire in the database | ||
+ | people := people + [lonelyHeartQ]; | ||
+ | lonelyHeartQ.wait(); | ||
+ | }.escape(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Below, we define an auxiliary method that illustrates how a lonely heart has to interact with the dating service. | ||
+ | |||
+ | < | ||
+ | def d := makeDatingService(); | ||
+ | def register(p) { | ||
+ | when: d< | ||
+ | system.println(p.name + " matched with " + name); | ||
+ | }; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The key to our conditional synchronization is that the '' | ||
+ | |||
+ | The complete source code of the above example can be found in the file '' | ||
===== Actor Mirrors ===== | ===== Actor Mirrors ===== | ||
Line 318: | Line 430: | ||
An actor in AmbientTalk is primarily a //host// for regular objects. It is equipped with a message queue to receive asynchronous messages sent to one of its objects. The mirrors on these objects have corresponding meta-level operations such as '' | An actor in AmbientTalk is primarily a //host// for regular objects. It is equipped with a message queue to receive asynchronous messages sent to one of its objects. The mirrors on these objects have corresponding meta-level operations such as '' | ||
- | Some operations, such as creating and sending asynchronous messages are useful to reify at the //actor level//. With such a reification, | + | Some operations, such as creating and sending asynchronous messages are useful to reify at the //actor level//. With such a reification, |
- | Overriding the actor' | + | Overriding the actor' |
< | < | ||
- | def oldmirror := actor.install: (extend: actor with: { | + | def actor := reflectOnActor(); |
+ | def oldmirror := actor.becomeMirroredBy: (extend: actor with: { | ||
def send(msg) { | def send(msg) { | ||
log(msg); | log(msg); | ||
Line 331: | Line 444: | ||
</ | </ | ||
- | Notice that, in this example, the new metaobject protocol is an extension of the old protocol. This enables it to invoke its parent' | + | Notice that, in this example, the new metaobject protocol is an extension of the old protocol. This enables it to invoke its parent' |
< | < |
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag