queue.h 6.0 KB

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