cs_queue.h 8.0 KB

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