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