cs_queue.h 6.6 KB

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