2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon rights
54 * to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * Type definitions for generic queues.
67 #ifndef _KERN_QUEUE_H_
68 #define _KERN_QUEUE_H_
70 #include <mach/mach_types.h>
71 #include <kern/macro_help.h>
73 #include <sys/cdefs.h>
78 * Queue Management APIs
80 * There are currently two subtly different methods of maintining
81 * a queue of objects. Both APIs are contained in this file, and
82 * unfortunately overlap.
83 * (there is also a third way maintained in bsd/sys/queue.h)
85 * Both methods use a common queue head and linkage pattern:
86 * The head of a queue is declared as:
87 * queue_head_t q_head;
89 * Elements in this queue are chained together using
90 * struct queue_entry objects embedded within a structure:
99 * struct some_data is referred to as the queue "element."
100 * (note that queue_chain_t is typedef'd to struct queue_entry)
102 * IMPORTANT: The two queue iteration methods described below are not
103 * compatible with one another. You must choose one and be careful
104 * to use only the supported APIs for that method.
106 * Method 1: chaining of queue_chain_t (linkage chains)
107 * This method uses the next and prev pointers of the struct queue_entry
108 * linkage object embedded in a queue element to point to the next or
109 * previous queue_entry structure in the chain. The head of the queue
110 * (the queue_head_t object) will point to the first and last
111 * struct queue_entry object, and both the next and prev pointer will
112 * point back to the head if the queue is empty.
114 * This method is the most flexible method of chaining objects together
115 * as it allows multiple chains through a given object, by embedding
116 * multiple queue_chain_t objects in the structure, while simultaneously
117 * providing fast removal and insertion into the queue using only
118 * struct queue_entry object pointers.
120 * ++ Valid APIs for this style queue ++
121 * -------------------------------------
144 * [1] qe_foreach_safe
145 * [1] qe_foreach_element
146 * [1] qe_foreach_element_safe
148 * Method 2: chaining of elements (element chains)
149 * This method uses the next and prev pointers of the struct queue_entry
150 * linkage object embedded in a queue element to point to the next or
151 * previous queue element (not another queue_entry). The head of the
152 * queue will point to the first and last queue element (struct some_data
153 * from the above example) NOT the embedded queue_entry structure. The
154 * first queue element will have a prev pointer that points to the
155 * queue_head_t, and the last queue element will have a next pointer
156 * that points to the queue_head_t.
158 * This method requires knowledge of the queue_head_t of the queue on
159 * which an element resides in order to remove the element. Iterating
160 * through the elements of the queue is also more cumbersome because
161 * a check against the head pointer plus a cast then offset operation
162 * must be performed at each step of the iteration.
164 * ++ Valid APIs for this style queue ++
165 * -------------------------------------
175 * [2] queue_enter_first
176 * [2] queue_insert_before
177 * [2] queue_insert_after
180 * [2] queue_remove_first
181 * [2] queue_remove_last
187 * [C] -> API common to both methods
188 * [1] -> API used only in method 1 (linkage chains)
189 * [2] -> API used only in method 2 (element chains)
193 * A generic doubly-linked list (queue).
197 struct queue_entry
*next
; /* next element */
198 struct queue_entry
*prev
; /* previous element */
202 typedef struct queue_entry
*queue_t
;
203 typedef struct queue_entry queue_head_t
;
204 typedef struct queue_entry queue_chain_t
;
205 typedef struct queue_entry
*queue_entry_t
;
208 * enqueue puts "elt" on the "queue".
209 * dequeue returns the first element in the "queue".
210 * remqueue removes the specified "elt" from its queue.
213 #define enqueue(queue,elt) enqueue_tail(queue, elt)
214 #define dequeue(queue) dequeue_head(queue)
216 #ifdef XNU_KERNEL_PRIVATE
217 #include <kern/debug.h>
218 #include <mach/branch_predicates.h>
219 static inline void __QUEUE_ELT_VALIDATE(queue_entry_t elt
) {
220 queue_entry_t elt_next
, elt_prev
;
222 if (__improbable(elt
== (queue_entry_t
)0)) {
223 panic("Invalid queue element %p", elt
);
226 elt_next
= elt
->next
;
227 elt_prev
= elt
->prev
;
229 if (__improbable(elt_next
== (queue_entry_t
)0 || elt_prev
== (queue_entry_t
)0)) {
230 panic("Invalid queue element pointers for %p: next %p prev %p", elt
, elt_next
, elt_prev
);
232 if (__improbable(elt_next
->prev
!= elt
|| elt_prev
->next
!= elt
)) {
233 panic("Invalid queue element linkage for %p: next %p next->prev %p prev %p prev->next %p",
234 elt
, elt_next
, elt_next
->prev
, elt_prev
, elt_prev
->next
);
238 static inline void __DEQUEUE_ELT_CLEANUP(queue_entry_t elt
) {
239 (elt
)->next
= (queue_entry_t
) 0;
240 (elt
)->prev
= (queue_entry_t
) 0;
243 #define __QUEUE_ELT_VALIDATE(elt) do { } while (0)
244 #define __DEQUEUE_ELT_CLEANUP(elt) do { } while(0)
245 #endif /* !XNU_KERNEL_PRIVATE */
247 static __inline__
void
252 queue_entry_t old_head
;
254 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
255 old_head
= que
->next
;
256 elt
->next
= old_head
;
258 old_head
->prev
= elt
;
262 static __inline__
void
267 queue_entry_t old_tail
;
269 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
270 old_tail
= que
->prev
;
272 elt
->prev
= old_tail
;
273 old_tail
->next
= elt
;
277 static __inline__ queue_entry_t
281 queue_entry_t elt
= (queue_entry_t
) 0;
282 queue_entry_t new_head
;
284 if (que
->next
!= que
) {
286 __QUEUE_ELT_VALIDATE(elt
);
287 new_head
= elt
->next
; /* new_head may point to que if elt was the only element */
288 new_head
->prev
= que
;
289 que
->next
= new_head
;
290 __DEQUEUE_ELT_CLEANUP(elt
);
296 static __inline__ queue_entry_t
300 queue_entry_t elt
= (queue_entry_t
) 0;
301 queue_entry_t new_tail
;
303 if (que
->prev
!= que
) {
305 __QUEUE_ELT_VALIDATE(elt
);
306 new_tail
= elt
->prev
; /* new_tail may point to queue if elt was the only element */
307 new_tail
->next
= que
;
308 que
->prev
= new_tail
;
309 __DEQUEUE_ELT_CLEANUP(elt
);
315 static __inline__
void
319 queue_entry_t next_elt
, prev_elt
;
321 __QUEUE_ELT_VALIDATE(elt
);
322 next_elt
= elt
->next
;
323 prev_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
324 next_elt
->prev
= prev_elt
;
325 prev_elt
->next
= next_elt
;
326 __DEQUEUE_ELT_CLEANUP(elt
);
329 static __inline__
void
334 queue_entry_t successor
;
336 __QUEUE_ELT_VALIDATE(pred
);
337 successor
= pred
->next
;
338 entry
->next
= successor
;
340 successor
->prev
= entry
;
344 static __inline__
void
348 queue_entry_t next_elt
, prev_elt
;
350 __QUEUE_ELT_VALIDATE(elt
);
351 next_elt
= elt
->next
;
352 prev_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
353 next_elt
->prev
= prev_elt
;
354 prev_elt
->next
= next_elt
;
355 __DEQUEUE_ELT_CLEANUP(elt
);
359 * Function: re_queue_head
361 * queue_t que : queue onto which elt will be pre-pended
362 * queue_entry_t elt : element to re-queue
364 * Remove elt from its current queue and put it onto the
365 * head of a new queue
367 * This should only be used with Method 1 queue iteration (linkage chains)
369 static __inline__
void
370 re_queue_head(queue_t que
, queue_entry_t elt
)
372 queue_entry_t n_elt
, p_elt
;
374 __QUEUE_ELT_VALIDATE(elt
);
375 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
379 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
392 * Function: re_queue_tail
394 * queue_t que : queue onto which elt will be appended
395 * queue_entry_t elt : element to re-queue
397 * Remove elt from its current queue and put it onto the
400 * This should only be used with Method 1 queue iteration (linkage chains)
402 static __inline__
void
403 re_queue_tail(queue_t que
, queue_entry_t elt
)
405 queue_entry_t n_elt
, p_elt
;
407 __QUEUE_ELT_VALIDATE(elt
);
408 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
412 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
427 * Convert a queue_entry_t to a queue element pointer.
428 * Get a pointer to the user-defined element containing
429 * a given queue_entry_t
431 * <type> * qe_element(queue_entry_t qe, <type>, field)
432 * qe - queue entry to convert
433 * <type> - what's in the queue (e.g., struct some_data)
434 * <field> - is the chain field in <type>
436 * Do not use pointer types for <type>
438 #define qe_element(qe, type, field) \
439 ((type *)((void *)((char *)(qe) - __offsetof(type, field))))
444 * Iterate over each queue_entry_t structure.
445 * Generates a 'for' loop, setting 'qe' to
446 * each queue_entry_t in the queue.
448 * qe_foreach(queue_entry_t qe, queue_t head)
449 * qe - iteration variable
450 * head - pointer to queue_head_t (head of queue)
452 * This should only be used with Method 1 queue iteration (linkage chains)
454 #define qe_foreach(qe, head) \
455 for (qe = (head)->next; qe != (head); qe = (qe)->next)
458 * Macro: qe_foreach_safe
460 * Safely iterate over each queue_entry_t structure.
462 * Use this iterator macro if you plan to remove the
463 * queue_entry_t, qe, from the queue during the
466 * qe_foreach_safe(queue_entry_t qe, queue_t head)
467 * qe - iteration variable
468 * head - pointer to queue_head_t (head of queue)
470 * This should only be used with Method 1 queue iteration (linkage chains)
472 #define qe_foreach_safe(qe, head) \
473 for (queue_entry_t _ne = ((head)->next)->next, \
474 __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \
476 qe = _ne, _ne = (qe)->next)
479 * Macro: qe_foreach_element
481 * Iterate over each _element_ in a queue
482 * where each queue_entry_t points to another
483 * queue_entry_t, i.e., managed by the [de|en]queue_head/
484 * [de|en]queue_tail / remqueue / etc. function.
486 * qe_foreach_element(<type> *elt, queue_t head, <field>)
487 * elt - iteration variable
488 * <type> - what's in the queue (e.g., struct some_data)
489 * <field> - is the chain field in <type>
491 * This should only be used with Method 1 queue iteration (linkage chains)
493 #define qe_foreach_element(elt, head, field) \
494 for (elt = qe_element((head)->next, typeof(*(elt)), field); \
495 &((elt)->field) != (head); \
496 elt = qe_element((elt)->field.next, typeof(*(elt)), field))
499 * Macro: qe_foreach_element_safe
501 * Safely iterate over each _element_ in a queue
502 * where each queue_entry_t points to another
503 * queue_entry_t, i.e., managed by the [de|en]queue_head/
504 * [de|en]queue_tail / remqueue / etc. function.
506 * Use this iterator macro if you plan to remove the
507 * element, elt, from the queue during the iteration.
509 * qe_foreach_element_safe(<type> *elt, queue_t head, <field>)
510 * elt - iteration variable
511 * <type> - what's in the queue (e.g., struct some_data)
512 * <field> - is the chain field in <type>
514 * This should only be used with Method 1 queue iteration (linkage chains)
516 #define qe_foreach_element_safe(elt, head, field) \
517 for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field), \
518 *__ ## elt ## _unused_shadow __unused = \
519 (elt = qe_element((head)->next, typeof(*(elt)), field)); \
520 &((elt)->field) != (head); \
521 elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field)) \
526 * Initialize the given queue.
529 * queue_t q; \* MODIFIED *\
531 #define queue_init(q) \
538 * Macro: queue_head_init
540 * Initialize the given queue head
542 * void queue_head_init(q)
543 * queue_head_t q; \* MODIFIED *\
545 #define queue_head_init(q) \
549 * Macro: queue_chain_init
551 * Initialize the given queue chain element
553 * void queue_chain_init(q)
554 * queue_chain_t q; \* MODIFIED *\
556 #define queue_chain_init(q) \
562 * Returns the first entry in the queue,
564 * queue_entry_t queue_first(q)
565 * queue_t q; \* IN *\
567 #define queue_first(q) ((q)->next)
572 * Returns the entry after an item in the queue.
574 * queue_entry_t queue_next(qc)
577 #define queue_next(qc) ((qc)->next)
582 * Returns the last entry in the queue.
584 * queue_entry_t queue_last(q)
585 * queue_t q; \* IN *\
587 #define queue_last(q) ((q)->prev)
592 * Returns the entry before an item in the queue.
594 * queue_entry_t queue_prev(qc)
597 #define queue_prev(qc) ((qc)->prev)
602 * Tests whether a new entry is really the end of
605 * boolean_t queue_end(q, qe)
609 #define queue_end(q, qe) ((q) == (qe))
614 * Tests whether a queue is empty.
616 * boolean_t queue_empty(q)
619 #define queue_empty(q) queue_end((q), queue_first(q))
624 * queue_t _old : head of a queue whose items will be moved
625 * queue_t _new : new queue head onto which items will be moved
627 * Rebase queue items in _old onto _new then re-initialize
628 * the _old object to an empty queue.
629 * Equivalent to the queue_new_head Method 2 macro
631 * Similar to the queue_new_head macro, this macros is intented
632 * to function as an initializer method for '_new' and thus may
633 * leak any list items that happen to be on the '_new' list.
634 * This should only be used with Method 1 queue iteration (linkage chains)
636 static __inline__
void
637 movqueue(queue_t _old
, queue_t _new
)
639 queue_entry_t next_elt
, prev_elt
;
641 __QUEUE_ELT_VALIDATE((queue_entry_t
)_old
);
643 if (queue_empty(_old
)) {
649 * move the queue at _old to _new
650 * and re-initialize _old
652 next_elt
= _old
->next
;
653 prev_elt
= _old
->prev
;
655 _new
->next
= next_elt
;
656 _new
->prev
= prev_elt
;
657 next_elt
->prev
= _new
;
658 prev_elt
->next
= _new
;
663 /*----------------------------------------------------------------*/
665 * Macros that operate on generic structures. The queue
666 * chain may be at any location within the structure, and there
667 * may be more than one chain.
673 * Insert a new element at the tail of the queue.
675 * void queue_enter(q, elt, type, field)
678 * <type> is what's in our queue
679 * <field> is the chain field in (*<type>)
681 * This should only be used with Method 2 queue iteration (element chains)
683 #define queue_enter(head, elt, type, field) \
685 queue_entry_t __prev; \
687 __prev = (head)->prev; \
688 if ((head) == __prev) { \
689 (head)->next = (queue_entry_t) (elt); \
692 ((type)(void *)__prev)->field.next = \
693 (queue_entry_t)(elt); \
695 (elt)->field.prev = __prev; \
696 (elt)->field.next = head; \
697 (head)->prev = (queue_entry_t) elt; \
701 * Macro: queue_enter_first
703 * Insert a new element at the head of the queue.
705 * void queue_enter_first(q, elt, type, field)
708 * <type> is what's in our queue
709 * <field> is the chain field in (*<type>)
711 * This should only be used with Method 2 queue iteration (element chains)
713 #define queue_enter_first(head, elt, type, field) \
715 queue_entry_t __next; \
717 __next = (head)->next; \
718 if ((head) == __next) { \
719 (head)->prev = (queue_entry_t) (elt); \
722 ((type)(void *)__next)->field.prev = \
723 (queue_entry_t)(elt); \
725 (elt)->field.next = __next; \
726 (elt)->field.prev = head; \
727 (head)->next = (queue_entry_t) elt; \
731 * Macro: queue_insert_before
733 * Insert a new element before a given element.
735 * void queue_insert_before(q, elt, cur, type, field)
739 * <type> is what's in our queue
740 * <field> is the chain field in (*<type>)
742 * This should only be used with Method 2 queue iteration (element chains)
744 #define queue_insert_before(head, elt, cur, type, field) \
746 queue_entry_t __prev; \
748 if ((head) == (queue_entry_t)(cur)) { \
749 (elt)->field.next = (head); \
750 if ((head)->next == (head)) { /* only element */ \
751 (elt)->field.prev = (head); \
752 (head)->next = (queue_entry_t)(elt); \
753 } else { /* last element */ \
754 __prev = (elt)->field.prev = (head)->prev; \
755 ((type)(void *)__prev)->field.next = \
756 (queue_entry_t)(elt); \
758 (head)->prev = (queue_entry_t)(elt); \
760 (elt)->field.next = (queue_entry_t)(cur); \
761 if ((head)->next == (queue_entry_t)(cur)) { \
762 /* first element */ \
763 (elt)->field.prev = (head); \
764 (head)->next = (queue_entry_t)(elt); \
765 } else { /* middle element */ \
766 __prev = (elt)->field.prev = (cur)->field.prev; \
767 ((type)(void *)__prev)->field.next = \
768 (queue_entry_t)(elt); \
770 (cur)->field.prev = (queue_entry_t)(elt); \
775 * Macro: queue_insert_after
777 * Insert a new element after a given element.
779 * void queue_insert_after(q, elt, cur, type, field)
783 * <type> is what's in our queue
784 * <field> is the chain field in (*<type>)
786 * This should only be used with Method 2 queue iteration (element chains)
788 #define queue_insert_after(head, elt, cur, type, field) \
790 queue_entry_t __next; \
792 if ((head) == (queue_entry_t)(cur)) { \
793 (elt)->field.prev = (head); \
794 if ((head)->next == (head)) { /* only element */ \
795 (elt)->field.next = (head); \
796 (head)->prev = (queue_entry_t)(elt); \
797 } else { /* first element */ \
798 __next = (elt)->field.next = (head)->next; \
799 ((type)(void *)__next)->field.prev = \
800 (queue_entry_t)(elt); \
802 (head)->next = (queue_entry_t)(elt); \
804 (elt)->field.prev = (queue_entry_t)(cur); \
805 if ((head)->prev == (queue_entry_t)(cur)) { \
807 (elt)->field.next = (head); \
808 (head)->prev = (queue_entry_t)(elt); \
809 } else { /* middle element */ \
810 __next = (elt)->field.next = (cur)->field.next; \
811 ((type)(void *)__next)->field.prev = \
812 (queue_entry_t)(elt); \
814 (cur)->field.next = (queue_entry_t)(elt); \
819 * Macro: queue_field [internal use only]
821 * Find the queue_chain_t (or queue_t) for the
822 * given element (thing) in the given queue (head)
824 * This should only be used with Method 2 queue iteration (element chains)
826 #define queue_field(head, thing, type, field) \
827 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
830 * Macro: queue_remove
832 * Remove an arbitrary item from the queue.
834 * void queue_remove(q, qe, type, field)
835 * arguments as in queue_enter
837 * This should only be used with Method 2 queue iteration (element chains)
839 #define queue_remove(head, elt, type, field) \
841 queue_entry_t __next, __prev; \
843 __next = (elt)->field.next; \
844 __prev = (elt)->field.prev; \
846 if ((head) == __next) \
847 (head)->prev = __prev; \
849 ((type)(void *)__next)->field.prev = __prev; \
851 if ((head) == __prev) \
852 (head)->next = __next; \
854 ((type)(void *)__prev)->field.next = __next; \
856 (elt)->field.next = NULL; \
857 (elt)->field.prev = NULL; \
861 * Macro: queue_remove_first
863 * Remove and return the entry at the head of
866 * queue_remove_first(head, entry, type, field)
867 * entry is returned by reference
869 * This should only be used with Method 2 queue iteration (element chains)
871 #define queue_remove_first(head, entry, type, field) \
873 queue_entry_t __next; \
875 (entry) = (type)(void *) ((head)->next); \
876 __next = (entry)->field.next; \
878 if ((head) == __next) \
879 (head)->prev = (head); \
881 ((type)(void *)(__next))->field.prev = (head); \
882 (head)->next = __next; \
884 (entry)->field.next = NULL; \
885 (entry)->field.prev = NULL; \
889 * Macro: queue_remove_last
891 * Remove and return the entry at the tail of
894 * queue_remove_last(head, entry, type, field)
895 * entry is returned by reference
897 * This should only be used with Method 2 queue iteration (element chains)
899 #define queue_remove_last(head, entry, type, field) \
901 queue_entry_t __prev; \
903 (entry) = (type)(void *) ((head)->prev); \
904 __prev = (entry)->field.prev; \
906 if ((head) == __prev) \
907 (head)->next = (head); \
909 ((type)(void *)(__prev))->field.next = (head); \
910 (head)->prev = __prev; \
912 (entry)->field.next = NULL; \
913 (entry)->field.prev = NULL; \
917 * Macro: queue_assign
919 * This should only be used with Method 2 queue iteration (element chains)
921 #define queue_assign(to, from, type, field) \
923 ((type)(void *)((from)->prev))->field.next = (to); \
924 ((type)(void *)((from)->next))->field.prev = (to); \
929 * Macro: queue_new_head
931 * rebase old queue to new queue head
933 * queue_new_head(old, new, type, field)
936 * <type> is what's in our queue
937 * <field> is the chain field in (*<type>)
939 * This should only be used with Method 2 queue iteration (element chains)
941 #define queue_new_head(old, new, type, field) \
943 if (!queue_empty(old)) { \
945 ((type)(void *)((new)->next))->field.prev = \
947 ((type)(void *)((new)->prev))->field.next = \
955 * Macro: queue_iterate
957 * iterate over each item in the queue.
958 * Generates a 'for' loop, setting elt to
959 * each item in turn (by reference).
961 * queue_iterate(q, elt, type, field)
964 * <type> is what's in our queue
965 * <field> is the chain field in (*<type>)
967 * This should only be used with Method 2 queue iteration (element chains)
969 #define queue_iterate(head, elt, type, field) \
970 for ((elt) = (type)(void *) queue_first(head); \
971 !queue_end((head), (queue_entry_t)(elt)); \
972 (elt) = (type)(void *) queue_next(&(elt)->field))
974 #ifdef MACH_KERNEL_PRIVATE
976 #include <kern/locks.h>
978 /*----------------------------------------------------------------*/
980 * Define macros for queues with locks.
982 struct mpqueue_head
{
983 struct queue_entry head
; /* header for queue */
984 uint64_t earliest_soft_deadline
;
986 #if defined(__i386__) || defined(__x86_64__)
988 lck_mtx_ext_t lock_data_ext
;
990 lck_spin_t lock_data
;
994 typedef struct mpqueue_head mpqueue_head_t
;
996 #define round_mpq(size) (size)
999 #if defined(__i386__) || defined(__x86_64__)
1001 #define mpqueue_init(q, lck_grp, lck_attr) \
1003 queue_init(&(q)->head); \
1004 lck_mtx_init_ext(&(q)->lock_data, \
1005 &(q)->lock_data_ext, \
1008 (q)->earliest_soft_deadline = UINT64_MAX; \
1014 #define mpqueue_init(q, lck_grp, lck_attr) \
1016 queue_init(&(q)->head); \
1017 lck_spin_init(&(q)->lock_data, \
1024 #define mpenqueue_tail(q, elt) \
1026 lck_mtx_lock_spin_always(&(q)->lock_data); \
1027 enqueue_tail(&(q)->head, elt); \
1028 lck_mtx_unlock_always(&(q)->lock_data); \
1031 #define mpdequeue_head(q, elt) \
1033 lck_mtx_lock_spin_always(&(q)->lock_data); \
1034 if (queue_empty(&(q)->head)) \
1037 *(elt) = dequeue_head(&(q)->head); \
1038 lck_mtx_unlock_always(&(q)->lock_data); \
1041 #endif /* MACH_KERNEL_PRIVATE */
1045 #endif /* _KERN_QUEUE_H_ */