Skip to content

Commit 16e0cb1

Browse files
committed
Serialize access to global instance counter and instance cache.
1 parent 698e90d commit 16e0cb1

File tree

3 files changed

+87
-87
lines changed

3 files changed

+87
-87
lines changed

out/Bindings.swift

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
internal var pointerDebugDescription: String? = nil
2929

3030
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
3337
self.instantiationContext = instantiationContext
3438
}
3539

@@ -85,6 +89,10 @@
8589

8690
public class Bindings {
8791

92+
fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue")
93+
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
94+
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
95+
8896
internal static var suspendFreedom = false
8997

9098
internal static var minimumPrintSeverity: PrintSeverity = .WARNING
@@ -108,13 +116,13 @@
108116
// if #available(iOS 14.0, *) {
109117
// #if canImport(os)
110118
// if severity == Self.PrintSeverity.DEBUG {
111-
// logger.debug("\(string)")
119+
// logger.debug("(string)")
112120
// }else if severity == Self.PrintSeverity.WARNING {
113-
// logger.warning("\(string)")
121+
// logger.warning("(string)")
114122
// }else if severity == Self.PrintSeverity.ERROR {
115-
// logger.error("\(string)")
123+
// logger.error("(string)")
116124
// }else {
117-
// logger.log("\(string)")
125+
// logger.log("(string)")
118126
// }
119127
// #else
120128
// Swift.print(string)
@@ -130,58 +138,50 @@
130138
Self.minimumPrintSeverity = severity
131139
}
132140

133-
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
134-
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
135-
136141
public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) {
137142
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-
}
143+
144+
Bindings.instanceIndexQueue.sync {
145+
let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
146+
if (!countIdempotently || referenceCount == 1){
147+
// if we count non-idempotently, always update the counter
148+
// otherwise, only update the counter the first time
149+
Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
150+
}
151+
if referenceCount == 1 {
152+
print("Caching global instance (key). Cached instance count: (nativelyExposedInstanceReferenceCounter.count)")
153+
Self.nativelyExposedInstances[key] = instance
154+
}
155+
}
148156
}
149157

150158
public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer {
151159
let key = instance.globalInstanceNumber
152160
let pointer = UnsafeMutableRawPointer(bitPattern: key)!
153-
print("Caching instance \(key) -> \(pointer)", severity: .DEBUG)
161+
print("Caching instance (key) -> (pointer)", severity: .DEBUG)
154162
// don't automatically cache the trait instance
155-
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
163+
Bindings.instanceIndexQueue.sync {
164+
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
165+
}
156166
return pointer
157167
}
158168

159169
public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
160170
let key = UInt(bitPattern: pointer)
161-
print("Looking up instance \(pointer) -> \(key)", severity: .DEBUG)
162-
let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
163-
if referenceCount < 1 {
164-
print("Bad lookup: non-positive reference count for instance \(key): \(referenceCount)!", severity: .ERROR)
165-
}
166-
let value = Self.nativelyExposedInstances[key] as! T
171+
print("Looking up instance (pointer) -> (key)", severity: .DEBUG)
172+
173+
var rawValue: NativeTraitWrapper! = nil
174+
Bindings.instanceIndexQueue.sync {
175+
let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
176+
if referenceCount < 1 {
177+
print("Bad lookup: non-positive reference count for instance (key): (referenceCount)!", severity: .ERROR)
178+
}
179+
rawValue = Self.nativelyExposedInstances[key]
180+
}
181+
let value = rawValue as! T
167182
return value
168183
}
169184

170-
public class func removeInstancePointer(instance: NativeTraitWrapper) -> Bool {
171-
let key = instance.globalInstanceNumber
172-
let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) - 1
173-
Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
174-
if referenceCount == 0 {
175-
print("Uncaching global instance \(key)")
176-
// TODO: fix counting
177-
// Self.nativelyExposedInstances.removeValue(forKey: key)
178-
// instance.pointerDebugDescription = nil
179-
} else if referenceCount < 0 {
180-
print("Bad uncache: negative reference count (\(referenceCount)) for instance \(key)!", severity: .ERROR)
181-
}
182-
return true
183-
}
184-
185185
/*
186186
public class func clearInstancePointers() {
187187
for (_, currentInstance) in Self.nativelyExposedInstances {

out/VersionDescriptor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
extension Bindings {
77
public class func getLDKSwiftBindingsSerializationHash() -> String {
8-
return "951a04e511767a058d66c5eced47089c2d812e2c268bdb52749dad262cd92699"
8+
return "7ab6ba0549828e3bc55e8b6dc63e8c2c8878bbc1b7e3e7f7300bd90f8e038906"
99
}
1010
public class func getLDKSwiftBindingsVersion() -> String {
11-
return "0.0.114-25-gf98080ee-dirty"
11+
return "0.0.114-26-g698e90d7-dirty"
1212
}
1313
public class func getLDKSwiftBindingsCommitHash() -> String {
14-
return "f98080ee8136bf0c9502a55bbc4c65e748eba96e"
14+
return "698e90d745f3bded117d4a0188250c8570e51868"
1515
}
1616
}
1717

src/generation/bindings_file_generator.mts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
9999
internal var pointerDebugDescription: String? = nil
100100
101101
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
104108
self.instantiationContext = instantiationContext
105109
}
106110
@@ -156,6 +160,10 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
156160
157161
public class Bindings {
158162
163+
fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue")
164+
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
165+
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
166+
159167
internal static var suspendFreedom = false
160168
161169
internal static var minimumPrintSeverity: PrintSeverity = .WARNING
@@ -179,13 +187,13 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
179187
// if #available(iOS 14.0, *) {
180188
// #if canImport(os)
181189
// if severity == Self.PrintSeverity.DEBUG {
182-
// logger.debug("\\(string)")
190+
// logger.debug("\(string)")
183191
// }else if severity == Self.PrintSeverity.WARNING {
184-
// logger.warning("\\(string)")
192+
// logger.warning("\(string)")
185193
// }else if severity == Self.PrintSeverity.ERROR {
186-
// logger.error("\\(string)")
194+
// logger.error("\(string)")
187195
// }else {
188-
// logger.log("\\(string)")
196+
// logger.log("\(string)")
189197
// }
190198
// #else
191199
// Swift.print(string)
@@ -201,58 +209,50 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
201209
Self.minimumPrintSeverity = severity
202210
}
203211
204-
static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
205-
static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
206-
207212
public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) {
208213
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+
}
219227
}
220228
221229
public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer {
222230
let key = instance.globalInstanceNumber
223231
let pointer = UnsafeMutableRawPointer(bitPattern: key)!
224-
print("Caching instance \\(key) -> \\(pointer)", severity: .DEBUG)
232+
print("Caching instance \(key) -> \(pointer)", severity: .DEBUG)
225233
// don't automatically cache the trait instance
226-
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
234+
Bindings.instanceIndexQueue.sync {
235+
Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
236+
}
227237
return pointer
228238
}
229239
230240
public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
231241
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
238253
return value
239254
}
240255
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-
256256
/*
257257
public class func clearInstancePointers() {
258258
for (_, currentInstance) in Self.nativelyExposedInstances {

0 commit comments

Comments
 (0)