@@ -99,8 +99,12 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
99
99
internal var pointerDebugDescription: String? = nil
100
100
101
101
init(conflictAvoidingVariableName: UInt, instantiationContext: String) {
102
- Self.globalInstanceCounter += 1
103
- self.globalInstanceNumber = Self.globalInstanceCounter
102
+ var instanceIndex: UInt! = nil
103
+ Bindings.instanceIndexQueue.sync {
104
+ Self.globalInstanceCounter += 1
105
+ instanceIndex = Self.globalInstanceCounter
106
+ }
107
+ self.globalInstanceNumber = instanceIndex
104
108
self.instantiationContext = instantiationContext
105
109
}
106
110
@@ -156,6 +160,10 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
156
160
157
161
public class Bindings {
158
162
163
+ fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue")
164
+ static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
165
+ static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
166
+
159
167
internal static var suspendFreedom = false
160
168
161
169
internal static var minimumPrintSeverity: PrintSeverity = .WARNING
@@ -179,13 +187,13 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
179
187
// if #available(iOS 14.0, *) {
180
188
// #if canImport(os)
181
189
// if severity == Self.PrintSeverity.DEBUG {
182
- // logger.debug("\\ (string)")
190
+ // logger.debug("\(string)")
183
191
// }else if severity == Self.PrintSeverity.WARNING {
184
- // logger.warning("\\ (string)")
192
+ // logger.warning("\(string)")
185
193
// }else if severity == Self.PrintSeverity.ERROR {
186
- // logger.error("\\ (string)")
194
+ // logger.error("\(string)")
187
195
// }else {
188
- // logger.log("\\ (string)")
196
+ // logger.log("\(string)")
189
197
// }
190
198
// #else
191
199
// Swift.print(string)
@@ -201,58 +209,50 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
201
209
Self.minimumPrintSeverity = severity
202
210
}
203
211
204
- static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
205
- static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
206
-
207
212
public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) {
208
213
let key = instance.globalInstanceNumber
209
- let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
210
- if (!countIdempotently || referenceCount == 1){
211
- // if we count non-idempotently, always update the counter
212
- // otherwise, only update the counter the first time
213
- Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
214
- }
215
- if referenceCount == 1 {
216
- print("Caching global instance \\(key). Cached instance count: \\(nativelyExposedInstanceReferenceCounter.count)")
217
- Self.nativelyExposedInstances[key] = instance
218
- }
214
+
215
+ Bindings.instanceIndexQueue.sync {
216
+ let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
217
+ if (!countIdempotently || referenceCount == 1){
218
+ // if we count non-idempotently, always update the counter
219
+ // otherwise, only update the counter the first time
220
+ Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
221
+ }
222
+ if referenceCount == 1 {
223
+ print("Caching global instance \(key). Cached instance count: \(nativelyExposedInstanceReferenceCounter.count)")
224
+ Self.nativelyExposedInstances[key] = instance
225
+ }
226
+ }
219
227
}
220
228
221
229
public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer {
222
230
let key = instance.globalInstanceNumber
223
231
let pointer = UnsafeMutableRawPointer(bitPattern: key)!
224
- print("Caching instance \\ (key) -> \ \(pointer)", severity: .DEBUG)
232
+ print("Caching instance \(key) -> \(pointer)", severity: .DEBUG)
225
233
// don't automatically cache the trait instance
226
- Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
234
+ Bindings.instanceIndexQueue.sync {
235
+ Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
236
+ }
227
237
return pointer
228
238
}
229
239
230
240
public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
231
241
let key = UInt(bitPattern: pointer)
232
- print("Looking up instance \\(pointer) -> \\(key)", severity: .DEBUG)
233
- let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
234
- if referenceCount < 1 {
235
- print("Bad lookup: non-positive reference count for instance \\(key): \\(referenceCount)!", severity: .ERROR)
236
- }
237
- let value = Self.nativelyExposedInstances[key] as! T
242
+ print("Looking up instance \(pointer) -> \(key)", severity: .DEBUG)
243
+
244
+ var rawValue: NativeTraitWrapper! = nil
245
+ Bindings.instanceIndexQueue.sync {
246
+ let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
247
+ if referenceCount < 1 {
248
+ print("Bad lookup: non-positive reference count for instance \(key): \(referenceCount)!", severity: .ERROR)
249
+ }
250
+ rawValue = Self.nativelyExposedInstances[key]
251
+ }
252
+ let value = rawValue as! T
238
253
return value
239
254
}
240
255
241
- public class func removeInstancePointer(instance: NativeTraitWrapper) -> Bool {
242
- let key = instance.globalInstanceNumber
243
- let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) - 1
244
- Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
245
- if referenceCount == 0 {
246
- print("Uncaching global instance \\(key)")
247
- // TODO: fix counting
248
- // Self.nativelyExposedInstances.removeValue(forKey: key)
249
- // instance.pointerDebugDescription = nil
250
- } else if referenceCount < 0 {
251
- print("Bad uncache: negative reference count (\\(referenceCount)) for instance \\(key)!", severity: .ERROR)
252
- }
253
- return true
254
- }
255
-
256
256
/*
257
257
public class func clearInstancePointers() {
258
258
for (_, currentInstance) in Self.nativelyExposedInstances {
0 commit comments