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 <brian@perfmode.com>
Brian Tiger Chow committed
Dec 16, 2014 at 22:24 UTC
d069ae11f433077b6ac6b16a7322e13907a0feb8
1 file changed
+10
-6
exchange/bitswap/strategy/ledgermanager.go
+10
-6
@@ -22,15 +22,19 @@ type Envelope struct {
22
}
23
24
type LedgerManager struct {
25
- lock sync.RWMutex
26
- // ledgerMap lists Ledgers by their Partner key.
27
- ledgerMap map[u.Key]*ledger
28
- bs bstore.Blockstore
25
// FIXME taskqueue isn't threadsafe nor is it protected by a mutex. consider
26
// a way to avoid sharing the taskqueue between the worker and the receiver
31
- taskqueue *taskQueue
32
- outbox chan Envelope
27
+ taskqueue *taskQueue
28
+
29
workSignal chan struct{}
30
+
31
+ outbox chan Envelope
32
+
33
+ bs bstore.Blockstore
34
+
35
+ lock sync.RWMutex
36
+ // ledgerMap lists Ledgers by their Partner key.
37
+ ledgerMap map[u.Key]*ledger
38
}
39
40
func NewLedgerManager(ctx context.Context, bs bstore.Blockstore) *LedgerManager {