Use lighter atomic counters instead of mutexes

Where appropriate, it makes sense to use lighter weight atomic counters
instead of mutexes.
This commit is contained in:
Tomás Senart 2014-07-03 03:55:22 +02:00 committed by Dave Collins
parent 07bdbd9e3d
commit 2afc5a0af2

View file

@ -205,19 +205,18 @@ func newWorkState() *workState {
// rpcServer holds the items the rpc server may need to access (config, // rpcServer holds the items the rpc server may need to access (config,
// shutdown, main server, etc.) // shutdown, main server, etc.)
type rpcServer struct { type rpcServer struct {
started int32 started int32
shutdown int32 shutdown int32
server *server server *server
authsha [fastsha256.Size]byte authsha [fastsha256.Size]byte
ntfnMgr *wsNotificationManager ntfnMgr *wsNotificationManager
numClients int numClients int32
numClientsMutex sync.Mutex statusLines map[int]string
statusLines map[int]string statusLock sync.RWMutex
statusLock sync.RWMutex wg sync.WaitGroup
wg sync.WaitGroup listeners []net.Listener
listeners []net.Listener workState *workState
workState *workState quit chan int
quit chan int
} }
// Start is used by server.go to start the rpc listener. // Start is used by server.go to start the rpc listener.
@ -353,10 +352,7 @@ func (s *rpcServer) writeHTTPResponseHeaders(req *http.Request, headers http.Hea
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (s *rpcServer) limitConnections(w http.ResponseWriter, remoteAddr string) bool { func (s *rpcServer) limitConnections(w http.ResponseWriter, remoteAddr string) bool {
s.numClientsMutex.Lock() if int(atomic.LoadInt32(&s.numClients)+1) > cfg.RPCMaxClients {
defer s.numClientsMutex.Unlock()
if s.numClients+1 > cfg.RPCMaxClients {
rpcsLog.Infof("Max RPC clients exceeded [%d] - "+ rpcsLog.Infof("Max RPC clients exceeded [%d] - "+
"disconnecting client %s", cfg.RPCMaxClients, "disconnecting client %s", cfg.RPCMaxClients,
remoteAddr) remoteAddr)
@ -373,10 +369,7 @@ func (s *rpcServer) limitConnections(w http.ResponseWriter, remoteAddr string) b
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (s *rpcServer) incrementClients() { func (s *rpcServer) incrementClients() {
s.numClientsMutex.Lock() atomic.AddInt32(&s.numClients, 1)
defer s.numClientsMutex.Unlock()
s.numClients++
} }
// decrementClients subtracts one from the number of connected RPC clients. // decrementClients subtracts one from the number of connected RPC clients.
@ -385,10 +378,7 @@ func (s *rpcServer) incrementClients() {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (s *rpcServer) decrementClients() { func (s *rpcServer) decrementClients() {
s.numClientsMutex.Lock() atomic.AddInt32(&s.numClients, -1)
defer s.numClientsMutex.Unlock()
s.numClients--
} }
// checkAuth checks the HTTP Basic authentication supplied by a wallet // checkAuth checks the HTTP Basic authentication supplied by a wallet