From d298d8a1993171520e0227de1a5e495fee9c7628 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 4 Mar 2025 20:49:52 +0100 Subject: [PATCH] fix: added queue to GPU list to prevent data race (#2417) --- Modules/GPU/main.swift | 26 ++++++++++++++++++++++++-- Stats/Supporting Files/Info.plist | 2 +- Widgets/Supporting Files/Info.plist | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Modules/GPU/main.swift b/Modules/GPU/main.swift index 3c57a66e..9d781b17 100644 --- a/Modules/GPU/main.swift +++ b/Modules/GPU/main.swift @@ -54,8 +54,30 @@ public struct GPU_Info: Codable { } } -public struct GPUs: Codable { - public var list: [GPU_Info] = [] +public class GPUs: Codable { + private var queue: DispatchQueue = DispatchQueue(label: "eu.exelban.Stats.GPU.SynchronizedArray") + + private var _list: [GPU_Info] = [] + public var list: [GPU_Info] { + get { self.queue.sync { self._list } } + set { self.queue.sync { self._list = newValue } } + } + + enum CodingKeys: String, CodingKey { + case list + } + + required public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.list = try container.decode(Array.self, forKey: CodingKeys.list) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(list, forKey: .list) + } + + init() {} internal func active() -> [GPU_Info] { return self.list.filter{ $0.state && $0.utilization != nil }.sorted{ $0.utilization ?? 0 > $1.utilization ?? 0 } diff --git a/Stats/Supporting Files/Info.plist b/Stats/Supporting Files/Info.plist index 80535eb4..bc4965f4 100755 --- a/Stats/Supporting Files/Info.plist +++ b/Stats/Supporting Files/Info.plist @@ -17,7 +17,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 679 + 680 Description Simple macOS system monitor in your menu bar LSApplicationCategoryType diff --git a/Widgets/Supporting Files/Info.plist b/Widgets/Supporting Files/Info.plist index 3278cad4..1078e9f7 100644 --- a/Widgets/Supporting Files/Info.plist +++ b/Widgets/Supporting Files/Info.plist @@ -13,7 +13,7 @@ CFBundleShortVersionString 2.11.33 CFBundleVersion - 679 + 680 NSExtension NSExtensionPointIdentifier