Module: Perfect-Thread
struct Threading
A wrapper around a variety of threading related functions and classes.
static var noTimeout = 0.0
Indicates that the call should have no timeout.
typealias typealias ThreadClosure = () -> ()
The function type which can be given to `Threading.dispatch`.
extension Threading
class Lock
A mutex-type thread lock.
The lock can be held by only one thread. Other threads attempting to secure the lock while it is held will block.
The lock is initialized as being recursive. The locking thread may lock multiple times, but each lock should be accompanied by an unlock.
func init()
Initialize a new lock object.
func lock() -> Bool
Attempt to grab the lock.
Returns true if the lock was successful.
func tryLock() -> Bool
Attempt to grab the lock.
Will only return true if the lock was not being held by any other thread.
Returns false if the lock is currently being held by another thread.
func unlock() -> Bool
Unlock. Returns true if the lock was held by the current thread and was successfully unlocked. ior the lock count was decremented.
func doWithLock(closure: () throws -> ()) rethrows
Acquire the lock, execute the closure, release the lock.
extension Threading
class Event: Lock
A thread event object. Inherits from `Threading.Lock`.
The event MUST be locked before `wait` or `signal` is called.
While inside the `wait` call, the event is automatically placed in the unlocked state.
After `wait` or `signal` return the event will be in the locked state and must be unlocked.
func override public init()
Initialize a new Event object.
func signal() -> Bool
Signal at most ONE thread which may be waiting on this event.
Has no effect if there is no waiting thread.
func broadcast() -> Bool
Signal ALL threads which may be waiting on this event.
Has no effect if there is no waiting thread.
func wait(seconds secs: Double = Threading.noTimeout) -> Bool
Wait on this event for another thread to call signal.
Blocks the calling thread until a signal is received or the timeout occurs.
Returns true only if the signal was received.
Returns false upon timeout or error.
extension Threading
class RWLock
A read-write thread lock.
Permits multiple readers to hold the while, while only allowing at most one writer to hold the lock.
For a writer to acquire the lock all readers must have unlocked.
For a reader to acquire the lock no writers must hold the lock.
func init()
Initialize a new read-write lock.
func readLock() -> Bool
Attempt to acquire the lock for reading.
Returns false if an error occurs.
func tryReadLock() -> Bool
Attempts to acquire the lock for reading.
Returns false if the lock is held by a writer or an error occurs.
func writeLock() -> Bool
Attempt to acquire the lock for writing.
Returns false if an error occurs.
func tryWriteLock() -> Bool
Attempt to acquire the lock for writing.
Returns false if the lock is held by readers or a writer or an error occurs.
func unlock() -> Bool
Unlock a lock which is held for either reading or writing.
Returns false if an error occurs.
func doWithReadLock(closure: () throws -> ()) rethrows
Acquire the read lock, execute the closure, release the lock.
func doWithWriteLock(closure: () throws -> ()) rethrows
Acquire the write lock, execute the closure, release the lock.
extension Threading
static func func sleep(seconds inSeconds: Double)
Block the current thread for the indicated time.
protocol ThreadQueue
A thread queue which can dispatch a closure according to the queue type.
extension Threading
enum QueueType
Queue type indicator.
case serial
A queue which operates on only one thread.
case concurrent
A queue which operates on a number of threads, usually equal to the number of logical CPUs.
static func func dispatch(closure: @escaping Threading.ThreadClosure)
Call the given closure on the "default" concurrent queue
Returns immediately.