forked from grailbio/base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifo.go
56 lines (49 loc) · 1.12 KB
/
lifo.go
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
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package syncqueue
import (
"sync"
)
// LIFO implements a last-in, first-out producer-consumer queue. Thread safe.
type LIFO struct {
mu sync.Mutex
cond *sync.Cond
queue []interface{}
closed bool
}
// NewFIFO creates an empty FIFO queue.
func NewLIFO() *LIFO {
q := &LIFO{}
q.cond = sync.NewCond(&q.mu)
return q
}
// Put adds the object in the queue.
func (q *LIFO) Put(v interface{}) {
q.mu.Lock()
q.queue = append(q.queue, v)
q.cond.Signal()
q.mu.Unlock()
}
// Close informs the queue that no more objects will be added via Put().
func (q *LIFO) Close() {
q.closed = true
q.cond.Broadcast()
}
// Get removes the newest object added to the queue. It blocks the caller if
// the queue is empty.
func (q *LIFO) Get() (interface{}, bool) {
q.mu.Lock()
for !q.closed && len(q.queue) == 0 {
q.cond.Wait()
}
var v interface{}
var ok bool
if n := len(q.queue); n > 0 {
v = q.queue[n-1]
q.queue = q.queue[:n-1]
ok = true
}
q.mu.Unlock()
return v, ok
}