Use Mutex.

This commit is contained in:
Brent Simmons committed 2025-11-10 22:27:56 -08:00
1 parent ffccb12d7d
commit 0da1713cb6
1 file changed
+12 -9
@@ -7,7 +7,7 @@
//
import Foundation
import os
import Synchronization
nonisolated public final class ArticleStatus: Hashable, Sendable {
@@ -19,31 +19,34 @@ nonisolated public final class ArticleStatus: Hashable, Sendable {
public let articleID: String
public let dateArrived: Date
private let _read: OSAllocatedUnfairLock<Bool>
private let _starred: OSAllocatedUnfairLock<Bool>
private struct State: Sendable {
var read: Bool
var starred: Bool
}
private let state: Mutex<State>
public var read: Bool {
get {
_read.withLock { $0 }
state.withLock { $0.read }
}
set {
_read.withLock { $0 = newValue }
state.withLock { $0.read = newValue }
}
}
public var starred: Bool {
get {
_starred.withLock { $0 }
state.withLock { $0.starred }
}
set {
_starred.withLock { $0 = newValue }
state.withLock { $0.starred = newValue }
}
}
public init(articleID: String, read: Bool, starred: Bool, dateArrived: Date) {
self.articleID = articleID
self._read = OSAllocatedUnfairLock(initialState: read)
self._starred = OSAllocatedUnfairLock(initialState: starred)
self.state = Mutex(State(read: read, starred: starred))
self.dateArrived = dateArrived
}