mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
24 lines
441 B
Swift
24 lines
441 B
Swift
//
|
|
// Mutex.swift
|
|
// App
|
|
//
|
|
// Created by EYHN on 2025/1/11.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class Mutex<Wrapped>: @unchecked Sendable {
|
|
private let lock = NSLock.init()
|
|
private var wrapped: Wrapped
|
|
|
|
init(_ wrapped: Wrapped) {
|
|
self.wrapped = wrapped
|
|
}
|
|
|
|
func withLock<R>(_ body: @Sendable (inout Wrapped) throws -> R) rethrows -> R {
|
|
self.lock.lock()
|
|
defer { self.lock.unlock() }
|
|
return try body(&wrapped)
|
|
}
|
|
}
|