mirror of
https://github.com/apple/swift.git
synced 2026-11-06 00:14:32 +01:00
Add SwiftMutexFlags to the EmbeddedPlatform mutex hook so callers can request checked and recursive mutex behavior without introducing a separate recursive mutex entrypoint. Update the single-threaded EmbeddedPlatform implementation to track recursive lock depth, update the embedded Synchronization mutex hook declaration to match the flags-shaped ABI, and extend the embedded mutex tests to cover recursive checked behavior. Also avoid compiling the continuation-checking LazyMutex state in embedded Concurrency builds, where unchecked-continuation tracking is disabled.
77 lines
2.3 KiB
Swift
77 lines
2.3 KiB
Swift
//===----------- EmbeddedPlatformSingleThreaded.swift ---------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
fileprivate struct SingleThreadedMutex {
|
|
var checked: Bool
|
|
var recursive: Bool
|
|
var lockCount: UInt
|
|
}
|
|
|
|
@implementation @c
|
|
public func _swift_mutex_init(
|
|
_ mutex: UnsafeMutableRawPointer,
|
|
_ flags: SwiftMutexFlags
|
|
) {
|
|
let storage = mutex.assumingMemoryBound(to: SingleThreadedMutex.self)
|
|
storage.pointee = SingleThreadedMutex(
|
|
checked: flags.contains(.checked),
|
|
recursive: flags.contains(.recursive),
|
|
lockCount: 0)
|
|
}
|
|
|
|
@implementation @c
|
|
public func _swift_mutex_destroy(_ mutex: UnsafeMutableRawPointer) {
|
|
let storage = mutex.assumingMemoryBound(to: SingleThreadedMutex.self)
|
|
if storage.pointee.checked && storage.pointee.lockCount != 0 {
|
|
fatalError("destroying a locked mutex")
|
|
}
|
|
|
|
storage.pointee = SingleThreadedMutex(
|
|
checked: false,
|
|
recursive: false,
|
|
lockCount: 0)
|
|
}
|
|
|
|
@implementation @c
|
|
public func _swift_mutex_lock(_ mutex: UnsafeMutableRawPointer) {
|
|
let storage = mutex.assumingMemoryBound(to: SingleThreadedMutex.self)
|
|
if storage.pointee.checked {
|
|
if storage.pointee.lockCount != 0 && !storage.pointee.recursive {
|
|
fatalError("locking an already locked mutex")
|
|
}
|
|
storage.pointee.lockCount += 1
|
|
}
|
|
}
|
|
|
|
@implementation @c
|
|
public func _swift_mutex_unlock(_ mutex: UnsafeMutableRawPointer) {
|
|
let storage = mutex.assumingMemoryBound(to: SingleThreadedMutex.self)
|
|
if storage.pointee.checked {
|
|
if storage.pointee.lockCount == 0 {
|
|
fatalError("unlocking an unlocked mutex")
|
|
}
|
|
storage.pointee.lockCount -= 1
|
|
}
|
|
}
|
|
|
|
@implementation @c
|
|
public func _swift_mutex_tryLock(_ mutex: UnsafeMutableRawPointer) -> Int {
|
|
let storage = mutex.assumingMemoryBound(to: SingleThreadedMutex.self)
|
|
if storage.pointee.checked {
|
|
if storage.pointee.lockCount != 0 && !storage.pointee.recursive {
|
|
return 0
|
|
}
|
|
storage.pointee.lockCount += 1
|
|
}
|
|
return 1
|
|
}
|