summaryrefslogtreecommitdiff
path: root/src/uasyncqueue.c
blob: f7a7fbe449eee7fdb2afb52bdc6eda37bb5a6e34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *  uasyncqueue.c - Asynchronous queues
 *
 *  libva-driver-vdpau (C) 2009-2011 Splitted-Desktop Systems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include "sysdeps.h"
#include "uasyncqueue.h"
#include "uqueue.h"
#include <pthread.h>

struct _UAsyncQueue {
    UQueue             *queue;
    pthread_mutex_t     mutex;
    pthread_cond_t      cond;
    unsigned int        is_waiting;
};

UAsyncQueue *async_queue_new(void)
{
    UAsyncQueue *queue = malloc(sizeof(*queue));

    if (!queue)
        return NULL;

    queue->queue = queue_new();
    if (!queue->queue)
        goto error;

    if (pthread_cond_init(&queue->cond, NULL) != 0)
        goto error;

    pthread_mutex_init(&queue->mutex, NULL);
    queue->is_waiting = 0;
    return queue;

error:
    async_queue_free(queue);
    return NULL;
}

void async_queue_free(UAsyncQueue *queue)
{
    if (!queue)
        return;

    pthread_mutex_unlock(&queue->mutex);
    queue_free(queue->queue);
    free(queue);
}

int async_queue_is_empty(UAsyncQueue *queue)
{
    return queue && queue_is_empty(queue->queue);
}

static UAsyncQueue *async_queue_push_unlocked(UAsyncQueue *queue, void *data)
{
    queue_push(queue->queue, data);
    if (queue->is_waiting)
        pthread_cond_signal(&queue->cond);
    return queue;
}

UAsyncQueue *async_queue_push(UAsyncQueue *queue, void *data)
{
    if (!queue)
        return NULL;

    pthread_mutex_lock(&queue->mutex);
    async_queue_push_unlocked(queue, data);
    pthread_mutex_unlock(&queue->mutex);
    return queue;
}

static void *
async_queue_timed_pop_unlocked(UAsyncQueue *queue, uint64_t end_time)
{
    if (queue_is_empty(queue->queue)) {
        assert(!queue->is_waiting);
        ++queue->is_waiting;
        if (!end_time)
            pthread_cond_wait(&queue->cond, &queue->mutex);
        else {
            struct timespec timeout;
            timeout.tv_sec  = end_time / 1000000;
            timeout.tv_nsec = 1000 * (end_time % 1000000);
            pthread_cond_timedwait(&queue->cond, &queue->mutex, &timeout);
        }
        --queue->is_waiting;
        if (queue_is_empty(queue->queue))
            return NULL;
    }
    return queue_pop(queue->queue);
}

void *async_queue_timed_pop(UAsyncQueue *queue, uint64_t end_time)
{
    void *data;

    if (!queue)
        return NULL;

    pthread_mutex_lock(&queue->mutex);
    data = async_queue_timed_pop_unlocked(queue, end_time);
    pthread_mutex_unlock(&queue->mutex);
    return data;
}