From f9ab3f4b8157a42d657735101bc2c768c663e814 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 28 Jun 2021 13:31:21 +0100 Subject: [PATCH 1/2] Tweaks --- roomserver/internal/input/input_fifo.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/roomserver/internal/input/input_fifo.go b/roomserver/internal/input/input_fifo.go index d8c075d2..d6f4b6d1 100644 --- a/roomserver/internal/input/input_fifo.go +++ b/roomserver/internal/input/input_fifo.go @@ -5,7 +5,7 @@ import ( ) type fifoQueue struct { - frames []*inputTask + tasks []*inputTask count int mutex sync.Mutex notifs chan struct{} @@ -18,16 +18,15 @@ func newFIFOQueue() *fifoQueue { return q } -func (q *fifoQueue) push(frame *inputTask) bool { +func (q *fifoQueue) push(frame *inputTask) { q.mutex.Lock() defer q.mutex.Unlock() - q.frames = append(q.frames, frame) + q.tasks = append(q.tasks, frame) q.count++ select { case q.notifs <- struct{}{}: default: } - return true } func (q *fifoQueue) pop() (*inputTask, bool) { @@ -36,14 +35,14 @@ func (q *fifoQueue) pop() (*inputTask, bool) { if q.count == 0 { return nil, false } - frame := q.frames[0] - q.frames[0] = nil - q.frames = q.frames[1:] + frame := q.tasks[0] + q.tasks[0] = nil + q.tasks = q.tasks[1:] q.count-- if q.count == 0 { // Force a GC of the underlying array, since it might have // grown significantly if the queue was hammered for some reason - q.frames = nil + q.tasks = nil } return frame, true } @@ -51,7 +50,7 @@ func (q *fifoQueue) pop() (*inputTask, bool) { func (q *fifoQueue) wait() <-chan struct{} { q.mutex.Lock() defer q.mutex.Unlock() - if q.count > 0 { + if q.count > 0 && len(q.notifs) == 0 { ch := make(chan struct{}) close(ch) return ch From 4a37b19a8f6fe8af02e979827253d22a0ccdedb8 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 28 Jun 2021 13:34:54 +0100 Subject: [PATCH 2/2] Add comments --- roomserver/internal/input/input_fifo.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/roomserver/internal/input/input_fifo.go b/roomserver/internal/input/input_fifo.go index d6f4b6d1..694b1724 100644 --- a/roomserver/internal/input/input_fifo.go +++ b/roomserver/internal/input/input_fifo.go @@ -29,6 +29,9 @@ func (q *fifoQueue) push(frame *inputTask) { } } +// pop returns the first item of the queue, if there is one. +// The second return value will indicate if a task was returned. +// You must check this value, even after calling wait(). func (q *fifoQueue) pop() (*inputTask, bool) { q.mutex.Lock() defer q.mutex.Unlock() @@ -47,6 +50,8 @@ func (q *fifoQueue) pop() (*inputTask, bool) { return frame, true } +// wait returns a channel which can be used to detect when an +// item is waiting in the queue. func (q *fifoQueue) wait() <-chan struct{} { q.mutex.Lock() defer q.mutex.Unlock()