queue.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon rights
  24. * to redistribute these changes.
  25. */
  26. /*
  27. * File: queue.h
  28. * Author: Avadis Tevanian, Jr.
  29. * Date: 1985
  30. *
  31. * Type definitions for generic queues.
  32. *
  33. */
  34. #ifndef _KERN_QUEUE_H_
  35. #define _KERN_QUEUE_H_
  36. #include <kern/lock.h>
  37. /*
  38. * Queue of abstract objects. Queue is maintained
  39. * within that object.
  40. *
  41. * Supports fast removal from within the queue.
  42. *
  43. * How to declare a queue of elements of type "foo_t":
  44. * In the "*foo_t" type, you must have a field of
  45. * type "queue_chain_t" to hold together this queue.
  46. * There may be more than one chain through a
  47. * "foo_t", for use by different queues.
  48. *
  49. * Declare the queue as a "queue_t" type.
  50. *
  51. * Elements of the queue (of type "foo_t", that is)
  52. * are referred to by reference, and cast to type
  53. * "queue_entry_t" within this module.
  54. */
  55. /*
  56. * A generic doubly-linked list (queue).
  57. */
  58. struct queue_entry {
  59. struct queue_entry *next; /* next element */
  60. struct queue_entry *prev; /* previous element */
  61. };
  62. typedef struct queue_entry *queue_t;
  63. typedef struct queue_entry queue_head_t;
  64. typedef struct queue_entry queue_chain_t;
  65. typedef struct queue_entry *queue_entry_t;
  66. /*
  67. * enqueue puts "elt" on the "queue".
  68. * dequeue returns the first element in the "queue".
  69. * remqueue removes the specified "elt" from the specified "queue".
  70. */
  71. #define enqueue(queue,elt) enqueue_tail(queue, elt)
  72. #define dequeue(queue) dequeue_head(queue)
  73. void enqueue_head(queue_t, queue_entry_t);
  74. void enqueue_tail(queue_t, queue_entry_t);
  75. queue_entry_t dequeue_head(queue_t);
  76. queue_entry_t dequeue_tail(queue_t);
  77. void remqueue(queue_t, queue_entry_t);
  78. void insque(queue_entry_t, queue_entry_t);
  79. /*
  80. * Macro: queue_assert
  81. * Function:
  82. * Used by macros to assert that the given argument is a
  83. * queue.
  84. */
  85. #define queue_assert(q) (void) ((void) (q)->next, (q)->prev)
  86. /*
  87. * Macro: queue_init
  88. * Function:
  89. * Initialize the given queue.
  90. * Header:
  91. * void queue_init(q)
  92. * queue_t q; *MODIFIED*
  93. */
  94. #define queue_init(q) ((q)->next = (q)->prev = q)
  95. /*
  96. * Macro: queue_first
  97. * Function:
  98. * Returns the first entry in the queue,
  99. * Header:
  100. * queue_entry_t queue_first(q)
  101. * queue_t q; *IN*
  102. */
  103. #define queue_first(q) (queue_assert(q), (q)->next)
  104. /*
  105. * Macro: queue_next
  106. * Function:
  107. * Returns the entry after an item in the queue.
  108. * Header:
  109. * queue_entry_t queue_next(qc)
  110. * queue_t qc;
  111. */
  112. #define queue_next(qc) (queue_assert(qc), (qc)->next)
  113. /*
  114. * Macro: queue_last
  115. * Function:
  116. * Returns the last entry in the queue.
  117. * Header:
  118. * queue_entry_t queue_last(q)
  119. * queue_t q; *IN*
  120. */
  121. #define queue_last(q) (queue_assert(q), (q)->prev)
  122. /*
  123. * Macro: queue_prev
  124. * Function:
  125. * Returns the entry before an item in the queue.
  126. * Header:
  127. * queue_entry_t queue_prev(qc)
  128. * queue_t qc;
  129. */
  130. #define queue_prev(qc) (queue_assert(qc), (qc)->prev)
  131. /*
  132. * Macro: queue_end
  133. * Function:
  134. * Tests whether a new entry is really the end of
  135. * the queue.
  136. * Header:
  137. * boolean_t queue_end(q, qe)
  138. * queue_t q;
  139. * queue_entry_t qe;
  140. */
  141. #define queue_end(q, qe) (queue_assert(q), queue_assert(qe), \
  142. (q) == (qe))
  143. /*
  144. * Macro: queue_empty
  145. * Function:
  146. * Tests whether a queue is empty.
  147. * Header:
  148. * boolean_t queue_empty(q)
  149. * queue_t q;
  150. */
  151. #define queue_empty(q) queue_end((q), queue_first(q))
  152. /*----------------------------------------------------------------*/
  153. /*
  154. * Macros that operate on generic structures. The queue
  155. * chain may be at any location within the structure, and there
  156. * may be more than one chain.
  157. */
  158. /*
  159. * Macro: queue_enter
  160. * Function:
  161. * Insert a new element at the tail of the queue.
  162. * Header:
  163. * void queue_enter(q, elt, type, field)
  164. * queue_t q;
  165. * <type> elt;
  166. * <type> is what's in our queue
  167. * <field> is the chain field in (*<type>)
  168. */
  169. #define queue_enter(head, elt, type, field) \
  170. { \
  171. queue_assert(head); \
  172. queue_assert(&(elt)->field); \
  173. queue_entry_t prev; \
  174. \
  175. prev = (head)->prev; \
  176. if ((head) == prev) { \
  177. (head)->next = (queue_entry_t) (elt); \
  178. } \
  179. else { \
  180. ((type)prev)->field.next = (queue_entry_t)(elt);\
  181. } \
  182. (elt)->field.prev = prev; \
  183. (elt)->field.next = head; \
  184. (head)->prev = (queue_entry_t) elt; \
  185. }
  186. /*
  187. * Macro: queue_enter_first
  188. * Function:
  189. * Insert a new element at the head of the queue.
  190. * Header:
  191. * void queue_enter_first(q, elt, type, field)
  192. * queue_t q;
  193. * <type> elt;
  194. * <type> is what's in our queue
  195. * <field> is the chain field in (*<type>)
  196. */
  197. #define queue_enter_first(head, elt, type, field) \
  198. { \
  199. queue_assert(head); \
  200. queue_assert(&(elt)->field); \
  201. queue_entry_t next; \
  202. \
  203. next = (head)->next; \
  204. if ((head) == next) { \
  205. (head)->prev = (queue_entry_t) (elt); \
  206. } \
  207. else { \
  208. ((type)next)->field.prev = (queue_entry_t)(elt);\
  209. } \
  210. (elt)->field.next = next; \
  211. (elt)->field.prev = head; \
  212. (head)->next = (queue_entry_t) elt; \
  213. }
  214. /*
  215. * Macro: queue_field [internal use only]
  216. * Function:
  217. * Find the queue_chain_t (or queue_t) for the
  218. * given element (thing) in the given queue (head)
  219. */
  220. #define queue_field(head, thing, type, field) \
  221. (((head) == (thing)) ? (head) : &((type)(thing))->field)
  222. /*
  223. * Macro: queue_remove
  224. * Function:
  225. * Remove an arbitrary item from the queue.
  226. * Header:
  227. * void queue_remove(q, qe, type, field)
  228. * arguments as in queue_enter
  229. */
  230. #define queue_remove(head, elt, type, field) \
  231. { \
  232. queue_assert(head); \
  233. queue_assert(&(elt)->field); \
  234. queue_entry_t next, prev; \
  235. \
  236. next = (elt)->field.next; \
  237. prev = (elt)->field.prev; \
  238. \
  239. if ((head) == next) \
  240. (head)->prev = prev; \
  241. else \
  242. ((type)next)->field.prev = prev; \
  243. \
  244. if ((head) == prev) \
  245. (head)->next = next; \
  246. else \
  247. ((type)prev)->field.next = next; \
  248. }
  249. /*
  250. * Macro: queue_remove_first
  251. * Function:
  252. * Remove and return the entry at the head of
  253. * the queue.
  254. * Header:
  255. * queue_remove_first(head, entry, type, field)
  256. * entry is returned by reference
  257. */
  258. #define queue_remove_first(head, entry, type, field) \
  259. { \
  260. queue_assert(head); \
  261. queue_assert(&(entry)->field); \
  262. queue_entry_t next; \
  263. \
  264. (entry) = (type) ((head)->next); \
  265. next = (entry)->field.next; \
  266. \
  267. if ((head) == next) \
  268. (head)->prev = (head); \
  269. else \
  270. ((type)(next))->field.prev = (head); \
  271. (head)->next = next; \
  272. }
  273. /*
  274. * Macro: queue_remove_last
  275. * Function:
  276. * Remove and return the entry at the tail of
  277. * the queue.
  278. * Header:
  279. * queue_remove_last(head, entry, type, field)
  280. * entry is returned by reference
  281. */
  282. #define queue_remove_last(head, entry, type, field) \
  283. { \
  284. queue_assert(head); \
  285. queue_assert(&(entry)->field); \
  286. queue_entry_t prev; \
  287. \
  288. (entry) = (type) ((head)->prev); \
  289. prev = (entry)->field.prev; \
  290. \
  291. if ((head) == prev) \
  292. (head)->next = (head); \
  293. else \
  294. ((type)(prev))->field.next = (head); \
  295. (head)->prev = prev; \
  296. }
  297. /*
  298. * Macro: queue_assign
  299. */
  300. #define queue_assign(to, from, type, field) \
  301. { \
  302. queue_assert(&(to)->field); \
  303. queue_assert(&(from)->field); \
  304. ((type)((from)->prev))->field.next = (to); \
  305. ((type)((from)->next))->field.prev = (to); \
  306. *to = *from; \
  307. }
  308. /*
  309. * Macro: queue_iterate
  310. * Function:
  311. * iterate over each item in the queue.
  312. * Generates a 'for' loop, setting elt to
  313. * each item in turn (by reference).
  314. * Header:
  315. * queue_iterate(q, elt, type, field)
  316. * queue_t q;
  317. * <type> elt;
  318. * <type> is what's in our queue
  319. * <field> is the chain field in (*<type>)
  320. */
  321. #define queue_iterate(head, elt, type, field) \
  322. for ((elt) = (type) queue_first(head); \
  323. !queue_end((head), (queue_entry_t)(elt)); \
  324. (elt) = (type) queue_next(&(elt)->field))
  325. /*----------------------------------------------------------------*/
  326. /*
  327. * Define macros for queues with locks.
  328. */
  329. struct mpqueue_head {
  330. struct queue_entry head; /* header for queue */
  331. struct slock lock; /* lock for queue */
  332. };
  333. typedef struct mpqueue_head mpqueue_head_t;
  334. #define round_mpq(size) (size)
  335. #define mpqueue_init(q) \
  336. { \
  337. queue_init(&(q)->head); \
  338. simple_lock_init(&(q)->lock); \
  339. }
  340. #define mpenqueue_tail(q, elt) \
  341. simple_lock(&(q)->lock); \
  342. enqueue_tail(&(q)->head, elt); \
  343. simple_unlock(&(q)->lock);
  344. #define mpdequeue_head(q, elt) \
  345. simple_lock(&(q)->lock); \
  346. if (queue_empty(&(q)->head)) \
  347. *(elt) = 0; \
  348. else \
  349. *(elt) = dequeue_head(&(q)->head); \
  350. simple_unlock(&(q)->lock);
  351. /*
  352. * Old queue stuff, will go away soon.
  353. */
  354. #endif /* _KERN_QUEUE_H_ */