]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/queue.h
f45899ce220e4fbeeb2b40c719f49c0c8150373e
[apple/xnu.git] / osfmk / kern / queue.h
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon rights
54 * to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: queue.h
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1985
62 *
63 * Type definitions for generic queues.
64 *
65 */
66
67 #ifndef _KERN_QUEUE_H_
68 #define _KERN_QUEUE_H_
69
70 #include <mach/mach_types.h>
71 #include <kern/macro_help.h>
72
73 #include <sys/cdefs.h>
74
75 __BEGIN_DECLS
76
77 /*
78 * Queue Management APIs
79 *
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)
84 *
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;
88 *
89 * Elements in this queue are chained together using
90 * struct queue_entry objects embedded within a structure:
91 * struct some_data {
92 * int field1;
93 * int field2;
94 * ...
95 * queue_chain_t link;
96 * ...
97 * int last_field;
98 * };
99 * struct some_data is referred to as the queue "element."
100 * (note that queue_chain_t is typedef'd to struct queue_entry)
101 *
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.
105 *
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.
113 *
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.
119 *
120 * ++ Valid APIs for this style queue ++
121 * -------------------------------------
122 * [C] queue_init
123 * [C] queue_first
124 * [C] queue_next
125 * [C] queue_last
126 * [C] queue_prev
127 * [C] queue_end
128 * [C] queue_empty
129 *
130 * [1] enqueue
131 * [1] dequeue
132 * [1] enqueue_head
133 * [1] enqueue_tail
134 * [1] dequeue_head
135 * [1] dequeue_tail
136 * [1] remqueue
137 * [1] insque
138 * [1] remque
139 * [1] re_queue
140 * [1] re_queue_tail
141 * [1] movqueue
142 * [1] qe_element
143 * [1] qe_foreach
144 * [1] qe_foreach_safe
145 * [1] qe_foreach_element
146 * [1] qe_foreach_element_safe
147 *
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.
157 *
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.
163 *
164 * ++ Valid APIs for this style queue ++
165 * -------------------------------------
166 * [C] queue_init
167 * [C] queue_first
168 * [C] queue_next
169 * [C] queue_last
170 * [C] queue_prev
171 * [C] queue_end
172 * [C] queue_empty
173 *
174 * [2] queue_enter
175 * [2] queue_enter_first
176 * [2] queue_insert_before
177 * [2] queue_insert_after
178 * [2] queue_field
179 * [2] queue_remove
180 * [2] queue_remove_first
181 * [2] queue_remove_last
182 * [2] queue_assign
183 * [2] queue_new_head
184 * [2] queue_iterate
185 *
186 * Legend:
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)
190 */
191
192 /*
193 * A generic doubly-linked list (queue).
194 */
195
196 struct queue_entry {
197 struct queue_entry *next; /* next element */
198 struct queue_entry *prev; /* previous element */
199
200 };
201
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;
206
207 /*
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.
211 */
212
213 #define enqueue(queue,elt) enqueue_tail(queue, elt)
214 #define dequeue(queue) dequeue_head(queue)
215
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;
221
222 if (__improbable(elt == (queue_entry_t)0)) {
223 panic("Invalid queue element %p", elt);
224 }
225
226 elt_next = elt->next;
227 elt_prev = elt->prev;
228
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);
231 }
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);
235 }
236 }
237
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;
241 }
242 #else
243 #define __QUEUE_ELT_VALIDATE(elt) do { } while (0)
244 #define __DEQUEUE_ELT_CLEANUP(elt) do { } while(0)
245 #endif /* !XNU_KERNEL_PRIVATE */
246
247 static __inline__ void
248 enqueue_head(
249 queue_t que,
250 queue_entry_t elt)
251 {
252 queue_entry_t old_head;
253
254 __QUEUE_ELT_VALIDATE((queue_entry_t)que);
255 old_head = que->next;
256 elt->next = old_head;
257 elt->prev = que;
258 old_head->prev = elt;
259 que->next = elt;
260 }
261
262 static __inline__ void
263 enqueue_tail(
264 queue_t que,
265 queue_entry_t elt)
266 {
267 queue_entry_t old_tail;
268
269 __QUEUE_ELT_VALIDATE((queue_entry_t)que);
270 old_tail = que->prev;
271 elt->next = que;
272 elt->prev = old_tail;
273 old_tail->next = elt;
274 que->prev = elt;
275 }
276
277 static __inline__ queue_entry_t
278 dequeue_head(
279 queue_t que)
280 {
281 queue_entry_t elt = (queue_entry_t) 0;
282 queue_entry_t new_head;
283
284 if (que->next != que) {
285 elt = que->next;
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);
291 }
292
293 return (elt);
294 }
295
296 static __inline__ queue_entry_t
297 dequeue_tail(
298 queue_t que)
299 {
300 queue_entry_t elt = (queue_entry_t) 0;
301 queue_entry_t new_tail;
302
303 if (que->prev != que) {
304 elt = que->prev;
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);
310 }
311
312 return (elt);
313 }
314
315 static __inline__ void
316 remqueue(
317 queue_entry_t elt)
318 {
319 queue_entry_t next_elt, prev_elt;
320
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);
327 }
328
329 static __inline__ void
330 insque(
331 queue_entry_t entry,
332 queue_entry_t pred)
333 {
334 queue_entry_t successor;
335
336 __QUEUE_ELT_VALIDATE(pred);
337 successor = pred->next;
338 entry->next = successor;
339 entry->prev = pred;
340 successor->prev = entry;
341 pred->next = entry;
342 }
343
344 static __inline__ void
345 remque(
346 queue_entry_t elt)
347 {
348 queue_entry_t next_elt, prev_elt;
349
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);
356 }
357
358 /*
359 * Function: re_queue_head
360 * Parameters:
361 * queue_t que : queue onto which elt will be pre-pended
362 * queue_entry_t elt : element to re-queue
363 * Description:
364 * Remove elt from its current queue and put it onto the
365 * head of a new queue
366 * Note:
367 * This should only be used with Method 1 queue iteration (linkage chains)
368 */
369 static __inline__ void
370 re_queue_head(queue_t que, queue_entry_t elt)
371 {
372 queue_entry_t n_elt, p_elt;
373
374 __QUEUE_ELT_VALIDATE(elt);
375 __QUEUE_ELT_VALIDATE((queue_entry_t)que);
376
377 /* remqueue */
378 n_elt = elt->next;
379 p_elt = elt->prev; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
380 n_elt->prev = p_elt;
381 p_elt->next = n_elt;
382
383 /* enqueue_head */
384 n_elt = que->next;
385 elt->next = n_elt;
386 elt->prev = que;
387 n_elt->prev = elt;
388 que->next = elt;
389 }
390
391 /*
392 * Function: re_queue_tail
393 * Parameters:
394 * queue_t que : queue onto which elt will be appended
395 * queue_entry_t elt : element to re-queue
396 * Description:
397 * Remove elt from its current queue and put it onto the
398 * end of a new queue
399 * Note:
400 * This should only be used with Method 1 queue iteration (linkage chains)
401 */
402 static __inline__ void
403 re_queue_tail(queue_t que, queue_entry_t elt)
404 {
405 queue_entry_t n_elt, p_elt;
406
407 __QUEUE_ELT_VALIDATE(elt);
408 __QUEUE_ELT_VALIDATE((queue_entry_t)que);
409
410 /* remqueue */
411 n_elt = elt->next;
412 p_elt = elt->prev; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
413 n_elt->prev = p_elt;
414 p_elt->next = n_elt;
415
416 /* enqueue_tail */
417 p_elt = que->prev;
418 elt->next = que;
419 elt->prev = p_elt;
420 p_elt->next = elt;
421 que->prev = elt;
422 }
423
424 /*
425 * Macro: qe_element
426 * Function:
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
430 * Header:
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>
435 * Note:
436 * Do not use pointer types for <type>
437 */
438 #define qe_element(qe, type, field) \
439 ((type *)((void *)((char *)(qe) - __offsetof(type, field))))
440
441 /*
442 * Macro: qe_foreach
443 * Function:
444 * Iterate over each queue_entry_t structure.
445 * Generates a 'for' loop, setting 'qe' to
446 * each queue_entry_t in the queue.
447 * Header:
448 * qe_foreach(queue_entry_t qe, queue_t head)
449 * qe - iteration variable
450 * head - pointer to queue_head_t (head of queue)
451 * Note:
452 * This should only be used with Method 1 queue iteration (linkage chains)
453 */
454 #define qe_foreach(qe, head) \
455 for (qe = (head)->next; qe != (head); qe = (qe)->next)
456
457 /*
458 * Macro: qe_foreach_safe
459 * Function:
460 * Safely iterate over each queue_entry_t structure.
461 *
462 * Use this iterator macro if you plan to remove the
463 * queue_entry_t, qe, from the queue during the
464 * iteration.
465 * Header:
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)
469 * Note:
470 * This should only be used with Method 1 queue iteration (linkage chains)
471 */
472 #define qe_foreach_safe(qe, head) \
473 for (queue_entry_t _ne = ((head)->next)->next, \
474 __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \
475 qe != (head); \
476 qe = _ne, _ne = (qe)->next)
477
478 /*
479 * Macro: qe_foreach_element
480 * Function:
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.
485 * Header:
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>
490 * Note:
491 * This should only be used with Method 1 queue iteration (linkage chains)
492 */
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))
497
498 /*
499 * Macro: qe_foreach_element_safe
500 * Function:
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.
505 *
506 * Use this iterator macro if you plan to remove the
507 * element, elt, from the queue during the iteration.
508 * Header:
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>
513 * Note:
514 * This should only be used with Method 1 queue iteration (linkage chains)
515 */
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)) \
522
523 /*
524 * Macro: queue_init
525 * Function:
526 * Initialize the given queue.
527 * Header:
528 * void queue_init(q)
529 * queue_t q; \* MODIFIED *\
530 */
531 #define queue_init(q) \
532 MACRO_BEGIN \
533 (q)->next = (q);\
534 (q)->prev = (q);\
535 MACRO_END
536
537 /*
538 * Macro: queue_head_init
539 * Function:
540 * Initialize the given queue head
541 * Header:
542 * void queue_head_init(q)
543 * queue_head_t q; \* MODIFIED *\
544 */
545 #define queue_head_init(q) \
546 queue_init(&(q))
547
548 /*
549 * Macro: queue_chain_init
550 * Function:
551 * Initialize the given queue chain element
552 * Header:
553 * void queue_chain_init(q)
554 * queue_chain_t q; \* MODIFIED *\
555 */
556 #define queue_chain_init(q) \
557 queue_init(&(q))
558
559 /*
560 * Macro: queue_first
561 * Function:
562 * Returns the first entry in the queue,
563 * Header:
564 * queue_entry_t queue_first(q)
565 * queue_t q; \* IN *\
566 */
567 #define queue_first(q) ((q)->next)
568
569 /*
570 * Macro: queue_next
571 * Function:
572 * Returns the entry after an item in the queue.
573 * Header:
574 * queue_entry_t queue_next(qc)
575 * queue_t qc;
576 */
577 #define queue_next(qc) ((qc)->next)
578
579 /*
580 * Macro: queue_last
581 * Function:
582 * Returns the last entry in the queue.
583 * Header:
584 * queue_entry_t queue_last(q)
585 * queue_t q; \* IN *\
586 */
587 #define queue_last(q) ((q)->prev)
588
589 /*
590 * Macro: queue_prev
591 * Function:
592 * Returns the entry before an item in the queue.
593 * Header:
594 * queue_entry_t queue_prev(qc)
595 * queue_t qc;
596 */
597 #define queue_prev(qc) ((qc)->prev)
598
599 /*
600 * Macro: queue_end
601 * Function:
602 * Tests whether a new entry is really the end of
603 * the queue.
604 * Header:
605 * boolean_t queue_end(q, qe)
606 * queue_t q;
607 * queue_entry_t qe;
608 */
609 #define queue_end(q, qe) ((q) == (qe))
610
611 /*
612 * Macro: queue_empty
613 * Function:
614 * Tests whether a queue is empty.
615 * Header:
616 * boolean_t queue_empty(q)
617 * queue_t q;
618 */
619 #define queue_empty(q) queue_end((q), queue_first(q))
620
621 /*
622 * Function: movqueue
623 * Parameters:
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
626 * Description:
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
630 * Note:
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)
635 */
636 static __inline__ void
637 movqueue(queue_t _old, queue_t _new)
638 {
639 queue_entry_t next_elt, prev_elt;
640
641 __QUEUE_ELT_VALIDATE((queue_entry_t)_old);
642
643 if (queue_empty(_old)) {
644 queue_init(_new);
645 return;
646 }
647
648 /*
649 * move the queue at _old to _new
650 * and re-initialize _old
651 */
652 next_elt = _old->next;
653 prev_elt = _old->prev;
654
655 _new->next = next_elt;
656 _new->prev = prev_elt;
657 next_elt->prev = _new;
658 prev_elt->next = _new;
659
660 queue_init(_old);
661 }
662
663 /*----------------------------------------------------------------*/
664 /*
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.
668 */
669
670 /*
671 * Macro: queue_enter
672 * Function:
673 * Insert a new element at the tail of the queue.
674 * Header:
675 * void queue_enter(q, elt, type, field)
676 * queue_t q;
677 * <type> elt;
678 * <type> is what's in our queue
679 * <field> is the chain field in (*<type>)
680 * Note:
681 * This should only be used with Method 2 queue iteration (element chains)
682 */
683 #define queue_enter(head, elt, type, field) \
684 MACRO_BEGIN \
685 queue_entry_t __prev; \
686 \
687 __prev = (head)->prev; \
688 if ((head) == __prev) { \
689 (head)->next = (queue_entry_t) (elt); \
690 } \
691 else { \
692 ((type)(void *)__prev)->field.next = \
693 (queue_entry_t)(elt); \
694 } \
695 (elt)->field.prev = __prev; \
696 (elt)->field.next = head; \
697 (head)->prev = (queue_entry_t) elt; \
698 MACRO_END
699
700 /*
701 * Macro: queue_enter_first
702 * Function:
703 * Insert a new element at the head of the queue.
704 * Header:
705 * void queue_enter_first(q, elt, type, field)
706 * queue_t q;
707 * <type> elt;
708 * <type> is what's in our queue
709 * <field> is the chain field in (*<type>)
710 * Note:
711 * This should only be used with Method 2 queue iteration (element chains)
712 */
713 #define queue_enter_first(head, elt, type, field) \
714 MACRO_BEGIN \
715 queue_entry_t __next; \
716 \
717 __next = (head)->next; \
718 if ((head) == __next) { \
719 (head)->prev = (queue_entry_t) (elt); \
720 } \
721 else { \
722 ((type)(void *)__next)->field.prev = \
723 (queue_entry_t)(elt); \
724 } \
725 (elt)->field.next = __next; \
726 (elt)->field.prev = head; \
727 (head)->next = (queue_entry_t) elt; \
728 MACRO_END
729
730 /*
731 * Macro: queue_insert_before
732 * Function:
733 * Insert a new element before a given element.
734 * Header:
735 * void queue_insert_before(q, elt, cur, type, field)
736 * queue_t q;
737 * <type> elt;
738 * <type> cur;
739 * <type> is what's in our queue
740 * <field> is the chain field in (*<type>)
741 * Note:
742 * This should only be used with Method 2 queue iteration (element chains)
743 */
744 #define queue_insert_before(head, elt, cur, type, field) \
745 MACRO_BEGIN \
746 queue_entry_t __prev; \
747 \
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); \
757 } \
758 (head)->prev = (queue_entry_t)(elt); \
759 } else { \
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); \
769 } \
770 (cur)->field.prev = (queue_entry_t)(elt); \
771 } \
772 MACRO_END
773
774 /*
775 * Macro: queue_insert_after
776 * Function:
777 * Insert a new element after a given element.
778 * Header:
779 * void queue_insert_after(q, elt, cur, type, field)
780 * queue_t q;
781 * <type> elt;
782 * <type> cur;
783 * <type> is what's in our queue
784 * <field> is the chain field in (*<type>)
785 * Note:
786 * This should only be used with Method 2 queue iteration (element chains)
787 */
788 #define queue_insert_after(head, elt, cur, type, field) \
789 MACRO_BEGIN \
790 queue_entry_t __next; \
791 \
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); \
801 } \
802 (head)->next = (queue_entry_t)(elt); \
803 } else { \
804 (elt)->field.prev = (queue_entry_t)(cur); \
805 if ((head)->prev == (queue_entry_t)(cur)) { \
806 /* last element */ \
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); \
813 } \
814 (cur)->field.next = (queue_entry_t)(elt); \
815 } \
816 MACRO_END
817
818 /*
819 * Macro: queue_field [internal use only]
820 * Function:
821 * Find the queue_chain_t (or queue_t) for the
822 * given element (thing) in the given queue (head)
823 * Note:
824 * This should only be used with Method 2 queue iteration (element chains)
825 */
826 #define queue_field(head, thing, type, field) \
827 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
828
829 /*
830 * Macro: queue_remove
831 * Function:
832 * Remove an arbitrary item from the queue.
833 * Header:
834 * void queue_remove(q, qe, type, field)
835 * arguments as in queue_enter
836 * Note:
837 * This should only be used with Method 2 queue iteration (element chains)
838 */
839 #define queue_remove(head, elt, type, field) \
840 MACRO_BEGIN \
841 queue_entry_t __next, __prev; \
842 \
843 __next = (elt)->field.next; \
844 __prev = (elt)->field.prev; \
845 \
846 if ((head) == __next) \
847 (head)->prev = __prev; \
848 else \
849 ((type)(void *)__next)->field.prev = __prev; \
850 \
851 if ((head) == __prev) \
852 (head)->next = __next; \
853 else \
854 ((type)(void *)__prev)->field.next = __next; \
855 \
856 (elt)->field.next = NULL; \
857 (elt)->field.prev = NULL; \
858 MACRO_END
859
860 /*
861 * Macro: queue_remove_first
862 * Function:
863 * Remove and return the entry at the head of
864 * the queue.
865 * Header:
866 * queue_remove_first(head, entry, type, field)
867 * entry is returned by reference
868 * Note:
869 * This should only be used with Method 2 queue iteration (element chains)
870 */
871 #define queue_remove_first(head, entry, type, field) \
872 MACRO_BEGIN \
873 queue_entry_t __next; \
874 \
875 (entry) = (type)(void *) ((head)->next); \
876 __next = (entry)->field.next; \
877 \
878 if ((head) == __next) \
879 (head)->prev = (head); \
880 else \
881 ((type)(void *)(__next))->field.prev = (head); \
882 (head)->next = __next; \
883 \
884 (entry)->field.next = NULL; \
885 (entry)->field.prev = NULL; \
886 MACRO_END
887
888 /*
889 * Macro: queue_remove_last
890 * Function:
891 * Remove and return the entry at the tail of
892 * the queue.
893 * Header:
894 * queue_remove_last(head, entry, type, field)
895 * entry is returned by reference
896 * Note:
897 * This should only be used with Method 2 queue iteration (element chains)
898 */
899 #define queue_remove_last(head, entry, type, field) \
900 MACRO_BEGIN \
901 queue_entry_t __prev; \
902 \
903 (entry) = (type)(void *) ((head)->prev); \
904 __prev = (entry)->field.prev; \
905 \
906 if ((head) == __prev) \
907 (head)->next = (head); \
908 else \
909 ((type)(void *)(__prev))->field.next = (head); \
910 (head)->prev = __prev; \
911 \
912 (entry)->field.next = NULL; \
913 (entry)->field.prev = NULL; \
914 MACRO_END
915
916 /*
917 * Macro: queue_assign
918 * Note:
919 * This should only be used with Method 2 queue iteration (element chains)
920 */
921 #define queue_assign(to, from, type, field) \
922 MACRO_BEGIN \
923 ((type)(void *)((from)->prev))->field.next = (to); \
924 ((type)(void *)((from)->next))->field.prev = (to); \
925 *to = *from; \
926 MACRO_END
927
928 /*
929 * Macro: queue_new_head
930 * Function:
931 * rebase old queue to new queue head
932 * Header:
933 * queue_new_head(old, new, type, field)
934 * queue_t old;
935 * queue_t new;
936 * <type> is what's in our queue
937 * <field> is the chain field in (*<type>)
938 * Note:
939 * This should only be used with Method 2 queue iteration (element chains)
940 */
941 #define queue_new_head(old, new, type, field) \
942 MACRO_BEGIN \
943 if (!queue_empty(old)) { \
944 *(new) = *(old); \
945 ((type)(void *)((new)->next))->field.prev = \
946 (new); \
947 ((type)(void *)((new)->prev))->field.next = \
948 (new); \
949 } else { \
950 queue_init(new); \
951 } \
952 MACRO_END
953
954 /*
955 * Macro: queue_iterate
956 * Function:
957 * iterate over each item in the queue.
958 * Generates a 'for' loop, setting elt to
959 * each item in turn (by reference).
960 * Header:
961 * queue_iterate(q, elt, type, field)
962 * queue_t q;
963 * <type> elt;
964 * <type> is what's in our queue
965 * <field> is the chain field in (*<type>)
966 * Note:
967 * This should only be used with Method 2 queue iteration (element chains)
968 */
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))
973
974 #ifdef MACH_KERNEL_PRIVATE
975
976 #include <kern/locks.h>
977
978 /*----------------------------------------------------------------*/
979 /*
980 * Define macros for queues with locks.
981 */
982 struct mpqueue_head {
983 struct queue_entry head; /* header for queue */
984 uint64_t earliest_soft_deadline;
985 uint64_t count;
986 #if defined(__i386__) || defined(__x86_64__)
987 lck_mtx_t lock_data;
988 lck_mtx_ext_t lock_data_ext;
989 #else
990 lck_spin_t lock_data;
991 #endif
992 };
993
994 typedef struct mpqueue_head mpqueue_head_t;
995
996 #define round_mpq(size) (size)
997
998
999 #if defined(__i386__) || defined(__x86_64__)
1000
1001 #define mpqueue_init(q, lck_grp, lck_attr) \
1002 MACRO_BEGIN \
1003 queue_init(&(q)->head); \
1004 lck_mtx_init_ext(&(q)->lock_data, \
1005 &(q)->lock_data_ext, \
1006 lck_grp, \
1007 lck_attr); \
1008 (q)->earliest_soft_deadline = UINT64_MAX; \
1009 (q)->count = 0; \
1010 MACRO_END
1011
1012 #else
1013
1014 #define mpqueue_init(q, lck_grp, lck_attr) \
1015 MACRO_BEGIN \
1016 queue_init(&(q)->head); \
1017 lck_spin_init(&(q)->lock_data, \
1018 lck_grp, \
1019 lck_attr); \
1020 MACRO_END
1021 #endif
1022
1023
1024 #define mpenqueue_tail(q, elt) \
1025 MACRO_BEGIN \
1026 lck_mtx_lock_spin_always(&(q)->lock_data); \
1027 enqueue_tail(&(q)->head, elt); \
1028 lck_mtx_unlock_always(&(q)->lock_data); \
1029 MACRO_END
1030
1031 #define mpdequeue_head(q, elt) \
1032 MACRO_BEGIN \
1033 lck_mtx_lock_spin_always(&(q)->lock_data); \
1034 if (queue_empty(&(q)->head)) \
1035 *(elt) = 0; \
1036 else \
1037 *(elt) = dequeue_head(&(q)->head); \
1038 lck_mtx_unlock_always(&(q)->lock_data); \
1039 MACRO_END
1040
1041 #endif /* MACH_KERNEL_PRIVATE */
1042
1043 __END_DECLS
1044
1045 #endif /* _KERN_QUEUE_H_ */