|
28 | 28 | internal var pointerDebugDescription: String? = nil |
29 | 29 |
|
30 | 30 | init(conflictAvoidingVariableName: UInt, instantiationContext: String) { |
31 | | - Self.globalInstanceCounter += 1 |
32 | | - self.globalInstanceNumber = Self.globalInstanceCounter |
| 31 | + var instanceIndex: UInt! = nil |
| 32 | + Bindings.instanceIndexQueue.sync { |
| 33 | + Self.globalInstanceCounter += 1 |
| 34 | + instanceIndex = Self.globalInstanceCounter |
| 35 | + } |
| 36 | + self.globalInstanceNumber = instanceIndex |
33 | 37 | self.instantiationContext = instantiationContext |
34 | 38 | } |
35 | 39 |
|
|
85 | 89 |
|
86 | 90 | public class Bindings { |
87 | 91 |
|
| 92 | + fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue") |
| 93 | + static var nativelyExposedInstances = [UInt: NativeTraitWrapper]() |
| 94 | + static var nativelyExposedInstanceReferenceCounter = [UInt: Int]() |
| 95 | + |
88 | 96 | internal static var suspendFreedom = false |
89 | 97 |
|
90 | 98 | internal static var minimumPrintSeverity: PrintSeverity = .WARNING |
|
100 | 108 |
|
101 | 109 | internal class func print(_ string: String, severity: PrintSeverity = .DEBUG) { |
102 | 110 | if severity.rawValue >= Self.minimumPrintSeverity.rawValue { |
103 | | - |
104 | | - // Swift.print(string) |
105 | 111 | NSLog(string) |
106 | 112 | fflush(stdout) |
107 | | - |
108 | | - // if #available(iOS 14.0, *) { |
109 | | - // #if canImport(os) |
110 | | - // if severity == Self.PrintSeverity.DEBUG { |
111 | | - // logger.debug("\(string)") |
112 | | - // }else if severity == Self.PrintSeverity.WARNING { |
113 | | - // logger.warning("\(string)") |
114 | | - // }else if severity == Self.PrintSeverity.ERROR { |
115 | | - // logger.error("\(string)") |
116 | | - // }else { |
117 | | - // logger.log("\(string)") |
118 | | - // } |
119 | | - // #else |
120 | | - // Swift.print(string) |
121 | | - // #endif |
122 | | - // } else { |
123 | | - // // Fallback on earlier versions |
124 | | - // Swift.print(string) |
125 | | - // } |
126 | 113 | } |
127 | 114 | } |
128 | 115 |
|
129 | 116 | public class func setLogThreshold(severity: PrintSeverity){ |
130 | 117 | Self.minimumPrintSeverity = severity |
131 | 118 | } |
132 | 119 |
|
133 | | - static var nativelyExposedInstances = [UInt: NativeTraitWrapper]() |
134 | | - static var nativelyExposedInstanceReferenceCounter = [UInt: Int]() |
135 | | - |
136 | 120 | public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) { |
137 | 121 | let key = instance.globalInstanceNumber |
138 | | - let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1 |
139 | | - if (!countIdempotently || referenceCount == 1){ |
140 | | - // if we count non-idempotently, always update the counter |
141 | | - // otherwise, only update the counter the first time |
142 | | - Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount |
143 | | - } |
144 | | - if referenceCount == 1 { |
145 | | - print("Caching global instance \(key). Cached instance count: \(nativelyExposedInstanceReferenceCounter.count)") |
146 | | - Self.nativelyExposedInstances[key] = instance |
147 | | - } |
| 122 | + |
| 123 | + Bindings.instanceIndexQueue.sync { |
| 124 | + let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1 |
| 125 | + if (!countIdempotently || referenceCount == 1){ |
| 126 | + // if we count non-idempotently, always update the counter |
| 127 | + // otherwise, only update the counter the first time |
| 128 | + Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount |
| 129 | + } |
| 130 | + if referenceCount == 1 { |
| 131 | + print("Caching global instance (key). Cached instance count: (nativelyExposedInstanceReferenceCounter.count)") |
| 132 | + Self.nativelyExposedInstances[key] = instance |
| 133 | + } |
| 134 | + } |
148 | 135 | } |
149 | 136 |
|
150 | 137 | public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer { |
151 | 138 | let key = instance.globalInstanceNumber |
152 | 139 | let pointer = UnsafeMutableRawPointer(bitPattern: key)! |
| 140 | + print("Caching instance (key) -> (pointer)", severity: .DEBUG) |
153 | 141 | // don't automatically cache the trait instance |
154 | | - Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance |
| 142 | + Bindings.instanceIndexQueue.sync { |
| 143 | + Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance |
| 144 | + } |
155 | 145 | return pointer |
156 | 146 | } |
157 | 147 |
|
158 | 148 | public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{ |
159 | 149 | let key = UInt(bitPattern: pointer) |
160 | | - let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0 |
161 | | - if referenceCount < 1 { |
162 | | - print("Bad lookup: non-positive reference count for instance \(key): \(referenceCount)!", severity: .ERROR) |
163 | | - } |
164 | | - let value = Self.nativelyExposedInstances[key] as! T |
| 150 | + print("Looking up instance (pointer) -> (key)", severity: .DEBUG) |
| 151 | + |
| 152 | + var rawValue: NativeTraitWrapper! = nil |
| 153 | + Bindings.instanceIndexQueue.sync { |
| 154 | + let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0 |
| 155 | + if referenceCount < 1 { |
| 156 | + print("Bad lookup: non-positive reference count for instance (key): (referenceCount)!", severity: .ERROR) |
| 157 | + } |
| 158 | + rawValue = Self.nativelyExposedInstances[key] |
| 159 | + } |
| 160 | + let value = rawValue as! T |
165 | 161 | return value |
166 | 162 | } |
167 | 163 |
|
168 | | - public class func removeInstancePointer(instance: NativeTraitWrapper) -> Bool { |
169 | | - let key = instance.globalInstanceNumber |
170 | | - let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) - 1 |
171 | | - Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount |
172 | | - if referenceCount == 0 { |
173 | | - print("Uncaching global instance \(key)") |
174 | | - // TODO: fix counting |
175 | | - // Self.nativelyExposedInstances.removeValue(forKey: key) |
176 | | - // instance.pointerDebugDescription = nil |
177 | | - } else if referenceCount < 0 { |
178 | | - print("Bad uncache: negative reference count (\(referenceCount)) for instance \(key)!", severity: .ERROR) |
179 | | - } |
180 | | - return true |
181 | | - } |
182 | | - |
183 | 164 | /* |
184 | 165 | public class func clearInstancePointers() { |
185 | 166 | for (_, currentInstance) in Self.nativelyExposedInstances { |
@@ -1031,7 +1012,7 @@ return returnValue |
1031 | 1012 | /// [`ChannelManager::list_usable_channels`] will never include such channels. |
1032 | 1013 | /// |
1033 | 1014 | /// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels |
1034 | | - /// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed |
| 1015 | + /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed |
1035 | 1016 | /// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph |
1036 | 1017 | /// |
1037 | 1018 | /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None |
|
0 commit comments