Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed package.pins from the repo and added it to .gitignore, removed trailing whitespace #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.build
/Packages
/*.xcodeproj
Package.pins
5 changes: 0 additions & 5 deletions Package.pins

This file was deleted.

54 changes: 27 additions & 27 deletions Sources/Venice/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import CLibdill
/// ```
public final class Channel<Type> {
private typealias Handle = Int32

private enum ChannelResult<Type> {
case value(Type)
case error(Error)
Expand All @@ -35,7 +35,7 @@ public final class Channel<Type> {
}
}
}

private let handle: Handle
private var buffer = List<ChannelResult<Type>>()

Expand Down Expand Up @@ -66,14 +66,14 @@ public final class Channel<Type> {

handle = result
}

deinit {
hclose(handle)
}

/// Reference to the channel which can only send.
public lazy var sending: Sending = Sending(self)

/// Reference to the channel which can only receive.
public lazy var receiving: Receiving = Receiving(self)

Expand Down Expand Up @@ -125,7 +125,7 @@ public final class Channel<Type> {

return try buffer.removeFirst().getValue()
}

/// This function is used to inform the channel that no more `send` or `receive` should be
/// performed on the channel.
///
Expand All @@ -135,7 +135,7 @@ public final class Channel<Type> {
public func done() {
hdone(handle, 0)
}

/// Send-only reference to an existing channel.
///
/// ## Example:
Expand All @@ -151,27 +151,27 @@ public final class Channel<Type> {
/// ```
public final class Sending {
private let channel: Channel<Type>

fileprivate init(_ channel: Channel<Type>) {
self.channel = channel
}

/// :nodoc:
public func send(_ value: Type, deadline: Deadline) throws {
try channel.send(value, deadline: deadline)
}

/// :nodoc:
public func send(_ error: Error, deadline: Deadline) throws {
try channel.send(error, deadline: deadline)
}

/// :nodoc:
public func done() {
channel.done()
}
}

/// Receive-only reference to an existing channel.
///
/// ## Example:
Expand All @@ -187,16 +187,16 @@ public final class Channel<Type> {
/// ```
public final class Receiving {
private let channel: Channel<Type>

fileprivate init(_ channel: Channel<Type>) {
self.channel = channel
}

/// :nodoc:
@discardableResult public func receive(deadline: Deadline) throws -> Type {
return try channel.receive(deadline: deadline)
}

/// :nodoc:
public func done() {
channel.done()
Expand All @@ -222,7 +222,7 @@ class Node<T> {
var value: T
var next: Node<T>?
weak var previous: Node<T>?

init(value: T) {
self.value = value
}
Expand All @@ -231,48 +231,48 @@ class Node<T> {
fileprivate class List<T> {
private var head: Node<T>?
private var tail: Node<T>?

@discardableResult fileprivate func append(_ value: T) -> Node<T> {
let newNode = Node(value: value)

if let tailNode = tail {
newNode.previous = tailNode
tailNode.next = newNode
} else {
head = newNode
}

tail = newNode
return newNode
}

@discardableResult fileprivate func remove(_ node: Node<T>) -> T {
let prev = node.previous
let next = node.next

if let prev = prev {
prev.next = next
} else {
head = next
}

next?.previous = prev

if next == nil {
tail = prev
}

node.previous = nil
node.next = nil

return node.value
}

@discardableResult fileprivate func removeFirst() throws -> T {
guard let head = head else {
throw VeniceError.unexpectedError
}

return remove(head)
}
}
50 changes: 25 additions & 25 deletions Sources/Venice/Coroutine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import CLibdill
public final class Coroutine {
private typealias Handle = Int32
private let handle: Handle

/// Launches a coroutine that executes the closure passed as argument.
/// The coroutine is executed concurrently, and its lifetime may exceed the lifetime
/// of the caller.
Expand Down Expand Up @@ -91,11 +91,11 @@ public final class Coroutine {

handle = result
}

deinit {
cancel()
}

/// Cancels the coroutine.
///
/// - Warning:
Expand All @@ -104,8 +104,8 @@ public final class Coroutine {
public func cancel() {
hclose(handle)
}
/// Explicitly passes control to other coroutines.

/// Explicitly passes control to other coroutines.
/// By calling this function, you give other coroutines a chance to run.
///
/// You should consider using `Coroutiner.yield()` when doing lengthy computations
Expand All @@ -129,7 +129,7 @@ public final class Coroutine {
/// Thrown when the operation is performed within a canceled coroutine.
public static func yield() throws {
let result = CLibdill.yield()

guard result == 0 else {
switch errno {
case ECANCELED:
Expand Down Expand Up @@ -164,7 +164,7 @@ public final class Coroutine {
/// Thrown when the operation is performed within a canceled coroutine.
public static func wakeUp(_ deadline: Deadline) throws {
let result = msleep(deadline.value)

guard result == 0 else {
switch errno {
case ECANCELED:
Expand All @@ -174,7 +174,7 @@ public final class Coroutine {
}
}
}

/// Coroutine groups are useful for canceling multiple coroutines at the
/// same time.
///
Expand All @@ -196,21 +196,21 @@ public final class Coroutine {
public class Group {
private var coroutines: [Int: Coroutine]
private var finishedCoroutines: Set<Int> = []

private static var id = 0

private static func getNextID() -> Int {
defer {
if id == Int.max {
id = -1
}

id += 1
}

return id
}

/// Creates a new, empty coroutine group with at least the specified number
/// of elements' worth of buffer.
///
Expand Down Expand Up @@ -242,11 +242,11 @@ public final class Coroutine {
public init(minimumCapacity: Int = 0) {
coroutines = [Int: Coroutine](minimumCapacity: minimumCapacity)
}

deinit {
cancel()
}

/// Creates a lightweight coroutine and adds it to the group.
///
/// ## Example:
Expand All @@ -268,48 +268,48 @@ public final class Coroutine {
/// - Returns: Newly created coroutine
@discardableResult public func addCoroutine(body: @escaping () throws -> Void) throws -> Coroutine {
removeFinishedCoroutines()

var finished = false
let id = Group.getNextID()

let coroutine = try Coroutine { [unowned self] in
defer {
finished = true
self.finishedCoroutines.insert(id)
}

try body()
}

if !finished {
coroutines[id] = coroutine
}

return coroutine
}

/// Cancels all coroutines in the group.
///
/// - Warning:
/// Once a coroutine is canceled any coroutine-blocking operation within the coroutine
/// will throw `VeniceError.canceledCoroutine`.
public func cancel() {
removeFinishedCoroutines()

for (id, coroutine) in coroutines {
defer {
coroutines[id] = nil
}

coroutine.cancel()
}
}

private func removeFinishedCoroutines() {
for id in finishedCoroutines {
coroutines[id] = nil
}

finishedCoroutines.removeAll()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Venice/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum VeniceError : Error, Equatable {
case readFailed
/// Thrown when a write operation fails.
case writeFailed

/// Thrown when an unexpected error occurs.
/// This should never happen in the regular flow of an application.
case unexpectedError
Expand Down
Loading