Last active
January 4, 2016 09:06
-
-
Save kyleve/3331e1afb9c1b8c2e36f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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