mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-02-03 20:37:34 +01:00
Make ArticleStatus thread-safe and mark it as @unchecked Sendable.
This commit is contained in:
parent
f613340f2f
commit
2061adf595
@ -7,14 +7,15 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import os
|
||||||
|
|
||||||
// Threading rules:
|
/// Read and starred status for an Article.
|
||||||
// * Main-thread only
|
///
|
||||||
// * Except: may be created on background thread by StatusesTable.
|
/// These are uniqued — there is never more than one instance per articleID.
|
||||||
// Which is safe, because at creation time it’s not yet shared,
|
///
|
||||||
// and it won’t be mutated ever on a background thread.
|
/// Its two Bool properties, `read` and `starred`, are both protected
|
||||||
|
/// by an internal lock, which makes `ArticleStatus` thread-safe.
|
||||||
public final class ArticleStatus: Hashable {
|
public final class ArticleStatus: Hashable, @unchecked Sendable {
|
||||||
|
|
||||||
public enum Key: String {
|
public enum Key: String {
|
||||||
case read = "read"
|
case read = "read"
|
||||||
@ -24,14 +25,54 @@ public final class ArticleStatus: Hashable {
|
|||||||
public let articleID: String
|
public let articleID: String
|
||||||
public let dateArrived: Date
|
public let dateArrived: Date
|
||||||
|
|
||||||
public var read = false
|
// Sharing one lock for all instances is preferred to having one (or two)
|
||||||
public var starred = false
|
// locks per instance — that could means thousands of locks in memory.
|
||||||
|
private static let lock = OSAllocatedUnfairLock()
|
||||||
|
|
||||||
|
public var read: Bool {
|
||||||
|
get {
|
||||||
|
Self.lock.lock()
|
||||||
|
defer {
|
||||||
|
Self.lock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return _read
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
Self.lock.lock()
|
||||||
|
defer {
|
||||||
|
Self.lock.unlock()
|
||||||
|
}
|
||||||
|
_read = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var starred: Bool {
|
||||||
|
get {
|
||||||
|
Self.lock.lock()
|
||||||
|
defer {
|
||||||
|
Self.lock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return _starred
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
Self.lock.lock()
|
||||||
|
defer {
|
||||||
|
Self.lock.unlock()
|
||||||
|
}
|
||||||
|
_starred = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var _read = false
|
||||||
|
private var _starred = false
|
||||||
|
|
||||||
public init(articleID: String, read: Bool, starred: Bool, dateArrived: Date) {
|
public init(articleID: String, read: Bool, starred: Bool, dateArrived: Date) {
|
||||||
self.articleID = articleID
|
self.articleID = articleID
|
||||||
self.read = read
|
|
||||||
self.starred = starred
|
|
||||||
self.dateArrived = dateArrived
|
self.dateArrived = dateArrived
|
||||||
|
self._read = read
|
||||||
|
self._starred = starred
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(articleID: String, read: Bool, dateArrived: Date) {
|
public convenience init(articleID: String, read: Bool, dateArrived: Date) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user