0001 // 0002 // Lock.swift 0003 // Rx 0004 // 0005 // Created by Krunoslav Zaher on 3/31/15. 0006 // Copyright © 2015 Krunoslav Zaher. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 protocol Lock{ 0012 func lock
AsyncLock.swift:23 , LockLock.swift:71 extension NSRecursiveLock : Lock {LockOwnerType.swift:11 protocol LockOwnerType : class, Lock {SynchronizedDisposeType.swift:11 protocol SynchronizedDisposeType : class, Disposable, Lock {SynchronizedOnType.swift:11 protocol SynchronizedOnType : class, ObserverType, Lock {SynchronizedSubscribeType.swift:11 protocol SynchronizedSubscribeType : class, ObservableType, Lock {() 0013 func unlock
SynchronizedDisposeType.swift:17 lock(); defer { unlock() }SynchronizedOnType.swift:17 lock(); defer { unlock() }SynchronizedSubscribeType.swift:17 lock(); defer { unlock() }() 0014 } 0015 0016 #if os(Linux) 0017 import Glibc 0018 0019 /** 0020 Simple wrapper for spin lock. 0021 */ 0022 class SpinLock { 0023 private var _lock: pthread_spinlock_t = 0 0024 0025 init() { 0026 if (pthread_spin_init(&_lock, 0) != 0) { 0027 fatalError("Spin lock initialization failed") 0028 } 0029 } 0030 0031 func lock() { 0032 pthread_spin_lock(&_lock) 0033 } 0034 0035 func unlock() { 0036 pthread_spin_unlock(&_lock) 0037 } 0038 0039 func performLocked(@noescape action: () -> Void) { 0040 pthread_spin_lock(&_lock) 0041 action() 0042 pthread_spin_unlock(&_lock) 0043 } 0044 0045 func calculateLocked<T>(@noescape action: () -> T) -> T { 0046 pthread_spin_lock(&_lock) 0047 let result = action() 0048 pthread_spin_unlock(&_lock) 0049 return result 0050 } 0051 0052 func calculateLockedOrFail<T>(@noescape action: () throws -> T) throws -> T { 0053 pthread_spin_lock(&_lock) 0054 defer { 0055 pthread_spin_unlock(&_lock) 0056 } 0057 let result = try action() 0058 return result 0059 } 0060 0061 deinit { 0062 pthread_spin_destroy(&_lock) 0063 } 0064 } 0065 #else 0066 0067 // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000321.html 0068 typealias SpinLock
SynchronizedDisposeType.swift:17 lock(); defer { unlock() }SynchronizedOnType.swift:17 lock(); defer { unlock() }SynchronizedSubscribeType.swift:17 lock(); defer { unlock() }= NSRecursiveLock 0069 #endif 0070 0071 extension NSRecursiveLock : Lock { 0072 func performLocked
AsyncLock.swift:27 var _lock = SpinLock()CompositeDisposable.swift:17 private var _lock = SpinLock()DisposeBag.swift:36 private var _lock = SpinLock()ObserveOn.swift:49 var _lock = SpinLock()RecursiveScheduler.swift:131 private var _lock = SpinLock()RefCountDisposable.swift:15 private var _lock = SpinLock()SerialDisposable.swift:15 private var _lock = SpinLock()SingleAssignmentDisposable.swift:17 private var _lock = SpinLock()Variable.swift:23 private var _lock = SpinLock()(@noescape action: () -> Void) { 0073 self.lock() 0074 action() 0075 self.unlock() 0076 } 0077 0078 func calculateLocked
Amb.swift:76 self._lock.performLocked {BehaviorSubject.swift:147 _lock.performLocked {Buffer.swift:108 self._lock.performLocked {ConnectableObservable.swift:42 parent._lock.performLocked {RecursiveScheduler.swift:66 _lock.performLocked {RecursiveScheduler.swift:109 _lock.performLocked {RecursiveScheduler.swift:118 _lock.performLocked {RecursiveScheduler.swift:179 _lock.performLocked {RecursiveScheduler.swift:188 _lock.performLocked {Timeout.swift:46 _lock.performLocked() {Timeout.swift:60 _lock.performLocked() {Timeout.swift:86 self._lock.performLocked() {Window.swift:115 self._lock.performLocked {<T>(@noescape action: () -> T) -> T { 0079 self.lock() 0080 let result = action() 0081 self.unlock() 0082 return result 0083 } 0084 0085 func calculateLockedOrFail<T>(@noescape action: () throws -> T) throws -> T { 0086 self.lock() 0087 defer { 0088 self.unlock() 0089 } 0090 let result = try action() 0091 return result 0092 } 0093 } 0094 0095 /* 0096 let RECURSIVE_MUTEX = _initializeRecursiveMutex() 0097 0098 func _initializeRecursiveMutex() -> pthread_mutex_t { 0099 var mutex: pthread_mutex_t = pthread_mutex_t() 0100 var mta: pthread_mutexattr_t = pthread_mutexattr_t() 0101 0102 pthread_mutex_init(&mutex, nil) 0103 pthread_mutexattr_init(&mta) 0104 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE) 0105 pthread_mutex_init(&mutex, &mta) 0106 0107 return mutex 0108 } 0109 0110 extension pthread_mutex_t { 0111 mutating func lock() { 0112 pthread_mutex_lock(&self) 0113 } 0114 0115 mutating func unlock() { 0116 pthread_mutex_unlock(&self) 0117 } 0118 } 0119 */ 0120
ConnectableObservable.swift:77 return _lock.calculateLocked {ObserveOn.swift:65 let shouldStart = _lock.calculateLocked { () -> Bool inObserveOn.swift:83 let (nextEvent, observer) = self._lock.calculateLocked { () -> (Event<E>?, O?) inRecursiveScheduler.swift:48 let action = self._lock.calculateLocked { () -> Action? inRecursiveScheduler.swift:91 let action = self._lock.calculateLocked { () -> Action? inRecursiveScheduler.swift:161 let action = self._lock.calculateLocked { () -> Action? inRefCountDisposable.swift:44 return _lock.calculateLocked {RefCountDisposable.swift:64 let oldDisposable: Disposable? = _lock.calculateLocked {RefCountDisposable.swift:85 let oldDisposable: Disposable? = _lock.calculateLocked {SerialDisposable.swift:46 return _lock.calculateLocked {SerialDisposable.swift:51 let disposable: Disposable? = _lock.calculateLocked {