queue.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. struct queue {
  40. int head;
  41. int tail;
  42. int used;
  43. int usedhw;
  44. int size;
  45. void *items;
  46. int size_per_item;
  47. int iterator;
  48. pthread_mutex_t mutex;
  49. };
  50. static inline int queue_init (struct queue *queue, int queue_items, int size_per_item) {
  51. queue->head = 0;
  52. queue->tail = queue_items - 1;
  53. queue->used = 0;
  54. queue->usedhw = 0;
  55. queue->size = queue_items;
  56. queue->size_per_item = size_per_item;
  57. queue->items = malloc (queue_items * size_per_item);
  58. if (queue->items == 0) {
  59. return (-ENOMEM);
  60. }
  61. memset (queue->items, 0, queue_items * size_per_item);
  62. pthread_mutex_init (&queue->mutex, NULL);
  63. return (0);
  64. }
  65. static inline int queue_reinit (struct queue *queue)
  66. {
  67. pthread_mutex_lock (&queue->mutex);
  68. queue->head = 0;
  69. queue->tail = queue->size - 1;
  70. queue->used = 0;
  71. queue->usedhw = 0;
  72. memset (queue->items, 0, queue->size * queue->size_per_item);
  73. pthread_mutex_unlock (&queue->mutex);
  74. return (0);
  75. }
  76. static inline void queue_free (struct queue *queue) {
  77. free (queue->items);
  78. }
  79. static inline int queue_is_full (struct queue *queue) {
  80. int full;
  81. pthread_mutex_lock (&queue->mutex);
  82. full = queue->size - 1 == queue->used;
  83. pthread_mutex_unlock (&queue->mutex);
  84. return (full);
  85. }
  86. static inline int queue_is_empty (struct queue *queue) {
  87. int empty;
  88. pthread_mutex_lock (&queue->mutex);
  89. empty = queue->used == 0;
  90. pthread_mutex_unlock (&queue->mutex);
  91. return (empty);
  92. }
  93. static inline void queue_item_add (struct queue *queue, void *item)
  94. {
  95. char *queue_item;
  96. int queue_position;
  97. pthread_mutex_lock (&queue->mutex);
  98. queue_position = queue->head;
  99. queue_item = queue->items;
  100. queue_item += queue_position * queue->size_per_item;
  101. memcpy (queue_item, item, queue->size_per_item);
  102. assert (queue->tail != queue->head);
  103. queue->head = (queue->head + 1) % queue->size;
  104. queue->used++;
  105. if (queue->used > queue->usedhw) {
  106. queue->usedhw = queue->used;
  107. }
  108. pthread_mutex_unlock (&queue->mutex);
  109. }
  110. static inline void *queue_item_get (struct queue *queue)
  111. {
  112. char *queue_item;
  113. int queue_position;
  114. pthread_mutex_lock (&queue->mutex);
  115. queue_position = (queue->tail + 1) % queue->size;
  116. queue_item = queue->items;
  117. queue_item += queue_position * queue->size_per_item;
  118. pthread_mutex_unlock (&queue->mutex);
  119. return ((void *)queue_item);
  120. }
  121. static inline void queue_item_remove (struct queue *queue) {
  122. pthread_mutex_lock (&queue->mutex);
  123. queue->tail = (queue->tail + 1) % queue->size;
  124. assert (queue->tail != queue->head);
  125. queue->used--;
  126. assert (queue->used >= 0);
  127. pthread_mutex_unlock (&queue->mutex);
  128. }
  129. static inline void queue_items_remove (struct queue *queue, int rel_count)
  130. {
  131. pthread_mutex_lock (&queue->mutex);
  132. queue->tail = (queue->tail + rel_count) % queue->size;
  133. assert (queue->tail != queue->head);
  134. queue->used -= rel_count;
  135. pthread_mutex_unlock (&queue->mutex);
  136. }
  137. static inline void queue_item_iterator_init (struct queue *queue)
  138. {
  139. pthread_mutex_lock (&queue->mutex);
  140. queue->iterator = (queue->tail + 1) % queue->size;
  141. pthread_mutex_unlock (&queue->mutex);
  142. }
  143. static inline void *queue_item_iterator_get (struct queue *queue)
  144. {
  145. char *queue_item;
  146. int queue_position;
  147. pthread_mutex_lock (&queue->mutex);
  148. queue_position = (queue->iterator) % queue->size;
  149. if (queue->iterator == queue->head) {
  150. pthread_mutex_unlock (&queue->mutex);
  151. return (0);
  152. }
  153. queue_item = queue->items;
  154. queue_item += queue_position * queue->size_per_item;
  155. pthread_mutex_unlock (&queue->mutex);
  156. return ((void *)queue_item);
  157. }
  158. static inline int queue_item_iterator_next (struct queue *queue)
  159. {
  160. int next_res;
  161. pthread_mutex_lock (&queue->mutex);
  162. queue->iterator = (queue->iterator + 1) % queue->size;
  163. next_res = queue->iterator == queue->head;
  164. pthread_mutex_unlock (&queue->mutex);
  165. return (next_res);
  166. }
  167. static inline void queue_avail (struct queue *queue, int *avail)
  168. {
  169. pthread_mutex_lock (&queue->mutex);
  170. *avail = queue->size - queue->used - 2;
  171. assert (*avail >= 0);
  172. pthread_mutex_unlock (&queue->mutex);
  173. }
  174. static inline int queue_used (struct queue *queue) {
  175. int used;
  176. pthread_mutex_lock (&queue->mutex);
  177. used = queue->used;
  178. pthread_mutex_unlock (&queue->mutex);
  179. return (used);
  180. }
  181. #endif /* QUEUE_H_DEFINED */