mirror of
https://github.com/GLEGram/GLEGram-iOS.git
synced 2026-04-30 14:57:53 +02:00
4647310322
Based on Swiftgram 12.5 (Telegram iOS 12.5). All GLEGram features ported and organized in GLEGram/ folder. Features: Ghost Mode, Saved Deleted Messages, Content Protection Bypass, Font Replacement, Fake Profile, Chat Export, Plugin System, and more. See CHANGELOG_12.5.md for full details.
58 lines
1.3 KiB
Swift
58 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
public enum AtomicLockError: Error {
|
|
case isLocked
|
|
}
|
|
|
|
public final class Atomic<T> {
|
|
private var lock: pthread_mutex_t
|
|
private var value: T
|
|
|
|
public init(value: T) {
|
|
self.lock = pthread_mutex_t()
|
|
self.value = value
|
|
|
|
pthread_mutex_init(&self.lock, nil)
|
|
}
|
|
|
|
deinit {
|
|
pthread_mutex_destroy(&self.lock)
|
|
}
|
|
|
|
public func with<R>(_ f: (T) -> R) -> R {
|
|
pthread_mutex_lock(&self.lock)
|
|
let result = f(self.value)
|
|
pthread_mutex_unlock(&self.lock)
|
|
|
|
return result
|
|
}
|
|
|
|
public func tryWith<R>(_ f: (T) -> R) throws -> R {
|
|
if pthread_mutex_trylock(&self.lock) == 0 {
|
|
let result = f(self.value)
|
|
pthread_mutex_unlock(&self.lock)
|
|
return result
|
|
} else {
|
|
throw AtomicLockError.isLocked
|
|
}
|
|
}
|
|
|
|
public func modify(_ f: (T) -> T) -> T {
|
|
pthread_mutex_lock(&self.lock)
|
|
let result = f(self.value)
|
|
self.value = result
|
|
pthread_mutex_unlock(&self.lock)
|
|
|
|
return result
|
|
}
|
|
|
|
public func swap(_ value: T) -> T {
|
|
pthread_mutex_lock(&self.lock)
|
|
let previous = self.value
|
|
self.value = value
|
|
pthread_mutex_unlock(&self.lock)
|
|
|
|
return previous
|
|
}
|
|
}
|