Skip to content

Commit 366fc46

Browse files
committed
Lock version for Android
1 parent daba0ce commit 366fc46

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Sources/CombineSchedulers/Internal/Lock.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,33 @@
4343
}
4444
}
4545
#else
46+
import pthread
4647
struct Lock {
48+
private let lockPtr: UnsafeMutablePointer<pthread_mutex_t>
49+
4750
internal init() {
48-
51+
lockPtr = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
52+
var attr = pthread_mutexattr_t()
53+
pthread_mutexattr_settype(&attr, Int32(PTHREAD_MUTEX_RECURSIVE))
54+
pthread_mutex_init(lockPtr, &attr)
4955
}
50-
56+
5157
internal func cleanupLock() {
52-
58+
pthread_mutex_destroy(lockPtr)
59+
lockPtr.deinitialize(count: 1)
60+
lockPtr.deallocate()
5361
}
54-
62+
5563
internal func lock() {
56-
64+
pthread_mutex_lock(lockPtr)
5765
}
58-
66+
5967
internal func tryLock() -> Bool {
60-
return false
68+
return pthread_mutex_trylock(lockPtr) == 0
6169
}
62-
70+
6371
internal func unlock() {
64-
72+
pthread_mutex_unlock(lockPtr)
6573
}
6674
}
6775
#endif

0 commit comments

Comments
 (0)