#include <kern/queue.h>

kern/queue.h Kernel.framework

includes: mach/mach_types.h, kern/macro_help.h, sys/cdefs.h, Kernel/string.h
30 macros · 10 functions · 4 typedefs · 1 struct

structqueue_entry

size 16, align 8
struct queue_entry *nextnext element
struct queue_entry *prevprevious element

typedefqueue_t

typedef struct queue_entry *queue_t

typedefqueue_head_t

typedef struct queue_entry queue_head_t

typedefqueue_chain_t

typedef struct queue_entry queue_chain_t

typedefqueue_entry_t

typedef struct queue_entry *queue_entry_t

macroenqueue

#define enqueue(queue, elt) enqueue_tail(queue, elt)

macrodequeue

#define dequeue(queue) dequeue_head(queue)

functionenqueue_head

static __inline__ void enqueue_head(queue_t que, queue_entry_t elt)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Inserts the element at the head of the queue (linkage-chain method: the queue_entry is embedded at the start of the element or is the element itself). Validates existing linkage when queue validation is configured.

functionenqueue_tail

static __inline__ void enqueue_tail(queue_t que, queue_entry_t elt)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Inserts the element at the tail of the queue. The enqueue() macro is an alias for this. Validates existing linkage when queue validation is configured.

functiondequeue_head

static __inline__ queue_entry_t dequeue_head(queue_t que)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Removes and returns the first element of the queue, or NULL if the queue is empty. The dequeue() macro is an alias for this. The removed element's next/prev pointers are cleared when queue validation is configured.

functiondequeue_tail

static __inline__ queue_entry_t dequeue_tail(queue_t que)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Removes and returns the last element of the queue, or NULL if the queue is empty.

functionremqueue

static __inline__ void remqueue(queue_entry_t elt)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Removes the specified element from the queue it is linked into, relinking its neighbors. The element must be on a queue; linkage is validated when queue validation is configured, and the removed element's next/prev pointers are cleared.

functioninsque

static __inline__ void insque(queue_entry_t entry, queue_entry_t pred)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Inserts entry into a queue immediately after pred, in the manner of the classic BSD insque. Validates pred's linkage when queue validation is configured.

functionremque

static __inline__ void remque(queue_entry_t elt)
claude-fable-5, 2026-08-24 · not from Apple sources · verified against xnu osfmk/kern/queue.h
Removes the element from the queue it is linked into; equivalent to remqueue. Counterpart of insque.

functionre_queue_head

static __inline__ void re_queue_head(queue_t que, queue_entry_t elt)
Function: re_queue_head Parameters: queue_t que : queue onto which elt will be pre-pended queue_entry_t elt : element to re-queue Description: Remove elt from its current queue and put it onto the head of a new queue Note: This should only be used with Method 1 queue iteration (linkage chains)

functionre_queue_tail

static __inline__ void re_queue_tail(queue_t que, queue_entry_t elt)
Function: re_queue_tail Parameters: queue_t que : queue onto which elt will be appended queue_entry_t elt : element to re-queue Description: Remove elt from its current queue and put it onto the end of a new queue Note: This should only be used with Method 1 queue iteration (linkage chains)

macroqe_element

Macro: qe_element Function: Convert a queue_entry_t to a queue element pointer. Get a pointer to the user-defined element containing a given queue_entry_t Header: <type> * qe_element(queue_entry_t qe, <type>, field) qe - queue entry to convert <type> - what's in the queue (e.g., struct some_data) <field> - is the chain field in <type> Note: Do not use pointer types for <type>
#define qe_element(qe, type, field) __container_of(qe, type, field)

macroqe_foreach

Macro: qe_foreach Function: Iterate over each queue_entry_t structure. Generates a 'for' loop, setting 'qe' to each queue_entry_t in the queue. Header: qe_foreach(queue_entry_t qe, queue_t head) qe - iteration variable head - pointer to queue_head_t (head of queue) Note: This should only be used with Method 1 queue iteration (linkage chains)
#define qe_foreach(qe, head) for (qe = (head)->next; qe != (head); qe = (qe)->next)

macroqe_foreach_safe

Macro: qe_foreach_safe Function: Safely iterate over each queue_entry_t structure. Use this iterator macro if you plan to remove the queue_entry_t, qe, from the queue during the iteration. Header: qe_foreach_safe(queue_entry_t qe, queue_t head) qe - iteration variable head - pointer to queue_head_t (head of queue) Note: This should only be used with Method 1 queue iteration (linkage chains)
#define qe_foreach_safe(qe, head) for (queue_entry_t _ne = ((head)->next)->next,
	         __ ## qe ## _unused_shadow __unused = (qe = (head)->next);
	     qe != (head);
	     qe = _ne, _ne = (qe)->next)

