Skip to content

Instantly share code, notes, and snippets.

@kyleve
Last active January 4, 2016 09:06
Show Gist options
  • Save kyleve/3331e1afb9c1b8c2e36f to your computer and use it in GitHub Desktop.
Save kyleve/3331e1afb9c1b8c2e36f to your computer and use it in GitHub Desktop.
public func Synchronize(semaphore : AnyObject, @noescape block : Void throws -> Void) rethrows -> Void
{
objc_sync_enter(semaphore)
defer { objc_sync_exit(semaphore) }
try block()
}
@warn_unused_result
public func Synchronize<Type>(semaphore : AnyObject, @noescape block : Void throws -> Type) rethrows -> Type
{
objc_sync_enter(semaphore)
defer { objc_sync_exit(semaphore) }
return try block()
}
func example()
{
let semaphore = NSObject()
let value = Synchronize(semaphore) {
return someFuncThatNeedsLocking()
}
Synchronize(semaphore) {
someOtherFuncThatNeedsLocking()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment