Skip to content

Instantly share code, notes, and snippets.

@dbworku
Created November 29, 2015 21:12
Show Gist options
  • Save dbworku/da3d22d84fc162f3db32 to your computer and use it in GitHub Desktop.
Save dbworku/da3d22d84fc162f3db32 to your computer and use it in GitHub Desktop.
Swift-ful idiomatic extensions of dispatch_semaphore_t
//
// Example.swift
//
// Created by Daniel Worku on 11/16/15.
//
// The MIT License (MIT)
// Copyright (c) 2014 Daniel Worku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import Dispatch
let defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
// Initialize count down from 2
let sema = Semaphore(2)
for index in 1...999 {
dispatch_async(defaultQueue, { () -> Void in
// Intensive task
sema.signal()
})
sema.wait(DISPATCH_TIME_FOREVER)
}
//
// Semaphore.swift
//
// Created by Daniel Worku on 11/16/15.
//
// The MIT License (MIT)
// Copyright (c) 2014 Daniel Worku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import Foundation
/**
Pretty version of `dispatch_semaphore_create()` with a value that defaults to `0`
- parameter value: A `Int` starting value for the semaphore.
- returns: The newly created `dispatch_semaphore_t`
*/
public func Semaphore(value: UInt = 0) -> dispatch_semaphore_t {
return dispatch_semaphore_create(Int(value))
}
// MARK: - Dispatch
extension dispatch_semaphore_t {
/*!
Increments the semaphore to signal that it is no longer being held by the current thread
- returns: A `Bool` with `true` if thread is woken
*/
final func signal() -> Bool {
return dispatch_semaphore_signal(self) != 0
}
/**
Blocks the thread until the semaphore is free or the timeout passes
- parameter timeout: A `dispatch_time_t ` to delay execution. Should be in the future.
- returns: A `Bool` with `true` on success and `false` on timeout
*/
final func wait(timeout: dispatch_time_t) -> Bool {
return dispatch_semaphore_wait(self, timeout) != 0
}
/*!
Blocks the thread until the semaphore is free.
- warning Will block the thread indefinitely if you don't signal the semaphore
*/
final func waitFovever() {
wait(DISPATCH_TIME_FOREVER)
}
/*!
Blocks the thread until the semaphore is free or the timeout passes
- parameter timeout: A `Int64` of nanoseconds to delay execution
- returns: A `Bool` with `true` on success and `false` on timeout
*/
final func wait(nanosecondTimeout: Int64) -> Bool {
return wait(dispatch_time(DISPATCH_TIME_NOW, nanosecondTimeout))
}
}
// MARK: - Foundation
extension dispatch_semaphore_t {
/*!
Blocks the thread until the semaphore is free or the timeout passes
- parameter timeout: A `NSTimeInterval` to delay execution
- returns: A `Bool` with `true` on success and `false` on timeout
*/
final func wait(secondsTimeout: NSTimeInterval) -> Bool {
return wait(Int64(secondsTimeout) * Int64(NSEC_PER_SEC))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment