-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathqueue.c
157 lines (137 loc) · 3.26 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* soliloque-server, an open source implementation of the TeamSpeak protocol.
* Copyright (C) 2009 Hugo Camboulive <hugo.camboulive AT gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "queue.h"
#include "log.h"
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
/**
* Create a new queue
*
* @return the newly allocated queue
*/
struct queue *new_queue()
{
struct queue *q = (struct queue *)calloc(sizeof(struct queue), 1);
pthread_mutex_init(&q->mutex, NULL);
return q;
}
void destroy_queue(struct queue *q)
{
if (q->first != NULL)
logger(LOG_ERR, "destroy_queue : destroyed a queue that was NOT empty! That should not happen!");
pthread_mutex_destroy(&q->mutex);
free(q);
}
/**
* Add an element at the end of a queue.
* Should be thread-safe
*
* @param q the queue
* @param elem the element
*/
void add_to_queue(struct queue *q, void *elem, size_t size)
{
struct q_elem *q_e;
q_e = (struct q_elem *)calloc(sizeof(struct q_elem), 1);
q_e->elem = elem;
q_e->size = size;
timerclear(&q_e->last_sent);
pthread_mutex_lock(&q->mutex);
if(q->first == NULL) {
q->first = q_e;
} else {
q_e->prev = q->last;
q->last->next = q_e;
}
q->last = q_e;
pthread_mutex_unlock(&q->mutex);
}
void queue_update_time(struct queue *q)
{
if (q->first != NULL)
gettimeofday(&q->first->last_sent, NULL);
}
struct timeval * queue_get_time(struct queue *q)
{
if (q->first != NULL)
return &q->first->last_sent;
return NULL;
}
/**
* Get an element from the beginning of the
* queue and remove it.
* Should be thread-safe.
*
* @param q the queue
*
* @return the element
*/
void *get_from_queue(struct queue *q)
{
void *elem;
struct q_elem *new_first;
struct q_elem *old_first;
old_first = q->first;
if(old_first == NULL) { /* empty queue */
elem = NULL;
} else {
new_first = old_first->next; /* NULL or the n-1th element */
if(new_first == NULL) {
/* only one element */
q->last = NULL;
q->first = NULL;
} else {
/* retrieve the data and free the container */
q->first = new_first;
new_first->prev = NULL;
}
elem = old_first->elem;
free(old_first);
}
return elem;
}
/**
* Peek at the first element of the queue.
* NB : the queue mutex has to be locked MANUALLY
* when using the peek function.
*
* @param q the queue
*
* @return the element
*/
void *peek_at_queue(struct queue *q)
{
void *elem;
if(q->first == 0) { /* empty queue */
elem = NULL;
} else {
elem = q->first->elem; /* retrieve the data */
}
return elem;
}
size_t peek_at_size(struct queue *q)
{
size_t size;
if(q->first == 0) { /* empty queue */
size = 0;
} else {
size = q->first->size; /* retrieve the data */
}
return size;
}