Skip to content

Commit 433548a

Browse files
Diggoryktoso
authored andcommitted
Made Types consistent in re-entrancy sample code
The Actor type should either be a `Person` or a `DecisionMaker` Similarly the `opinion` property's type should either be `Judgment` or 'Decision'
1 parent e19dcac commit 433548a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

proposals/0306-actors.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ Reentrancy means that execution of asynchronous actor-isolated functions may "in
308308
Interleaving executions still respect the actor's "single-threaded illusion", i.e., no two functions will ever execute *concurrently* on any given actor. However they may *interleave* at suspension points. In broad terms this means that reentrant actors are *thread-safe* but are not automatically protecting from the "high level" kinds of races that may still occur, potentially invalidating invariants upon which an executing asynchronous function may be relying on. To further clarify the implications of this, let us consider the following actor, which thinks of an idea and then returns it, after telling its friend about it.
309309

310310
```swift
311-
actor Person {
311+
actor DecisionMaker {
312312
let friend: Friend
313313

314314
// actor-isolated opinion
315-
var opinion: Judgment = .noIdea
315+
var opinion: Decision = .noIdea
316316

317317
func thinkOfGoodIdea() async -> Decision {
318318
opinion = .goodIdea // <1>
@@ -328,13 +328,13 @@ actor Person {
328328
}
329329
```
330330

331-
In the example above the `Person` can think of a good or bad idea, shares that opinion with a friend, and returns that opinion that it stored. Since the actor is reentrant this code is wrong and will return an arbitrary opinion if the actor begins to think of a few ideas at the same time.
331+
In the example above the `DecisionMaker` can think of a good or bad idea, shares that opinion with a friend, and returns that opinion that it stored. Since the actor is reentrant this code is wrong and will return an arbitrary opinion if the actor begins to think of a few ideas at the same time.
332332

333333
This is exemplified by the following piece of code, exercising the `decisionMaker` actor:
334334

335335
```swift
336-
let goodThink = detach { await person.thinkOfGoodIdea() } // runs async
337-
let badThink = detach { await person.thinkOfBadIdea() } // runs async
336+
let goodThink = detach { await decisionMaker.thinkOfGoodIdea() } // runs async
337+
let badThink = detach { await decisionMaker.thinkOfBadIdea() } // runs async
338338

339339
let shouldBeGood = await goodThink.get()
340340
let shouldBeBad = await badThink.get()
@@ -372,7 +372,7 @@ If we take the example from the previous section and use a non-reentrant actor,
372372
// assume non-reentrant
373373
actor DecisionMaker {
374374
let friend: DecisionMaker
375-
var opinion: Judgment = .noIdea
375+
var opinion: Decision = .noIdea
376376

377377
func thinkOfGoodIdea() async -> Decision {
378378
opinion = .goodIdea
@@ -392,7 +392,7 @@ However, non-entrancy can result in deadlock if a task involves calling back int
392392

393393
```swift
394394
extension DecisionMaker {
395-
func tell(_ opinion: Judgment, heldBy friend: DecisionMaker) async {
395+
func tell(_ opinion: Decision, heldBy friend: DecisionMaker) async {
396396
if opinion == .badIdea {
397397
await friend.convinceOtherwise(opinion)
398398
}

0 commit comments

Comments
 (0)