macroqe_foreach_element

Macro: qe_foreach_element Function: Iterate over each _element_ in a queue where each queue_entry_t points to another queue_entry_t, i.e., managed by the [de|en]queue_head/ [de|en]queue_tail / remqueue / etc. function. Header: qe_foreach_element(<type> *elt, queue_t head, <field>) elt - iteration variable <type> - what's in the queue (e.g., struct some_data) <field> - is the chain field in <type> Note: This should only be used with Method 1 queue iteration (linkage chains)
#define qe_foreach_element(elt, head, field) for (elt = qe_element((head)->next, typeof(*(elt)), field);
	     &((elt)->field) != (head);
	     elt = qe_element((elt)->field.next, typeof(*(elt)), field))

macroqe_foreach_element_safe

Macro: qe_foreach_element_safe Function: Safely iterate over each _element_ in a queue where each queue_entry_t points to another queue_entry_t, i.e., managed by the [de|en]queue_head/ [de|en]queue_tail / remqueue / etc. function. Use this iterator macro if you plan to remove the element, elt, from the queue during the iteration. Header: qe_foreach_element_safe(<type> *elt, queue_t head, <field>) elt - iteration variable <type> - what's in the queue (e.g., struct some_data) <field> - is the chain field in <type> Note: This should only be used with Method 1 queue iteration (linkage chains)
#define qe_foreach_element_safe(elt, head, field) for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field),
	     *__ ## elt ## _unused_shadow __unused =
	         (elt = qe_element((head)->next, typeof(*(elt)), field));
	     &((elt)->field) != (head);
	     elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field))

macroQUEUE_HEAD_INITIALIZER

Macro: QUEUE_HEAD_INITIALIZER() Function: Static queue head initializer
#define QUEUE_HEAD_INITIALIZER(name) { &name, &name }

macroqueue_init

Macro: queue_init Function: Initialize the given queue. Header: void queue_init(q) queue_t q; \* MODIFIED *\
#define queue_init(q) MACRO_BEGIN
	(q)->next = (q);
	(q)->prev = (q);
MACRO_END

macroqueue_head_init

Macro: queue_head_init Function: Initialize the given queue head Header: void queue_head_init(q) queue_head_t q; \* MODIFIED *\
#define queue_head_init(q) queue_init(&(q))

macroqueue_chain_init

Macro: queue_chain_init Function: Initialize the given queue chain element Header: void queue_chain_init(q) queue_chain_t q; \* MODIFIED *\
#define queue_chain_init(q) queue_init(&(q))

macroqueue_first

Macro: queue_first Function: Returns the first entry in the queue, Header: queue_entry_t queue_first(q) queue_t q; \* IN *\
#define queue_first(q) ((q)->next)

macroqueue_next

Macro: queue_next Function: Returns the entry after an item in the queue. Header: queue_entry_t queue_next(qc) queue_t qc;
#define queue_next(qc) ((qc)->next)

macroqueue_last

Macro: queue_last Function: Returns the last entry in the queue. Header: queue_entry_t queue_last(q) queue_t q; \* IN *\
#define queue_last(q) ((q)->prev)

macroqueue_prev

Macro: queue_prev Function: Returns the entry before an item in the queue. Header: queue_entry_t queue_prev(qc) queue_t qc;
#define queue_prev(qc) ((qc)->prev)

macroqueue_end

Macro: queue_end Function: Tests whether a new entry is really the end of the queue. Header: boolean_t queue_end(q, qe) queue_t q; queue_entry_t qe;
#define queue_end(q, qe) ((q) == (qe))

macroqueue_empty

Macro: queue_empty Function: Tests whether a queue is empty. Header: boolean_t queue_empty(q) queue_t q;
#define queue_empty(q) queue_end((q), queue_first(q))

functionmovqueue

static __inline__ void movqueue(queue_t _old, queue_t _new)
Function: movqueue Parameters: queue_t _old : head of a queue whose items will be moved queue_t _new : new queue head onto which items will be moved Description: Rebase queue items in _old onto _new then re-initialize the _old object to an empty queue. Equivalent to the queue_new_head Method 2 macro Note: Similar to the queue_new_head macro, this macros is intented to function as an initializer method for '_new' and thus may leak any list items that happen to be on the '_new' list. This should only be used with Method 1 queue iteration (linkage chains)

macroqueue_enter

