worker/types/worker: remove mutex

Worker.callbacks contains two types of callbacks: some are action callbacks,
some are message callbacks. Each of those is access from one side of the
communication channel (UI goroutine vs. worker goroutine).

Instead of using a channel, we can use two different maps for each kind. This
simplifies the code and also ensures we don't call an action callback instead
of a message callback (or the other way around).
This commit is contained in:
Simon Ser 2019-05-19 09:50:21 +00:00 committed by Drew DeVault
parent f27db33305
commit 248345d875

View file

@ -2,7 +2,6 @@ package types
import ( import (
"log" "log"
"sync"
"sync/atomic" "sync/atomic"
) )
@ -18,16 +17,17 @@ type Worker struct {
Messages chan WorkerMessage Messages chan WorkerMessage
Logger *log.Logger Logger *log.Logger
callbacks map[int64]func(msg WorkerMessage) // protected by mutex actionCallbacks map[int64]func(msg WorkerMessage)
mutex sync.Mutex messageCallbacks map[int64]func(msg WorkerMessage)
} }
func NewWorker(logger *log.Logger) *Worker { func NewWorker(logger *log.Logger) *Worker {
return &Worker{ return &Worker{
Actions: make(chan WorkerMessage, 50), Actions: make(chan WorkerMessage, 50),
Messages: make(chan WorkerMessage, 50), Messages: make(chan WorkerMessage, 50),
Logger: logger, Logger: logger,
callbacks: make(map[int64]func(msg WorkerMessage)), actionCallbacks: make(map[int64]func(msg WorkerMessage)),
messageCallbacks: make(map[int64]func(msg WorkerMessage)),
} }
} }
@ -36,29 +36,6 @@ func (worker *Worker) setId(msg WorkerMessage) {
msg.setId(id) msg.setId(id)
} }
func (worker *Worker) setCallback(msg WorkerMessage,
cb func(msg WorkerMessage)) {
if cb != nil {
worker.mutex.Lock()
worker.callbacks[msg.getId()] = cb
worker.mutex.Unlock()
}
}
func (worker *Worker) getCallback(msg WorkerMessage) (func(msg WorkerMessage),
bool) {
if msg == nil {
return nil, false
}
worker.mutex.Lock()
cb, ok := worker.callbacks[msg.getId()]
worker.mutex.Unlock()
return cb, ok
}
func (worker *Worker) PostAction(msg WorkerMessage, func (worker *Worker) PostAction(msg WorkerMessage,
cb func(msg WorkerMessage)) { cb func(msg WorkerMessage)) {
@ -71,7 +48,9 @@ func (worker *Worker) PostAction(msg WorkerMessage,
} }
worker.Actions <- msg worker.Actions <- msg
worker.setCallback(msg, cb) if cb != nil {
worker.actionCallbacks[msg.getId()] = cb
}
} }
func (worker *Worker) PostMessage(msg WorkerMessage, func (worker *Worker) PostMessage(msg WorkerMessage,
@ -86,7 +65,9 @@ func (worker *Worker) PostMessage(msg WorkerMessage,
} }
worker.Messages <- msg worker.Messages <- msg
worker.setCallback(msg, cb) if cb != nil {
worker.messageCallbacks[msg.getId()] = cb
}
} }
func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage { func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
@ -95,8 +76,10 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
} else { } else {
worker.Logger.Printf("(ui)<= %T\n", msg) worker.Logger.Printf("(ui)<= %T\n", msg)
} }
if cb, ok := worker.getCallback(msg.InResponseTo()); ok { if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
cb(msg) if f, ok := worker.actionCallbacks[inResponseTo.getId()]; ok {
f(msg)
}
} }
return msg return msg
} }
@ -107,8 +90,10 @@ func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage {
} else { } else {
worker.Logger.Printf("<-(ui) %T\n", msg) worker.Logger.Printf("<-(ui) %T\n", msg)
} }
if cb, ok := worker.getCallback(msg.InResponseTo()); ok { if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
cb(msg) if f, ok := worker.messageCallbacks[inResponseTo.getId()]; ok {
f(msg)
}
} }
return msg return msg
} }