Skip to content

Commit

Permalink
Merge pull request #29 from VeniceX/08-04-quark
Browse files Browse the repository at this point in the history
Update to August 4, 2016, sync with quark, add tests
  • Loading branch information
antonmes committed Aug 22, 2016
2 parents 7a0fd97 + 36bf371 commit e435c0b
Show file tree
Hide file tree
Showing 21 changed files with 1,678 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-07-25-a
DEVELOPMENT-SNAPSHOT-2016-08-18-a
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ let package = Package(
name: "Venice",
dependencies: [
.Package(url: "https://github.com/VeniceX/CLibvenice.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 10),
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 12),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@

import CLibvenice

public struct ChannelGenerator<T>: IteratorProtocol {
public struct ChannelGenerator<T> : IteratorProtocol {
internal let channel: ReceivingChannel<T>

public mutating func next() -> T? {
return channel.receive()
}
}

public final class Channel<T>: Sequence {
public final class Channel<T> : Sequence {
private let channel: chan
public var closed: Bool = false
private var buffer: [T] = []
public let bufferSize: Int

public var isBuffered: Bool {
return bufferSize > 0
}


public convenience init() {
self.init(bufferSize: 0)
}
Expand Down Expand Up @@ -82,28 +78,29 @@ public final class Channel<T>: Sequence {
}
}

internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
internal func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
if !closed {
buffer.append(value)
mill_choose_out(clause, channel, Int32(index))
}
}

/// Receives a value from the channel.
@discardableResult
public func receive() -> T? {
if closed && buffer.count <= 0 {
if closed && buffer.isEmpty {
return nil
}
mill_chr(channel, "Channel receive")
return getValueFromBuffer()
}

internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
internal func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
mill_choose_in(clause, channel, Int32(index))
}

internal func getValueFromBuffer() -> T? {
if closed && buffer.count <= 0 {
if closed && buffer.isEmpty {
return nil
}
return buffer.removeFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

public final class ReceivingChannel<T>: Sequence {
public final class ReceivingChannel<T> : Sequence {
private let channel: Channel<T>

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

@discardableResult
public func receive() -> T? {
return channel.receive()
}
Expand All @@ -41,7 +42,7 @@ public final class ReceivingChannel<T>: Sequence {
channel.close()
}

internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
internal func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
return channel.registerReceive(clause, index: index)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class SendingChannel<T> {
return channel.send(value)
}

internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
internal func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
return channel.send(value, clause: clause, index: index)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
// SOFTWARE.

import CLibvenice
@_exported import C7

public typealias PID = pid_t

Expand All @@ -34,31 +33,44 @@ public extension Double {
}

/// Runs the expression in a lightweight coroutine.
public func co(_ routine: (Void) -> Void) {
public func coroutine(_ routine: @escaping (Void) -> Void) {
var _routine = routine
CLibvenice.co(&_routine, { routinePointer in
UnsafeMutablePointer<((Void) -> Void)>(routinePointer!).pointee()
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
}, "co")
}

/// Runs the expression in a lightweight coroutine.
public func co(_ routine: @autoclosure(escaping) (Void) -> Void) {
public func coroutine(_ routine: @autoclosure @escaping (Void) -> Void) {
var _routine: (Void) -> Void = routine
CLibvenice.co(&_routine, { routinePointer in
UnsafeMutablePointer<((Void) -> Void)>(routinePointer!).pointee()
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
}, "co")
}

/// Runs the expression in a lightweight coroutine.
public func co(_ routine: @escaping (Void) -> Void) {
coroutine(routine)
}

/// Runs the expression in a lightweight coroutine.
public func co(_ routine: @autoclosure @escaping (Void) -> Void) {
var _routine: (Void) -> Void = routine
CLibvenice.co(&_routine, { routinePointer in
routinePointer!.assumingMemoryBound(to: ((Void) -> Void).self).pointee()
}, "co")
}

/// Runs the expression in a lightweight coroutine after the given duration.
public func after(_ napDuration: Double, routine: (Void) -> Void) {
public func after(_ napDuration: Double, routine: @escaping (Void) -> Void) {
co {
nap(for: napDuration)
routine()
}
}

/// Runs the expression in a lightweight coroutine periodically. Call done() to leave the loop.
public func every(_ napDuration: Double, routine: (done: (Void) -> Void) -> Void) {
public func every(_ napDuration: Double, routine: @escaping (_ done: (Void) -> Void) -> Void) {
co {
var done = false
while !done {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import CLibvenice

public struct FallibleChannelGenerator<T>: IteratorProtocol {
public struct FallibleChannelGenerator<T> : IteratorProtocol {
internal let channel: FallibleReceivingChannel<T>

public mutating func next() -> ChannelResult<T>? {
Expand All @@ -36,22 +36,22 @@ public enum ChannelResult<T> {
case value(T)
case error(Error)

public func success(_ closure: @noescape (T) -> Void) {
public func success(_ closure: (T) -> Void) {
switch self {
case .value(let value): closure(value)
default: break
}
}

public func failure(_ closure: @noescape (Error) -> Void) {
public func failure(_ closure: (Error) -> Void) {
switch self {
case .error(let error): closure(error)
default: break
}
}
}

public final class FallibleChannel<T>: Sequence {
public final class FallibleChannel<T> : Sequence {
private let channel: chan
public var closed: Bool = false
private var buffer: [ChannelResult<T>] = []
Expand Down Expand Up @@ -106,7 +106,7 @@ public final class FallibleChannel<T>: Sequence {
}
}

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

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

/// Receive a value from channel.
@discardableResult
public func receive() throws -> T? {
if closed && buffer.count <= 0 {
if closed && buffer.isEmpty {
return nil
}
mill_chr(channel, "FallibleChannel receive")
Expand All @@ -148,20 +149,21 @@ public final class FallibleChannel<T>: Sequence {
}

/// Receive a result from channel.
@discardableResult
public func receiveResult() -> ChannelResult<T>? {
if closed && buffer.count <= 0 {
if closed && buffer.isEmpty {
return nil
}
mill_chr(channel, "FallibleChannel receiveResult")
return getResultFromBuffer()
}

internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
mill_choose_in(clause, channel, Int32(index))
}

internal func getResultFromBuffer() -> ChannelResult<T>? {
if closed && buffer.count <= 0 {
func getResultFromBuffer() -> ChannelResult<T>? {
if closed && buffer.isEmpty {
return nil
}
return buffer.removeFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

public final class FallibleReceivingChannel<T>: Sequence {
public final class FallibleReceivingChannel<T> : Sequence {
private let channel: FallibleChannel<T>

internal init(_ channel: FallibleChannel<T>) {
init(_ channel: FallibleChannel<T>) {
self.channel = channel
}

@discardableResult
public func receive() throws -> T? {
return try channel.receive()
}

@discardableResult
public func receiveResult() -> ChannelResult<T>? {
return channel.receiveResult()
}
Expand All @@ -45,11 +47,11 @@ public final class FallibleReceivingChannel<T>: Sequence {
channel.close()
}

internal func registerReceive(_ clause: UnsafeMutablePointer<Void>, index: Int) {
func registerReceive(_ clause: UnsafeMutableRawPointer, index: Int) {
return channel.registerReceive(clause, index: index)
}

internal func getResultFromBuffer() -> ChannelResult<T>? {
func getResultFromBuffer() -> ChannelResult<T>? {
return channel.getResultFromBuffer()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public final class FallibleSendingChannel<T> {
private let channel: FallibleChannel<T>

internal init(_ channel: FallibleChannel<T>) {
init(_ channel: FallibleChannel<T>) {
self.channel = channel
}

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

internal func send(_ value: T, clause: UnsafeMutablePointer<Void>, index: Int) {
func send(_ value: T, clause: UnsafeMutableRawPointer, index: Int) {
return channel.send(value, clause: clause, index: index)
}

public func send(_ error: Error) {
return channel.send(error)
}

internal func send(_ error: Error, clause: UnsafeMutablePointer<Void>, index: Int) {
func send(_ error: Error, clause: UnsafeMutableRawPointer, index: Int) {
return channel.send(error, clause: clause, index: index)
}

Expand Down
10 changes: 5 additions & 5 deletions Source/Venice/Poll/Poll.swift → Sources/Venice/Poll/Poll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ import C7

public typealias FileDescriptor = Int32

public enum PollError: Error {
public enum PollError : Error {
case timeout
case failure
}

public struct PollEvent: OptionSet {
public struct PollEvent : OptionSet {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public static let reading = PollEvent(rawValue: Int(FDW_IN))
public static let writing = PollEvent(rawValue: Int(FDW_OUT))
public static let read = PollEvent(rawValue: Int(FDW_IN))
public static let write = PollEvent(rawValue: Int(FDW_OUT))
}

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

if event == 0 {
Expand Down

0 comments on commit e435c0b

Please sign in to comment.