Переглянути джерело

add new AtomicBoolean class to handle access to a boolean from different threads

cyberta 2 роки тому
батько
коміт
71536afe1b

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
 		21D6C941260623F500D0755A /* NotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D6C9392606190600D0755A /* NotificationManager.swift */; };
+		3001B560294265C200816B0C /* AtomicBoolean.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3001B55F294265C200816B0C /* AtomicBoolean.swift */; };
 		3008CB7224F93EB900E6A617 /* AudioMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3008CB7124F93EB900E6A617 /* AudioMessageCell.swift */; };
 		3008CB7424F9436C00E6A617 /* AudioPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3008CB7324F9436C00E6A617 /* AudioPlayerView.swift */; };
 		3008CB7624F95B6D00E6A617 /* AudioController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3008CB7524F95B6D00E6A617 /* AudioController.swift */; };
@@ -253,6 +254,7 @@
 		21D6C9392606190600D0755A /* NotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManager.swift; sourceTree = "<group>"; };
 		21EE28844E7A690D73BF5285 /* Pods-deltachat-iosTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-iosTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-iosTests/Pods-deltachat-iosTests.debug.xcconfig"; sourceTree = "<group>"; };
 		2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_iosTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
+		3001B55F294265C200816B0C /* AtomicBoolean.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicBoolean.swift; sourceTree = "<group>"; };
 		3008CB7124F93EB900E6A617 /* AudioMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioMessageCell.swift; sourceTree = "<group>"; };
 		3008CB7324F9436C00E6A617 /* AudioPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayerView.swift; sourceTree = "<group>"; };
 		3008CB7524F95B6D00E6A617 /* AudioController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioController.swift; sourceTree = "<group>"; };
@@ -1000,6 +1002,7 @@
 				30E83EFC289BF32C0035614C /* ShortcutManager.swift */,
 				30CE137728D9C40700158DF4 /* ChatDropInteraction.swift */,
 				30E6E359293FEEA70093871E /* SimpleLogger.swift */,
+				3001B55F294265C200816B0C /* AtomicBoolean.swift */,
 			);
 			path = Helper;
 			sourceTree = "<group>";
@@ -1533,6 +1536,7 @@
 				21D6C941260623F500D0755A /* NotificationManager.swift in Sources */,
 				3080A023277DE09900E74565 /* SeparatorLine.swift in Sources */,
 				302B84C72396770B001C261F /* RelayHelper.swift in Sources */,
+				3001B560294265C200816B0C /* AtomicBoolean.swift in Sources */,
 				305961CF2346125100C80F33 /* UIColor+Extensions.swift in Sources */,
 				AEACE2E51FB32E1900DCDD78 /* Utils.swift in Sources */,
 				3052C60E253F088E007D13EA /* DetectorType.swift in Sources */,

+ 29 - 0
deltachat-ios/Helper/AtomicBoolean.swift

@@ -0,0 +1,29 @@
+import Foundation
+
+public class AtomicBoolean {
+    private var val: UInt8 = 0
+    public init(initialValue: Bool) {
+        self.val = (initialValue == false ? 0 : 1)
+    }
+
+
+    public func set(value: Bool) {
+        if value {
+            OSAtomicTestAndSet(7, &val)
+        } else {
+            OSAtomicTestAndClear(7, &val)
+        }
+    }
+    
+  public func getAndSet(value: Bool) -> Bool {
+    if value {
+      return  OSAtomicTestAndSet(7, &val)
+    } else {
+      return  OSAtomicTestAndClear(7, &val)
+    }
+  }
+
+  public func get() -> Bool {
+    return val != 0
+  }
+}