Skip to content

Commit

Permalink
Merge pull request mozilla#1503 from nesium/fix-swift-callback-interf…
Browse files Browse the repository at this point in the history
…ace-race

Fix race condition affecting callback interfaces in generated Swift code
  • Loading branch information
bendk authored Mar 30, 2023
2 parents e5b99ac + d964ed0 commit 41e94c0
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,15 @@ fileprivate let {{ foreign_callback }} : ForeignCallback =

// FfiConverter protocol for callback interfaces
fileprivate struct {{ ffi_converter_name }} {
// Initialize our callback method with the scaffolding code
private static var callbackInitialized = false
private static func initCallback() {
private static let initCallbackOnce: () = {
// Swift ensures this initializer code will once run once, even when accessed by multiple threads.
try! rustCall { (err: UnsafeMutablePointer<RustCallStatus>) in
{{ cbi.ffi_init_callback().name() }}({{ foreign_callback }}, err)
{{ cbi.ffi_init_callback().name() }}({{ foreign_callback }}, err)
}
}
}()

private static func ensureCallbackinitialized() {
if !callbackInitialized {
initCallback()
callbackInitialized = true
}
_ = initCallbackOnce
}

static func drop(handle: UniFFICallbackHandle) {
Expand Down

0 comments on commit 41e94c0

Please sign in to comment.