Skip to content

Commit

Permalink
Switch to RWMutex for devices map access
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDunne committed Jun 8, 2021
1 parent 9eb37a6 commit 7db3e9e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/sni/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type devicesService struct {
sni.UnimplementedDevicesServiceServer

// track opened devices by URI
devicesRw sync.Mutex
devicesRw sync.RWMutex
devices map[string]snes.Queue
}

Expand Down Expand Up @@ -54,6 +54,7 @@ func (s *devicesService) ListDevices(ctx context.Context, request *sni.DevicesRe
})
}
}

return &sni.DevicesResponse{Devices: devices}, nil
}

Expand All @@ -62,15 +63,15 @@ func (s *devicesService) ReadMemory(ctx context.Context, request *sni.ReadMemory
}

func (s *devicesService) WriteMemory(ctx context.Context, request *sni.WriteMemoryRequest) (*sni.WriteMemoryResponse, error) {
panic("implement me")
return nil, fmt.Errorf("unimplemented")
}

func (s *devicesService) AcquireDevice(uri string) (dev snes.Queue, err error) {
defer s.devicesRw.Unlock()
s.devicesRw.Lock()

var ok bool
s.devicesRw.RLock()
dev, ok = s.devices[uri]
s.devicesRw.RUnlock()
if ok {
return
}
Expand All @@ -95,6 +96,8 @@ func (s *devicesService) AcquireDevice(uri string) (dev snes.Queue, err error) {
return
}

s.devicesRw.Lock()
s.devices[uri] = dev
s.devicesRw.Unlock()
return
}

0 comments on commit 7db3e9e

Please sign in to comment.