mobile/sensor: remove the unnecessary init mutex
Manager ops are not promised to be thread-safe.
Change-Id: Ib28b80d758234a7a19287c457c57099e36d73f9b
Reviewed-on: https://go-review.googlesource.com/8248
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/sensor/sensor.go b/sensor/sensor.go
index 04098bd..8172fae 100644
--- a/sensor/sensor.go
+++ b/sensor/sensor.go
@@ -7,7 +7,6 @@
import (
"errors"
- "sync"
"time"
)
@@ -66,12 +65,7 @@
// Manager multiplexes sensor event data from various sensor sources.
type Manager struct {
- once sync.Once
- m *manager // platform-specific implementation of the underlying manager
-}
-
-func (m *Manager) init() {
- m.m = &manager{}
+ m *manager // platform-specific implementation of the underlying manager
}
// Enable enables a sensor with the specified delay rate.
@@ -81,7 +75,9 @@
// Valid sensor types supported by this package are Accelerometer,
// Gyroscope, Magnetometer and Altimeter.
func (m *Manager) Enable(t Type, delay time.Duration) error {
- m.once.Do(m.init)
+ if m.m == nil {
+ m.m = new(manager)
+ }
if t < 0 || int(t) >= len(sensorNames) {
return errors.New("sensor: unknown sensor type")
}
@@ -90,7 +86,9 @@
// Disable disables to feed the manager with the specified sensor.
func (m *Manager) Disable(t Type) error {
- m.once.Do(m.init)
+ if m.m == nil {
+ m.m = new(manager)
+ }
if t < 0 || int(t) >= len(sensorNames) {
return errors.New("sensor: unknown sensor type")
}
@@ -101,13 +99,17 @@
// It may read up to len(e) number of events, but will return
// less events if timeout occurs.
func (m *Manager) Read(e []Event) (n int, err error) {
- m.once.Do(m.init)
+ if m.m == nil {
+ m.m = new(manager)
+ }
return read(m.m, e)
}
// Close stops the manager and frees the related resources.
// Once Close is called, Manager becomes invalid to use.
func (m *Manager) Close() error {
- m.once.Do(m.init)
+ if m.m == nil {
+ m.m = new(manager)
+ }
return close(m.m)
}