Branch data Line data Source code
1 : : /*
2 : : * event queue implementation.
3 : : *
4 : : * This code is licensed under the GNU LGPL, version 2.1 or later.
5 : : * See the COPYING file in the top-level directory.
6 : : */
7 : :
8 : : #include <glib.h>
9 : :
10 : : #include "vcard.h"
11 : : #include "vreader.h"
12 : : #include "vevent.h"
13 : :
14 : : VEvent *
15 : 28 : vevent_new(VEventType type, VReader *reader, VCard *card)
16 : : {
17 : : VEvent *new_vevent;
18 : :
19 : 28 : new_vevent = g_new(VEvent, 1);
20 : 28 : new_vevent->next = NULL;
21 : 28 : new_vevent->type = type;
22 : 28 : new_vevent->reader = vreader_reference(reader);
23 : 28 : new_vevent->card = vcard_reference(card);
24 : :
25 : 28 : return new_vevent;
26 : : }
27 : :
28 : : void
29 : 26 : vevent_delete(VEvent *vevent)
30 : : {
31 [ + - ]: 26 : if (vevent == NULL) {
32 : : return;
33 : : }
34 : 26 : vreader_free(vevent->reader);
35 : 26 : vcard_free(vevent->card);
36 : 26 : g_free(vevent);
37 : : }
38 : :
39 : : /*
40 : : * VEvent queue management
41 : : */
42 : :
43 : : static VEvent *vevent_queue_head;
44 : : static VEvent *vevent_queue_tail;
45 : : static GMutex vevent_queue_lock;
46 : : static GCond vevent_queue_condition;
47 : :
48 : 7 : void vevent_queue_init(void)
49 : : {
50 : 7 : vevent_queue_head = vevent_queue_tail = NULL;
51 : 7 : }
52 : :
53 : : void
54 : 28 : vevent_queue_vevent(VEvent *vevent)
55 : : {
56 : 28 : vevent->next = NULL;
57 : 28 : g_mutex_lock(&vevent_queue_lock);
58 [ + + ]: 28 : if (vevent_queue_head) {
59 [ - + ]: 9 : assert(vevent_queue_tail);
60 : 9 : vevent_queue_tail->next = vevent;
61 : : } else {
62 : 19 : vevent_queue_head = vevent;
63 : : }
64 : 28 : vevent_queue_tail = vevent;
65 : 28 : g_cond_signal(&vevent_queue_condition);
66 : 28 : g_mutex_unlock(&vevent_queue_lock);
67 : 28 : }
68 : :
69 : : /* must have lock */
70 : : static VEvent *
71 : : vevent_dequeue_vevent(void)
72 : : {
73 : : VEvent *vevent = NULL;
74 [ - - + + ]: 44 : if (vevent_queue_head) {
75 : : vevent = vevent_queue_head;
76 : 26 : vevent_queue_head = vevent->next;
77 : 0 : vevent->next = NULL;
78 : : }
79 : : return vevent;
80 : : }
81 : :
82 : 26 : VEvent *vevent_wait_next_vevent(void)
83 : : {
84 : : VEvent *vevent;
85 : :
86 : 26 : g_mutex_lock(&vevent_queue_lock);
87 : 26 : while ((vevent = vevent_dequeue_vevent()) == NULL) {
88 : 18 : g_cond_wait(&vevent_queue_condition, &vevent_queue_lock);
89 : : }
90 : 26 : g_mutex_unlock(&vevent_queue_lock);
91 : 26 : return vevent;
92 : : }
93 : :
94 : 0 : VEvent *vevent_get_next_vevent(void)
95 : : {
96 : : VEvent *vevent;
97 : :
98 : 0 : g_mutex_lock(&vevent_queue_lock);
99 : : vevent = vevent_dequeue_vevent();
100 : 0 : g_mutex_unlock(&vevent_queue_lock);
101 : 0 : return vevent;
102 : : }
103 : :
|