Skip to content

Commit bb7a67b

Browse files
committed
Merge pull request #25 from CodaFi/big-merge
Update to Swift 2.0
2 parents 6a97548 + 5ccfac2 commit bb7a67b

26 files changed

+218
-345
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ DerivedData
1616
*.hmap
1717
*.ipa
1818
*.xcuserstate
19+
*.xcscmblueprint
20+
Cartfile.resolved
1921
Carthage/
2022

2123
# CocoaPods

.gitmodules

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[submodule "Carthage/Checkouts/Swiftz"]
2-
path = Carthage/Checkouts/Swiftz
3-
url = https://github.com/typelift/Swiftz.git
41
[submodule "Carthage/Checkouts/SwiftCheck"]
52
path = Carthage/Checkouts/SwiftCheck
63
url = https://github.com/typelift/SwiftCheck.git

.travis.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
language: objective-c
2-
osx_image: beta-xcode6.3
2+
osx_image: xcode7
3+
4+
before_install:
5+
- gem install xcpretty-travis-formatter --no-rdoc --no-ri --no-document --quiet
36

47
install:
58
- git submodule update -i --recursive
69

710
script:
8-
- xctool -project Concurrent.xcodeproj -scheme Concurrent -sdk macosx ONLY_ACTIVE_ARCH=NO clean build test
9-
- xctool -project Concurrent.xcodeproj -scheme Concurrent-iOS -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO clean build test
11+
- set -o pipefail && xcodebuild -project Concurrent.xcodeproj -scheme Concurrent -sdk macosx ONLY_ACTIVE_ARCH=NO clean build test | xcpretty -c -f `xcpretty-travis-formatter`
12+
- set -o pipefail && xcodebuild -project Concurrent.xcodeproj -scheme Concurrent-iOS -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO clean build test | xcpretty -c -f `xcpretty-travis-formatter`

Cartfile.private

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
github "typelift/Swiftz"
2-
github "typelift/SwiftCheck"
1+
github "typelift/SwiftCheck"

Cartfile.resolved

-2
This file was deleted.

Carthage/Checkouts/SwiftCheck

Submodule SwiftCheck updated 46 files

Carthage/Checkouts/Swiftz

-1
This file was deleted.

Concurrent.xcodeproj/project.pbxproj

+17-88
Large diffs are not rendered by default.

Concurrent.xcodeproj/xcshareddata/xcschemes/Concurrent-iOS.xcscheme

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0630"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -62,6 +62,8 @@
6262
ReferencedContainer = "container:Concurrent.xcodeproj">
6363
</BuildableReference>
6464
</MacroExpansion>
65+
<AdditionalOptions>
66+
</AdditionalOptions>
6567
</TestAction>
6668
<LaunchAction
6769
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
@@ -71,6 +73,7 @@
7173
buildConfiguration = "Debug"
7274
ignoresPersistentStateOnLaunch = "NO"
7375
debugDocumentVersioning = "YES"
76+
debugServiceExtension = "internal"
7477
allowLocationSimulation = "YES">
7578
<MacroExpansion>
7679
<BuildableReference

Concurrent.xcodeproj/xcshareddata/xcschemes/Concurrent.xcscheme

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0610"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -62,6 +62,8 @@
6262
ReferencedContainer = "container:Concurrent.xcodeproj">
6363
</BuildableReference>
6464
</MacroExpansion>
65+
<AdditionalOptions>
66+
</AdditionalOptions>
6567
</TestAction>
6668
<LaunchAction
6769
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
@@ -71,6 +73,7 @@
7173
buildConfiguration = "Debug"
7274
ignoresPersistentStateOnLaunch = "NO"
7375
debugDocumentVersioning = "YES"
76+
debugServiceExtension = "internal"
7477
allowLocationSimulation = "YES">
7578
<MacroExpansion>
7679
<BuildableReference

Concurrent/Chan.swift

