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:19 +0000
committerDrew DeVault <sir@cmpwn.com>2019-05-19 11:51:22 -0400
commitf27db333052aa24a0aad135c09ce7969bf31bc56 (patch)
tree92acd47ded90d7c1a5e0c283c446122ff9aef594 /worker
parent34dd6bc6354cd3592ed51da83de1a0839e08a37d (diff)
downloadaerc-f27db333052aa24a0aad135c09ce7969bf31bc56.tar.gz
worker/types/worker: make ID allocation atomic
Message IDs are allocated for both messages from UI to workers and the other
way around. Hence, the global nextId variable is accessed from multiple
goroutines.

Instead, use atomic to access the global counter.
Diffstat (limited to 'worker')
-rw-r--r--worker/types/messages.go10
-rw-r--r--worker/types/worker.go11
2 files changed, 11 insertions, 10 deletions
diff --git a/worker/types/messages.go b/worker/types/messages.go
index 0cb6eeb..eb54a15 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -12,13 +12,13 @@ import (
 
 type WorkerMessage interface {
 	InResponseTo() WorkerMessage
-	getId() int
-	setId(id int)
+	getId() int64
+	setId(id int64)
 }
 
 type Message struct {
 	inResponseTo WorkerMessage
-	id           int
+	id           int64
 }
 
 func RespondTo(msg WorkerMessage) Message {
@@ -31,11 +31,11 @@ func (m Message) InResponseTo() WorkerMessage {
 	return m.inResponseTo
 }
 
-func (m Message) getId() int {
+func (m Message) getId() int64 {
 	return m.id
 }
 
-func (m Message) setId(id int) {
+func (m Message) setId(id int64) {
 	m.id = id
 }
 
diff --git a/worker/types/worker.go b/worker/types/worker.go
index 5647ae3..2ca142b 100644
--- a/worker/types/worker.go
+++ b/worker/types/worker.go
@@ -3,9 +3,10 @@ package types
 import (
 	"log"
 	"sync"
+	"sync/atomic"
 )
 
-var nextId int = 1
+var lastId int64 = 1 // access via atomic
 
 type Backend interface {
 	Run()
@@ -17,7 +18,7 @@ type Worker struct {
 	Messages chan WorkerMessage
 	Logger   *log.Logger
 
-	callbacks map[int]func(msg WorkerMessage) // protected by mutex
+	callbacks map[int64]func(msg WorkerMessage) // protected by mutex
 	mutex     sync.Mutex
 }
 
@@ -26,13 +27,13 @@ func NewWorker(logger *log.Logger) *Worker {
 		Actions:   make(chan WorkerMessage, 50),
 		Messages:  make(chan WorkerMessage, 50),
 		Logger:    logger,
-		callbacks: make(map[int]func(msg WorkerMessage)),
+		callbacks: make(map[int64]func(msg WorkerMessage)),
 	}
 }
 
 func (worker *Worker) setId(msg WorkerMessage) {
-	msg.setId(nextId)
-	nextId++
+	id := atomic.AddInt64(&lastId, 1)
+	msg.setId(id)
 }
 
 func (worker *Worker) setCallback(msg WorkerMessage,
n236'>236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375