From c2aafe7e511d3e9449d5068eed7fa999e8aed590 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 2 Aug 2017 20:55:51 -0700 Subject: [PATCH] server: add a method to query if the server has started This commit adds a new utility method to the server struct itself. This method will allow callers to query the state of the server in order to decide if the server has been started or not. This can be useful elsewhere in the project as we start to decouple the lifetime of certain sub-systems from others. --- server.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index aa57c8b6..fdb19e89 100644 --- a/server.go +++ b/server.go @@ -302,11 +302,16 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, return s, nil } +// Started returns true if the server has been started, and false otherwise. +func (s *server) Started() bool { + return atomic.LoadInt32(&s.started) != 0 +} + // Start starts the main daemon server, all requested listeners, and any helper // goroutines. func (s *server) Start() error { // Already running? - if atomic.AddInt32(&s.started, 1) != 1 { + if !atomic.CompareAndSwapInt32(&s.started, 0, 1) { return nil } @@ -353,7 +358,7 @@ func (s *server) Start() error { // all successfully exited. Additionally, any/all listeners are closed. func (s *server) Stop() error { // Bail if we're already shutting down. - if atomic.AddInt32(&s.shutdown, 1) != 1 { + if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) { return nil }