-
Notifications
You must be signed in to change notification settings - Fork 30
/
queue.c
71 lines (60 loc) · 1.55 KB
/
queue.c
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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
#include "queue.h"
#include <assert.h>
/*
* Single producer, single consumer queue, thread-safe.
* Draining is handled externally by sending a sentinel down the queue.
*/
void
queue_init(queue_t *queue) {
int rv;
assert(queue);
tommy_list_init(&queue->list);
rv = pthread_mutex_init(&queue->mutex, NULL);
assert(rv == 0);
rv = pthread_cond_init(&queue->notempty, NULL);
assert(rv == 0);
queue->size = 0;
}
void
queue_destroy(queue_t *queue) {
assert(queue);
(void)pthread_cond_destroy(&queue->notempty);
(void)pthread_mutex_destroy(&queue->mutex);
}
void
queue_enqueue(queue_t *queue, tommy_node *node, void *data) {
assert(queue);
assert(node);
assert(data);
pthread_mutex_lock(&queue->mutex);
tommy_list_insert_tail(&queue->list, node, data);
queue->size++;
pthread_mutex_unlock(&queue->mutex);
pthread_cond_signal(&queue->notempty);
}
void *
queue_dequeue(queue_t *queue) {
void *data;
assert(queue);
pthread_mutex_lock(&queue->mutex);
while (tommy_list_empty(&queue->list)) {
pthread_cond_wait(&queue->notempty, &queue->mutex);
}
assert(!tommy_list_empty(&queue->list));
assert(queue->size > 0);
data = tommy_list_remove_existing(&queue->list,
tommy_list_head(&queue->list));
queue->size--;
pthread_mutex_unlock(&queue->mutex);
assert(data);
return data;
}