queue.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef QUEUE_H_DEFINED
  35. #define QUEUE_H_DEFINED
  36. #include <string.h>
  37. #include <pthread.h>
  38. #include "assert.h"
  39. #ifdef OPENAIS_SOLARIS
  40. /* struct queue is already defined in sys/stream.h on Solaris */
  41. #define queue _queue
  42. #endif
  43. struct queue {
  44. int head;
  45. int tail;
  46. int used;
  47. int usedhw;
  48. int size;
  49. void *items;
  50. int size_per_item;
  51. int iterator;
  52. pthread_mutex_t mutex;
  53. };
  54. static inline int queue_init (struct queue *queue, int queue_items, int size_per_item) {
  55. queue->head = 0;
  56. queue->tail = queue_items - 1;
  57. queue->used = 0;
  58. queue->usedhw = 0;
  59. queue->size = queue_items;
  60. queue->size_per_item = size_per_item;
  61. queue->items = malloc (queue_items * size_per_item);
  62. if (queue->items == 0) {
  63. return (-ENOMEM);
  64. }
  65. memset (queue->items, 0, queue_items * size_per_item);
  66. pthread_mutex_init (&queue->mutex, NULL);
  67. return (0);
  68. }
  69. static inline int queue_reinit (struct queue *queue)
  70. {
  71. pthread_mutex_lock (&queue->mutex);
  72. queue->head = 0;
  73. queue->tail = queue->size - 1;
  74. queue->used = 0;
  75. queue->usedhw = 0;
  76. memset (queue->items, 0, queue->size * queue->size_per_item);
  77. pthread_mutex_unlock (&queue->mutex);
  78. return (0);
  79. }
  80. static inline void queue_free (struct queue *queue) {
  81. pthread_mutex_destroy (&queue->mutex);
  82. free (queue->items);
  83. }
  84. static inline int queue_is_full (struct queue *queue) {
  85. int full;
  86. pthread_mutex_lock (&queue->mutex);
  87. full = queue->size - 1 == queue->used;
  88. pthread_mutex_unlock (&queue->mutex);
  89. return (full);
  90. }
  91. static inline int queue_is_empty (struct queue *queue) {
  92. int empty;
  93. pthread_mutex_lock (&queue->mutex);
  94. empty = (queue->used == 0);
  95. pthread_mutex_unlock (&queue->mutex);
  96. return (empty);
  97. }
  98. static inline void queue_item_add (struct queue *queue, void *item)
  99. {
  100. char *queue_item;
  101. int queue_position;
  102. pthread_mutex_lock (&queue->mutex);
  103. queue_position = queue->head;
  104. queue_item = queue->items;
  105. queue_item += queue_position * queue->size_per_item;
  106. memcpy (queue_item, item, queue->size_per_item);
  107. assert (queue->tail != queue->head);
  108. queue->head = (queue->head + 1) % queue->size;
  109. queue->used++;
  110. if (queue->used > queue->usedhw) {
  111. queue->usedhw = queue->used;
  112. }
  113. pthread_mutex_unlock (&queue->mutex);
  114. }
  115. static inline void *queue_item_get (struct queue *queue)
  116. {
  117. char *queue_item;
  118. int queue_position;
  119. pthread_mutex_lock (&queue->mutex);
  120. queue_position = (queue->tail + 1) % queue->size;
  121. queue_item = queue->items;
  122. queue_item += queue_position * queue->size_per_item;
  123. pthread_mutex_unlock (&queue->mutex);
  124. return ((void *)queue_item);
  125. }
  126. static inline void queue_item_remove (struct queue *queue) {
  127. pthread_mutex_lock (&queue->mutex);
  128. queue->tail = (queue->tail + 1) % queue->size;
  129. assert (queue->tail != queue->head);
  130. queue->used--;
  131. assert (queue->used >= 0);
  132. pthread_mutex_unlock (&queue->mutex);
  133. }
  134. static inline void queue_items_remove (struct queue *queue, int rel_count)
  135. {
  136. pthread_mutex_lock (&queue->mutex);
  137. queue->tail = (queue->tail + rel_count) % queue->size;
  138. assert (queue->tail != queue->head);
  139. queue->used -= rel_count;
  140. pthread_mutex_unlock (&queue->mutex);
  141. }
  142. static inline void queue_item_iterator_init (struct queue *queue)
  143. {
  144. pthread_mutex_lock (&queue->mutex);
  145. queue->iterator = (queue->tail + 1) % queue->size;
  146. pthread_mutex_unlock (&queue->mutex);
  147. }
  148. static inline void *queue_item_iterator_get (struct queue *queue)
  149. {
  150. char *queue_item;
  151. int queue_position;
  152. pthread_mutex_lock (&queue->mutex);
  153. queue_position = (queue->iterator) % queue->size;
  154. if (queue->iterator == queue->head) {
  155. pthread_mutex_unlock (&queue->mutex);
  156. return (0);
  157. }
  158. queue_item = queue->items;
  159. queue_item += queue_position * queue->size_per_item;
  160. pthread_mutex_unlock (&queue->mutex);
  161. return ((void *)queue_item);
  162. }
  163. static inline int queue_item_iterator_next (struct queue *queue)
  164. {
  165. int next_res;
  166. pthread_mutex_lock (&queue->mutex);
  167. queue->iterator = (queue->iterator + 1) % queue->size;
  168. next_res = queue->iterator == queue->head;
  169. pthread_mutex_unlock (&queue->mutex);
  170. return (next_res);
  171. }
  172. static inline void queue_avail (struct queue *queue, int *avail)
  173. {
  174. pthread_mutex_lock (&queue->mutex);
  175. *avail = queue->size - queue->used - 2;
  176. assert (*avail >= 0);
  177. pthread_mutex_unlock (&queue->mutex);
  178. }
  179. static inline int queue_used (struct queue *queue) {
  180. int used;
  181. pthread_mutex_lock (&queue->mutex);
  182. used = queue->used;
  183. pthread_mutex_unlock (&queue->mutex);
  184. return (used);
  185. }
  186. static inline int queue_usedhw (struct queue *queue) {
  187. int usedhw;
  188. pthread_mutex_lock (&queue->mutex);
  189. usedhw = queue->usedhw;
  190. pthread_mutex_unlock (&queue->mutex);
  191. return (usedhw);
  192. }
  193. #endif /* QUEUE_H_DEFINED */