4
0

cs_queue.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. int threaded_mode_enabled;
  52. };
  53. static inline int cs_queue_init (struct cs_queue *cs_queue, int cs_queue_items, int size_per_item, int threaded_mode_enabled) {
  54. cs_queue->head = 0;
  55. cs_queue->tail = cs_queue_items - 1;
  56. cs_queue->used = 0;
  57. cs_queue->usedhw = 0;
  58. cs_queue->size = cs_queue_items;
  59. cs_queue->size_per_item = size_per_item;
  60. cs_queue->threaded_mode_enabled = threaded_mode_enabled;
  61. cs_queue->items = malloc (cs_queue_items * size_per_item);
  62. if (cs_queue->items == 0) {
  63. return (-ENOMEM);
  64. }
  65. memset (cs_queue->items, 0, cs_queue_items * size_per_item);
  66. if (cs_queue->threaded_mode_enabled) {
  67. pthread_mutex_init (&cs_queue->mutex, NULL);
  68. }
  69. return (0);
  70. }
  71. static inline int cs_queue_reinit (struct cs_queue *cs_queue)
  72. {
  73. if (cs_queue->threaded_mode_enabled) {
  74. pthread_mutex_lock (&cs_queue->mutex);
  75. }
  76. cs_queue->head = 0;
  77. cs_queue->tail = cs_queue->size - 1;
  78. cs_queue->used = 0;
  79. cs_queue->usedhw = 0;
  80. memset (cs_queue->items, 0, cs_queue->size * cs_queue->size_per_item);
  81. if (cs_queue->threaded_mode_enabled) {
  82. pthread_mutex_unlock (&cs_queue->mutex);
  83. }
  84. return (0);
  85. }
  86. static inline void cs_queue_free (struct cs_queue *cs_queue) {
  87. if (cs_queue->threaded_mode_enabled) {
  88. pthread_mutex_destroy (&cs_queue->mutex);
  89. }
  90. free (cs_queue->items);
  91. }
  92. static inline int cs_queue_is_full (struct cs_queue *cs_queue) {
  93. int full;
  94. if (cs_queue->threaded_mode_enabled) {
  95. pthread_mutex_lock (&cs_queue->mutex);
  96. }
  97. full = ((cs_queue->size - 1) == cs_queue->used);
  98. if (cs_queue->threaded_mode_enabled) {
  99. pthread_mutex_unlock (&cs_queue->mutex);
  100. }
  101. return (full);
  102. }
  103. static inline int cs_queue_is_empty (struct cs_queue *cs_queue) {
  104. int empty;
  105. if (cs_queue->threaded_mode_enabled) {
  106. pthread_mutex_lock (&cs_queue->mutex);
  107. }
  108. empty = (cs_queue->used == 0);
  109. if (cs_queue->threaded_mode_enabled) {
  110. pthread_mutex_unlock (&cs_queue->mutex);
  111. }
  112. return (empty);
  113. }
  114. static inline void cs_queue_item_add (struct cs_queue *cs_queue, void *item)
  115. {
  116. char *cs_queue_item;
  117. int cs_queue_position;
  118. if (cs_queue->threaded_mode_enabled) {
  119. pthread_mutex_lock (&cs_queue->mutex);
  120. }
  121. cs_queue_position = cs_queue->head;
  122. cs_queue_item = cs_queue->items;
  123. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  124. memcpy (cs_queue_item, item, cs_queue->size_per_item);
  125. assert (cs_queue->tail != cs_queue->head);
  126. cs_queue->head = (cs_queue->head + 1) % cs_queue->size;
  127. cs_queue->used++;
  128. if (cs_queue->used > cs_queue->usedhw) {
  129. cs_queue->usedhw = cs_queue->used;
  130. }
  131. if (cs_queue->threaded_mode_enabled) {
  132. pthread_mutex_unlock (&cs_queue->mutex);
  133. }
  134. }
  135. static inline void *cs_queue_item_get (struct cs_queue *cs_queue)
  136. {
  137. char *cs_queue_item;
  138. int cs_queue_position;
  139. if (cs_queue->threaded_mode_enabled) {
  140. pthread_mutex_lock (&cs_queue->mutex);
  141. }
  142. cs_queue_position = (cs_queue->tail + 1) % cs_queue->size;
  143. cs_queue_item = cs_queue->items;
  144. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  145. if (cs_queue->threaded_mode_enabled) {
  146. pthread_mutex_unlock (&cs_queue->mutex);
  147. }
  148. return ((void *)cs_queue_item);
  149. }
  150. static inline void cs_queue_item_remove (struct cs_queue *cs_queue) {
  151. if (cs_queue->threaded_mode_enabled) {
  152. pthread_mutex_lock (&cs_queue->mutex);
  153. }
  154. cs_queue->tail = (cs_queue->tail + 1) % cs_queue->size;
  155. assert (cs_queue->tail != cs_queue->head);
  156. cs_queue->used--;
  157. assert (cs_queue->used >= 0);
  158. if (cs_queue->threaded_mode_enabled) {
  159. pthread_mutex_unlock (&cs_queue->mutex);
  160. }
  161. }
  162. static inline void cs_queue_items_remove (struct cs_queue *cs_queue, int rel_count)
  163. {
  164. if (cs_queue->threaded_mode_enabled) {
  165. pthread_mutex_lock (&cs_queue->mutex);
  166. }
  167. cs_queue->tail = (cs_queue->tail + rel_count) % cs_queue->size;
  168. assert (cs_queue->tail != cs_queue->head);
  169. cs_queue->used -= rel_count;
  170. if (cs_queue->threaded_mode_enabled) {
  171. pthread_mutex_unlock (&cs_queue->mutex);
  172. }
  173. }
  174. static inline void cs_queue_item_iterator_init (struct cs_queue *cs_queue)
  175. {
  176. if (cs_queue->threaded_mode_enabled) {
  177. pthread_mutex_lock (&cs_queue->mutex);
  178. }
  179. cs_queue->iterator = (cs_queue->tail + 1) % cs_queue->size;
  180. if (cs_queue->threaded_mode_enabled) {
  181. pthread_mutex_unlock (&cs_queue->mutex);
  182. }
  183. }
  184. static inline void *cs_queue_item_iterator_get (struct cs_queue *cs_queue)
  185. {
  186. char *cs_queue_item;
  187. int cs_queue_position;
  188. if (cs_queue->threaded_mode_enabled) {
  189. pthread_mutex_lock (&cs_queue->mutex);
  190. }
  191. cs_queue_position = (cs_queue->iterator) % cs_queue->size;
  192. if (cs_queue->iterator == cs_queue->head) {
  193. if (cs_queue->threaded_mode_enabled) {
  194. pthread_mutex_unlock (&cs_queue->mutex);
  195. }
  196. return (0);
  197. }
  198. cs_queue_item = cs_queue->items;
  199. cs_queue_item += cs_queue_position * cs_queue->size_per_item;
  200. if (cs_queue->threaded_mode_enabled) {
  201. pthread_mutex_unlock (&cs_queue->mutex);
  202. }
  203. return ((void *)cs_queue_item);
  204. }
  205. static inline int cs_queue_item_iterator_next (struct cs_queue *cs_queue)
  206. {
  207. int next_res;
  208. if (cs_queue->threaded_mode_enabled) {
  209. pthread_mutex_lock (&cs_queue->mutex);
  210. }
  211. cs_queue->iterator = (cs_queue->iterator + 1) % cs_queue->size;
  212. next_res = cs_queue->iterator == cs_queue->head;
  213. if (cs_queue->threaded_mode_enabled) {
  214. pthread_mutex_unlock (&cs_queue->mutex);
  215. }
  216. return (next_res);
  217. }
  218. static inline void cs_queue_avail (struct cs_queue *cs_queue, int *avail)
  219. {
  220. if (cs_queue->threaded_mode_enabled) {
  221. pthread_mutex_lock (&cs_queue->mutex);
  222. }
  223. *avail = cs_queue->size - cs_queue->used - 2;
  224. assert (*avail >= 0);
  225. if (cs_queue->threaded_mode_enabled) {
  226. pthread_mutex_unlock (&cs_queue->mutex);
  227. }
  228. }
  229. static inline int cs_queue_used (struct cs_queue *cs_queue) {
  230. int used;
  231. if (cs_queue->threaded_mode_enabled) {
  232. pthread_mutex_lock (&cs_queue->mutex);
  233. }
  234. used = cs_queue->used;
  235. if (cs_queue->threaded_mode_enabled) {
  236. pthread_mutex_unlock (&cs_queue->mutex);
  237. }
  238. return (used);
  239. }
  240. static inline int cs_queue_usedhw (struct cs_queue *cs_queue) {
  241. int usedhw;
  242. if (cs_queue->threaded_mode_enabled) {
  243. pthread_mutex_lock (&cs_queue->mutex);
  244. }
  245. usedhw = cs_queue->usedhw;
  246. if (cs_queue->threaded_mode_enabled) {
  247. pthread_mutex_unlock (&cs_queue->mutex);
  248. }
  249. return (usedhw);
  250. }
  251. #endif /* CS_QUEUE_H_DEFINED */