Macro: queue_enter Function: Insert a new element at the tail of the queue. Header: void queue_enter(q, elt, type, field) queue_t q; <type> elt; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains) We insert a compiler barrier after setting the fields in the element to ensure that the element is updated before being added to the queue, which is especially important because stackshot, which operates from debugger context, iterates several queues that use this macro (the tasks lists and threads lists) without locks. Without this barrier, the compiler may re-order the instructions for this macro in a way that could cause stackshot to trip over an inconsistent queue during iteration.
#define queue_enter(head, elt, type, field) MACRO_BEGIN
	queue_entry_t __head, __prev;
	type __elt;
	int __fail = 0;

	__elt  = (elt);
	__head = (head);
	__prev = __head->prev;

	__QUEUE2_CHECK_NEXT(__fail, __head, __prev, __head, type, field);
	__QUEUE2_CHECK_FAIL(__fail, __head);

	__elt->field.prev = __prev;
	__elt->field.next = __head;
	__compiler_barrier();
	__QUEUE2_SET_NEXT(__prev, __elt, __head, type, field);
	__head->prev = (queue_entry_t)__elt;
MACRO_END

macroqueue_enter_first

Macro: queue_enter_first Function: Insert a new element at the head of the queue. Header: void queue_enter_first(q, elt, type, field) queue_t q; <type> elt; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_enter_first(head, elt, type, field) MACRO_BEGIN
	queue_entry_t __head, __next;
	type __elt;
	int __fail = 0;

	__elt  = (elt);
	__head = (head);
	__next = __head->next;

	__QUEUE2_CHECK_PREV(__fail, __head, __next, __head, type, field);
	__QUEUE2_CHECK_FAIL(__fail, __head);

	__elt->field.next = __next;
	__elt->field.prev = __head;
	__compiler_barrier();
	__QUEUE2_SET_PREV(__next, __elt, __head, type, field);
	__head->next = (queue_entry_t)__elt;
MACRO_END

macroqueue_insert_before

Macro: queue_insert_before Function: Insert a new element before a given element. Header: void queue_insert_before(q, elt, cur, type, field) queue_t q; <type> elt; <type> cur; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_insert_before(head, elt, cur, type, field) MACRO_BEGIN
	queue_entry_t __head, __cur, __prev;
	type __elt;
	int __fail = 0;

	__elt  = (elt);
	__cur  = (queue_entry_t)(cur);
	__head = (head);

	if (__head == __cur) {
	        __prev = __head->prev;
	} else {
	        __prev = ((type)(void *)__cur)->field.prev;
	}

	__QUEUE2_CHECK_NEXT(__fail, __cur, __prev, __head, type, field);
	__QUEUE2_CHECK_FAIL(__fail, __head);

	__elt->field.prev = __prev;
	__elt->field.next = __cur;
	__compiler_barrier();
	__QUEUE2_SET_NEXT(__prev, __elt, __head, type, field);
	__QUEUE2_SET_PREV(__cur, __elt, __head, type, field);
MACRO_END

macroqueue_insert_after

Macro: queue_insert_after Function: Insert a new element after a given element. Header: void queue_insert_after(q, elt, cur, type, field) queue_t q; <type> elt; <type> cur; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_insert_after(head, elt, cur, type, field) MACRO_BEGIN
	queue_entry_t __head, __cur, __next;
	type __elt;
	int __fail = 0;

	__elt  = (elt);
	__cur  = (queue_entry_t)(cur);
	__head = (head);

	if (__head == __cur) {
	        __next = __head->next;
	} else {
	        __next = ((type)(void *)__cur)->field.next;
	}

	__QUEUE2_CHECK_PREV(__fail, __cur, __next, __head, type, field);
	__QUEUE2_CHECK_FAIL(__fail, __head);

	__elt->field.prev = __cur;
	__elt->field.next = __next;
	__compiler_barrier();
	__QUEUE2_SET_NEXT(__cur, __elt, __head, type, field);
	__QUEUE2_SET_PREV(__next, __elt, __head, type, field);
MACRO_END

macroqueue_field

Macro: queue_field [internal use only] Function: Find the queue_chain_t (or queue_t) for the given element (thing) in the given queue (head) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_field(head, thing, type, field) (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)

macroqueue_remove

