Skip to content

[stdlib] Condition Mutex.swift on platform #74143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion stdlib/public/Synchronization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ set(SWIFT_SYNCHRONIZATION_SOURCES
${SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES}

Cell.swift
Mutex/Mutex.swift
)

set(SWIFT_SYNCHRONIZATION_GYB_SOURCES
Expand All @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions stdlib/public/Synchronization/Mutex/MutexUnavailable.swift
Original file line number Diff line number Diff line change
@@ -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<Value: ~Copyable>: ~Copyable {}