2 * Copyright (c) 2018 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@
29 #ifndef _KERN_PRIORITY_QUEUE_H_
30 #define _KERN_PRIORITY_QUEUE_H_
33 #include <kern/kern_types.h>
34 #include <kern/macro_help.h>
35 #include <kern/assert.h>
39 #include <sys/cdefs.h>
41 #pragma GCC visibility push(hidden)
46 * A generic priorty ordered queue implementation based on pairing heaps.
49 * - A Back-to-Basics Empirical Study of Priority Queues (https://arxiv.org/abs/1403.0252)
50 * - The Pairing Heap: A New Form of Self-Adjusting Heap
51 * (https://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf)
53 * The XNU implementation is a basic version of the pairing heap.
54 * It allows for O(1) insertion and amortized O(log n) deletion.
56 * It is not a stable data structure by default since adding stability would
57 * need more pointers and hence more memory.
61 * There are several types of priority queues, with types named:
63 * struct priority_queue_<subtype>_<min|max>
65 * In the rest of this header, `struct priority_queue` is used as
66 * a generic type to mean any priority_queue type.
68 * min/max refers to whether the priority queue is a min or a max heap.
72 * - sched, in which case the key is built in the linkage and assumed to
73 * be a scheduler priority.
75 * - sched_stable, in which case the key is a combination of:
76 * * a scheduler priority
77 * * whether the entry was preempted or not
80 * - generic, in which case a comparison function must be passed to
81 * the priority_queue_init.
85 * Both types use a common queue head and linkage pattern.
86 * The head of a priority queue is declared as:
88 * struct priority_queue_<subtype>_<min|max> pq_head;
90 * Elements in this queue are linked together using one of the struct
91 * priority_queue_entry_<subtype> objects embedded within a structure:
97 * struct priority_queue_entry link;
101 * struct some_data is referred to as the queue "element"
103 * This method uses the next, prev and child pointers of the struct
104 * priority_queue_entry linkage object embedded in a queue element to
105 * point to other elements in the queue. The head of the priority queue
106 * (the priority_queue object) will point to the root of the pairing
107 * heap (NULL if heap is empty). This method allows multiple chains
108 * through a given object, by embedding multiple priority_queue_entry
109 * objects in the structure, while simultaneously providing fast removal
110 * and insertion into the heap using only priority_queue_entry object
116 * Priority keys maintained by the data structure.
117 * Since the priority is packed in the node itself, it restricts keys to be 16-bits only.
119 #define PRIORITY_QUEUE_KEY_NONE 0
120 typedef uint16_t priority_queue_key_t
;
125 * For 64-bit platforms, pack the priority key into the child pointer
126 * The packing/unpacking is done using a compiler trick to sign extend long.
127 * This avoids additional NULL checks which are needed in typical packing
128 * implementation. The idea is to define the packed location as a long and
129 * for unpacking simply cast it to a full pointer which sign extends it.
131 #define PRIORITY_QUEUE_ENTRY_CHILD_BITS 48
132 #define PRIORITY_QUEUE_ENTRY_KEY_BITS 16
134 typedef struct priority_queue_entry
{
135 struct priority_queue_entry
*next
;
136 struct priority_queue_entry
*prev
;
137 long __key
: PRIORITY_QUEUE_ENTRY_KEY_BITS
;
138 long child
: PRIORITY_QUEUE_ENTRY_CHILD_BITS
;
139 } *priority_queue_entry_t
;
141 typedef struct priority_queue_entry_deadline
{
142 struct priority_queue_entry_deadline
*next
;
143 struct priority_queue_entry_deadline
*prev
;
144 long __key
: PRIORITY_QUEUE_ENTRY_KEY_BITS
;
145 long child
: PRIORITY_QUEUE_ENTRY_CHILD_BITS
;
147 } *priority_queue_entry_deadline_t
;
149 typedef struct priority_queue_entry_sched
{
150 struct priority_queue_entry_sched
*next
;
151 struct priority_queue_entry_sched
*prev
;
152 long key
: PRIORITY_QUEUE_ENTRY_KEY_BITS
;
153 long child
: PRIORITY_QUEUE_ENTRY_CHILD_BITS
;
154 } *priority_queue_entry_sched_t
;
156 typedef struct priority_queue_entry_stable
{
157 struct priority_queue_entry_stable
*next
;
158 struct priority_queue_entry_stable
*prev
;
159 long key
: PRIORITY_QUEUE_ENTRY_KEY_BITS
;
160 long child
: PRIORITY_QUEUE_ENTRY_CHILD_BITS
;
162 } *priority_queue_entry_stable_t
;
166 typedef struct priority_queue_entry
{
167 struct priority_queue_entry
*next
;
168 struct priority_queue_entry
*prev
;
170 } *priority_queue_entry_t
;
172 typedef struct priority_queue_entry_deadline
{
173 struct priority_queue_entry_deadline
*next
;
174 struct priority_queue_entry_deadline
*prev
;
177 } *priority_queue_entry_deadline_t
;
180 * For 32-bit platforms, use an extra field to store the key since child pointer packing
181 * is not an option. The child is maintained as a long to use the same packing/unpacking
182 * routines that work for 64-bit platforms.
184 typedef struct priority_queue_entry_sched
{
185 struct priority_queue_entry_sched
*next
;
186 struct priority_queue_entry_sched
*prev
;
188 priority_queue_key_t key
;
189 } *priority_queue_entry_sched_t
;
191 typedef struct priority_queue_entry_stable
{
192 struct priority_queue_entry_stable
*next
;
193 struct priority_queue_entry_stable
*prev
;
195 priority_queue_key_t key
;
197 } *priority_queue_entry_stable_t
;
199 #endif /* __LP64__ */
202 * Comparator block prototype
204 * - elements to compare
206 * comparision result to indicate relative ordering of elements according to the heap type
208 typedef int (^priority_queue_compare_fn_t
)(struct priority_queue_entry
*e1
,
209 struct priority_queue_entry
*e2
);
211 #define priority_heap_compare_ints(a, b) ((a) < (b) ? 1 : -1)
213 #define priority_heap_make_comparator(name1, name2, type, field, ...) \
214 (^int(priority_queue_entry_t __e1, priority_queue_entry_t __e2){ \
215 type *name1 = pqe_element_fast(__e1, type, field); \
216 type *name2 = pqe_element_fast(__e2, type, field); \
221 * Type for any priority queue, only used for documentation purposes.
223 struct priority_queue
;
226 * Type of generic heaps
228 struct priority_queue_min
{
229 struct priority_queue_entry
*pq_root
;
230 priority_queue_compare_fn_t pq_cmp_fn
;
232 struct priority_queue_max
{
233 struct priority_queue_entry
*pq_root
;
234 priority_queue_compare_fn_t pq_cmp_fn
;
238 * Type of deadline heaps
240 struct priority_queue_deadline_min
{
241 struct priority_queue_entry_deadline
*pq_root
;
243 struct priority_queue_deadline_max
{
244 struct priority_queue_entry_deadline
*pq_root
;
248 * Type of scheduler priority based heaps
250 struct priority_queue_sched_min
{
251 struct priority_queue_entry_sched
*pq_root
;
253 struct priority_queue_sched_max
{
254 struct priority_queue_entry_sched
*pq_root
;
258 * Type of scheduler priority based stable heaps
260 struct priority_queue_sched_stable_min
{
261 struct priority_queue_entry_stable
*pq_root
;
263 struct priority_queue_sched_stable_max
{
264 struct priority_queue_entry_stable
*pq_root
;
267 #pragma mark generic interface
269 #define PRIORITY_QUEUE_INITIALIZER { .pq_root = NULL }
271 #define __pqueue_overloadable __attribute__((overloadable))
273 #define priority_queue_is_min_heap(pq) _Generic(pq, \
274 struct priority_queue_min *: true, \
275 struct priority_queue_max *: false, \
276 struct priority_queue_deadline_min *: true, \
277 struct priority_queue_deadline_max *: false, \
278 struct priority_queue_sched_min *: true, \
279 struct priority_queue_sched_max *: false, \
280 struct priority_queue_sched_stable_min *: true, \
281 struct priority_queue_sched_stable_max *: false)
283 #define priority_queue_is_max_heap(pq) \
284 (!priority_queue_is_min_heap(pq))
287 * Macro: pqe_element_fast
289 * Convert a priority_queue_entry_t to a queue element pointer.
290 * Get a pointer to the user-defined element containing
291 * a given priority_queue_entry_t
293 * The fast variant assumes that `qe` is not NULL
295 * pqe_element_fast(qe, type, field)
296 * <priority_queue_entry_t> qe
297 * <type> type of element in priority queue
298 * <field> chain field in (*<type>)
300 * <type *> containing qe
302 #define pqe_element_fast(qe, type, field) __container_of(qe, type, field)
307 * Convert a priority_queue_entry_t to a queue element pointer.
308 * Get a pointer to the user-defined element containing
309 * a given priority_queue_entry_t
311 * The non fast variant handles NULL `qe`
313 * pqe_element(qe, type, field)
314 * <priority_queue_entry_t> qe
315 * <type> type of element in priority queue
316 * <field> chain field in (*<type>)
318 * <type *> containing qe
320 #define pqe_element(qe, type, field) ({ \
321 __auto_type _tmp_entry = (qe); \
322 _tmp_entry ? pqe_element_fast(_tmp_entry, type, field) : ((type *)NULL);\
326 * Priority Queue functionality routines
330 * Macro: priority_queue_empty
332 * Tests whether a priority queue is empty.
334 * boolean_t priority_queue_empty(pq)
335 * <struct priority_queue *> pq
337 #define priority_queue_empty(pq) ((pq)->pq_root == NULL)
340 * Macro: priority_queue_init
342 * Initialize a <struct priority_queue *>.
344 * priority_queue_init(pq)
345 * <struct priority_queue *> pq
346 * (optional) <cmp_fn> comparator function
350 __pqueue_overloadable
352 priority_queue_init(struct priority_queue
*pq
, ...);
355 * Macro: priority_queue_entry_init
357 * Initialize a priority_queue_entry_t
359 * priority_queue_entry_init(qe)
360 * <priority_queue_entry_t> qe
364 #define priority_queue_entry_init(qe) \
365 __builtin_bzero(qe, sizeof(*(qe)))
368 * Macro: priority_queue_destroy
370 * Destroy a priority queue safely. This routine accepts a callback
371 * to handle any cleanup for elements in the priority queue. The queue does
372 * not maintain its invariants while getting destroyed. The priority queue and
373 * the linkage nodes need to be re-initialized before re-using them.
375 * priority_queue_destroy(pq, type, field, callback)
376 * <struct priority_queue *> pq
377 * <callback> callback for each element
382 #define priority_queue_destroy(pq, type, field, callback) \
384 void (^__callback)(type *) = (callback); /* type check */ \
385 _priority_queue_destroy(pq, offsetof(type, field), \
386 (void (^)(void *))(__callback)); \
390 * Macro: priority_queue_min
392 * Lookup the minimum in a min-priority queue.
395 * priority_queue_min(pq, type, field)
396 * <struct priority_queue *> pq
397 * <type> type of element in priority queue
398 * <field> chain field in (*<type>)
400 * <type *> root element
402 #define priority_queue_min(pq, type, field) ({ \
403 static_assert(priority_queue_is_min_heap(pq), "queue is min heap"); \
404 pqe_element((pq)->pq_root, type, field); \
408 * Macro: priority_queue_max
410 * Lookup the maximum element in a max-priority queue.
413 * priority_queue_max(pq, type, field)
414 * <struct priority_queue *> pq
415 * <type> type of element in priority queue
416 * <field> chain field in (*<type>)
418 * <type *> root element
420 #define priority_queue_max(pq, type, field) ({ \
421 static_assert(priority_queue_is_max_heap(pq), "queue is max heap"); \
422 pqe_element((pq)->pq_root, type, field); \
426 * Macro: priority_queue_insert
428 * Insert an element into the priority queue
430 * The caller must have set the key prio to insertion
433 * priority_queue_insert(pq, elt, new_key)
434 * <struct priority_queue *> pq
435 * <priority_queue_entry_t> elt
437 * Whether the inserted element became the new root
440 priority_queue_insert(struct priority_queue
*pq
,
441 struct priority_queue_entry
*elt
) __pqueue_overloadable
;
444 * Macro: priority_queue_remove_min
446 * Remove the minimum element in a min-heap priority queue.
448 * priority_queue_remove_min(pq, type, field)
449 * <struct priority_queue *> pq
450 * <type> type of element in priority queue
451 * <field> chain field in (*<type>)
453 * <type *> max element
455 #define priority_queue_remove_min(pq, type, field) ({ \
456 static_assert(priority_queue_is_min_heap(pq), "queue is min heap"); \
457 pqe_element(_priority_queue_remove_root(pq), type, field); \
461 * Macro: priority_queue_remove_max
463 * Remove the maximum element in a max-heap priority queue.
465 * priority_queue_remove_max(pq, type, field)
466 * <struct priority_queue *> pq
467 * <type> type of element in priority queue
468 * <field> chain field in (*<type>)
470 * <type *> max element
472 #define priority_queue_remove_max(pq, type, field) ({ \
473 static_assert(priority_queue_is_max_heap(pq), "queue is max heap"); \
474 pqe_element(_priority_queue_remove_root(pq), type, field); \
478 * Macro: priority_queue_remove
480 * Removes an element from the priority queue
482 * priority_queue_remove(pq, elt)
483 * <struct priority_queue *> pq
484 * <priority_queue_entry_t> elt
486 * Whether the removed element was the root
489 priority_queue_remove(struct priority_queue
*pq
,
490 struct priority_queue_entry
*elt
) __pqueue_overloadable
;
494 * Macro: priority_queue_entry_decreased
497 * Signal the priority queue that the entry priority has decreased.
499 * The new value for the element priority must have been set
500 * prior to calling this function.
503 * priority_queue_entry_decreased(pq, elt)
504 * <struct priority_queue *> pq
505 * <priority_queue_entry_t> elt
507 * Whether the update caused the root or its key to change.
510 priority_queue_entry_decreased(struct priority_queue
*pq
,
511 struct priority_queue_entry
*elt
) __pqueue_overloadable
;
514 * Macro: priority_queue_entry_increased
517 * Signal the priority queue that the entry priority has increased.
519 * The new value for the element priority must have been set
520 * prior to calling this function.
523 * priority_queue_entry_increased(pq, elt, new_key)
524 * <struct priority_queue *> pq
525 * <priority_queue_entry_t> elt
527 * Whether the update caused the root or its key to change.
530 priority_queue_entry_increased(struct priority_queue
*pq
,
531 struct priority_queue_entry
*elt
) __pqueue_overloadable
;
534 #pragma mark priority_queue_sched_*
536 __enum_decl(priority_queue_entry_sched_modifier_t
, uint8_t, {
537 PRIORITY_QUEUE_ENTRY_NONE
= 0,
538 PRIORITY_QUEUE_ENTRY_PREEMPTED
= 1,
541 #define priority_queue_is_sched_heap(pq) _Generic(pq, \
542 struct priority_queue_sched_min *: true, \
543 struct priority_queue_sched_max *: true, \
544 struct priority_queue_sched_stable_min *: true, \
545 struct priority_queue_sched_stable_max *: true, \
549 * Macro: priority_queue_entry_set_sched_pri
552 * Sets the scheduler priority on an entry supporting this concept.
554 * The priority is expected to fit on 8 bits.
555 * An optional sorting modifier.
558 * priority_queue_entry_set_sched_pri(pq, elt, pri, modifier)
559 * <struct priority_queue *> pq
560 * <priority_queue_entry_t> elt
562 * <priority_queue_entry_sched_modifier_t> modifier
564 #define priority_queue_entry_set_sched_pri(pq, elt, pri, modifier) \
566 static_assert(priority_queue_is_sched_heap(pq), "is a sched heap"); \
567 (elt)->key = (priority_queue_key_t)(((pri) << 8) + (modifier)); \
571 * Macro: priority_queue_entry_sched_pri
574 * Return the scheduler priority on an entry supporting this
578 * priority_queue_entry_sched_pri(pq, elt)
579 * <struct priority_queue *> pq
580 * <priority_queue_entry_t> elt
583 * The scheduler priority of this entry
585 #define priority_queue_entry_sched_pri(pq, elt) ({ \
586 static_assert(priority_queue_is_sched_heap(pq), "is a sched heap"); \
587 (priority_queue_key_t)((elt)->key >> 8); \
591 * Macro: priority_queue_entry_sched_modifier
594 * Return the scheduler modifier on an entry supporting this
598 * priority_queue_entry_sched_modifier(pq, elt)
599 * <struct priority_queue *> pq
600 * <priority_queue_entry_t> elt
603 * The scheduler priority of this entry
605 #define priority_queue_entry_sched_modifier(pq, elt) ({ \
606 static_assert(priority_queue_is_sched_heap(pq), "is a sched heap"); \
607 (priority_queue_entry_sched_modifier_t)(elt)->key; \
611 * Macro: priority_queue_min_sched_pri
614 * Return the scheduler priority of the minimum element
615 * of a scheduler priority queue.
618 * priority_queue_min_sched_pri(pq)
619 * <struct priority_queue *> pq
622 * The scheduler priority of this entry
624 #define priority_queue_min_sched_pri(pq) ({ \
625 static_assert(priority_queue_is_min_heap(pq), "queue is min heap"); \
626 priority_queue_entry_sched_pri(pq, (pq)->pq_root); \
630 * Macro: priority_queue_max_sched_pri
633 * Return the scheduler priority of the maximum element
634 * of a scheduler priority queue.
637 * priority_queue_max_sched_pri(pq)
638 * <struct priority_queue *> pq
641 * The scheduler priority of this entry
643 #define priority_queue_max_sched_pri(pq) ({ \
644 static_assert(priority_queue_is_max_heap(pq), "queue is max heap"); \
645 priority_queue_entry_sched_pri(pq, (pq)->pq_root); \
649 #pragma mark implementation details
651 #define PRIORITY_QUEUE_MAKE_BASE(pqueue_t, pqelem_t) \
653 __pqueue_overloadable extern void \
654 _priority_queue_destroy(pqueue_t pq, uintptr_t offset, void (^cb)(void *)); \
656 __pqueue_overloadable extern bool \
657 priority_queue_insert(pqueue_t que, pqelem_t elt); \
659 __pqueue_overloadable extern pqelem_t \
660 _priority_queue_remove_root(pqueue_t que); \
662 __pqueue_overloadable extern bool \
663 priority_queue_remove(pqueue_t que, pqelem_t elt); \
665 __pqueue_overloadable extern bool \
666 priority_queue_entry_decreased(pqueue_t que, pqelem_t elt); \
668 __pqueue_overloadable extern bool \
669 priority_queue_entry_increased(pqueue_t que, pqelem_t elt)
671 #define PRIORITY_QUEUE_MAKE(pqueue_t, pqelem_t) \
672 __pqueue_overloadable \
674 priority_queue_init(pqueue_t que) \
676 __builtin_bzero(que, sizeof(*que)); \
679 PRIORITY_QUEUE_MAKE_BASE(pqueue_t, pqelem_t)
681 #define PRIORITY_QUEUE_MAKE_CB(pqueue_t, pqelem_t) \
682 __pqueue_overloadable \
684 priority_queue_init(pqueue_t pq, priority_queue_compare_fn_t cmp_fn) \
686 pq->pq_root = NULL; \
687 pq->pq_cmp_fn = cmp_fn; \
690 PRIORITY_QUEUE_MAKE_BASE(pqueue_t, pqelem_t)
692 PRIORITY_QUEUE_MAKE_CB(struct priority_queue_min
*, priority_queue_entry_t
);
693 PRIORITY_QUEUE_MAKE_CB(struct priority_queue_max
*, priority_queue_entry_t
);
695 PRIORITY_QUEUE_MAKE(struct priority_queue_deadline_min
*, priority_queue_entry_deadline_t
);
696 PRIORITY_QUEUE_MAKE(struct priority_queue_deadline_max
*, priority_queue_entry_deadline_t
);
698 PRIORITY_QUEUE_MAKE(struct priority_queue_sched_min
*, priority_queue_entry_sched_t
);
699 PRIORITY_QUEUE_MAKE(struct priority_queue_sched_max
*, priority_queue_entry_sched_t
);
701 PRIORITY_QUEUE_MAKE(struct priority_queue_sched_stable_min
*, priority_queue_entry_stable_t
);
702 PRIORITY_QUEUE_MAKE(struct priority_queue_sched_stable_max
*, priority_queue_entry_stable_t
);
706 #pragma GCC visibility pop
708 #endif /* _KERN_PRIORITY_QUEUE_H_ */