1 /* A (synchronized) message queue. */
2 /* Popping an empty queue is blocking, as well as pushing a full one */
4 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
13 #include "xbt/dynar.h"
15 #include "xbt/synchro.h"
16 #include "xbt/queue.h" /* this module */
17 #include "gras/virtu.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_queue, xbt,
19 "Message exchanging queue");
21 typedef struct s_xbt_queue_ {
25 xbt_cond_t not_full, not_empty;
28 /** @brief Create a new message exchange queue.
30 * @param capacity the capacity of the queue. If non-nul, any attempt to push an item which would let the size of the queue over this number will be blocking until someone else pop some data
31 * @param elm_size size of each element stored in it (see #xbt_dynar_new)
33 xbt_queue_t xbt_queue_new(int capacity, unsigned long elm_size)
35 xbt_queue_t res = xbt_new0(s_xbt_queue_t, 1);
36 xbt_assert0(capacity >= 0, "Capacity cannot be negative");
38 res->capacity = capacity;
39 res->data = xbt_dynar_new(elm_size, NULL);
40 res->mutex = xbt_mutex_init();
41 res->not_full = xbt_cond_init();
42 res->not_empty = xbt_cond_init();
46 /** @brief Destroy a message exchange queue.
48 * Any remaining content is leaked.
50 void xbt_queue_free(xbt_queue_t * queue)
53 xbt_dynar_free(&((*queue)->data));
54 xbt_mutex_destroy((*queue)->mutex);
55 xbt_cond_destroy((*queue)->not_full);
56 xbt_cond_destroy((*queue)->not_empty);
61 /** @brief Get the queue size */
62 unsigned long xbt_queue_length(const xbt_queue_t queue)
65 xbt_mutex_acquire(queue->mutex);
66 res = xbt_dynar_length(queue->data);
67 xbt_mutex_release(queue->mutex);
71 /** @brief Push something to the message exchange queue.
73 * This is blocking if the declared capacity is non-nul, and if this amount is reached.
75 * @see #xbt_dynar_push
77 void xbt_queue_push(xbt_queue_t queue, const void *src)
79 xbt_mutex_acquire(queue->mutex);
80 while (queue->capacity != 0
81 && queue->capacity == xbt_dynar_length(queue->data)) {
82 DEBUG2("Capacity of %p exceded (=%d). Waiting", queue,
84 xbt_cond_wait(queue->not_full, queue->mutex);
86 xbt_dynar_push(queue->data, src);
87 xbt_cond_signal(queue->not_empty);
88 xbt_mutex_release(queue->mutex);
92 /** @brief Pop something from the message exchange queue.
94 * This is blocking if the queue is empty.
99 void xbt_queue_pop(xbt_queue_t queue, void *const dst)
101 xbt_mutex_acquire(queue->mutex);
102 while (xbt_dynar_length(queue->data) == 0) {
103 DEBUG1("Queue %p empty. Waiting", queue);
104 xbt_cond_wait(queue->not_empty, queue->mutex);
106 xbt_dynar_pop(queue->data, dst);
107 xbt_cond_signal(queue->not_full);
108 xbt_mutex_release(queue->mutex);
111 /** @brief Unshift something to the message exchange queue.
113 * This is blocking if the declared capacity is non-nul, and if this amount is reached.
115 * @see #xbt_dynar_unshift
117 void xbt_queue_unshift(xbt_queue_t queue, const void *src)
119 xbt_mutex_acquire(queue->mutex);
120 while (queue->capacity != 0
121 && queue->capacity == xbt_dynar_length(queue->data)) {
122 DEBUG2("Capacity of %p exceded (=%d). Waiting", queue,
124 xbt_cond_wait(queue->not_full, queue->mutex);
126 xbt_dynar_unshift(queue->data, src);
127 xbt_cond_signal(queue->not_empty);
128 xbt_mutex_release(queue->mutex);
132 /** @brief Shift something from the message exchange queue.
134 * This is blocking if the queue is empty.
136 * @see #xbt_dynar_shift
139 void xbt_queue_shift(xbt_queue_t queue, void *const dst)
141 xbt_mutex_acquire(queue->mutex);
142 while (xbt_dynar_length(queue->data) == 0) {
143 DEBUG1("Queue %p empty. Waiting", queue);
144 xbt_cond_wait(queue->not_empty, queue->mutex);
146 xbt_dynar_shift(queue->data, dst);
147 xbt_cond_signal(queue->not_full);
148 xbt_mutex_release(queue->mutex);
154 /** @brief Push something to the message exchange queue, with a timeout.
156 * @see #xbt_queue_push
158 void xbt_queue_push_timed(xbt_queue_t queue, const void *src, double delay)
160 double begin = xbt_time();
163 xbt_mutex_acquire(queue->mutex);
166 if (queue->capacity != 0 &&
167 queue->capacity == xbt_dynar_length(queue->data)) {
169 xbt_mutex_release(queue->mutex);
170 THROW2(timeout_error, 0,
171 "Capacity of %p exceded (=%d), and delay = 0", queue,
175 while (queue->capacity != 0 &&
176 queue->capacity == xbt_dynar_length(queue->data) &&
177 (delay < 0 || (xbt_time() - begin) <= delay)) {
179 DEBUG2("Capacity of %p exceded (=%d). Waiting", queue,
182 xbt_cond_timedwait(queue->not_full, queue->mutex,
183 delay < 0 ? -1 : delay - (xbt_time() - begin));
186 xbt_mutex_release(queue->mutex);
192 xbt_dynar_push(queue->data, src);
193 xbt_cond_signal(queue->not_empty);
194 xbt_mutex_release(queue->mutex);
198 /** @brief Pop something from the message exchange queue, with a timeout.
200 * @see #xbt_queue_pop
203 void xbt_queue_pop_timed(xbt_queue_t queue, void *const dst, double delay)
205 double begin = xbt_time();
208 xbt_mutex_acquire(queue->mutex);
211 if (xbt_dynar_length(queue->data) == 0) {
212 xbt_mutex_release(queue->mutex);
213 THROW0(timeout_error, 0, "Delay = 0, and queue is empty");
216 while ((xbt_dynar_length(queue->data) == 0) &&
217 (delay < 0 || (xbt_time() - begin) <= delay)) {
218 DEBUG1("Queue %p empty. Waiting", queue);
220 xbt_cond_timedwait(queue->not_empty, queue->mutex,
221 delay < 0 ? -1 : delay - (xbt_time() - begin));
224 xbt_mutex_release(queue->mutex);
230 xbt_dynar_pop(queue->data, dst);
231 xbt_cond_signal(queue->not_full);
232 xbt_mutex_release(queue->mutex);
235 /** @brief Unshift something to the message exchange queue, with a timeout.
237 * @see #xbt_queue_unshift
239 void xbt_queue_unshift_timed(xbt_queue_t queue, const void *src,
242 double begin = xbt_time();
245 xbt_mutex_acquire(queue->mutex);
248 if (queue->capacity != 0 &&
249 queue->capacity == xbt_dynar_length(queue->data)) {
251 xbt_mutex_release(queue->mutex);
252 THROW2(timeout_error, 0,
253 "Capacity of %p exceded (=%d), and delay = 0", queue,
257 while (queue->capacity != 0 &&
258 queue->capacity == xbt_dynar_length(queue->data) &&
259 (delay < 0 || (xbt_time() - begin) <= delay)) {
261 DEBUG2("Capacity of %p exceded (=%d). Waiting", queue,
264 xbt_cond_timedwait(queue->not_full, queue->mutex,
265 delay < 0 ? -1 : delay - (xbt_time() - begin));
268 xbt_mutex_release(queue->mutex);
274 xbt_dynar_unshift(queue->data, src);
275 xbt_cond_signal(queue->not_empty);
276 xbt_mutex_release(queue->mutex);
280 /** @brief Shift something from the message exchange queue, with a timeout.
282 * @see #xbt_queue_shift
285 void xbt_queue_shift_timed(xbt_queue_t queue, void *const dst,
288 double begin = xbt_time();
291 xbt_mutex_acquire(queue->mutex);
294 if (xbt_dynar_length(queue->data) == 0) {
295 xbt_mutex_release(queue->mutex);
296 THROW0(timeout_error, 0, "Delay = 0, and queue is empty");
299 while ((xbt_dynar_length(queue->data) == 0) &&
300 (delay < 0 || (xbt_time() - begin) <= delay)) {
301 DEBUG1("Queue %p empty. Waiting", queue);
303 xbt_cond_timedwait(queue->not_empty, queue->mutex,
304 delay < 0 ? -1 : delay - (xbt_time() - begin));
307 xbt_mutex_release(queue->mutex);
313 if (xbt_dynar_length(queue->data) == 0) {
314 xbt_mutex_release(queue->mutex);
315 THROW1(timeout_error, 0, "Timeout (%f) elapsed, but queue still empty",
319 xbt_dynar_shift(queue->data, dst);
320 xbt_cond_signal(queue->not_full);
321 xbt_mutex_release(queue->mutex);