Use a separate lock for the subscriber stuff

This commit is contained in:
Jakob Borg 2014-10-07 10:34:53 +02:00
parent 640d5135df
commit c3c9c4cde5

View File

@ -49,8 +49,10 @@ type ConfigWrapper struct {
deviceMap map[protocol.DeviceID]DeviceConfiguration deviceMap map[protocol.DeviceID]DeviceConfiguration
folderMap map[string]FolderConfiguration folderMap map[string]FolderConfiguration
replaces chan Configuration replaces chan Configuration
subs []Handler
mut sync.Mutex mut sync.Mutex
subs []Handler
sMut sync.Mutex
} }
// Wrap wraps an existing Configuration structure and ties it to a file on // Wrap wraps an existing Configuration structure and ties it to a file on
@ -84,26 +86,27 @@ func Load(path string, myID protocol.DeviceID) (*ConfigWrapper, error) {
// be run manually. // be run manually.
func (w *ConfigWrapper) Serve() { func (w *ConfigWrapper) Serve() {
for cfg := range w.replaces { for cfg := range w.replaces {
w.mut.Lock() w.sMut.Lock()
subs := w.subs subs := w.subs
w.mut.Unlock() w.sMut.Unlock()
for _, h := range subs { for _, h := range subs {
h.Changed(cfg) h.Changed(cfg)
} }
} }
} }
// Stop stops the Serve() loop. // Stop stops the Serve() loop. Set and Replace operations will panic after a
// Stop.
func (w *ConfigWrapper) Stop() { func (w *ConfigWrapper) Stop() {
close(w.replaces) close(w.replaces)
} }
// Subscriber registers the given handler to be called on any future // Subscribe registers the given handler to be called on any future
// configuration changes. // configuration changes.
func (w *ConfigWrapper) Subscribe(h Handler) { func (w *ConfigWrapper) Subscribe(h Handler) {
w.mut.Lock() w.sMut.Lock()
w.subs = append(w.subs, h) w.subs = append(w.subs, h)
w.mut.Unlock() w.sMut.Unlock()
} }
// Raw returns the currently wrapped Configuration object. // Raw returns the currently wrapped Configuration object.