Skip to content

Commit 243d120

Browse files
committed
Add missing lock in callbacks
Use RWMutex, because callback invocations are read heavy.
1 parent 62347ac commit 243d120

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

bindgen/templates/CallbackInterfaceRuntime.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type concurrentHandleMap[T any] struct {
1111
leftMap map[uint64]*T
1212
rightMap map[*T]uint64
1313
currentHandle uint64
14-
lock sync.Mutex
14+
lock sync.RWMutex
1515
}
1616

1717
func newConcurrentHandleMap[T any]() *concurrentHandleMap[T] {
@@ -24,6 +24,7 @@ func newConcurrentHandleMap[T any]() *concurrentHandleMap[T] {
2424
func (cm *concurrentHandleMap[T]) insert(obj *T) uint64 {
2525
cm.lock.Lock()
2626
defer cm.lock.Unlock()
27+
2728
if existingHandle, ok := cm.rightMap[obj]; ok {
2829
return existingHandle
2930
}
@@ -36,6 +37,7 @@ func (cm *concurrentHandleMap[T]) insert(obj *T) uint64 {
3637
func (cm *concurrentHandleMap[T]) remove(handle uint64) bool {
3738
cm.lock.Lock()
3839
defer cm.lock.Unlock()
40+
3941
if val, ok := cm.leftMap[handle]; ok {
4042
delete(cm.leftMap, handle)
4143
delete(cm.rightMap, val)
@@ -44,6 +46,9 @@ func (cm *concurrentHandleMap[T]) remove(handle uint64) bool {
4446
}
4547

4648
func (cm *concurrentHandleMap[T]) tryGet(handle uint64) (*T, bool) {
49+
cm.lock.Lock()
50+
defer cm.lock.Unlock()
51+
4752
val, ok := cm.leftMap[handle]
4853
return val, ok
4954
}

0 commit comments

Comments
 (0)