Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Created July 31, 2015 02:32
Show Gist options
  • Save jtbandes/a5ce62019585dd4f998e to your computer and use it in GitHub Desktop.
Save jtbandes/a5ce62019585dd4f998e to your computer and use it in GitHub Desktop.
//: Convenience functions/extension on top of GCD.
import Dispatch
var MainQueue: dispatch_queue_t { return dispatch_get_main_queue() }
func GlobalQueue(qos: dispatch_qos_class_t = .Default) -> dispatch_queue_t
{
return dispatch_get_global_queue(qos, 0)
}
enum DispatchError: ErrorType
{
case Timeout
}
extension dispatch_qos_class_t
{
static var UserInteractive: dispatch_qos_class_t { return QOS_CLASS_USER_INTERACTIVE }
static var UserInitiated: dispatch_qos_class_t { return QOS_CLASS_USER_INITIATED }
static var Default: dispatch_qos_class_t { return QOS_CLASS_DEFAULT }
static var Utility: dispatch_qos_class_t { return QOS_CLASS_UTILITY }
static var Background: dispatch_qos_class_t { return QOS_CLASS_BACKGROUND }
static var Unspecified: dispatch_qos_class_t { return QOS_CLASS_UNSPECIFIED }
}
extension dispatch_time_t
{
static var Now: dispatch_time_t { return DISPATCH_TIME_NOW }
static var Forever: dispatch_time_t { return DISPATCH_TIME_FOREVER }
static func After<T: IntegerType>(sec: T) -> dispatch_time_t
{
return dispatch_time(DISPATCH_TIME_NOW, Int64(sec.toIntMax() * 1_000_000_000))
}
static func After(sec: Double) -> dispatch_time_t
{
return dispatch_time(DISPATCH_TIME_NOW, Int64(sec * 1_000_000_000))
}
}
extension dispatch_semaphore_t
{
final func signal() { dispatch_semaphore_signal(self) }
final func waitForever()
{
let result = dispatch_semaphore_wait(self, .Forever)
assert(result == 0, "failed to wait on semaphore")
}
final func wait(timeout timeout: dispatch_time_t = .Forever) throws
{
if dispatch_semaphore_wait(self, timeout) != 0 { throw DispatchError.Timeout }
}
final func with<Result>(@noescape body: Void throws -> Result) rethrows -> Result
{
waitForever()
defer { signal() }
return try body()
}
final func with<Result>(timeout timeout: dispatch_time_t = .Forever, @noescape _ body: Void throws -> Result) throws -> Result
{
try wait(timeout: timeout)
defer { signal() }
return try body()
}
}
extension dispatch_queue_t
{
final func sync(block: dispatch_block_t) { dispatch_sync(self, block) }
final func async(block: dispatch_block_t) { dispatch_async(self, block) }
final func async(group: dispatch_group_t, _ block: dispatch_block_t)
{
dispatch_group_async(group, self, block)
}
final func after<T: IntegerType>(sec: T, _ block: dispatch_block_t)
{
dispatch_after(.After(sec), self, block)
}
final func after(sec: Double, _ block: dispatch_block_t)
{
dispatch_after(.After(sec), self, block)
}
}
extension dispatch_group_t
{
final func enter() { dispatch_group_enter(self) }
final func leave() { dispatch_group_leave(self) }
final func notify(queue: dispatch_queue_t, _ block: dispatch_block_t)
{
dispatch_group_notify(self, queue, block)
}
final func waitForever()
{
let result = dispatch_group_wait(self, .Forever)
assert(result == 0, "failed to wait on semaphore")
}
final func wait(timeout timeout: dispatch_time_t = .Forever) throws
{
if dispatch_group_wait(self, timeout) != 0 { throw DispatchError.Timeout }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment