about summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-05-19 09:50:21 +0000
committerDrew DeVault <sir@cmpwn.com>2019-05-19 11:51:25 -0400
commit248345d875e958b70f48797bfe6e822cc9485eb8 (patch)
tree4c46736cc4d1c80c661203c275a77d06ae0afa28 /worker
parentf27db333052aa24a0aad135c09ce7969bf31bc56 (diff)
downloadaerc-248345d875e958b70f48797bfe6e822cc9485eb8.tar.gz
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).
Diffstat (limited to 'worker')
-rw-r--r--worker/types/worker.go57
1 files changed, 21 insertions, 36 deletions
diff --git a/worker/types/worker.go b/worker/types/worker.go
index 2ca142b..38140e0 100644
--- a/worker/types/worker.go
+++ b/worker/types/worker.go
@@ -2,7 +2,6 @@ package types
 
 import (
 	"log"
-	"sync"
 	"sync/atomic"
 )
 
@@ -18,16 +17,17 @@ type Worker struct {
 	Messages chan WorkerMessage
 	Logger   *log.Logger
 
-	callbacks map[int64]func(msg WorkerMessage) // protected by mutex
-	mutex     sync.Mutex
+	actionCallbacks  map[int64]func(msg WorkerMessage)
+	messageCallbacks map[int64]func(msg WorkerMessage)
 }
 
 func NewWorker(logger *log.Logger) *Worker {
 	return &Worker{
-		Actions:   make(chan WorkerMessage, 50),
-		Messages:  make(chan WorkerMessage, 50),
-		Logger:    logger,
-		callbacks: make(map[int64]func(msg WorkerMessage)),
+		Actions:          make(chan WorkerMessage, 50),
+		Messages:         make(chan WorkerMessage, 50),
+		Logger:           logger,
+		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)
 }
 
-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,
 	cb func(msg WorkerMessage)) {
 
@@ -71,7 +48,9 @@ func (worker *Worker) PostAction(msg WorkerMessage,
 	}
 	worker.Actions <- msg
 
-	worker.setCallback(msg, cb)
+	if cb != nil {
+		worker.actionCallbacks[msg.getId()] = cb
+	}
 }
 
 func (worker *Worker) PostMessage(msg WorkerMessage,
@@ -86,7 +65,9 @@ func (worker *Worker) PostMessage(msg WorkerMessage,
 	}
 	worker.Messages <- msg
 
-	worker.setCallback(msg, cb)
+	if cb != nil {
+		worker.messageCallbacks[msg.getId()] = cb
+	}
 }
 
 func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
@@ -95,8 +76,10 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage {
 	} else {
 		worker.Logger.Printf("(ui)<= %T\n", msg)
 	}
-	if cb, ok := worker.getCallback(msg.InResponseTo()); ok {
-		cb(msg)
+	if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
+		if f, ok := worker.actionCallbacks[inResponseTo.getId()]; ok {
+			f(msg)
+		}
 	}
 	return msg
 }
@@ -107,8 +90,10 @@ func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage {
 	} else {
 		worker.Logger.Printf("<-(ui) %T\n", msg)
 	}
-	if cb, ok := worker.getCallback(msg.InResponseTo()); ok {
-		cb(msg)
+	if inResponseTo := msg.InResponseTo(); inResponseTo != nil {
+		if f, ok := worker.messageCallbacks[inResponseTo.getId()]; ok {
+			f(msg)
+		}
 	}
 	return msg
 }
0d4182397cfea'>^
33352536 ^
c2a74205 ^
33352536 ^


6070c23e ^
c2a74205 ^
71eb22a5 ^
33352536 ^

2a2a5b1e ^
33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^


33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^
33352536 ^




7a583220 ^
33352536 ^

c2a74205 ^









33352536 ^
c2a74205 ^
33352536 ^

c2a74205 ^










33352536 ^
686a52bd ^
c2a74205 ^
686a52bd ^
c2a74205 ^


33352536 ^
c2a74205 ^





33352536 ^
c2a74205 ^






33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^





33352536 ^
c2a74205 ^







33352536 ^
c2a74205 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159