From d069ae11f433077b6ac6b16a7322e13907a0feb8 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Tue, 16 Dec 2014 22:24:54 -0800 Subject: [PATCH] refactor: put mutex next to the things it protects If we put the lock next to the fields it protects, it can sometimes make it easier to reason about threadsafety. In this case, it reveals that the task queue (not threadsafe) isn't protected by the mutex, yet shared between the worker and callers. @whyrusleeping License: MIT Signed-off-by: Brian Tiger Chow --- exchange/bitswap/strategy/ledgermanager.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/exchange/bitswap/strategy/ledgermanager.go b/exchange/bitswap/strategy/ledgermanager.go index 258f92fd1..92e6ea9c2 100644 --- a/exchange/bitswap/strategy/ledgermanager.go +++ b/exchange/bitswap/strategy/ledgermanager.go @@ -22,15 +22,19 @@ type Envelope struct { } type LedgerManager struct { + // FIXME taskqueue isn't threadsafe nor is it protected by a mutex. consider + // a way to avoid sharing the taskqueue between the worker and the receiver + taskqueue *taskQueue + + workSignal chan struct{} + + outbox chan Envelope + + bs bstore.Blockstore + lock sync.RWMutex // ledgerMap lists Ledgers by their Partner key. ledgerMap map[u.Key]*ledger - bs bstore.Blockstore - // FIXME taskqueue isn't threadsafe nor is it protected by a mutex. consider - // a way to avoid sharing the taskqueue between the worker and the receiver - taskqueue *taskQueue - outbox chan Envelope - workSignal chan struct{} } func NewLedgerManager(ctx context.Context, bs bstore.Blockstore) *LedgerManager {