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 */
200 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
201 /* For the newer ARMv7k ABI where 64-bit types are 64-bit aligned, but pointers
203 * Since this type is so often cast to various 64-bit aligned types
204 * aligning it to 64-bits will avoid -wcast-align without needing
205 * to disable it entirely. The impact on memory footprint should be
208 } __attribute__ ((aligned (8)));
213 typedef struct queue_entry
*queue_t
;
214 typedef struct queue_entry queue_head_t
;
215 typedef struct queue_entry queue_chain_t
;
216 typedef struct queue_entry
*queue_entry_t
;
219 * enqueue puts "elt" on the "queue".
220 * dequeue returns the first element in the "queue".
221 * remqueue removes the specified "elt" from its queue.
224 #define enqueue(queue,elt) enqueue_tail(queue, elt)
225 #define dequeue(queue) dequeue_head(queue)
227 #ifdef XNU_KERNEL_PRIVATE
228 #include <kern/debug.h>
229 #include <mach/branch_predicates.h>
230 static inline void __QUEUE_ELT_VALIDATE(queue_entry_t elt
) {
231 queue_entry_t elt_next
, elt_prev
;
233 if (__improbable(elt
== (queue_entry_t
)0)) {
234 panic("Invalid queue element %p", elt
);
237 elt_next
= elt
->next
;
238 elt_prev
= elt
->prev
;
240 if (__improbable(elt_next
== (queue_entry_t
)0 || elt_prev
== (queue_entry_t
)0)) {
241 panic("Invalid queue element pointers for %p: next %p prev %p", elt
, elt_next
, elt_prev
);
243 if (__improbable(elt_next
->prev
!= elt
|| elt_prev
->next
!= elt
)) {
244 panic("Invalid queue element linkage for %p: next %p next->prev %p prev %p prev->next %p",
245 elt
, elt_next
, elt_next
->prev
, elt_prev
, elt_prev
->next
);
249 static inline void __DEQUEUE_ELT_CLEANUP(queue_entry_t elt
) {
250 (elt
)->next
= (queue_entry_t
) 0;
251 (elt
)->prev
= (queue_entry_t
) 0;
254 #define __QUEUE_ELT_VALIDATE(elt) do { } while (0)
255 #define __DEQUEUE_ELT_CLEANUP(elt) do { } while(0)
256 #endif /* !XNU_KERNEL_PRIVATE */
258 static __inline__
void
263 queue_entry_t old_head
;
265 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
266 old_head
= que
->next
;
267 elt
->next
= old_head
;
269 old_head
->prev
= elt
;
273 static __inline__
void
278 queue_entry_t old_tail
;
280 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
281 old_tail
= que
->prev
;
283 elt
->prev
= old_tail
;
284 old_tail
->next
= elt
;
288 static __inline__ queue_entry_t
292 queue_entry_t elt
= (queue_entry_t
) 0;
293 queue_entry_t new_head
;
295 if (que
->next
!= que
) {
297 __QUEUE_ELT_VALIDATE(elt
);
298 new_head
= elt
->next
; /* new_head may point to que if elt was the only element */
299 new_head
->prev
= que
;
300 que
->next
= new_head
;
301 __DEQUEUE_ELT_CLEANUP(elt
);
307 static __inline__ queue_entry_t
311 queue_entry_t elt
= (queue_entry_t
) 0;
312 queue_entry_t new_tail
;
314 if (que
->prev
!= que
) {
316 __QUEUE_ELT_VALIDATE(elt
);
317 new_tail
= elt
->prev
; /* new_tail may point to queue if elt was the only element */
318 new_tail
->next
= que
;
319 que
->prev
= new_tail
;
320 __DEQUEUE_ELT_CLEANUP(elt
);
326 static __inline__
void
330 queue_entry_t next_elt
, prev_elt
;
332 __QUEUE_ELT_VALIDATE(elt
);
333 next_elt
= elt
->next
;
334 prev_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
335 next_elt
->prev
= prev_elt
;
336 prev_elt
->next
= next_elt
;
337 __DEQUEUE_ELT_CLEANUP(elt
);
340 static __inline__
void
345 queue_entry_t successor
;
347 __QUEUE_ELT_VALIDATE(pred
);
348 successor
= pred
->next
;
349 entry
->next
= successor
;
351 successor
->prev
= entry
;
355 static __inline__
void
359 queue_entry_t next_elt
, prev_elt
;
361 __QUEUE_ELT_VALIDATE(elt
);
362 next_elt
= elt
->next
;
363 prev_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
364 next_elt
->prev
= prev_elt
;
365 prev_elt
->next
= next_elt
;
366 __DEQUEUE_ELT_CLEANUP(elt
);
370 * Function: re_queue_head
372 * queue_t que : queue onto which elt will be pre-pended
373 * queue_entry_t elt : element to re-queue
375 * Remove elt from its current queue and put it onto the
376 * head of a new queue
378 * This should only be used with Method 1 queue iteration (linkage chains)
380 static __inline__
void
381 re_queue_head(queue_t que
, queue_entry_t elt
)
383 queue_entry_t n_elt
, p_elt
;
385 __QUEUE_ELT_VALIDATE(elt
);
386 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
390 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
403 * Function: re_queue_tail
405 * queue_t que : queue onto which elt will be appended
406 * queue_entry_t elt : element to re-queue
408 * Remove elt from its current queue and put it onto the
411 * This should only be used with Method 1 queue iteration (linkage chains)
413 static __inline__
void
414 re_queue_tail(queue_t que
, queue_entry_t elt
)
416 queue_entry_t n_elt
, p_elt
;
418 __QUEUE_ELT_VALIDATE(elt
);
419 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
423 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
438 * Convert a queue_entry_t to a queue element pointer.
439 * Get a pointer to the user-defined element containing
440 * a given queue_entry_t
442 * <type> * qe_element(queue_entry_t qe, <type>, field)
443 * qe - queue entry to convert
444 * <type> - what's in the queue (e.g., struct some_data)
445 * <field> - is the chain field in <type>
447 * Do not use pointer types for <type>
449 #define qe_element(qe, type, field) \
450 ((type *)((void *)((char *)(qe) - __offsetof(type, field))))
455 * Iterate over each queue_entry_t structure.
456 * Generates a 'for' loop, setting 'qe' to
457 * each queue_entry_t in the queue.
459 * qe_foreach(queue_entry_t qe, queue_t head)
460 * qe - iteration variable
461 * head - pointer to queue_head_t (head of queue)
463 * This should only be used with Method 1 queue iteration (linkage chains)
465 #define qe_foreach(qe, head) \
466 for (qe = (head)->next; qe != (head); qe = (qe)->next)
469 * Macro: qe_foreach_safe
471 * Safely iterate over each queue_entry_t structure.
473 * Use this iterator macro if you plan to remove the
474 * queue_entry_t, qe, from the queue during the
477 * qe_foreach_safe(queue_entry_t qe, queue_t head)
478 * qe - iteration variable
479 * head - pointer to queue_head_t (head of queue)
481 * This should only be used with Method 1 queue iteration (linkage chains)
483 #define qe_foreach_safe(qe, head) \
484 for (queue_entry_t _ne = ((head)->next)->next, \
485 __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \
487 qe = _ne, _ne = (qe)->next)
490 * Macro: qe_foreach_element
492 * Iterate over each _element_ in a queue
493 * where each queue_entry_t points to another
494 * queue_entry_t, i.e., managed by the [de|en]queue_head/
495 * [de|en]queue_tail / remqueue / etc. function.
497 * qe_foreach_element(<type> *elt, queue_t head, <field>)
498 * elt - iteration variable
499 * <type> - what's in the queue (e.g., struct some_data)
500 * <field> - is the chain field in <type>
502 * This should only be used with Method 1 queue iteration (linkage chains)
504 #define qe_foreach_element(elt, head, field) \
505 for (elt = qe_element((head)->next, typeof(*(elt)), field); \
506 &((elt)->field) != (head); \
507 elt = qe_element((elt)->field.next, typeof(*(elt)), field))
510 * Macro: qe_foreach_element_safe
512 * Safely iterate over each _element_ in a queue
513 * where each queue_entry_t points to another
514 * queue_entry_t, i.e., managed by the [de|en]queue_head/
515 * [de|en]queue_tail / remqueue / etc. function.
517 * Use this iterator macro if you plan to remove the
518 * element, elt, from the queue during the iteration.
520 * qe_foreach_element_safe(<type> *elt, queue_t head, <field>)
521 * elt - iteration variable
522 * <type> - what's in the queue (e.g., struct some_data)
523 * <field> - is the chain field in <type>
525 * This should only be used with Method 1 queue iteration (linkage chains)
527 #define qe_foreach_element_safe(elt, head, field) \
528 for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field), \
529 *__ ## elt ## _unused_shadow __unused = \
530 (elt = qe_element((head)->next, typeof(*(elt)), field)); \
531 &((elt)->field) != (head); \
532 elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field)) \
534 #ifdef XNU_KERNEL_PRIVATE
536 /* Dequeue an element from head, or return NULL if the queue is empty */
537 #define qe_dequeue_head(head, type, field) ({ \
538 queue_entry_t _tmp_entry = dequeue_head((head)); \
539 type *_tmp_element = (type*) NULL; \
540 if (_tmp_entry != (queue_entry_t) NULL) \
541 _tmp_element = qe_element(_tmp_entry, type, field); \
545 /* Dequeue an element from tail, or return NULL if the queue is empty */
546 #define qe_dequeue_tail(head, type, field) ({ \
547 queue_entry_t _tmp_entry = dequeue_tail((head)); \
548 type *_tmp_element = (type*) NULL; \
549 if (_tmp_entry != (queue_entry_t) NULL) \
550 _tmp_element = qe_element(_tmp_entry, type, field); \
554 /* Peek at the first element, or return NULL if the queue is empty */
555 #define qe_queue_first(head, type, field) ({ \
556 queue_entry_t _tmp_entry = queue_first((head)); \
557 type *_tmp_element = (type*) NULL; \
558 if (_tmp_entry != (queue_entry_t) head) \
559 _tmp_element = qe_element(_tmp_entry, type, field); \
563 /* Peek at the last element, or return NULL if the queue is empty */
564 #define qe_queue_last(head, type, field) ({ \
565 queue_entry_t _tmp_entry = queue_last((head)); \
566 type *_tmp_element = (type*) NULL; \
567 if (_tmp_entry != (queue_entry_t) head) \
568 _tmp_element = qe_element(_tmp_entry, type, field); \
572 #endif /* XNU_KERNEL_PRIVATE */
577 * Initialize the given queue.
580 * queue_t q; \* MODIFIED *\
582 #define queue_init(q) \
589 * Macro: queue_head_init
591 * Initialize the given queue head
593 * void queue_head_init(q)
594 * queue_head_t q; \* MODIFIED *\
596 #define queue_head_init(q) \
600 * Macro: queue_chain_init
602 * Initialize the given queue chain element
604 * void queue_chain_init(q)
605 * queue_chain_t q; \* MODIFIED *\
607 #define queue_chain_init(q) \
613 * Returns the first entry in the queue,
615 * queue_entry_t queue_first(q)
616 * queue_t q; \* IN *\
618 #define queue_first(q) ((q)->next)
623 * Returns the entry after an item in the queue.
625 * queue_entry_t queue_next(qc)
628 #define queue_next(qc) ((qc)->next)
633 * Returns the last entry in the queue.
635 * queue_entry_t queue_last(q)
636 * queue_t q; \* IN *\
638 #define queue_last(q) ((q)->prev)
643 * Returns the entry before an item in the queue.
645 * queue_entry_t queue_prev(qc)
648 #define queue_prev(qc) ((qc)->prev)
653 * Tests whether a new entry is really the end of
656 * boolean_t queue_end(q, qe)
660 #define queue_end(q, qe) ((q) == (qe))
665 * Tests whether a queue is empty.
667 * boolean_t queue_empty(q)
670 #define queue_empty(q) queue_end((q), queue_first(q))
675 * queue_t _old : head of a queue whose items will be moved
676 * queue_t _new : new queue head onto which items will be moved
678 * Rebase queue items in _old onto _new then re-initialize
679 * the _old object to an empty queue.
680 * Equivalent to the queue_new_head Method 2 macro
682 * Similar to the queue_new_head macro, this macros is intented
683 * to function as an initializer method for '_new' and thus may
684 * leak any list items that happen to be on the '_new' list.
685 * This should only be used with Method 1 queue iteration (linkage chains)
687 static __inline__
void
688 movqueue(queue_t _old
, queue_t _new
)
690 queue_entry_t next_elt
, prev_elt
;
692 __QUEUE_ELT_VALIDATE((queue_entry_t
)_old
);
694 if (queue_empty(_old
)) {
700 * move the queue at _old to _new
701 * and re-initialize _old
703 next_elt
= _old
->next
;
704 prev_elt
= _old
->prev
;
706 _new
->next
= next_elt
;
707 _new
->prev
= prev_elt
;
708 next_elt
->prev
= _new
;
709 prev_elt
->next
= _new
;
714 /*----------------------------------------------------------------*/
716 * Macros that operate on generic structures. The queue
717 * chain may be at any location within the structure, and there
718 * may be more than one chain.
724 * Insert a new element at the tail of the queue.
726 * void queue_enter(q, elt, type, field)
729 * <type> is what's in our queue
730 * <field> is the chain field in (*<type>)
732 * This should only be used with Method 2 queue iteration (element chains)
734 #define queue_enter(head, elt, type, field) \
736 queue_entry_t __prev; \
738 __prev = (head)->prev; \
739 if ((head) == __prev) { \
740 (head)->next = (queue_entry_t) (elt); \
743 ((type)(void *)__prev)->field.next = \
744 (queue_entry_t)(elt); \
746 (elt)->field.prev = __prev; \
747 (elt)->field.next = head; \
748 (head)->prev = (queue_entry_t) elt; \
752 * Macro: queue_enter_first
754 * Insert a new element at the head of the queue.
756 * void queue_enter_first(q, elt, type, field)
759 * <type> is what's in our queue
760 * <field> is the chain field in (*<type>)
762 * This should only be used with Method 2 queue iteration (element chains)
764 #define queue_enter_first(head, elt, type, field) \
766 queue_entry_t __next; \
768 __next = (head)->next; \
769 if ((head) == __next) { \
770 (head)->prev = (queue_entry_t) (elt); \
773 ((type)(void *)__next)->field.prev = \
774 (queue_entry_t)(elt); \
776 (elt)->field.next = __next; \
777 (elt)->field.prev = head; \
778 (head)->next = (queue_entry_t) elt; \
782 * Macro: queue_insert_before
784 * Insert a new element before a given element.
786 * void queue_insert_before(q, elt, cur, type, field)
790 * <type> is what's in our queue
791 * <field> is the chain field in (*<type>)
793 * This should only be used with Method 2 queue iteration (element chains)
795 #define queue_insert_before(head, elt, cur, type, field) \
797 queue_entry_t __prev; \
799 if ((head) == (queue_entry_t)(cur)) { \
800 (elt)->field.next = (head); \
801 if ((head)->next == (head)) { /* only element */ \
802 (elt)->field.prev = (head); \
803 (head)->next = (queue_entry_t)(elt); \
804 } else { /* last element */ \
805 __prev = (elt)->field.prev = (head)->prev; \
806 ((type)(void *)__prev)->field.next = \
807 (queue_entry_t)(elt); \
809 (head)->prev = (queue_entry_t)(elt); \
811 (elt)->field.next = (queue_entry_t)(cur); \
812 if ((head)->next == (queue_entry_t)(cur)) { \
813 /* first element */ \
814 (elt)->field.prev = (head); \
815 (head)->next = (queue_entry_t)(elt); \
816 } else { /* middle element */ \
817 __prev = (elt)->field.prev = (cur)->field.prev; \
818 ((type)(void *)__prev)->field.next = \
819 (queue_entry_t)(elt); \
821 (cur)->field.prev = (queue_entry_t)(elt); \
826 * Macro: queue_insert_after
828 * Insert a new element after a given element.
830 * void queue_insert_after(q, elt, cur, type, field)
834 * <type> is what's in our queue
835 * <field> is the chain field in (*<type>)
837 * This should only be used with Method 2 queue iteration (element chains)
839 #define queue_insert_after(head, elt, cur, type, field) \
841 queue_entry_t __next; \
843 if ((head) == (queue_entry_t)(cur)) { \
844 (elt)->field.prev = (head); \
845 if ((head)->next == (head)) { /* only element */ \
846 (elt)->field.next = (head); \
847 (head)->prev = (queue_entry_t)(elt); \
848 } else { /* first element */ \
849 __next = (elt)->field.next = (head)->next; \
850 ((type)(void *)__next)->field.prev = \
851 (queue_entry_t)(elt); \
853 (head)->next = (queue_entry_t)(elt); \
855 (elt)->field.prev = (queue_entry_t)(cur); \
856 if ((head)->prev == (queue_entry_t)(cur)) { \
858 (elt)->field.next = (head); \
859 (head)->prev = (queue_entry_t)(elt); \
860 } else { /* middle element */ \
861 __next = (elt)->field.next = (cur)->field.next; \
862 ((type)(void *)__next)->field.prev = \
863 (queue_entry_t)(elt); \
865 (cur)->field.next = (queue_entry_t)(elt); \
870 * Macro: queue_field [internal use only]
872 * Find the queue_chain_t (or queue_t) for the
873 * given element (thing) in the given queue (head)
875 * This should only be used with Method 2 queue iteration (element chains)
877 #define queue_field(head, thing, type, field) \
878 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
881 * Macro: queue_remove
883 * Remove an arbitrary item from the queue.
885 * void queue_remove(q, qe, type, field)
886 * arguments as in queue_enter
888 * This should only be used with Method 2 queue iteration (element chains)
890 #define queue_remove(head, elt, type, field) \
892 queue_entry_t __next, __prev; \
894 __next = (elt)->field.next; \
895 __prev = (elt)->field.prev; \
897 if ((head) == __next) \
898 (head)->prev = __prev; \
900 ((type)(void *)__next)->field.prev = __prev; \
902 if ((head) == __prev) \
903 (head)->next = __next; \
905 ((type)(void *)__prev)->field.next = __next; \
907 (elt)->field.next = NULL; \
908 (elt)->field.prev = NULL; \
912 * Macro: queue_remove_first
914 * Remove and return the entry at the head of
917 * queue_remove_first(head, entry, type, field)
918 * entry is returned by reference
920 * This should only be used with Method 2 queue iteration (element chains)
922 #define queue_remove_first(head, entry, type, field) \
924 queue_entry_t __next; \
926 (entry) = (type)(void *) ((head)->next); \
927 __next = (entry)->field.next; \
929 if ((head) == __next) \
930 (head)->prev = (head); \
932 ((type)(void *)(__next))->field.prev = (head); \
933 (head)->next = __next; \
935 (entry)->field.next = NULL; \
936 (entry)->field.prev = NULL; \
940 * Macro: queue_remove_last
942 * Remove and return the entry at the tail of
945 * queue_remove_last(head, entry, type, field)
946 * entry is returned by reference
948 * This should only be used with Method 2 queue iteration (element chains)
950 #define queue_remove_last(head, entry, type, field) \
952 queue_entry_t __prev; \
954 (entry) = (type)(void *) ((head)->prev); \
955 __prev = (entry)->field.prev; \
957 if ((head) == __prev) \
958 (head)->next = (head); \
960 ((type)(void *)(__prev))->field.next = (head); \
961 (head)->prev = __prev; \
963 (entry)->field.next = NULL; \
964 (entry)->field.prev = NULL; \
968 * Macro: queue_assign
970 * This should only be used with Method 2 queue iteration (element chains)
972 #define queue_assign(to, from, type, field) \
974 ((type)(void *)((from)->prev))->field.next = (to); \
975 ((type)(void *)((from)->next))->field.prev = (to); \
980 * Macro: queue_new_head
982 * rebase old queue to new queue head
984 * queue_new_head(old, new, type, field)
987 * <type> is what's in our queue
988 * <field> is the chain field in (*<type>)
990 * This should only be used with Method 2 queue iteration (element chains)
992 #define queue_new_head(old, new, type, field) \
994 if (!queue_empty(old)) { \
996 ((type)(void *)((new)->next))->field.prev = \
998 ((type)(void *)((new)->prev))->field.next = \
1006 * Macro: queue_iterate
1008 * iterate over each item in the queue.
1009 * Generates a 'for' loop, setting elt to
1010 * each item in turn (by reference).
1012 * queue_iterate(q, elt, type, field)
1015 * <type> is what's in our queue
1016 * <field> is the chain field in (*<type>)
1018 * This should only be used with Method 2 queue iteration (element chains)
1020 #define queue_iterate(head, elt, type, field) \
1021 for ((elt) = (type)(void *) queue_first(head); \
1022 !queue_end((head), (queue_entry_t)(elt)); \
1023 (elt) = (type)(void *) queue_next(&(elt)->field))
1025 #ifdef MACH_KERNEL_PRIVATE
1027 #include <kern/locks.h>
1029 /*----------------------------------------------------------------*/
1031 * Define macros for queues with locks.
1033 struct mpqueue_head
{
1034 struct queue_entry head
; /* header for queue */
1035 uint64_t earliest_soft_deadline
;
1037 lck_mtx_t lock_data
;
1038 #if defined(__i386__) || defined(__x86_64__)
1039 lck_mtx_ext_t lock_data_ext
;
1043 typedef struct mpqueue_head mpqueue_head_t
;
1045 #define round_mpq(size) (size)
1048 #if defined(__i386__) || defined(__x86_64__)
1050 #define mpqueue_init(q, lck_grp, lck_attr) \
1052 queue_init(&(q)->head); \
1053 lck_mtx_init_ext(&(q)->lock_data, \
1054 &(q)->lock_data_ext, \
1057 (q)->earliest_soft_deadline = UINT64_MAX; \
1063 #define mpqueue_init(q, lck_grp, lck_attr) \
1065 queue_init(&(q)->head); \
1066 lck_mtx_init(&(q)->lock_data, \
1073 #define mpenqueue_tail(q, elt) \
1075 lck_mtx_lock_spin_always(&(q)->lock_data); \
1076 enqueue_tail(&(q)->head, elt); \
1077 lck_mtx_unlock_always(&(q)->lock_data); \
1080 #define mpdequeue_head(q, elt) \
1082 lck_mtx_lock_spin_always(&(q)->lock_data); \
1083 if (queue_empty(&(q)->head)) \
1086 *(elt) = dequeue_head(&(q)->head); \
1087 lck_mtx_unlock_always(&(q)->lock_data); \
1090 #endif /* MACH_KERNEL_PRIVATE */
1094 #endif /* _KERN_QUEUE_H_ */