~ubuntu-branches/ubuntu/saucy/geary/saucy-updates

« back to all changes in this revision

Viewing changes to src/engine/nonblocking/nonblocking-mailbox.vala

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-03-14 13:48:23 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130314134823-gyk5av1g508zyj8a
Tags: 0.3.0~pr1-0ubuntu1
New upstream version (FFE lp: #1154316), supports multiple account as
well as full conversation views with inline replies

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
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; }
9
11
    
10
 
    private Gee.List<G> queue;
 
12
    private Gee.Queue<G> queue;
11
13
    private NonblockingSpinlock spinlock = new NonblockingSpinlock();
12
14
    
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>();
 
19
        else
 
20
            queue = new Gee.PriorityQueue<G>(comparator);
15
21
    }
16
22
    
17
 
    public void send(G msg) throws Error {
18
 
        queue.add(msg);
19
 
        spinlock.notify();
 
23
    public bool send(G msg) {
 
24
        if (!allow_duplicates && queue.contains(msg)) {
 
25
            if (requeue_duplicate)
 
26
                queue.remove(msg);
 
27
            else
 
28
                return false;
 
29
        }
 
30
        
 
31
        if (!queue.offer(msg))
 
32
            return false;
 
33
        
 
34
        spinlock.blind_notify();
 
35
        
 
36
        return true;
20
37
    }
21
38
    
22
39
    /**
23
40
     * Returns true if the message was revoked.
24
41
     */
25
 
    public bool revoke(G msg) throws Error {
 
42
    public bool revoke(G msg) {
26
43
        return queue.remove(msg);
27
44
    }
28
45
    
 
46
    /**
 
47
     * Returns number of removed items.
 
48
     */
 
49
    public int clear() {
 
50
        int count = queue.size;
 
51
        if (count != 0)
 
52
            queue.clear();
 
53
        
 
54
        return count;
 
55
    }
 
56
    
29
57
    public async G recv_async(Cancellable? cancellable = null) throws Error {
30
58
        for (;;) {
31
59
            if (queue.size > 0)
32
 
                return queue.remove_at(0);
 
60
                return queue.poll();
33
61
            
34
62
            yield spinlock.wait_async(cancellable);
35
63
        }
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.
44
72
     */
45
 
    public Gee.List<G> get_all() {
 
73
    public Gee.Collection<G> get_all() {
46
74
        return queue.read_only_view;
47
75
    }
48
76
}