Skip to content

Instantly share code, notes, and snippets.

@alexpersian
Last active July 3, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpersian/45677034bccce2a17432921846a61011 to your computer and use it in GitHub Desktop.
Save alexpersian/45677034bccce2a17432921846a61011 to your computer and use it in GitHub Desktop.
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func dispatchOnce(_ predicate: inout Bool, _ block: @noescape () throws -> Void) rethrows {
objc_sync_enter(predicate)
if !predicate {
do {
predicate = true
try block()
} catch let error {
objc_sync_exit(predicate)
thow error
}
}
objc_sync_exit(predicate)
}
testDispatchOnce(dispatchOnce)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment