event.c 2.08 KB
Newer Older
1 2 3 4
/*
 * event queue implementation.
 *
 * This code is licensed under the GNU LGPL, version 2.1 or later.
Marc-André Lureau's avatar
Marc-André Lureau committed
5
 * See the COPYING file in the top-level directory.
6 7
 */

8
#include "glib-compat.h"
9 10 11 12 13 14 15 16 17 18

#include "vcard.h"
#include "vreader.h"
#include "vevent.h"

VEvent *
vevent_new(VEventType type, VReader *reader, VCard *card)
{
    VEvent *new_vevent;

19
    new_vevent = g_new(VEvent, 1);
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    new_vevent->next = NULL;
    new_vevent->type = type;
    new_vevent->reader = vreader_reference(reader);
    new_vevent->card = vcard_reference(card);

    return new_vevent;
}

void
vevent_delete(VEvent *vevent)
{
    if (vevent == NULL) {
        return;
    }
    vreader_free(vevent->reader);
    vcard_free(vevent->card);
36
    g_free(vevent);
37 38 39 40 41 42 43 44
}

/*
 * VEvent queue management
 */

static VEvent *vevent_queue_head;
static VEvent *vevent_queue_tail;
45 46
static CompatGMutex vevent_queue_lock;
static CompatGCond vevent_queue_condition;
47 48 49 50 51 52 53 54 55 56

void vevent_queue_init(void)
{
    vevent_queue_head = vevent_queue_tail = NULL;
}

void
vevent_queue_vevent(VEvent *vevent)
{
    vevent->next = NULL;
57
    g_mutex_lock(&vevent_queue_lock);
58 59 60 61 62 63 64
    if (vevent_queue_head) {
        assert(vevent_queue_tail);
        vevent_queue_tail->next = vevent;
    } else {
        vevent_queue_head = vevent;
    }
    vevent_queue_tail = vevent;
65 66
    g_cond_signal(&vevent_queue_condition);
    g_mutex_unlock(&vevent_queue_lock);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

/* must have lock */
static VEvent *
vevent_dequeue_vevent(void)
{
    VEvent *vevent = NULL;
    if (vevent_queue_head) {
        vevent = vevent_queue_head;
        vevent_queue_head = vevent->next;
        vevent->next = NULL;
    }
    return vevent;
}

VEvent *vevent_wait_next_vevent(void)
{
    VEvent *vevent;

86
    g_mutex_lock(&vevent_queue_lock);
87
    while ((vevent = vevent_dequeue_vevent()) == NULL) {
88
        g_cond_wait(&vevent_queue_condition, &vevent_queue_lock);
89
    }
90
    g_mutex_unlock(&vevent_queue_lock);
91 92 93 94 95 96 97
    return vevent;
}

VEvent *vevent_get_next_vevent(void)
{
    VEvent *vevent;

98
    g_mutex_lock(&vevent_queue_lock);
99
    vevent = vevent_dequeue_vevent();
100
    g_mutex_unlock(&vevent_queue_lock);
101 102 103
    return vevent;
}