]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/queue.h
xnu-3789.51.2.tar.gz
[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_head
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 #ifdef XNU_KERNEL_PRIVATE
524
525 /* Dequeue an element from head, or return NULL if the queue is empty */
526 #define qe_dequeue_head(head, type, field) ({ \
527 queue_entry_t _tmp_entry = dequeue_head((head)); \
528 type *_tmp_element = (type*) NULL; \
529 if (_tmp_entry != (queue_entry_t) NULL) \
530 _tmp_element = qe_element(_tmp_entry, type, field); \
531 _tmp_element; \
532 })
533
534 /* Dequeue an element from tail, or return NULL if the queue is empty */
535 #define qe_dequeue_tail(head, type, field) ({ \
536 queue_entry_t _tmp_entry = dequeue_tail((head)); \
537 type *_tmp_element = (type*) NULL; \
538 if (_tmp_entry != (queue_entry_t) NULL) \
539 _tmp_element = qe_element(_tmp_entry, type, field); \
540 _tmp_element; \
541 })
542
543 /* Peek at the first element, or return NULL if the queue is empty */
544 #define qe_queue_first(head, type, field) ({ \
545 queue_entry_t _tmp_entry = queue_first((head)); \
546 type *_tmp_element = (type*) NULL; \
547 if (_tmp_entry != (queue_entry_t) head) \
548 _tmp_element = qe_element(_tmp_entry, type, field); \
549 _tmp_element; \
550 })
551
552 /* Peek at the last element, or return NULL if the queue is empty */
553 #define qe_queue_last(head, type, field) ({ \
554 queue_entry_t _tmp_entry = queue_last((head)); \
555 type *_tmp_element = (type*) NULL; \
556 if (_tmp_entry != (queue_entry_t) head) \
557 _tmp_element = qe_element(_tmp_entry, type, field); \
558 _tmp_element; \
559 })
560
561 #endif /* XNU_KERNEL_PRIVATE */
562
563 /*
564 * Macro: queue_init
565 * Function:
566 * Initialize the given queue.
567 * Header:
568 * void queue_init(q)
569 * queue_t q; \* MODIFIED *\
570 */
571 #define queue_init(q) \
572 MACRO_BEGIN \
573 (q)->next = (q);\
574 (q)->prev = (q);\
575 MACRO_END
576
577 /*
578 * Macro: queue_head_init
579 * Function:
580 * Initialize the given queue head
581 * Header:
582 * void queue_head_init(q)
583 * queue_head_t q; \* MODIFIED *\
584 */
585 #define queue_head_init(q) \
586 queue_init(&(q))
587
588 /*
589 * Macro: queue_chain_init
590 * Function:
591 * Initialize the given queue chain element
592 * Header:
593 * void queue_chain_init(q)
594 * queue_chain_t q; \* MODIFIED *\
595 */
596 #define queue_chain_init(q) \
597 queue_init(&(q))
598
599 /*
600 * Macro: queue_first
601 * Function:
602 * Returns the first entry in the queue,
603 * Header:
604 * queue_entry_t queue_first(q)
605 * queue_t q; \* IN *\
606 */
607 #define queue_first(q) ((q)->next)
608
609 /*
610 * Macro: queue_next
611 * Function:
612 * Returns the entry after an item in the queue.
613 * Header:
614 * queue_entry_t queue_next(qc)
615 * queue_t qc;
616 */
617 #define queue_next(qc) ((qc)->next)
618
619 /*
620 * Macro: queue_last
621 * Function:
622 * Returns the last entry in the queue.
623 * Header:
624 * queue_entry_t queue_last(q)
625 * queue_t q; \* IN *\
626 */
627 #define queue_last(q) ((q)->prev)
628
629 /*
630 * Macro: queue_prev
631 * Function:
632 * Returns the entry before an item in the queue.
633 * Header:
634 * queue_entry_t queue_prev(qc)
635 * queue_t qc;
636 */
637 #define queue_prev(qc) ((qc)->prev)
638
639 /*
640 * Macro: queue_end
641 * Function:
642 * Tests whether a new entry is really the end of
643 * the queue.
644 * Header:
645 * boolean_t queue_end(q, qe)
646 * queue_t q;
647 * queue_entry_t qe;
648 */
649 #define queue_end(q, qe) ((q) == (qe))
650
651 /*
652 * Macro: queue_empty
653 * Function:
654 * Tests whether a queue is empty.
655 * Header:
656 * boolean_t queue_empty(q)
657 * queue_t q;
658 */
659 #define queue_empty(q) queue_end((q), queue_first(q))
660
661 /*
662 * Function: movqueue
663 * Parameters:
664 * queue_t _old : head of a queue whose items will be moved
665 * queue_t _new : new queue head onto which items will be moved
666 * Description:
667 * Rebase queue items in _old onto _new then re-initialize
668 * the _old object to an empty queue.
669 * Equivalent to the queue_new_head Method 2 macro
670 * Note:
671 * Similar to the queue_new_head macro, this macros is intented
672 * to function as an initializer method for '_new' and thus may
673 * leak any list items that happen to be on the '_new' list.
674 * This should only be used with Method 1 queue iteration (linkage chains)
675 */
676 static __inline__ void
677 movqueue(queue_t _old, queue_t _new)
678 {
679 queue_entry_t next_elt, prev_elt;
680
681 __QUEUE_ELT_VALIDATE((queue_entry_t)_old);
682
683 if (queue_empty(_old)) {
684 queue_init(_new);
685 return;
686 }
687
688 /*
689 * move the queue at _old to _new
690 * and re-initialize _old
691 */
692 next_elt = _old->next;
693 prev_elt = _old->prev;
694
695 _new->next = next_elt;
696 _new->prev = prev_elt;
697 next_elt->prev = _new;
698 prev_elt->next = _new;
699
700 queue_init(_old);
701 }
702
703 /*----------------------------------------------------------------*/
704 /*
705 * Macros that operate on generic structures. The queue
706 * chain may be at any location within the structure, and there
707 * may be more than one chain.
708 */
709
710 /*
711 * Macro: queue_enter
712 * Function:
713 * Insert a new element at the tail of the queue.
714 * Header:
715 * void queue_enter(q, elt, type, field)
716 * queue_t q;
717 * <type> elt;
718 * <type> is what's in our queue
719 * <field> is the chain field in (*<type>)
720 * Note:
721 * This should only be used with Method 2 queue iteration (element chains)
722 */
723 #define queue_enter(head, elt, type, field) \
724 MACRO_BEGIN \
725 queue_entry_t __prev; \
726 \
727 __prev = (head)->prev; \
728 if ((head) == __prev) { \
729 (head)->next = (queue_entry_t) (elt); \
730 } \
731 else { \
732 ((type)(void *)__prev)->field.next = \
733 (queue_entry_t)(elt); \
734 } \
735 (elt)->field.prev = __prev; \
736 (elt)->field.next = head; \
737 (head)->prev = (queue_entry_t) elt; \
738 MACRO_END
739
740 /*
741 * Macro: queue_enter_first
742 * Function:
743 * Insert a new element at the head of the queue.
744 * Header:
745 * void queue_enter_first(q, elt, type, field)
746 * queue_t q;
747 * <type> elt;
748 * <type> is what's in our queue
749 * <field> is the chain field in (*<type>)
750 * Note:
751 * This should only be used with Method 2 queue iteration (element chains)
752 */
753 #define queue_enter_first(head, elt, type, field) \
754 MACRO_BEGIN \
755 queue_entry_t __next; \
756 \
757 __next = (head)->next; \
758 if ((head) == __next) { \
759 (head)->prev = (queue_entry_t) (elt); \
760 } \
761 else { \
762 ((type)(void *)__next)->field.prev = \
763 (queue_entry_t)(elt); \
764 } \
765 (elt)->field.next = __next; \
766 (elt)->field.prev = head; \
767 (head)->next = (queue_entry_t) elt; \
768 MACRO_END
769
770 /*
771 * Macro: queue_insert_before
772 * Function:
773 * Insert a new element before a given element.
774 * Header:
775 * void queue_insert_before(q, elt, cur, type, field)
776 * queue_t q;
777 * <type> elt;
778 * <type> cur;
779 * <type> is what's in our queue
780 * <field> is the chain field in (*<type>)
781 * Note:
782 * This should only be used with Method 2 queue iteration (element chains)
783 */
784 #define queue_insert_before(head, elt, cur, type, field) \
785 MACRO_BEGIN \
786 queue_entry_t __prev; \
787 \
788 if ((head) == (queue_entry_t)(cur)) { \
789 (elt)->field.next = (head); \
790 if ((head)->next == (head)) { /* only element */ \
791 (elt)->field.prev = (head); \
792 (head)->next = (queue_entry_t)(elt); \
793 } else { /* last element */ \
794 __prev = (elt)->field.prev = (head)->prev; \
795 ((type)(void *)__prev)->field.next = \
796 (queue_entry_t)(elt); \
797 } \
798 (head)->prev = (queue_entry_t)(elt); \
799 } else { \
800 (elt)->field.next = (queue_entry_t)(cur); \
801 if ((head)->next == (queue_entry_t)(cur)) { \
802 /* first element */ \
803 (elt)->field.prev = (head); \
804 (head)->next = (queue_entry_t)(elt); \
805 } else { /* middle element */ \
806 __prev = (elt)->field.prev = (cur)->field.prev; \
807 ((type)(void *)__prev)->field.next = \
808 (queue_entry_t)(elt); \
809 } \
810 (cur)->field.prev = (queue_entry_t)(elt); \
811 } \
812 MACRO_END
813
814 /*
815 * Macro: queue_insert_after
816 * Function:
817 * Insert a new element after a given element.
818 * Header:
819 * void queue_insert_after(q, elt, cur, type, field)
820 * queue_t q;
821 * <type> elt;
822 * <type> cur;
823 * <type> is what's in our queue
824 * <field> is the chain field in (*<type>)
825 * Note:
826 * This should only be used with Method 2 queue iteration (element chains)
827 */
828 #define queue_insert_after(head, elt, cur, type, field) \
829 MACRO_BEGIN \
830 queue_entry_t __next; \
831 \
832 if ((head) == (queue_entry_t)(cur)) { \
833 (elt)->field.prev = (head); \
834 if ((head)->next == (head)) { /* only element */ \
835 (elt)->field.next = (head); \
836 (head)->prev = (queue_entry_t)(elt); \
837 } else { /* first element */ \
838 __next = (elt)->field.next = (head)->next; \
839 ((type)(void *)__next)->field.prev = \
840 (queue_entry_t)(elt); \
841 } \
842 (head)->next = (queue_entry_t)(elt); \
843 } else { \
844 (elt)->field.prev = (queue_entry_t)(cur); \
845 if ((head)->prev == (queue_entry_t)(cur)) { \
846 /* last element */ \
847 (elt)->field.next = (head); \
848 (head)->prev = (queue_entry_t)(elt); \
849 } else { /* middle element */ \
850 __next = (elt)->field.next = (cur)->field.next; \
851 ((type)(void *)__next)->field.prev = \
852 (queue_entry_t)(elt); \
853 } \
854 (cur)->field.next = (queue_entry_t)(elt); \
855 } \
856 MACRO_END
857
858 /*
859 * Macro: queue_field [internal use only]
860 * Function:
861 * Find the queue_chain_t (or queue_t) for the
862 * given element (thing) in the given queue (head)
863 * Note:
864 * This should only be used with Method 2 queue iteration (element chains)
865 */
866 #define queue_field(head, thing, type, field) \
867 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
868
869 /*
870 * Macro: queue_remove
871 * Function:
872 * Remove an arbitrary item from the queue.
873 * Header:
874 * void queue_remove(q, qe, type, field)
875 * arguments as in queue_enter
876 * Note:
877 * This should only be used with Method 2 queue iteration (element chains)
878 */
879 #define queue_remove(head, elt, type, field) \
880 MACRO_BEGIN \
881 queue_entry_t __next, __prev; \
882 \
883 __next = (elt)->field.next; \
884 __prev = (elt)->field.prev; \
885 \
886 if ((head) == __next) \
887 (head)->prev = __prev; \
888 else \
889 ((type)(void *)__next)->field.prev = __prev; \
890 \
891 if ((head) == __prev) \
892 (head)->next = __next; \
893 else \
894 ((type)(void *)__prev)->field.next = __next; \
895 \
896 (elt)->field.next = NULL; \
897 (elt)->field.prev = NULL; \
898 MACRO_END
899
900 /*
901 * Macro: queue_remove_first
902 * Function:
903 * Remove and return the entry at the head of
904 * the queue.
905 * Header:
906 * queue_remove_first(head, entry, type, field)
907 * entry is returned by reference
908 * Note:
909 * This should only be used with Method 2 queue iteration (element chains)
910 */
911 #define queue_remove_first(head, entry, type, field) \
912 MACRO_BEGIN \
913 queue_entry_t __next; \
914 \
915 (entry) = (type)(void *) ((head)->next); \
916 __next = (entry)->field.next; \
917 \
918 if ((head) == __next) \
919 (head)->prev = (head); \
920 else \
921 ((type)(void *)(__next))->field.prev = (head); \
922 (head)->next = __next; \
923 \
924 (entry)->field.next = NULL; \
925 (entry)->field.prev = NULL; \
926 MACRO_END
927
928 /*
929 * Macro: queue_remove_last
930 * Function:
931 * Remove and return the entry at the tail of
932 * the queue.
933 * Header:
934 * queue_remove_last(head, entry, type, field)
935 * entry is returned by reference
936 * Note:
937 * This should only be used with Method 2 queue iteration (element chains)
938 */
939 #define queue_remove_last(head, entry, type, field) \
940 MACRO_BEGIN \
941 queue_entry_t __prev; \
942 \
943 (entry) = (type)(void *) ((head)->prev); \
944 __prev = (entry)->field.prev; \
945 \
946 if ((head) == __prev) \
947 (head)->next = (head); \
948 else \
949 ((type)(void *)(__prev))->field.next = (head); \
950 (head)->prev = __prev; \
951 \
952 (entry)->field.next = NULL; \
953 (entry)->field.prev = NULL; \
954 MACRO_END
955
956 /*
957 * Macro: queue_assign
958 * Note:
959 * This should only be used with Method 2 queue iteration (element chains)
960 */
961 #define queue_assign(to, from, type, field) \
962 MACRO_BEGIN \
963 ((type)(void *)((from)->prev))->field.next = (to); \
964 ((type)(void *)((from)->next))->field.prev = (to); \
965 *to = *from; \
966 MACRO_END
967
968 /*
969 * Macro: queue_new_head
970 * Function:
971 * rebase old queue to new queue head
972 * Header:
973 * queue_new_head(old, new, type, field)
974 * queue_t old;
975 * queue_t new;
976 * <type> is what's in our queue
977 * <field> is the chain field in (*<type>)
978 * Note:
979 * This should only be used with Method 2 queue iteration (element chains)
980 */
981 #define queue_new_head(old, new, type, field) \
982 MACRO_BEGIN \
983 if (!queue_empty(old)) { \
984 *(new) = *(old); \
985 ((type)(void *)((new)->next))->field.prev = \
986 (new); \
987 ((type)(void *)((new)->prev))->field.next = \
988 (new); \
989 } else { \
990 queue_init(new); \
991 } \
992 MACRO_END
993
994 /*
995 * Macro: queue_iterate
996 * Function:
997 * iterate over each item in the queue.
998 * Generates a 'for' loop, setting elt to
999 * each item in turn (by reference).
1000 * Header:
1001 * queue_iterate(q, elt, type, field)
1002 * queue_t q;
1003 * <type> elt;
1004 * <type> is what's in our queue
1005 * <field> is the chain field in (*<type>)
1006 * Note:
1007 * This should only be used with Method 2 queue iteration (element chains)
1008 */
1009 #define queue_iterate(head, elt, type, field) \
1010 for ((elt) = (type)(void *) queue_first(head); \
1011 !queue_end((head), (queue_entry_t)(elt)); \
1012 (elt) = (type)(void *) queue_next(&(elt)->field))
1013
1014 #ifdef MACH_KERNEL_PRIVATE
1015
1016 #include <kern/locks.h>
1017
1018 /*----------------------------------------------------------------*/
1019 /*
1020 * Define macros for queues with locks.
1021 */
1022 struct mpqueue_head {
1023 struct queue_entry head; /* header for queue */
1024 uint64_t earliest_soft_deadline;
1025 uint64_t count;
1026 lck_mtx_t lock_data;
1027 #if defined(__i386__) || defined(__x86_64__)
1028 lck_mtx_ext_t lock_data_ext;
1029 #endif
1030 };
1031
1032 typedef struct mpqueue_head mpqueue_head_t;
1033
1034 #define round_mpq(size) (size)
1035
1036
1037 #if defined(__i386__) || defined(__x86_64__)
1038
1039 #define mpqueue_init(q, lck_grp, lck_attr) \
1040 MACRO_BEGIN \
1041 queue_init(&(q)->head); \
1042 lck_mtx_init_ext(&(q)->lock_data, \
1043 &(q)->lock_data_ext, \
1044 lck_grp, \
1045 lck_attr); \
1046 (q)->earliest_soft_deadline = UINT64_MAX; \
1047 (q)->count = 0; \
1048 MACRO_END
1049
1050 #else
1051
1052 #define mpqueue_init(q, lck_grp, lck_attr) \
1053 MACRO_BEGIN \
1054 queue_init(&(q)->head); \
1055 lck_mtx_init(&(q)->lock_data, \
1056 lck_grp, \
1057 lck_attr); \
1058 MACRO_END
1059 #endif
1060
1061
1062 #define mpenqueue_tail(q, elt) \
1063 MACRO_BEGIN \
1064 lck_mtx_lock_spin_always(&(q)->lock_data); \
1065 enqueue_tail(&(q)->head, elt); \
1066 lck_mtx_unlock_always(&(q)->lock_data); \
1067 MACRO_END
1068
1069 #define mpdequeue_head(q, elt) \
1070 MACRO_BEGIN \
1071 lck_mtx_lock_spin_always(&(q)->lock_data); \
1072 if (queue_empty(&(q)->head)) \
1073 *(elt) = 0; \
1074 else \
1075 *(elt) = dequeue_head(&(q)->head); \
1076 lck_mtx_unlock_always(&(q)->lock_data); \
1077 MACRO_END
1078
1079 #endif /* MACH_KERNEL_PRIVATE */
1080
1081 __END_DECLS
1082
1083 #endif /* _KERN_QUEUE_H_ */