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