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>
This commit is contained in:
Burcu Dogan 2015-03-30 13:50:53 -07:00
parent d04e7395d9
commit dfdb6c5a4a
1 changed files with 13 additions and 11 deletions

View File

@ -7,7 +7,6 @@ package sensor
import ( import (
"errors" "errors"
"sync"
"time" "time"
) )
@ -66,12 +65,7 @@ type Event struct {
// Manager multiplexes sensor event data from various sensor sources. // Manager multiplexes sensor event data from various sensor sources.
type Manager struct { type Manager struct {
once sync.Once m *manager // platform-specific implementation of the underlying manager
m *manager // platform-specific implementation of the underlying manager
}
func (m *Manager) init() {
m.m = &manager{}
} }
// Enable enables a sensor with the specified delay rate. // Enable enables a sensor with the specified delay rate.
@ -81,7 +75,9 @@ func (m *Manager) init() {
// Valid sensor types supported by this package are Accelerometer, // Valid sensor types supported by this package are Accelerometer,
// Gyroscope, Magnetometer and Altimeter. // Gyroscope, Magnetometer and Altimeter.
func (m *Manager) Enable(t Type, delay time.Duration) error { 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) { if t < 0 || int(t) >= len(sensorNames) {
return errors.New("sensor: unknown sensor type") return errors.New("sensor: unknown sensor type")
} }
@ -90,7 +86,9 @@ func (m *Manager) Enable(t Type, delay time.Duration) error {
// Disable disables to feed the manager with the specified sensor. // Disable disables to feed the manager with the specified sensor.
func (m *Manager) Disable(t Type) error { 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) { if t < 0 || int(t) >= len(sensorNames) {
return errors.New("sensor: unknown sensor type") return errors.New("sensor: unknown sensor type")
} }
@ -101,13 +99,17 @@ func (m *Manager) Disable(t Type) error {
// It may read up to len(e) number of events, but will return // It may read up to len(e) number of events, but will return
// less events if timeout occurs. // less events if timeout occurs.
func (m *Manager) Read(e []Event) (n int, err error) { 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) return read(m.m, e)
} }
// Close stops the manager and frees the related resources. // Close stops the manager and frees the related resources.
// Once Close is called, Manager becomes invalid to use. // Once Close is called, Manager becomes invalid to use.
func (m *Manager) Close() error { func (m *Manager) Close() error {
m.once.Do(m.init) if m.m == nil {
m.m = new(manager)
}
return close(m.m) return close(m.m)
} }