cs_queue.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.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 CS_QUEUE_H_DEFINED
  35. #define CS_QUEUE_H_DEFINED
  36. #include <string.h>
  37. #include <pthread.h>
  38. #include "assert.h"
  39. struct cs_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 cs_queue_init (struct cs_queue *cs_queue, int cs_queue_items, int size_per_item) {
  51. cs_queue->head = 0;
  52. cs_queue->tail = cs_queue_items - 1;
  53. cs_queue->used = 0;
  54. cs_queue->usedhw = 0;
  55. cs_queue->size = cs_queue_items;
  56. cs_queue->size_per_item = size_per_item;
  57. cs_queue->items = malloc (cs_queue_items * size_per_item);
  58. if (cs_queue->items == 0) {
  59. return (-ENOMEM);
  60. }
  61. memset (cs_queue->items, 0, cs_queue_items * size_per_item);
  62. pthread_mutex_init (&cs_queue->mutex, NULL);
  63. return (0);
  64. }
  65. static inline int cs_queue_reinit (struct cs_queue *cs_queue)
  66. {
  67. pthread_mutex_lock (&cs_queue->mutex);
  68. cs_queue->head = 0;
  69. cs_queue->tail = cs_queue->size - 1;
  70. cs_queue->used = 0;
  71. cs_queue->usedhw = 0;
  72. memset (cs_queue->items, 0, cs_queue->size * cs_queue->size_per_item);
  73. pthread_mutex_unlock (&cs_queue->mutex);
  74. return (0);
  75. }
  76. static inline void cs_queue_free (struct cs_queue *cs_queue) {
  77. pthread_mutex_destroy (&cs_queue->mutex);
  78. free (cs_queue->items);
  79. }
  80. static inline int cs_queue_is_full (struct cs_queue *cs_queue) {
  81. int full;
  82. pthread_mutex_lock (&cs_queue->mutex);
  83. full = ((cs_queue->size - 1) == cs_queue->used);
  84. pthread_mutex_unlock (&cs_queue->mutex);
  85. return (full);
  86. }
  87. static inline int cs_queue_is_empty (struct cs_queue *cs_queue) {
  88. int empty;
  89. pthread_mutex_lock (&cs_queue->mutex);
  90. empty = (cs_queue->used == 0);
  91. pthread_mutex_unlock (&cs_queue->mutex);
  92. return (empty);
  93. }
  94. static inline void cs_queue_item_add (struct cs_queue *cs_queue, void *item)
  95. {
  96. char *cs_queue_item;
  97. int cs_queue_position;
  98. pthread_mutex_lock (&cs_queue->mutex);
  99. cs_queue_position = cs_queue->head;
  100. cs_queue_item = cs_queue->items;
  101. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  102. memcpy (cs_queue_item, item, cs_queue->size_per_item);
  103. assert (cs_queue->tail != cs_queue->head);
  104. cs_queue->head = (cs_queue->head + 1) % cs_queue->size;
  105. cs_queue->used++;
  106. if (cs_queue->used > cs_queue->usedhw) {
  107. cs_queue->usedhw = cs_queue->used;
  108. }
  109. pthread_mutex_unlock (&cs_queue->mutex);
  110. }
  111. static inline void *cs_queue_item_get (struct cs_queue *cs_queue)
  112. {
  113. char *cs_queue_item;
  114. int cs_queue_position;
  115. pthread_mutex_lock (&cs_queue->mutex);
  116. cs_queue_position = (cs_queue->tail + 1) % cs_queue->size;
  117. cs_queue_item = cs_queue->items;
  118. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  119. pthread_mutex_unlock (&cs_queue->mutex);
  120. return ((void *)cs_queue_item);
  121. }
  122. static inline void cs_queue_item_remove (struct cs_queue *cs_queue) {
  123. pthread_mutex_lock (&cs_queue->mutex);
  124. cs_queue->tail = (cs_queue->tail + 1) % cs_queue->size;
  125. assert (cs_queue->tail != cs_queue->head);
  126. cs_queue->used--;
  127. assert (cs_queue->used >= 0);
  128. pthread_mutex_unlock (&cs_queue->mutex);
  129. }
  130. static inline void cs_queue_items_remove (struct cs_queue *cs_queue, int rel_count)
  131. {
  132. pthread_mutex_lock (&cs_queue->mutex);
  133. cs_queue->tail = (cs_queue->tail + rel_count) % cs_queue->size;
  134. assert (cs_queue->tail != cs_queue->head);
  135. cs_queue->used -= rel_count;
  136. pthread_mutex_unlock (&cs_queue->mutex);
  137. }
  138. static inline void cs_queue_item_iterator_init (struct cs_queue *cs_queue)
  139. {
  140. pthread_mutex_lock (&cs_queue->mutex);
  141. cs_queue->iterator = (cs_queue->tail + 1) % cs_queue->size;
  142. pthread_mutex_unlock (&cs_queue->mutex);
  143. }
  144. static inline void *cs_queue_item_iterator_get (struct cs_queue *cs_queue)
  145. {
  146. char *cs_queue_item;
  147. int cs_queue_position;
  148. pthread_mutex_lock (&cs_queue->mutex);
  149. cs_queue_position = (cs_queue->iterator) % cs_queue->size;
  150. if (cs_queue->iterator == cs_queue->head) {
  151. pthread_mutex_unlock (&cs_queue->mutex);
  152. return (0);
  153. }
  154. cs_queue_item = cs_queue->items;
  155. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  156. pthread_mutex_unlock (&cs_queue->mutex);
  157. return ((void *)cs_queue_item);
  158. }
  159. static inline int cs_queue_item_iterator_next (struct cs_queue *cs_queue)
  160. {
  161. int next_res;
  162. pthread_mutex_lock (&cs_queue->mutex);
  163. cs_queue->iterator = (cs_queue->iterator + 1) % cs_queue->size;
  164. next_res = cs_queue->iterator == cs_queue->head;
  165. pthread_mutex_unlock (&cs_queue->mutex);
  166. return (next_res);
  167. }
  168. static inline void cs_queue_avail (struct cs_queue *cs_queue, int *avail)
  169. {
  170. pthread_mutex_lock (&cs_queue->mutex);
  171. *avail = cs_queue->size - cs_queue->used - 2;
  172. assert (*avail >= 0);
  173. pthread_mutex_unlock (&cs_queue->mutex);
  174. }
  175. static inline int cs_queue_used (struct cs_queue *cs_queue) {
  176. int used;
  177. pthread_mutex_lock (&cs_queue->mutex);
  178. used = cs_queue->used;
  179. pthread_mutex_unlock (&cs_queue->mutex);
  180. return (used);
  181. }
  182. static inline int cs_queue_usedhw (struct cs_queue *cs_queue) {
  183. int usedhw;
  184. pthread_mutex_lock (&cs_queue->mutex);
  185. usedhw = cs_queue->usedhw;
  186. pthread_mutex_unlock (&cs_queue->mutex);
  187. return (usedhw);
  188. }
  189. #endif /* CS_QUEUE_H_DEFINED */