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)) \
523 #ifdef XNU_KERNEL_PRIVATE
525 /* Dequeue an element from head, or return NULL if the queue is empty */
526 #define qe_dequeue_head(head, type, field) ({ \
527 queue_entry_t _tmp_entry = dequeue_head((head)); \
528 type *_tmp_element = (type*) NULL; \
529 if (_tmp_entry != (queue_entry_t) NULL) \
530 _tmp_element = qe_element(_tmp_entry, type, field); \
534 /* Dequeue an element from tail, or return NULL if the queue is empty */
535 #define qe_dequeue_tail(head, type, field) ({ \
536 queue_entry_t _tmp_entry = dequeue_tail((head)); \
537 type *_tmp_element = (type*) NULL; \
538 if (_tmp_entry != (queue_entry_t) NULL) \
539 _tmp_element = qe_element(_tmp_entry, type, field); \
543 /* Peek at the first element, or return NULL if the queue is empty */
544 #define qe_queue_first(head, type, field) ({ \
545 queue_entry_t _tmp_entry = queue_first((head)); \
546 type *_tmp_element = (type*) NULL; \
547 if (_tmp_entry != (queue_entry_t) head) \
548 _tmp_element = qe_element(_tmp_entry, type, field); \
552 /* Peek at the last element, or return NULL if the queue is empty */
553 #define qe_queue_last(head, type, field) ({ \
554 queue_entry_t _tmp_entry = queue_last((head)); \
555 type *_tmp_element = (type*) NULL; \
556 if (_tmp_entry != (queue_entry_t) head) \
557 _tmp_element = qe_element(_tmp_entry, type, field); \
561 #endif /* XNU_KERNEL_PRIVATE */
566 * Initialize the given queue.
569 * queue_t q; \* MODIFIED *\
571 #define queue_init(q) \
578 * Macro: queue_head_init
580 * Initialize the given queue head
582 * void queue_head_init(q)
583 * queue_head_t q; \* MODIFIED *\
585 #define queue_head_init(q) \
589 * Macro: queue_chain_init
591 * Initialize the given queue chain element
593 * void queue_chain_init(q)
594 * queue_chain_t q; \* MODIFIED *\
596 #define queue_chain_init(q) \
602 * Returns the first entry in the queue,
604 * queue_entry_t queue_first(q)
605 * queue_t q; \* IN *\
607 #define queue_first(q) ((q)->next)
612 * Returns the entry after an item in the queue.
614 * queue_entry_t queue_next(qc)
617 #define queue_next(qc) ((qc)->next)
622 * Returns the last entry in the queue.
624 * queue_entry_t queue_last(q)
625 * queue_t q; \* IN *\
627 #define queue_last(q) ((q)->prev)
632 * Returns the entry before an item in the queue.
634 * queue_entry_t queue_prev(qc)
637 #define queue_prev(qc) ((qc)->prev)
642 * Tests whether a new entry is really the end of
645 * boolean_t queue_end(q, qe)
649 #define queue_end(q, qe) ((q) == (qe))
654 * Tests whether a queue is empty.
656 * boolean_t queue_empty(q)
659 #define queue_empty(q) queue_end((q), queue_first(q))
664 * queue_t _old : head of a queue whose items will be moved
665 * queue_t _new : new queue head onto which items will be moved
667 * Rebase queue items in _old onto _new then re-initialize
668 * the _old object to an empty queue.
669 * Equivalent to the queue_new_head Method 2 macro
671 * Similar to the queue_new_head macro, this macros is intented
672 * to function as an initializer method for '_new' and thus may
673 * leak any list items that happen to be on the '_new' list.
674 * This should only be used with Method 1 queue iteration (linkage chains)
676 static __inline__
void
677 movqueue(queue_t _old
, queue_t _new
)
679 queue_entry_t next_elt
, prev_elt
;
681 __QUEUE_ELT_VALIDATE((queue_entry_t
)_old
);
683 if (queue_empty(_old
)) {
689 * move the queue at _old to _new
690 * and re-initialize _old
692 next_elt
= _old
->next
;
693 prev_elt
= _old
->prev
;
695 _new
->next
= next_elt
;
696 _new
->prev
= prev_elt
;
697 next_elt
->prev
= _new
;
698 prev_elt
->next
= _new
;
703 /*----------------------------------------------------------------*/
705 * Macros that operate on generic structures. The queue
706 * chain may be at any location within the structure, and there
707 * may be more than one chain.
713 * Insert a new element at the tail of the queue.
715 * void queue_enter(q, elt, type, field)
718 * <type> is what's in our queue
719 * <field> is the chain field in (*<type>)
721 * This should only be used with Method 2 queue iteration (element chains)
723 #define queue_enter(head, elt, type, field) \
725 queue_entry_t __prev; \
727 __prev = (head)->prev; \
728 if ((head) == __prev) { \
729 (head)->next = (queue_entry_t) (elt); \
732 ((type)(void *)__prev)->field.next = \
733 (queue_entry_t)(elt); \
735 (elt)->field.prev = __prev; \
736 (elt)->field.next = head; \
737 (head)->prev = (queue_entry_t) elt; \
741 * Macro: queue_enter_first
743 * Insert a new element at the head of the queue.
745 * void queue_enter_first(q, elt, type, field)
748 * <type> is what's in our queue
749 * <field> is the chain field in (*<type>)
751 * This should only be used with Method 2 queue iteration (element chains)
753 #define queue_enter_first(head, elt, type, field) \
755 queue_entry_t __next; \
757 __next = (head)->next; \
758 if ((head) == __next) { \
759 (head)->prev = (queue_entry_t) (elt); \
762 ((type)(void *)__next)->field.prev = \
763 (queue_entry_t)(elt); \
765 (elt)->field.next = __next; \
766 (elt)->field.prev = head; \
767 (head)->next = (queue_entry_t) elt; \
771 * Macro: queue_insert_before
773 * Insert a new element before a given element.
775 * void queue_insert_before(q, elt, cur, type, field)
779 * <type> is what's in our queue
780 * <field> is the chain field in (*<type>)
782 * This should only be used with Method 2 queue iteration (element chains)
784 #define queue_insert_before(head, elt, cur, type, field) \
786 queue_entry_t __prev; \
788 if ((head) == (queue_entry_t)(cur)) { \
789 (elt)->field.next = (head); \
790 if ((head)->next == (head)) { /* only element */ \
791 (elt)->field.prev = (head); \
792 (head)->next = (queue_entry_t)(elt); \
793 } else { /* last element */ \
794 __prev = (elt)->field.prev = (head)->prev; \
795 ((type)(void *)__prev)->field.next = \
796 (queue_entry_t)(elt); \
798 (head)->prev = (queue_entry_t)(elt); \
800 (elt)->field.next = (queue_entry_t)(cur); \
801 if ((head)->next == (queue_entry_t)(cur)) { \
802 /* first element */ \
803 (elt)->field.prev = (head); \
804 (head)->next = (queue_entry_t)(elt); \
805 } else { /* middle element */ \
806 __prev = (elt)->field.prev = (cur)->field.prev; \
807 ((type)(void *)__prev)->field.next = \
808 (queue_entry_t)(elt); \
810 (cur)->field.prev = (queue_entry_t)(elt); \
815 * Macro: queue_insert_after
817 * Insert a new element after a given element.
819 * void queue_insert_after(q, elt, cur, type, field)
823 * <type> is what's in our queue
824 * <field> is the chain field in (*<type>)
826 * This should only be used with Method 2 queue iteration (element chains)
828 #define queue_insert_after(head, elt, cur, type, field) \
830 queue_entry_t __next; \
832 if ((head) == (queue_entry_t)(cur)) { \
833 (elt)->field.prev = (head); \
834 if ((head)->next == (head)) { /* only element */ \
835 (elt)->field.next = (head); \
836 (head)->prev = (queue_entry_t)(elt); \
837 } else { /* first element */ \
838 __next = (elt)->field.next = (head)->next; \
839 ((type)(void *)__next)->field.prev = \
840 (queue_entry_t)(elt); \
842 (head)->next = (queue_entry_t)(elt); \
844 (elt)->field.prev = (queue_entry_t)(cur); \
845 if ((head)->prev == (queue_entry_t)(cur)) { \
847 (elt)->field.next = (head); \
848 (head)->prev = (queue_entry_t)(elt); \
849 } else { /* middle element */ \
850 __next = (elt)->field.next = (cur)->field.next; \
851 ((type)(void *)__next)->field.prev = \
852 (queue_entry_t)(elt); \
854 (cur)->field.next = (queue_entry_t)(elt); \
859 * Macro: queue_field [internal use only]
861 * Find the queue_chain_t (or queue_t) for the
862 * given element (thing) in the given queue (head)
864 * This should only be used with Method 2 queue iteration (element chains)
866 #define queue_field(head, thing, type, field) \
867 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
870 * Macro: queue_remove
872 * Remove an arbitrary item from the queue.
874 * void queue_remove(q, qe, type, field)
875 * arguments as in queue_enter
877 * This should only be used with Method 2 queue iteration (element chains)
879 #define queue_remove(head, elt, type, field) \
881 queue_entry_t __next, __prev; \
883 __next = (elt)->field.next; \
884 __prev = (elt)->field.prev; \
886 if ((head) == __next) \
887 (head)->prev = __prev; \
889 ((type)(void *)__next)->field.prev = __prev; \
891 if ((head) == __prev) \
892 (head)->next = __next; \
894 ((type)(void *)__prev)->field.next = __next; \
896 (elt)->field.next = NULL; \
897 (elt)->field.prev = NULL; \
901 * Macro: queue_remove_first
903 * Remove and return the entry at the head of
906 * queue_remove_first(head, entry, type, field)
907 * entry is returned by reference
909 * This should only be used with Method 2 queue iteration (element chains)
911 #define queue_remove_first(head, entry, type, field) \
913 queue_entry_t __next; \
915 (entry) = (type)(void *) ((head)->next); \
916 __next = (entry)->field.next; \
918 if ((head) == __next) \
919 (head)->prev = (head); \
921 ((type)(void *)(__next))->field.prev = (head); \
922 (head)->next = __next; \
924 (entry)->field.next = NULL; \
925 (entry)->field.prev = NULL; \
929 * Macro: queue_remove_last
931 * Remove and return the entry at the tail of
934 * queue_remove_last(head, entry, type, field)
935 * entry is returned by reference
937 * This should only be used with Method 2 queue iteration (element chains)
939 #define queue_remove_last(head, entry, type, field) \
941 queue_entry_t __prev; \
943 (entry) = (type)(void *) ((head)->prev); \
944 __prev = (entry)->field.prev; \
946 if ((head) == __prev) \
947 (head)->next = (head); \
949 ((type)(void *)(__prev))->field.next = (head); \
950 (head)->prev = __prev; \
952 (entry)->field.next = NULL; \
953 (entry)->field.prev = NULL; \
957 * Macro: queue_assign
959 * This should only be used with Method 2 queue iteration (element chains)
961 #define queue_assign(to, from, type, field) \
963 ((type)(void *)((from)->prev))->field.next = (to); \
964 ((type)(void *)((from)->next))->field.prev = (to); \
969 * Macro: queue_new_head
971 * rebase old queue to new queue head
973 * queue_new_head(old, new, type, field)
976 * <type> is what's in our queue
977 * <field> is the chain field in (*<type>)
979 * This should only be used with Method 2 queue iteration (element chains)
981 #define queue_new_head(old, new, type, field) \
983 if (!queue_empty(old)) { \
985 ((type)(void *)((new)->next))->field.prev = \
987 ((type)(void *)((new)->prev))->field.next = \
995 * Macro: queue_iterate
997 * iterate over each item in the queue.
998 * Generates a 'for' loop, setting elt to
999 * each item in turn (by reference).
1001 * queue_iterate(q, elt, type, field)
1004 * <type> is what's in our queue
1005 * <field> is the chain field in (*<type>)
1007 * This should only be used with Method 2 queue iteration (element chains)
1009 #define queue_iterate(head, elt, type, field) \
1010 for ((elt) = (type)(void *) queue_first(head); \
1011 !queue_end((head), (queue_entry_t)(elt)); \
1012 (elt) = (type)(void *) queue_next(&(elt)->field))
1014 #ifdef MACH_KERNEL_PRIVATE
1016 #include <kern/locks.h>
1018 /*----------------------------------------------------------------*/
1020 * Define macros for queues with locks.
1022 struct mpqueue_head
{
1023 struct queue_entry head
; /* header for queue */
1024 uint64_t earliest_soft_deadline
;
1026 lck_mtx_t lock_data
;
1027 #if defined(__i386__) || defined(__x86_64__)
1028 lck_mtx_ext_t lock_data_ext
;
1032 typedef struct mpqueue_head mpqueue_head_t
;
1034 #define round_mpq(size) (size)
1037 #if defined(__i386__) || defined(__x86_64__)
1039 #define mpqueue_init(q, lck_grp, lck_attr) \
1041 queue_init(&(q)->head); \
1042 lck_mtx_init_ext(&(q)->lock_data, \
1043 &(q)->lock_data_ext, \
1046 (q)->earliest_soft_deadline = UINT64_MAX; \
1052 #define mpqueue_init(q, lck_grp, lck_attr) \
1054 queue_init(&(q)->head); \
1055 lck_mtx_init(&(q)->lock_data, \
1062 #define mpenqueue_tail(q, elt) \
1064 lck_mtx_lock_spin_always(&(q)->lock_data); \
1065 enqueue_tail(&(q)->head, elt); \
1066 lck_mtx_unlock_always(&(q)->lock_data); \
1069 #define mpdequeue_head(q, elt) \
1071 lck_mtx_lock_spin_always(&(q)->lock_data); \
1072 if (queue_empty(&(q)->head)) \
1075 *(elt) = dequeue_head(&(q)->head); \
1076 lck_mtx_unlock_always(&(q)->lock_data); \
1079 #endif /* MACH_KERNEL_PRIVATE */
1083 #endif /* _KERN_QUEUE_H_ */