From b89c91b9d6d11a1890ebe408b00849c88e827c8c Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 17 Aug 2016 13:12:27 -0500 Subject: [PATCH] Refactor ntfnstate mutex. (#85) This refactors the notification state mutex out of the state itself to the client. This is being done since the code makes a copy of the notification state and accesses that copy immutably, and therefore there is no need for it to have its own mutex. --- infrastructure.go | 11 +++++++---- notify.go | 7 ------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/infrastructure.go b/infrastructure.go index 985441c9e..fe6cbc908 100644 --- a/infrastructure.go +++ b/infrastructure.go @@ -145,8 +145,9 @@ type Client struct { requestList *list.List // Notifications. - ntfnHandlers *NotificationHandlers - ntfnState *notificationState + ntfnHandlers *NotificationHandlers + ntfnStateLock sync.Mutex + ntfnState *notificationState // Networking infrastructure. sendChan chan []byte @@ -233,8 +234,8 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) { return } - c.ntfnState.Lock() - defer c.ntfnState.Unlock() + c.ntfnStateLock.Lock() + defer c.ntfnStateLock.Unlock() switch bcmd := cmd.(type) { case *btcjson.NotifyBlocksCmd: @@ -498,7 +499,9 @@ func (c *Client) reregisterNtfns() error { // the notification state (while not under the lock of course) which // also register it with the remote RPC server, so this prevents double // registrations. + c.ntfnStateLock.Lock() stateCopy := c.ntfnState.Copy() + c.ntfnStateLock.Unlock() // Reregister notifyblocks if needed. if stateCopy.notifyBlocks { diff --git a/notify.go b/notify.go index 45c0b8938..0e4ca1442 100644 --- a/notify.go +++ b/notify.go @@ -10,7 +10,6 @@ import ( "encoding/json" "errors" "fmt" - "sync" "time" "github.com/btcsuite/btcd/btcjson" @@ -32,7 +31,6 @@ var ( // registered notification so the state can be automatically re-established on // reconnect. type notificationState struct { - sync.Mutex notifyBlocks bool notifyNewTx bool notifyNewTxVerbose bool @@ -41,12 +39,7 @@ type notificationState struct { } // Copy returns a deep copy of the receiver. -// -// This function is safe for concurrent access. func (s *notificationState) Copy() *notificationState { - s.Lock() - defer s.Unlock() - var stateCopy notificationState stateCopy.notifyBlocks = s.notifyBlocks stateCopy.notifyNewTx = s.notifyNewTx