Skip to content

Commit 6c5c1fc

Browse files
committed
Merge pull request #23 from CodaFi/beta-sixx
Update to beta 6
2 parents a7a20a5 + 55c2d8e commit 6c5c1fc

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

Concurrent/Future.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88

99
public struct Future<A> {
1010
let threadID : MVar<Optional<ThreadID>>
11-
let cOptional : IVar<Optional<A>>
11+
let completionLock : IVar<Optional<A>>
1212
let finalizers : MVar<[Optional<A> -> ()]>
1313

1414
private init(_ threadID : MVar<Optional<ThreadID>>, _ cOptional : IVar<Optional<A>>, _ finalizers : MVar<[Optional<A> -> ()]>) {
1515
self.threadID = threadID
16-
self.cOptional = cOptional
16+
self.completionLock = cOptional
1717
self.finalizers = finalizers
1818
}
1919

2020
public func read() -> Optional<A> {
21-
return self.cOptional.read()
21+
return self.completionLock.read()
2222
}
2323

2424
public func then(finalize : Optional<A> -> ()) -> Future<A> {
2525
do {
2626
let ma : Optional<Optional<A>> = try self.finalizers.modify { sTodo in
27-
let res = self.cOptional.tryRead()
27+
let res = self.completionLock.tryRead()
2828
if res == nil {
2929
return (sTodo + [finalize], res)
3030
}
@@ -44,7 +44,7 @@ public struct Future<A> {
4444
}
4545

4646
private func complete(r : Optional<A>) -> Optional<A> {
47-
return self.cOptional.tryPut(r) ? r : self.cOptional.read()
47+
return self.completionLock.tryPut(r) ? r : self.completionLock.read()
4848
}
4949

5050
private func runFinalizerWithOptional<A>(val : Optional<A>) -> (Optional<A> -> ()) -> ThreadID {
@@ -60,10 +60,10 @@ public func forkFutures<A>(ios : [() -> A]) -> Chan<Optional<A>> {
6060

6161
public func forkFuture<A>(io : () throws -> A) -> Future<A> {
6262
let msTid = MVar<ThreadID?>()
63-
let Optional : IVar<A?> = IVar()
63+
let lock : IVar<A?> = IVar()
6464
let msTodo : MVar<[A? -> ()]> = MVar(initial: [])
6565

66-
let p = Future(msTid, Optional, msTodo)
66+
let p = Future(msTid, lock, msTodo)
6767
let act : () -> () = {
6868
p.threadID.put(.Some(myTheadID()))
6969
_ = p.complete({
@@ -80,7 +80,9 @@ public func forkFuture<A>(io : () throws -> A) -> Future<A> {
8080
p.threadID.modify_({ _ in .None })
8181
let val = p.complete(paranoid)
8282
let sTodo = p.finalizers.swap([])
83-
sTodo.forEach { _ in p.runFinalizerWithOptional(val) }
83+
sTodo.forEach { _ in
84+
let _ = p.runFinalizerWithOptional(val)
85+
}
8486
}
8587

8688
_ = forkIO {

Concurrent/SVar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public struct SVar<A> {
9393
/// have had its value swapped out from under you. It is better to `read()` the values yourself
9494
/// if you need a stricter equality.
9595
public func ==<A : Equatable>(lhs : SVar<A>, rhs : SVar<A>) -> Bool {
96-
if lhs.isEmpty && !rhs.isEmpty {
96+
if lhs.isEmpty && rhs.isEmpty {
9797
return true
9898
}
9999
if lhs.isEmpty != rhs.isEmpty {

ConcurrentTests/PiCalculus.swift

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@ import XCTest
1111

1212
typealias Name = String
1313

14-
internal class Box<T> {
15-
let value : T
16-
internal init(_ x : T) { self.value = x }
17-
}
18-
1914
/// An encoding of the Pi Calculus, a process calculus of names and channels with equal expressive
2015
/// power to the Lambda Calculus.
21-
enum π {
16+
indirect enum π {
2217
/// Run the left and right computations simultaneously
23-
case Simultaneously(Box<π>, Box<π>)
18+
case Simultaneously(π, π)
2419
/// Repeatedly spawn and execute copies of this computation forever.
25-
case Rep(Box<π>)
20+
case Rep(π)
2621
/// Create a new channel with a given name, then run the next computation.
27-
case New(Name, Box<π>)
22+
case New(Name, π)
2823
/// Send a value over the channel with a given name, then run the next computation.
29-
case Send(Name, Name, Box<π>)
30-
/// Recieve a value on the channel with a given name, bind the result to the name, then run
24+
case Send(Name, Name, π)
25+
/// Receive a value on the channel with a given name, bind the result to the name, then run
3126
/// the next computation.
32-
case Recieve(Name, Name, Box<π>)
27+
case Receive(Name, Name, π)
3328
/// Terminate the process.
3429
case Terminate
3530
}
@@ -45,19 +40,12 @@ struct Mu {
4540

4641
typealias Environment = Dictionary<Name, Mu>
4742

48-
func forever<A>(@autoclosure(escaping) io : () -> A) -> (() -> A) {
49-
return {
50-
io()
51-
return forever(io)()
52-
}
53-
}
54-
5543
func runπ(var env : Environment, _ pi : π) -> () {
5644
switch pi {
5745
case .Simultaneously(let ba, let bb):
5846
let f = { x in forkIO(runπ(env, x)) }
59-
f(ba.value)
60-
f(bb.value)
47+
f(ba)
48+
f(bb)
6149
case .Rep(let bp):
6250
return runπ(env, π.Rep(bp))
6351
case .Terminate:
@@ -66,16 +54,16 @@ func runπ(var env : Environment, _ pi : π) -> () {
6654
let c : Chan<Mu> = Chan()
6755
let mu = Mu(c)
6856
env[bind] = mu
69-
return runπ(env, bp.value)
57+
return runπ(env, bp)
7058
case .Send(let msg, let dest, let bp):
7159
let w = env[dest]
7260
w?.c.write(env[msg]!)
73-
return runπ(env, bp.value)
74-
case .Recieve(let src, let bind, let bp):
61+
return runπ(env, bp)
62+
case .Receive(let src, let bind, let bp):
7563
let w = env[src]
7664
let recv = w?.c.read()
7765
env[bind] = recv
78-
forkIO(runπ(env, bp.value))
66+
forkIO(runπ(env, bp))
7967
}
8068
}
8169

@@ -91,32 +79,37 @@ infix operator !|! {
9179
}
9280

9381
func !|! (l : π, r : π) -> π {
94-
return .Simultaneously(Box(l), Box(r))
82+
return .Simultaneously(l, r)
9583
}
9684

9785
func `repeat`(p : π) -> π {
98-
return .Rep(Box(p))
86+
return .Rep(p)
9987
}
10088

10189
func terminate() -> π {
10290
return .Terminate
10391
}
10492

10593
func newChannel(n : Name, then : π) -> π {
106-
return .New(n, Box(then))
94+
return .New(n, then)
10795
}
10896

10997
func send(v : Name, on : Name, then : π) -> π {
110-
return .Send(v, on, Box(then))
98+
return .Send(v, on, then)
11199
}
112100

113-
func recieve(on : Name, v : Name, then : π) -> π {
114-
return .Recieve(v, on, Box(then))
101+
func receive(on : Name, v : Name, then : π) -> π {
102+
return .Receive(v, on, then)
115103
}
116104

117105
class PiCalculusSpec : XCTestCase {
118106
func testPi() {
119-
let pi = newChannel("x", then: send("x", on: "z", then: terminate()) !|! recieve("x", v: "y", then: send("y", on: "x", then: recieve("x", v: "y", then: terminate()))) !|! send("z", on: "v", then: recieve("v", v: "v", then: terminate())))
107+
let pi = newChannel("x", then:
108+
send("x", on: "z", then: terminate())
109+
!|!
110+
receive("x", v: "y", then: send("y", on: "x", then: receive("x", v: "y", then: terminate())))
111+
!|!
112+
send("z", on: "v", then: receive("v", v: "v", then: terminate())))
120113
runCompute(pi)
121114
}
122115
}

0 commit comments

Comments
 (0)