Skip to content

Commit

Permalink
Add missing lock in callbacks
Browse files Browse the repository at this point in the history
Use RWMutex, because callback invocations are read heavy.
  • Loading branch information
arg0d committed Nov 13, 2023
1 parent 62347ac commit 192e0fd
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bindgen/templates/CallbackInterfaceRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type concurrentHandleMap[T any] struct {
leftMap map[uint64]*T
rightMap map[*T]uint64
currentHandle uint64
lock sync.Mutex
lock sync.RWMutex
}

func newConcurrentHandleMap[T any]() *concurrentHandleMap[T] {
Expand All @@ -24,6 +24,7 @@ func newConcurrentHandleMap[T any]() *concurrentHandleMap[T] {
func (cm *concurrentHandleMap[T]) insert(obj *T) uint64 {
cm.lock.Lock()
defer cm.lock.Unlock()

if existingHandle, ok := cm.rightMap[obj]; ok {
return existingHandle
}
Expand All @@ -36,6 +37,7 @@ func (cm *concurrentHandleMap[T]) insert(obj *T) uint64 {
func (cm *concurrentHandleMap[T]) remove(handle uint64) bool {
cm.lock.Lock()
defer cm.lock.Unlock()

if val, ok := cm.leftMap[handle]; ok {
delete(cm.leftMap, handle)
delete(cm.rightMap, val)
Expand All @@ -44,6 +46,9 @@ func (cm *concurrentHandleMap[T]) remove(handle uint64) bool {
}

func (cm *concurrentHandleMap[T]) tryGet(handle uint64) (*T, bool) {
cm.lock.RLock()
defer cm.lock.RUnlock()

val, ok := cm.leftMap[handle]
return val, ok
}
Expand Down

0 comments on commit 192e0fd

Please sign in to comment.