Skip to content

Commit 95d5d71

Browse files
committed
[embedded] Add asserts on predicate value into swift_once
1 parent b057417 commit 95d5d71

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,13 @@ public func swift_endAccess(buffer: UnsafeMutableRawPointer) {
241241

242242
@_silgen_name("swift_once")
243243
public func swift_once(predicate: UnsafeMutablePointer<Int>, fn: (@convention(c) (UnsafeMutableRawPointer)->()), context: UnsafeMutableRawPointer) {
244-
if loadAcquire(predicate) < 0 { return }
244+
let checkedLoadAcquire = { predicate in
245+
let value = loadAcquire(predicate)
246+
assert(value == -1 || value == 0 || value == 1)
247+
return value
248+
}
249+
250+
if checkedLoadAcquire(predicate) < 0 { return }
245251

246252
let won = compareExchangeRelaxed(predicate, expectedOldValue: 0, desiredNewValue: 1)
247253
if won {
@@ -251,7 +257,7 @@ public func swift_once(predicate: UnsafeMutablePointer<Int>, fn: (@convention(c)
251257
}
252258

253259
// TODO: This should really use an OS provided lock
254-
while loadAcquire(predicate) >= 0 {
260+
while checkedLoadAcquire(predicate) >= 0 {
255261
// spin
256262
}
257263
}

0 commit comments

Comments
 (0)