mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-15 18:59:54 +00:00
Merge branch 'tmp'
This commit is contained in:
commit
c3b00d270b
@ -3,6 +3,7 @@
|
||||
@interface SAtomic : NSObject
|
||||
|
||||
- (instancetype)initWithValue:(id)value;
|
||||
- (instancetype)initWithValue:(id)value recursive:(bool)recursive;
|
||||
- (id)swap:(id)newValue;
|
||||
- (id)value;
|
||||
- (id)modify:(id (^)(id))f;
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
#import "SAtomic.h"
|
||||
|
||||
#import <libkern/OSAtomic.h>
|
||||
#import <pthread.h>
|
||||
|
||||
@interface SAtomic ()
|
||||
{
|
||||
volatile OSSpinLock _lock;
|
||||
pthread_mutex_t _lock;
|
||||
pthread_mutexattr_t _attr;
|
||||
bool _isRecursive;
|
||||
id _value;
|
||||
}
|
||||
|
||||
@ -17,27 +19,54 @@
|
||||
self = [super init];
|
||||
if (self != nil)
|
||||
{
|
||||
pthread_mutex_init(&_lock, NULL);
|
||||
_value = value;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithValue:(id)value recursive:(bool)recursive {
|
||||
self = [super init];
|
||||
if (self != nil)
|
||||
{
|
||||
_isRecursive = recursive;
|
||||
|
||||
if (recursive) {
|
||||
pthread_mutexattr_init(&_attr);
|
||||
pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&_lock, &_attr);
|
||||
} else {
|
||||
pthread_mutex_init(&_lock, NULL);
|
||||
}
|
||||
|
||||
_value = value;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
if (_isRecursive) {
|
||||
pthread_mutexattr_destroy(&_attr);
|
||||
}
|
||||
pthread_mutex_destroy(&_lock);
|
||||
}
|
||||
|
||||
- (id)swap:(id)newValue
|
||||
{
|
||||
id previousValue = nil;
|
||||
OSSpinLockLock(&_lock);
|
||||
pthread_mutex_lock(&_lock);
|
||||
previousValue = _value;
|
||||
_value = newValue;
|
||||
OSSpinLockUnlock(&_lock);
|
||||
pthread_mutex_unlock(&_lock);
|
||||
return previousValue;
|
||||
}
|
||||
|
||||
- (id)value
|
||||
{
|
||||
id previousValue = nil;
|
||||
OSSpinLockLock(&_lock);
|
||||
pthread_mutex_lock(&_lock);
|
||||
previousValue = _value;
|
||||
OSSpinLockUnlock(&_lock);
|
||||
pthread_mutex_unlock(&_lock);
|
||||
|
||||
return previousValue;
|
||||
}
|
||||
@ -45,19 +74,19 @@
|
||||
- (id)modify:(id (^)(id))f
|
||||
{
|
||||
id newValue = nil;
|
||||
OSSpinLockLock(&_lock);
|
||||
pthread_mutex_lock(&_lock);
|
||||
newValue = f(_value);
|
||||
_value = newValue;
|
||||
OSSpinLockUnlock(&_lock);
|
||||
pthread_mutex_unlock(&_lock);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
- (id)with:(id (^)(id))f
|
||||
{
|
||||
id result = nil;
|
||||
OSSpinLockLock(&_lock);
|
||||
pthread_mutex_lock(&_lock);
|
||||
result = f(_value);
|
||||
OSSpinLockUnlock(&_lock);
|
||||
pthread_mutex_unlock(&_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -50,10 +50,17 @@
|
||||
|
||||
- (void)_assignDisposable:(id<SDisposable>)disposable
|
||||
{
|
||||
if (_terminated)
|
||||
[disposable dispose];
|
||||
else
|
||||
bool dispose = false;
|
||||
OSSpinLockLock(&_lock);
|
||||
if (_terminated) {
|
||||
dispose = true;
|
||||
} else {
|
||||
_disposable = disposable;
|
||||
}
|
||||
OSSpinLockUnlock(&_lock);
|
||||
if (dispose) {
|
||||
[disposable dispose];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_markTerminatedWithoutDisposal
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user