Skip to content

Commit 80f8637

Browse files
committed
implemented recursive lock using pthread mutex
1 parent 3b4dab4 commit 80f8637

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

Dip/Dip/Dip.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class DependencyContainer {
4141

4242
var definitions = [DefinitionKey : Definition]()
4343
let resolvedInstances = ResolvedInstances()
44-
let lock = NSRecursiveLock()
44+
let lock = RecursiveLock()
4545

4646
/**
4747
Designated initializer for a DependencyContainer

Dip/Dip/Utils.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
// THE SOFTWARE.
2323
//
2424

25-
import Foundation
26-
2725
extension Dictionary {
2826
subscript(key: Key?) -> Value? {
2927
get {
@@ -42,3 +40,46 @@ extension Optional {
4240
return self.map { "\($0)" } ?? "nil"
4341
}
4442
}
43+
44+
#if os(Linux)
45+
import Glibc
46+
class RecursiveLock {
47+
private var _lock = _initializeRecursiveMutex()
48+
49+
func lock() {
50+
_lock.lock()
51+
}
52+
53+
func unlock() {
54+
_lock.unlock()
55+
}
56+
57+
deinit {
58+
pthread_mutex_destroy(&_lock)
59+
}
60+
61+
}
62+
63+
private func _initializeRecursiveMutex() -> pthread_mutex_t {
64+
var mutex: pthread_mutex_t = pthread_mutex_t()
65+
var mta: pthread_mutexattr_t = pthread_mutexattr_t()
66+
pthread_mutex_init(&mutex, nil)
67+
pthread_mutexattr_init(&mta)
68+
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE)
69+
pthread_mutex_init(&mutex, &mta)
70+
return mutex
71+
}
72+
73+
extension pthread_mutex_t {
74+
mutating func lock() {
75+
pthread_mutex_lock(&self)
76+
}
77+
mutating func unlock() {
78+
pthread_mutex_unlock(&self)
79+
}
80+
}
81+
82+
#else
83+
import Foundation
84+
typealias RecursiveLock = NSRecursiveLock
85+
#endif

0 commit comments

Comments
 (0)