7
7
public class Geary.NonblockingMailbox<G> : Object {
8
8
public int size { get { return queue.size; } }
9
public bool allow_duplicates { get; set; default = true; }
10
public bool requeue_duplicate { get; set; default = false; }
10
private Gee.List<G> queue;
12
private Gee.Queue<G> queue;
11
13
private NonblockingSpinlock spinlock = new NonblockingSpinlock();
13
public NonblockingMailbox() {
14
queue = new Gee.LinkedList<G>();
15
public NonblockingMailbox(CompareFunc<G>? comparator = null) {
16
// can't use ternary here, Vala bug
17
if (comparator == null)
18
queue = new Gee.LinkedList<G>();
20
queue = new Gee.PriorityQueue<G>(comparator);
17
public void send(G msg) throws Error {
23
public bool send(G msg) {
24
if (!allow_duplicates && queue.contains(msg)) {
25
if (requeue_duplicate)
31
if (!queue.offer(msg))
34
spinlock.blind_notify();
23
40
* Returns true if the message was revoked.
25
public bool revoke(G msg) throws Error {
42
public bool revoke(G msg) {
26
43
return queue.remove(msg);
47
* Returns number of removed items.
50
int count = queue.size;
29
57
public async G recv_async(Cancellable? cancellable = null) throws Error {
31
59
if (queue.size > 0)
32
return queue.remove_at(0);
34
62
yield spinlock.wait_async(cancellable);
42
70
* This returns a read-only list in queue-order. Altering will not affect the queue. Use
43
71
* revoke() to remove enqueued operations.
45
public Gee.List<G> get_all() {
73
public Gee.Collection<G> get_all() {
46
74
return queue.read_only_view;