Skip to content

Commit e435c0b

Browse files
authored
Merge pull request #29 from VeniceX/08-04-quark
Update to August 4, 2016, sync with quark, add tests
2 parents 7a0fd97 + 36bf371 commit e435c0b

21 files changed

+1678
-137
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DEVELOPMENT-SNAPSHOT-2016-07-25-a
1+
DEVELOPMENT-SNAPSHOT-2016-08-18-a

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ let package = Package(
44
name: "Venice",
55
dependencies: [
66
.Package(url: "https://github.com/VeniceX/CLibvenice.git", majorVersion: 0, minor: 6),
7-
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 10),
7+
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 12),
88
]
99
)

Source/Venice/Channel/Channel.swift renamed to Sources/Venice/Channel/Channel.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,20 @@
2424

2525
import CLibvenice
2626

27-
public struct ChannelGenerator<T>: IteratorProtocol {
27+
public struct ChannelGenerator<T> : IteratorProtocol {
2828
internal let channel: ReceivingChannel<T>
2929

3030
public mutating func next() -> T? {
3131
return channel.receive()
3232
}
3333
}
3434

35-
public final class Channel<T>: Sequence {
35+
public final class Channel<T> : Sequence {
3636
private let channel: chan
3737
public var closed: Bool = false
3838
private var buffer: [T] = []
3939
public let bufferSize: Int
40-
41-
public var isBuffered: Bool {
42-
return bufferSize > 0
43-
}
44-
40+
4541
public convenience init() {
4642
self.init(bufferSize: 0)
4743
}
@@ -82,28 +78,29 @@ public final class Channel<T>: Sequence {
8278
}
8379
}
8480

85-
internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
81+
internal func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
8682
if !closed {
8783
buffer.append(value)
8884
mill_choose_out(clause, channel, Int32(index))
8985
}
9086
}
9187

9288
/// Receives a value from the channel.
89+
@discardableResult
9390
public func receive() -> T? {
94-
if closed && buffer.count <= 0 {
91+
if closed && buffer.isEmpty {
9592
return nil
9693
}
9794
mill_chr(channel, "Channel receive")
9895
return getValueFromBuffer()
9996
}
10097

101-
internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
98+
internal func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
10299
mill_choose_in(clause, channel, Int32(index))
103100
}
104101

105102
internal func getValueFromBuffer() -> T? {
106-
if closed && buffer.count <= 0 {
103+
if closed && buffer.isEmpty {
107104
return nil
108105
}
109106
return buffer.removeFirst()

Source/Venice/Channel/ReceivingChannel.swift renamed to Sources/Venice/Channel/ReceivingChannel.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
public final class ReceivingChannel<T>: Sequence {
25+
public final class ReceivingChannel<T> : Sequence {
2626
private let channel: Channel<T>
2727

2828
internal init(_ channel: Channel<T>) {
2929
self.channel = channel
3030
}
3131

32+
@discardableResult
3233
public func receive() -> T? {
3334
return channel.receive()
3435
}
@@ -41,7 +42,7 @@ public final class ReceivingChannel<T>: Sequence {
4142
channel.close()
4243
}
4344

44-
internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
45+
internal func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
4546
return channel.registerReceive(clause, index: index)
4647
}
4748

Source/Venice/Channel/SendingChannel.swift renamed to Sources/Venice/Channel/SendingChannel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class SendingChannel<T> {
3333
return channel.send(value)
3434
}
3535

36-
internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
36+
internal func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
3737
return channel.send(value, clause: clause, index: index)
3838
}
3939

Source/Venice/Coroutine/Coroutine.swift renamed to Sources/Venice/Coroutine/Coroutine.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
// SOFTWARE.
2424

2525
import CLibvenice
26-
@_exported import C7
2726

2827
public typealias PID = pid_t
2928

@@ -34,31 +33,44 @@ public extension Double {
3433
}
3534

3635
/// Runs the expression in a lightweight coroutine.
37-
public func co(_ routine: (Void) -> Void) {
36+
public func coroutine(_ routine: @escaping (Void) -> Void) {
3837
var _routine = routine
3938
CLibvenice.co(&_routine, { routinePointer in
40-
UnsafeMutablePointer<((Void) -> Void)>(routinePointer!).pointee()
39+
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
4140
}, "co")
4241
}
4342

4443
/// Runs the expression in a lightweight coroutine.
45-
public func co(_ routine: @autoclosure(escaping) (Void) -> Void) {
44+
public func coroutine(_ routine: @autoclosure @escaping (Void) -> Void) {
4645
var _routine: (Void) -> Void = routine
4746
CLibvenice.co(&_routine, { routinePointer in
48-
UnsafeMutablePointer<((Void) -> Void)>(routinePointer!).pointee()
47+
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
48+
}, "co")
49+
}
50+
51+
/// Runs the expression in a lightweight coroutine.
52+
public func co(_ routine: @escaping (Void) -> Void) {
53+
coroutine(routine)
54+
}
55+
56+
/// Runs the expression in a lightweight coroutine.
57+
public func co(_ routine: @autoclosure @escaping (Void) -> Void) {
58+
var _routine: (Void) -> Void = routine
59+
CLibvenice.co(&_routine, { routinePointer in
60+
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
4961
}, "co")
5062
}
5163

5264
/// Runs the expression in a lightweight coroutine after the given duration.
53-
public func after(_ napDuration: Double, routine: (Void) -> Void) {
65+
public func after(_ napDuration: Double, routine: @escaping (Void) -> Void) {
5466
co {
5567
nap(for: napDuration)
5668
routine()
5769
}
5870
}
5971

6072
/// Runs the expression in a lightweight coroutine periodically. Call done() to leave the loop.
61-
public func every(_ napDuration: Double, routine: (done: (Void) -> Void) -> Void) {
73+
public func every(_ napDuration: Double, routine: @escaping (_ done: (Void) -> Void) -> Void) {
6274
co {
6375
var done = false
6476
while !done {

Source/Venice/FallibleChannel/FallibleChannel.swift renamed to Sources/Venice/FallibleChannel/FallibleChannel.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import CLibvenice
2626

27-
public struct FallibleChannelGenerator<T>: IteratorProtocol {
27+
public struct FallibleChannelGenerator<T> : IteratorProtocol {
2828
internal let channel: FallibleReceivingChannel<T>
2929

3030
public mutating func next() -> ChannelResult<T>? {
@@ -36,22 +36,22 @@ public enum ChannelResult<T> {
3636
case value(T)
3737
case error(Error)
3838

39-
public func success(_ closure: @noescape (T) -> Void) {
39+
public func success(_ closure: (T) -> Void) {
4040
switch self {
4141
case .value(let value): closure(value)
4242
default: break
4343
}
4444
}
4545

46-
public func failure(_ closure: @noescape (Error) -> Void) {
46+
public func failure(_ closure: (Error) -> Void) {
4747
switch self {
4848
case .error(let error): closure(error)
4949
default: break
5050
}
5151
}
5252
}
5353

54-
public final class FallibleChannel<T>: Sequence {
54+
public final class FallibleChannel<T> : Sequence {
5555
private let channel: chan
5656
public var closed: Bool = false
5757
private var buffer: [ChannelResult<T>] = []
@@ -106,7 +106,7 @@ public final class FallibleChannel<T>: Sequence {
106106
}
107107
}
108108

109-
internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
109+
func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
110110
if !closed {
111111
let result = ChannelResult<T>.value(value)
112112
buffer.append(result)
@@ -123,7 +123,7 @@ public final class FallibleChannel<T>: Sequence {
123123
}
124124
}
125125

126-
internal func send(_ error: Error, clause: UnsafeMutablePointer<Void>, index: Int) {
126+
func send(_ error: Error, clause: UnsafeMutableRawPointer, index: Int) {
127127
if !closed {
128128
let result = ChannelResult<T>.error(error)
129129
buffer.append(result)
@@ -132,8 +132,9 @@ public final class FallibleChannel<T>: Sequence {
132132
}
133133

134134
/// Receive a value from channel.
135+
@discardableResult
135136
public func receive() throws -> T? {
136-
if closed && buffer.count <= 0 {
137+
if closed && buffer.isEmpty {
137138
return nil
138139
}
139140
mill_chr(channel, "FallibleChannel receive")
@@ -148,20 +149,21 @@ public final class FallibleChannel<T>: Sequence {
148149
}
149150

150151
/// Receive a result from channel.
152+
@discardableResult
151153
public func receiveResult() -> ChannelResult<T>? {
152-
if closed && buffer.count <= 0 {
154+
if closed && buffer.isEmpty {
153155
return nil
154156
}
155157
mill_chr(channel, "FallibleChannel receiveResult")
156158
return getResultFromBuffer()
157159
}
158160

159-
internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
161+
func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
160162
mill_choose_in(clause, channel, Int32(index))
161163
}
162164

163-
internal func getResultFromBuffer() -> ChannelResult<T>? {
164-
if closed && buffer.count <= 0 {
165+
func getResultFromBuffer() -> ChannelResult<T>? {
166+
if closed && buffer.isEmpty {
165167
return nil
166168
}
167169
return buffer.removeFirst()

Source/Venice/FallibleChannel/FallibleReceivingChannel.swift renamed to Sources/Venice/FallibleChannel/FallibleReceivingChannel.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
// SOFTWARE.
2424

25-
public final class FallibleReceivingChannel<T>: Sequence {
25+
public final class FallibleReceivingChannel<T> : Sequence {
2626
private let channel: FallibleChannel<T>
2727

28-
internal init(_ channel: FallibleChannel<T>) {
28+
init(_ channel: FallibleChannel<T>) {
2929
self.channel = channel
3030
}
3131

32+
@discardableResult
3233
public func receive() throws -> T? {
3334
return try channel.receive()
3435
}
3536

37+
@discardableResult
3638
public func receiveResult() -> ChannelResult<T>? {
3739
return channel.receiveResult()
3840
}
@@ -45,11 +47,11 @@ public final class FallibleReceivingChannel<T>: Sequence {
4547
channel.close()
4648
}
4749

48-
internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
50+
func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
4951
return channel.registerReceive(clause, index: index)
5052
}
5153

52-
internal func getResultFromBuffer() -> ChannelResult<T>? {
54+
func getResultFromBuffer() -> ChannelResult<T>? {
5355
return channel.getResultFromBuffer()
5456
}
5557
}

Source/Venice/FallibleChannel/FallibleSendingChannel.swift renamed to Sources/Venice/FallibleChannel/FallibleSendingChannel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public final class FallibleSendingChannel<T> {
2626
private let channel: FallibleChannel<T>
2727

28-
internal init(_ channel: FallibleChannel<T>) {
28+
init(_ channel: FallibleChannel<T>) {
2929
self.channel = channel
3030
}
3131

@@ -37,15 +37,15 @@ public final class FallibleSendingChannel<T> {
3737
return channel.send(value)
3838
}
3939

40-
internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
40+
func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
4141
return channel.send(value, clause: clause, index: index)
4242
}
4343

4444
public func send(_ error: Error) {
4545
return channel.send(error)
4646
}
4747

48-
internal func send(_ error: Error, clause: UnsafeMutablePointer<Void>, index: Int) {
48+
func send(_ error: Error, clause: UnsafeMutableRawPointer, index: Int) {
4949
return channel.send(error, clause: clause, index: index)
5050
}
5151

Source/Venice/Poll/Poll.swift renamed to Sources/Venice/Poll/Poll.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ import C7
2727

2828
public typealias FileDescriptor = Int32
2929

30-
public enum PollError: Error {
30+
public enum PollError : Error {
3131
case timeout
3232
case failure
3333
}
3434

35-
public struct PollEvent: OptionSet {
35+
public struct PollEvent : OptionSet {
3636
public let rawValue: Int
3737

3838
public init(rawValue: Int) {
3939
self.rawValue = rawValue
4040
}
4141

42-
public static let reading = PollEvent(rawValue: Int(FDW_IN))
43-
public static let writing = PollEvent(rawValue: Int(FDW_OUT))
42+
public static let read = PollEvent(rawValue: Int(FDW_IN))
43+
public static let write = PollEvent(rawValue: Int(FDW_OUT))
4444
}
4545

4646
/// Polls file descriptor for events
47-
public func poll(_ fileDescriptor: FileDescriptor, for events: PollEvent, timingOut deadline: Double = .never) throws -> PollEvent {
47+
public func poll(_ fileDescriptor: FileDescriptor, events: PollEvent, deadline: Double = .never) throws -> PollEvent {
4848
let event = mill_fdwait(fileDescriptor, Int32(events.rawValue), deadline.int64milliseconds, "pollFileDescriptor")
4949

5050
if event == 0 {

0 commit comments

Comments
 (0)