+15-7
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ public struct Chan<A> {
2929

3030
/// Reads a value from the channel.
3131
public func read() -> A {
32-
return self.readEnd.modify { readEnd in
33-
let item : ChItem<A> = readEnd.read()
34-
return (item.stream(), item.val())
32+
do {
33+
return try self.readEnd.modify { readEnd in
34+
let item : ChItem<A> = readEnd.read()
35+
return (item.stream(), item.val())
36+
}
37+
} catch _ {
38+
fatalError("Fatal: Could not modify read head.")
3539
}
3640
}
3741

@@ -46,7 +50,7 @@ public struct Chan<A> {
4650

4751
/// Writes a list of values to a channel.
4852
public func writeList(xs : [A]) {
49-
xs.map({ self.write($0) })
53+
xs.forEach(self.write)
5054
}
5155

5256
/// Returns whether the channel is empty.
@@ -55,9 +59,13 @@ public struct Chan<A> {
5559
/// concurrent computations, this may change out from under you without warning, or even by the
5660
/// time it can be acted on. It is better to use one of the direct actions above.
5761
public var isEmpty : Bool {
58-
return self.readEnd.withMVar { r in
59-
let w = r.tryRead()
60-
return w == nil
62+
do {
63+
return try self.readEnd.withMVar { r in
64+
let w = r.tryRead()
65+
return w == nil
66+
}
67+
} catch _ {
68+
fatalError("Fatal: Could not determine emptiness; read of underlying MVar failed.")
6169
}
6270
}
6371

Concurrent/Concurrent.swift

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// Copyright (c) 2014 TypeLift. All rights reserved.
77
//
88

9-
import Swiftz
10-
119
public typealias ThreadID = pthread_t
1210

1311
public func myTheadID() -> ThreadID {
@@ -21,11 +19,10 @@ public func forkIO(@autoclosure(escaping) io : () -> ()) -> ThreadID {
2119
})
2220
}
2321

24-
/// Forks a thread and calls the given function when the thread is about to terminate with either
25-
/// a value or an exception.
26-
public func forkFinally<A>(@autoclosure(escaping) io : () -> A, finally : Either<Exception, A> -> ()) -> ThreadID {
27-
return mask({ (let restore : A -> A) -> ThreadID in
28-
return forkIO(finally(try(restore(io()))))
22+
/// Forks a computation onto a new thread and returns its thread ID.
23+
public func forkIO(io : () -> ()) -> ThreadID {
24+
return CONCRealWorld.forkWithStart({
25+
return io()
2926
})
3027
}
3128

Concurrent/Exception.swift

-82
This file was deleted.

Concurrent/Future.swift

+52-51
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,88 @@
66
// Copyright (c) 2014 TypeLift. All rights reserved.
77
//
88

9-
import Swiftz
10-
119
public struct Future<A> {
1210
let threadID : MVar<Optional<ThreadID>>
13-
let cResult : IVar<Result<A>>
14-
let finalizers : MVar<[Result<A> -> ()]>
11+
let completionLock : IVar<Optional<A>>
12+
let finalizers : MVar<[Optional<A> -> ()]>
1513

16-
private init(_ threadID : MVar<Optional<ThreadID>>, _ cResult : IVar<Result<A>>, _ finalizers : MVar<[Result<A> -> ()]>) {
14+
private init(_ threadID : MVar<Optional<ThreadID>>, _ cOptional : IVar<Optional<A>>, _ finalizers : MVar<[Optional<A> -> ()]>) {
1715
self.threadID = threadID
18-
self.cResult = cResult
16+
self.completionLock = cOptional
1917
self.finalizers = finalizers
2018
}
2119

22-
public func read() -> Result<A> {
23-
return self.cResult.read()
20+
public func read() -> Optional<A> {
21+
return self.completionLock.read()
2422
}
2523

26-
public func then(finalize : Result<A> -> ()) -> Future<A> {
27-
let ma : Optional<Result<A>> = self.finalizers.modify { sTodo in
28-
let res = self.cResult.tryRead()
29-
if res == nil {
30-
return (sTodo + [finalize], res)
24+
public func then(finalize : Optional<A> -> ()) -> Future<A> {
25+
do {
26+
let ma : Optional<Optional<A>> = try self.finalizers.modify { sTodo in
27+
let res = self.completionLock.tryRead()
28+
if res == nil {
29+
return (sTodo + [finalize], res)
30+
}
31+
return (sTodo, res)
3132
}
32-
return (sTodo, res)
33-
}
34-
35-
switch ma {
36-
case .None:
37-
return self
38-
case .Some(let val):
39-
self.runFinalizerWithResult(val)(finalize)
40-
return self
33+
34+
switch ma {
35+
case .None:
36+
return self
37+
case .Some(let val):
38+
self.runFinalizerWithOptional(val)(finalize)
39+
return self
40+
}
41+
} catch _ {
42+
fatalError("Fatal: Could not read underlying MVar to spark finalizers.")
4143
}
4244
}
4345

44-
private func complete(r : Result<A>) -> Result<A> {
45-
return self.cResult.tryPut(r) ? r : self.cResult.read()
46+
private func complete(r : Optional<A>) -> Optional<A> {
47+
return self.completionLock.tryPut(r) ? r : self.completionLock.read()
4648
}
4749

48-
private func runFinalizerWithResult<A>(val : Result<A>) -> (Result<A> -> ()) -> ThreadID {
50+
private func runFinalizerWithOptional<A>(val : Optional<A>) -> (Optional<A> -> ()) -> ThreadID {
4951
return { todo in forkIO(todo(val)) }
5052
}
5153
}
5254

53-
public func forkFutures<A>(ios : [() -> A]) -> Chan<Result<A>> {
54-
let c : Chan<Result<A>> = Chan()
55-
let ps = ios.map({ forkFuture($0) }).map({ f in f.then({ c.write($0) }) })
55+
public func forkFutures<A>(ios : [() -> A]) -> Chan<Optional<A>> {
56+
let c : Chan<Optional<A>> = Chan()
57+
ios.map(forkFuture).forEach { f in f.then(c.write) }
5658
return c
5759
}
5860

59-
public func forkFuture<A>(io : () -> A) -> Future<A> {
60-
let msTid : MVar<Optional<ThreadID>> = MVar()
61-
let result : IVar<Result<A>> = IVar()
62-
let msTodo : MVar<[Result<A> -> ()]> = MVar(initial: [])
61+
public func forkFuture<A>(io : () throws -> A) -> Future<A> {
62+
let msTid = MVar<ThreadID?>()
63+
let lock : IVar<A?> = IVar()
64+
let msTodo : MVar<[A? -> ()]> = MVar(initial: [])
6365

64-
let p = Future(msTid, result, msTodo)
65-
let act : dispatch_block_t = {
66+
let p = Future(msTid, lock, msTodo)
67+
let act : () -> () = {
6668
p.threadID.put(.Some(myTheadID()))
67-
68-
let val : Result<A> = { r in
69-
let res : Result<A> = r.toResult({ e in return NSError(domain: "", code: 0, userInfo: [ NSLocalizedDescriptionKey : e.description ]) })
70-
return p.complete(res)
71-
}(try(io()))
72-
73-
switch val {
74-
case .Error(let err):
75-
return error("")
76-
case .Value(_):
77-
return ()
78-
}
69+
_ = p.complete({
70+
do {
71+
return try io()
72+
} catch _ {
73+
return nil
74+
}
75+
}())
7976
}
8077

8178
let process : dispatch_block_t = {
82-
let paranoid = Result<A>.error(NSError(domain: "", code: 0, userInfo: nil))
83-
84-
p.threadID.modify_(const(.None))
79+
let paranoid : A? = nil
80+
p.threadID.modify_({ _ in .None })
8581
let val = p.complete(paranoid)
8682
let sTodo = p.finalizers.swap([])
87-
let _ = sTodo.map(p.runFinalizerWithResult(val))
83+
sTodo.forEach { _ in
84+
let _ = p.runFinalizerWithOptional(val)
85+
}
8886
}
8987

90-
let thr = forkIO(finally(act())(then: process()))
88+
_ = forkIO {
89+
act()
90+
process()
91+
}
9192
return p
9293
}

Concurrent/IChan.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public struct IChan<A> {
4141
/// If the same head has been written to more than once, this function will throw an exception.
4242
public func write(x : A) -> IChan<A> {
4343
let ic = IChan()
44-
self.ivar.put((x, ic))
44+
do {
45+
try self.ivar.put((x, ic))
46+
} catch _ {
47+
fatalError("Fatal: Accidentally wrote to the same IChan head twice.")
48+
}
4549
return ic
4650
}
4751

0 commit comments

Comments
 (0)