Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Aug 13, 2024
1 parent c4ffeb3 commit 7605549
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions Sources/ConcurrencyExtras/Mutex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@
/// A synchronization primitive that protects shared mutable state via mutual exclusion.
///
/// A back-port of Swift's `Mutex` type for wider platform availability.
@frozen
@_staticExclusiveOnly
@available(iOS, obsoleted: 18, message: "Use 'Synchronization.Mutex', instead.")
@available(macOS, obsoleted: 15, message: "Use 'Synchronization.Mutex', instead.")
@available(tvOS, obsoleted: 18, message: "Use 'Synchronization.Mutex', instead.")
@available(visionOS, obsoleted: 2, message: "Use 'Synchronization.Mutex', instead.")
@available(watchOS, obsoleted: 11, message: "Use 'Synchronization.Mutex', instead.")
public struct Mutex<Value: ~Copyable>: ~Copyable {
@usableFromInline
let _lock = NSLock()

@usableFromInline
let _box: Box
private let _lock = NSLock()
private let _box: Box

/// Initializes a value of this mutex with the given initial state.
///
/// - Parameter initialValue: The initial value to give to the mutex.
@_transparent
public init(_ initialValue: consuming sending Value) {
_box = Box(initialValue)
}

@usableFromInline
final class Box {
@usableFromInline
private final class Box {
var value: Value
@usableFromInline
init(_ initialValue: consuming sending Value) {
value = initialValue
}
Expand All @@ -41,7 +33,6 @@

extension Mutex where Value: ~Copyable {
/// Calls the given closure after acquiring the lock and then releases ownership.
@_transparent
public borrowing func withLock<Result: ~Copyable, E: Error>(
_ body: (inout sending Value) throws(E) -> sending Result
) throws(E) -> sending Result {
Expand All @@ -51,7 +42,6 @@
}

/// Attempts to acquire the lock and then calls the given closure if successful.
@_transparent
public borrowing func withLockIfAvailable<Result: ~Copyable, E: Error>(
_ body: (inout sending Value) throws(E) -> sending Result
) throws(E) -> sending Result? {
Expand All @@ -62,17 +52,14 @@
}

extension Mutex where Value == Void {
@_transparent
public borrowing func _unsafeLock() {
_lock.lock()
}

@_transparent
public borrowing func _unsafeTryLock() -> Bool {
_lock.try()
}

@_transparent
public borrowing func _unsafeUnlock() {
_lock.unlock()
}
Expand Down

0 comments on commit 7605549

Please sign in to comment.