Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing lock in callbacks #29

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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