diff --git a/stdlib/public/Synchronization/CMakeLists.txt b/stdlib/public/Synchronization/CMakeLists.txt index 9d9ec0524cd2a..90bb76ae4fe6c 100644 --- a/stdlib/public/Synchronization/CMakeLists.txt +++ b/stdlib/public/Synchronization/CMakeLists.txt @@ -26,7 +26,6 @@ set(SWIFT_SYNCHRONIZATION_SOURCES ${SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES} Cell.swift - Mutex/Mutex.swift ) set(SWIFT_SYNCHRONIZATION_GYB_SOURCES @@ -38,24 +37,28 @@ set(SWIFT_SYNCHRONIZATION_GYB_SOURCES set(SWIFT_SYNCHRONIZATION_DARWIN_SOURCES Mutex/DarwinImpl.swift + Mutex/Mutex.swift ) # Linux sources set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES Mutex/LinuxImpl.swift + Mutex/Mutex.swift Mutex/SpinLoopHint.swift ) # Wasm sources set(SWIFT_SYNCHRONIZATION_WASM_SOURCES + Mutex/Mutex.swift Mutex/WasmImpl.swift ) # Windows sources set(SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES + Mutex/Mutex.swift Mutex/WindowsImpl.swift ) @@ -89,6 +92,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES ${SWIFT_SYNCHRONIZATION_WASM_SOURCES} SWIFT_SOURCES_DEPENDS_WINDOWS ${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES} + SWIFT_SOURCES_DEPENDS_FREESTANDING + Mutex/MutexUnavailable.swift SWIFT_MODULE_DEPENDS_OSX Darwin diff --git a/stdlib/public/Synchronization/Mutex/MutexUnavailable.swift b/stdlib/public/Synchronization/Mutex/MutexUnavailable.swift new file mode 100644 index 0000000000000..f44144eea5130 --- /dev/null +++ b/stdlib/public/Synchronization/Mutex/MutexUnavailable.swift @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Atomics open source project +// +// Copyright (c) 2024 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 +// +//===----------------------------------------------------------------------===// + +/// A synchronization primitive that protects shared mutable state via +/// mutual exclusion. +/// +/// The `Mutex` type offers non-recursive exclusive access to the state +/// it is protecting by blocking threads attempting to acquire the lock. +/// Only one execution context at a time has access to the value stored +/// within the `Mutex` allowing for exclusive access. +/// +/// An example use of `Mutex` in a class used simultaneously by many +/// threads protecting a `Dictionary` value: +/// +/// class Manager { +/// let cache = Mutex<[Key: Resource]>([:]) +/// +/// func saveResouce(_ resource: Resouce, as key: Key) { +/// cache.withLock { +/// $0[key] = resource +/// } +/// } +/// } +/// +@available(unavailable, *, message: "Mutex is not available on this platform") +@frozen +@_staticExclusiveOnly +public struct Mutex: ~Copyable {}