Merge branch 'upstream-merge' into next
[qemu-dev-zwu.git] / libcacard / event.c
blobbb2f9219f9b43b3ae4017dd2bcc8a7b7067e176d
1 /*
2 * event queue implementation.
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 */
8 #include "qemu-common.h"
9 #include "qemu-thread.h"
11 #include "vcard.h"
12 #include "vreader.h"
13 #include "vevent.h"
15 VEvent *
16 vevent_new(VEventType type, VReader *reader, VCard *card)
18 VEvent *new_vevent;
20 new_vevent = (VEvent *)qemu_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);
26 return new_vevent;
29 void
30 vevent_delete(VEvent *vevent)
32 if (vevent == NULL) {
33 return;
35 vreader_free(vevent->reader);
36 vcard_free(vevent->card);
37 qemu_free(vevent);
41 * VEvent queue management
44 static VEvent *vevent_queue_head;
45 static VEvent *vevent_queue_tail;
46 static QemuMutex vevent_queue_lock;
47 static QemuCond vevent_queue_condition;
49 void vevent_queue_init(void)
51 qemu_mutex_init(&vevent_queue_lock);
52 qemu_cond_init(&vevent_queue_condition);
53 vevent_queue_head = vevent_queue_tail = NULL;
56 void
57 vevent_queue_vevent(VEvent *vevent)
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;
67 vevent_queue_tail = vevent;
68 qemu_cond_signal(&vevent_queue_condition);
69 qemu_mutex_unlock(&vevent_queue_lock);
72 /* must have lock */
73 static VEvent *
74 vevent_dequeue_vevent(void)
76 VEvent *vevent = NULL;
77 if (vevent_queue_head) {
78 vevent = vevent_queue_head;
79 vevent_queue_head = vevent->next;
80 vevent->next = NULL;
82 return vevent;
85 VEvent *vevent_wait_next_vevent(void)
87 VEvent *vevent;
89 qemu_mutex_lock(&vevent_queue_lock);
90 while ((vevent = vevent_dequeue_vevent()) == NULL) {
91 qemu_cond_wait(&vevent_queue_condition, &vevent_queue_lock);
93 qemu_mutex_unlock(&vevent_queue_lock);
94 return vevent;
97 VEvent *vevent_get_next_vevent(void)
99 VEvent *vevent;
101 qemu_mutex_lock(&vevent_queue_lock);
102 vevent = vevent_dequeue_vevent();
103 qemu_mutex_unlock(&vevent_queue_lock);
104 return vevent;