Macro: queue_remove Function: Remove an arbitrary item from the queue. Header: void queue_remove(q, qe, type, field) arguments as in queue_enter Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_remove(head, elt, type, field) MACRO_BEGIN
	queue_entry_t __head, __next, __prev;
	type __elt;
	int __fail = 0;

	__elt  = (elt);
	__head = (head);
	__next = __elt->field.next;
	__prev = __elt->field.prev;

	__QUEUE2_CHECK_PREV(__fail, __elt, __next, __head, type, field);
	__QUEUE2_CHECK_NEXT(__fail, __elt, __prev, __head, type, field);
	__QUEUE2_CHECK_FAIL(__fail, __head);

	__QUEUE2_SET_PREV(__next, __prev, __head, type, field);
	__QUEUE2_SET_NEXT(__prev, __next, __head, type, field);
	__compiler_barrier();
	__elt->field.next = NULL;
	__elt->field.prev = NULL;
MACRO_END

macroqueue_remove_first

Macro: queue_remove_first Function: Remove and return the entry at the head of the queue. Header: queue_remove_first(head, entry, type, field) entry is returned by reference Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_remove_first(head, entry, type, field) MACRO_BEGIN
	queue_entry_t __hd;
	type __entry;

	__hd    = (head);
	__entry = (type)(void *)__hd->next;

	if ((queue_entry_t)__entry != __hd) {
	        queue_remove(__hd, __entry, type, field);
	}
	(entry) = __entry;
MACRO_END

macroqueue_remove_last

Macro: queue_remove_last Function: Remove and return the entry at the tail of the queue. Header: queue_remove_last(head, entry, type, field) entry is returned by reference Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_remove_last(head, entry, type, field) MACRO_BEGIN
	queue_entry_t __hd;
	type __entry;

	__hd    = (head);
	__entry = (type)(void *)__hd->prev;

	if ((queue_entry_t)__entry != __hd) {
	        queue_remove(__hd, __entry, type, field);
	}
	(entry) = __entry;
MACRO_END

macroqueue_assign

Macro: queue_assign Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_assign(to, from, type, field) MACRO_BEGIN
	((type)(void *)((from)->prev))->field.next = (to);
	((type)(void *)((from)->next))->field.prev = (to);
	*to = *from;
MACRO_END

macroqueue_new_head

Macro: queue_new_head Function: rebase old queue to new queue head Header: queue_new_head(old, new, type, field) queue_t old; queue_t new; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_new_head(old, new, type, field) MACRO_BEGIN
	if (!queue_empty(old)) {
	        *(new) = *(old);
	        ((type)(void *)((new)->next))->field.prev =
	                (new);
	        ((type)(void *)((new)->prev))->field.next =
	                (new);
	} else {
	        queue_init(new);
	}
MACRO_END

macroqueue_extend_last

Macro: queue_extend_last Function: Move the elements of a source queue to the end of a destination queue. Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_extend_last(dst, src, type, field) MACRO_BEGIN
	queue_entry_t __src = (src);
	queue_entry_t __dst = (dst);
	if (queue_empty(__dst)) {
	        queue_new_head(__src, __dst, type, field);
	        queue_init(__src);
	} else if (!queue_empty(__src)) {
	        ((type)(void *)queue_first(__src))->field.prev =
	                queue_last(__dst);
	        ((type)(void *)queue_last(__dst))->field.next =
	                queue_first(__src);
	        queue_last(__dst) = queue_last(__src);
	        ((type)(void *)queue_last(__dst))->field.next = __dst;
	        queue_init(__src);
	}
MACRO_END

macroqueue_extend_first

Macro: queue_extend_first Function: Move the elements of a source queue to the beginning of a destination queue. Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_extend_first(dst, src, type, field) MACRO_BEGIN
	queue_entry_t __src = (src);
	queue_entry_t __dst = (dst);
	if (queue_empty(__dst)) {
	        queue_new_head(__src, __dst, type, field);
	        queue_init(__src);
	} else if (!queue_empty(__src)) {
	        ((type)(void *)queue_first(__dst))->field.prev =
	                queue_last(__src);
	        ((type)(void *)queue_last(__src))->field.next =
	                queue_first(__dst);
	        queue_first(__dst) = queue_first(__src);
	        ((type)(void *)queue_first(__dst))->field.prev = __dst;
	        queue_init(__src);
	}
MACRO_END

macroqueue_iterate

Macro: queue_iterate Function: iterate over each item in the queue. Generates a 'for' loop, setting elt to each item in turn (by reference). Header: queue_iterate(q, elt, type, field) queue_t q; <type> elt; <type> is what's in our queue <field> is the chain field in (*<type>) Note: This should only be used with Method 2 queue iteration (element chains)
#define queue_iterate(head, elt, type, field) for ((elt) = (type)(void *) queue_first(head);
	     !queue_end((head), (queue_entry_t)(elt));
	     (elt) = (type)(void *) queue_next(&(elt)->field))