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.
This commit is contained in:
Olaoluwa Osuntokun 2017-08-02 20:55:51 -07:00
parent e2112702e7
commit c2aafe7e51
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -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
}