1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

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>
This commit is contained in:
Brian Tiger Chow
2014-12-16 22:24:54 -08:00
committed by Juan Batiz-Benet
parent 962a9477cc
commit d069ae11f4

View File

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