2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: wait_queue.c (adapted from sched_prim.c)
54 * Author: Avadis Tevanian, Jr.
57 * Primitives for manipulating wait queues: either global
58 * ones from sched_prim.c, or private ones associated with
59 * particular structures(pots, semaphores, etc..).
62 #include <kern/kern_types.h>
63 #include <kern/simple_lock.h>
64 #include <kern/kalloc.h>
65 #include <kern/queue.h>
67 #include <mach/sync_policy.h>
68 #include <kern/sched_prim.h>
70 #include <kern/wait_queue.h>
73 * Routine: wait_queue_init
75 * Initialize a previously allocated wait queue.
77 * KERN_SUCCESS - The wait_queue_t was initialized
78 * KERN_INVALID_ARGUMENT - The policy parameter was invalid
85 if (!((policy
& SYNC_POLICY_ORDER_MASK
) == SYNC_POLICY_FIFO
))
86 return KERN_INVALID_ARGUMENT
;
89 wq
->wq_type
= _WAIT_QUEUE_inited
;
90 queue_init(&wq
->wq_queue
);
91 hw_lock_init(&wq
->wq_interlock
);
96 * Routine: wait_queue_alloc
98 * Allocate and initialize a wait queue for use outside of
99 * of the mach part of the kernel.
101 * Nothing locked - can block.
103 * The allocated and initialized wait queue
104 * WAIT_QUEUE_NULL if there is a resource shortage
113 wq
= (wait_queue_t
) kalloc(sizeof(struct wait_queue
));
114 if (wq
!= WAIT_QUEUE_NULL
) {
115 ret
= wait_queue_init(wq
, policy
);
116 if (ret
!= KERN_SUCCESS
) {
117 kfree((vm_offset_t
)wq
, sizeof(struct wait_queue
));
118 wq
= WAIT_QUEUE_NULL
;
125 * Routine: wait_queue_free
127 * Free an allocated wait queue.
135 if (!wait_queue_is_queue(wq
))
136 return KERN_INVALID_ARGUMENT
;
137 if (!queue_empty(&wq
->wq_queue
))
139 kfree((vm_offset_t
)wq
, sizeof(struct wait_queue
));
144 * Routine: wait_queue_set_init
146 * Initialize a previously allocated wait queue set.
148 * KERN_SUCCESS - The wait_queue_set_t was initialized
149 * KERN_INVALID_ARGUMENT - The policy parameter was invalid
153 wait_queue_set_t wqset
,
158 ret
= wait_queue_init(&wqset
->wqs_wait_queue
, policy
);
159 if (ret
!= KERN_SUCCESS
)
162 wqset
->wqs_wait_queue
.wq_type
= _WAIT_QUEUE_SET_inited
;
163 if (policy
& SYNC_POLICY_PREPOST
)
164 wqset
->wqs_wait_queue
.wq_isprepost
= TRUE
;
166 wqset
->wqs_wait_queue
.wq_isprepost
= FALSE
;
167 queue_init(&wqset
->wqs_setlinks
);
168 wqset
->wqs_refcount
= 0;
175 wait_queue_set_t wqset
,
178 return wait_queue_set_init(wqset
, policy
);
182 * Routine: wait_queue_set_alloc
184 * Allocate and initialize a wait queue set for
185 * use outside of the mach part of the kernel.
189 * The allocated and initialized wait queue set
190 * WAIT_QUEUE_SET_NULL if there is a resource shortage
193 wait_queue_set_alloc(
196 wait_queue_set_t wq_set
;
198 wq_set
= (wait_queue_set_t
) kalloc(sizeof(struct wait_queue_set
));
199 if (wq_set
!= WAIT_QUEUE_SET_NULL
) {
202 ret
= wait_queue_set_init(wq_set
, policy
);
203 if (ret
!= KERN_SUCCESS
) {
204 kfree((vm_offset_t
)wq_set
, sizeof(struct wait_queue_set
));
205 wq_set
= WAIT_QUEUE_SET_NULL
;
212 * Routine: wait_queue_set_free
214 * Free an allocated wait queue set
220 wait_queue_set_t wq_set
)
222 if (!wait_queue_is_set(wq_set
))
223 return KERN_INVALID_ARGUMENT
;
225 if (!queue_empty(&wq_set
->wqs_wait_queue
.wq_queue
))
228 kfree((vm_offset_t
)wq_set
, sizeof(struct wait_queue_set
));
233 wait_queue_sub_clearrefs(
234 wait_queue_set_t wq_set
)
236 if (!wait_queue_is_set(wq_set
))
237 return KERN_INVALID_ARGUMENT
;
240 wq_set
->wqs_refcount
= 0;
247 * Routine: wait_queue_set_size
248 * Routine: wait_queue_link_size
250 * Return the size of opaque wait queue structures
252 unsigned int wait_queue_set_size(void) { return sizeof(WaitQueueSet
); }
253 unsigned int wait_queue_link_size(void) { return sizeof(WaitQueueLink
); }
255 /* declare a unique type for wait queue link structures */
256 static unsigned int _wait_queue_link
;
257 static unsigned int _wait_queue_unlinked
;
259 #define WAIT_QUEUE_LINK ((void *)&_wait_queue_link)
260 #define WAIT_QUEUE_UNLINKED ((void *)&_wait_queue_unlinked)
262 #define WAIT_QUEUE_ELEMENT_CHECK(wq, wqe) \
263 WQASSERT(((wqe)->wqe_queue == (wq) && \
264 queue_next(queue_prev((queue_t) (wqe))) == (queue_t)(wqe)), \
265 "wait queue element list corruption: wq=%#x, wqe=%#x", \
268 #define WQSPREV(wqs, wql) ((wait_queue_link_t)queue_prev( \
269 ((&(wqs)->wqs_setlinks == (queue_t)(wql)) ? \
270 (queue_t)(wql) : &(wql)->wql_setlinks)))
272 #define WQSNEXT(wqs, wql) ((wait_queue_link_t)queue_next( \
273 ((&(wqs)->wqs_setlinks == (queue_t)(wql)) ? \
274 (queue_t)(wql) : &(wql)->wql_setlinks)))
276 #define WAIT_QUEUE_SET_LINK_CHECK(wqs, wql) \
277 WQASSERT((((wql)->wql_type == WAIT_QUEUE_LINK) && \
278 ((wql)->wql_setqueue == (wqs)) && \
279 ((wql)->wql_queue->wq_type == _WAIT_QUEUE_inited) && \
280 (WQSNEXT((wqs), WQSPREV((wqs),(wql))) == (wql))), \
281 "wait queue set links corruption: wqs=%#x, wql=%#x", \
284 #if defined(_WAIT_QUEUE_DEBUG_)
286 #define WQASSERT(e, s, p0, p1) ((e) ? 0 : panic(s, p0, p1))
288 #define WAIT_QUEUE_CHECK(wq) \
290 queue_t q2 = &(wq)->wq_queue; \
291 wait_queue_element_t wqe2 = (wait_queue_element_t) queue_first(q2); \
292 while (!queue_end(q2, (queue_entry_t)wqe2)) { \
293 WAIT_QUEUE_ELEMENT_CHECK((wq), wqe2); \
294 wqe2 = (wait_queue_element_t) queue_next((queue_t) wqe2); \
298 #define WAIT_QUEUE_SET_CHECK(wqs) \
300 queue_t q2 = &(wqs)->wqs_setlinks; \
301 wait_queue_link_t wql2 = (wait_queue_link_t) queue_first(q2); \
302 while (!queue_end(q2, (queue_entry_t)wql2)) { \
303 WAIT_QUEUE_SET_LINK_CHECK((wqs), wql2); \
304 wql2 = (wait_queue_link_t) wql2->wql_setlinks.next; \
308 #else /* !_WAIT_QUEUE_DEBUG_ */
310 #define WQASSERT(e, s, p0, p1) assert(e)
312 #define WAIT_QUEUE_CHECK(wq)
313 #define WAIT_QUEUE_SET_CHECK(wqs)
315 #endif /* !_WAIT_QUEUE_DEBUG_ */
318 * Routine: wait_queue_member_locked
320 * Indicate if this set queue is a member of the queue
322 * The wait queue is locked
323 * The set queue is just that, a set queue
325 __private_extern__ boolean_t
326 wait_queue_member_locked(
328 wait_queue_set_t wq_set
)
330 wait_queue_element_t wq_element
;
333 assert(wait_queue_held(wq
));
334 assert(wait_queue_is_set(wq_set
));
338 wq_element
= (wait_queue_element_t
) queue_first(q
);
339 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
340 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
341 if ((wq_element
->wqe_type
== WAIT_QUEUE_LINK
)) {
342 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
344 if (wql
->wql_setqueue
== wq_set
)
347 wq_element
= (wait_queue_element_t
)
348 queue_next((queue_t
) wq_element
);
355 * Routine: wait_queue_member
357 * Indicate if this set queue is a member of the queue
359 * The set queue is just that, a set queue
364 wait_queue_set_t wq_set
)
369 if (!wait_queue_is_set(wq_set
))
374 ret
= wait_queue_member_locked(wq
, wq_set
);
375 wait_queue_unlock(wq
);
383 * Routine: wait_queue_link_noalloc
385 * Insert a set wait queue into a wait queue. This
386 * requires us to link the two together using a wait_queue_link
387 * structure that we allocate.
389 * The wait queue being inserted must be inited as a set queue
392 wait_queue_link_noalloc(
394 wait_queue_set_t wq_set
,
395 wait_queue_link_t wql
)
397 wait_queue_element_t wq_element
;
401 if (!wait_queue_is_queue(wq
) || !wait_queue_is_set(wq_set
))
402 return KERN_INVALID_ARGUMENT
;
405 * There are probably less threads and sets associated with
406 * the wait queue, then there are wait queues associated with
407 * the set. So lets validate it that way.
412 wq_element
= (wait_queue_element_t
) queue_first(q
);
413 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
414 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
415 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
&&
416 ((wait_queue_link_t
)wq_element
)->wql_setqueue
== wq_set
) {
417 wait_queue_unlock(wq
);
419 return KERN_ALREADY_IN_SET
;
421 wq_element
= (wait_queue_element_t
)
422 queue_next((queue_t
) wq_element
);
426 * Not already a member, so we can add it.
430 WAIT_QUEUE_SET_CHECK(wq_set
);
433 queue_enter(&wq
->wq_queue
, wql
, wait_queue_link_t
, wql_links
);
434 wql
->wql_setqueue
= wq_set
;
435 queue_enter(&wq_set
->wqs_setlinks
, wql
, wait_queue_link_t
, wql_setlinks
);
436 wql
->wql_type
= WAIT_QUEUE_LINK
;
439 wait_queue_unlock(wq
);
446 * Routine: wait_queue_link
448 * Insert a set wait queue into a wait queue. This
449 * requires us to link the two together using a wait_queue_link
450 * structure that we allocate.
452 * The wait queue being inserted must be inited as a set queue
457 wait_queue_set_t wq_set
)
459 wait_queue_link_t wql
;
462 wql
= (wait_queue_link_t
) kalloc(sizeof(struct wait_queue_link
));
463 if (wql
== WAIT_QUEUE_LINK_NULL
)
464 return KERN_RESOURCE_SHORTAGE
;
466 ret
= wait_queue_link_noalloc(wq
, wq_set
, wql
);
467 if (ret
!= KERN_SUCCESS
)
468 kfree((vm_offset_t
)wql
, sizeof(struct wait_queue_link
));
475 * Routine: wait_queue_unlink_nofree
477 * Undo the linkage between a wait queue and a set.
480 wait_queue_unlink_locked(
482 wait_queue_set_t wq_set
,
483 wait_queue_link_t wql
)
485 assert(wait_queue_held(wq
));
486 assert(wait_queue_held(&wq_set
->wqs_wait_queue
));
488 wql
->wql_queue
= WAIT_QUEUE_NULL
;
489 queue_remove(&wq
->wq_queue
, wql
, wait_queue_link_t
, wql_links
);
490 wql
->wql_setqueue
= WAIT_QUEUE_SET_NULL
;
491 queue_remove(&wq_set
->wqs_setlinks
, wql
, wait_queue_link_t
, wql_setlinks
);
492 wql
->wql_type
= WAIT_QUEUE_UNLINKED
;
494 WAIT_QUEUE_CHECK(wq
);
495 WAIT_QUEUE_SET_CHECK(wq_set
);
499 * Routine: wait_queue_unlink
501 * Remove the linkage between a wait queue and a set,
502 * freeing the linkage structure.
504 * The wait queue being must be a member set queue
509 wait_queue_set_t wq_set
)
511 wait_queue_element_t wq_element
;
512 wait_queue_link_t wql
;
516 if (!wait_queue_is_queue(wq
) || !wait_queue_is_set(wq_set
)) {
517 return KERN_INVALID_ARGUMENT
;
523 wq_element
= (wait_queue_element_t
) queue_first(q
);
524 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
525 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
526 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
527 wql
= (wait_queue_link_t
)wq_element
;
529 if (wql
->wql_setqueue
== wq_set
) {
531 wait_queue_unlink_locked(wq
, wq_set
, wql
);
533 wait_queue_unlock(wq
);
535 kfree((vm_offset_t
)wql
, sizeof(struct wait_queue_link
));
539 wq_element
= (wait_queue_element_t
)
540 queue_next((queue_t
) wq_element
);
542 wait_queue_unlock(wq
);
544 return KERN_NOT_IN_SET
;
549 * Routine: wait_queue_unlinkall_nofree
551 * Remove the linkage between a wait queue and all its
552 * sets. The caller is responsible for freeing
553 * the wait queue link structures.
557 wait_queue_unlinkall_nofree(
560 wait_queue_element_t wq_element
;
561 wait_queue_element_t wq_next_element
;
562 wait_queue_set_t wq_set
;
563 wait_queue_link_t wql
;
564 queue_head_t links_queue_head
;
565 queue_t links
= &links_queue_head
;
569 if (!wait_queue_is_queue(wq
)) {
570 return KERN_INVALID_ARGUMENT
;
580 wq_element
= (wait_queue_element_t
) queue_first(q
);
581 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
582 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
583 wq_next_element
= (wait_queue_element_t
)
584 queue_next((queue_t
) wq_element
);
586 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
587 wql
= (wait_queue_link_t
)wq_element
;
588 wq_set
= wql
->wql_setqueue
;
590 wait_queue_unlink_locked(wq
, wq_set
, wql
);
593 wq_element
= wq_next_element
;
595 wait_queue_unlock(wq
);
597 return(KERN_SUCCESS
);
602 * Routine: wait_queue_unlink_all
604 * Remove the linkage between a wait queue and all its sets.
605 * All the linkage structures are freed.
607 * Nothing of interest locked.
611 wait_queue_unlink_all(
614 wait_queue_element_t wq_element
;
615 wait_queue_element_t wq_next_element
;
616 wait_queue_set_t wq_set
;
617 wait_queue_link_t wql
;
618 queue_head_t links_queue_head
;
619 queue_t links
= &links_queue_head
;
623 if (!wait_queue_is_queue(wq
)) {
624 return KERN_INVALID_ARGUMENT
;
634 wq_element
= (wait_queue_element_t
) queue_first(q
);
635 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
636 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
637 wq_next_element
= (wait_queue_element_t
)
638 queue_next((queue_t
) wq_element
);
640 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
641 wql
= (wait_queue_link_t
)wq_element
;
642 wq_set
= wql
->wql_setqueue
;
644 wait_queue_unlink_locked(wq
, wq_set
, wql
);
646 enqueue(links
, &wql
->wql_links
);
648 wq_element
= wq_next_element
;
650 wait_queue_unlock(wq
);
653 while(!queue_empty(links
)) {
654 wql
= (wait_queue_link_t
) dequeue(links
);
655 kfree((vm_offset_t
) wql
, sizeof(struct wait_queue_link
));
658 return(KERN_SUCCESS
);
662 * Routine: wait_queue_set_unlink_all_nofree
664 * Remove the linkage between a set wait queue and all its
665 * member wait queues. The link structures are not freed, nor
666 * returned. It is the caller's responsibility to track and free
669 * The wait queue being must be a member set queue
672 wait_queue_set_unlink_all_nofree(
673 wait_queue_set_t wq_set
)
675 wait_queue_link_t wql
;
681 if (!wait_queue_is_set(wq_set
)) {
682 return KERN_INVALID_ARGUMENT
;
689 q
= &wq_set
->wqs_setlinks
;
691 wql
= (wait_queue_link_t
)queue_first(q
);
692 while (!queue_end(q
, (queue_entry_t
)wql
)) {
693 WAIT_QUEUE_SET_LINK_CHECK(wq_set
, wql
);
695 if (wait_queue_lock_try(wq
)) {
696 wait_queue_unlink_locked(wq
, wq_set
, wql
);
697 wait_queue_unlock(wq
);
698 wql
= (wait_queue_link_t
)queue_first(q
);
709 return(KERN_SUCCESS
);
712 /* legacy interface naming */
714 wait_subqueue_unlink_all(
715 wait_queue_set_t wq_set
)
717 return wait_queue_set_unlink_all_nofree(wq_set
);
722 * Routine: wait_queue_set_unlink_all
724 * Remove the linkage between a set wait queue and all its
725 * member wait queues. The link structures are freed.
727 * The wait queue must be a set
730 wait_queue_set_unlink_all(
731 wait_queue_set_t wq_set
)
733 wait_queue_link_t wql
;
736 queue_head_t links_queue_head
;
737 queue_t links
= &links_queue_head
;
741 if (!wait_queue_is_set(wq_set
)) {
742 return KERN_INVALID_ARGUMENT
;
751 q
= &wq_set
->wqs_setlinks
;
753 wql
= (wait_queue_link_t
)queue_first(q
);
754 while (!queue_end(q
, (queue_entry_t
)wql
)) {
755 WAIT_QUEUE_SET_LINK_CHECK(wq_set
, wql
);
757 if (wait_queue_lock_try(wq
)) {
758 wait_queue_unlink_locked(wq
, wq_set
, wql
);
759 wait_queue_unlock(wq
);
760 enqueue(links
, &wql
->wql_links
);
761 wql
= (wait_queue_link_t
)queue_first(q
);
772 while (!queue_empty (links
)) {
773 wql
= (wait_queue_link_t
) dequeue(links
);
774 kfree((vm_offset_t
)wql
, sizeof(struct wait_queue_link
));
776 return(KERN_SUCCESS
);
781 * Routine: wait_queue_unlink_one
783 * Find and unlink one set wait queue
785 * Nothing of interest locked.
788 wait_queue_unlink_one(
790 wait_queue_set_t
*wq_setp
)
792 wait_queue_element_t wq_element
;
801 wq_element
= (wait_queue_element_t
) queue_first(q
);
802 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
804 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
805 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
806 wait_queue_set_t wq_set
= wql
->wql_setqueue
;
809 wait_queue_unlink_locked(wq
, wq_set
, wql
);
811 wait_queue_unlock(wq
);
813 kfree((vm_offset_t
)wql
,sizeof(struct wait_queue_link
));
818 wq_element
= (wait_queue_element_t
)
819 queue_next((queue_t
) wq_element
);
821 wait_queue_unlock(wq
);
823 *wq_setp
= WAIT_QUEUE_SET_NULL
;
828 * Routine: wait_queue_assert_wait64_locked
830 * Insert the current thread into the supplied wait queue
831 * waiting for a particular event to be posted to that queue.
834 * The wait queue is assumed locked.
837 __private_extern__ wait_result_t
838 wait_queue_assert_wait64_locked(
841 wait_interrupt_t interruptible
,
845 wait_result_t wait_result
;
847 if (wq
->wq_type
== _WAIT_QUEUE_SET_inited
) {
848 wait_queue_set_t wqs
= (wait_queue_set_t
)wq
;
849 if (wqs
->wqs_isprepost
&& wqs
->wqs_refcount
> 0) {
851 wait_queue_unlock(wq
);
852 return(THREAD_AWAKENED
);
857 * This is the extent to which we currently take scheduling attributes
858 * into account. If the thread is vm priviledged, we stick it at
859 * the front of the queue. Later, these queues will honor the policy
860 * value set at wait_queue_init time.
862 thread
= current_thread();
864 wait_result
= thread_mark_wait_locked(thread
, interruptible
);
865 if (wait_result
== THREAD_WAITING
) {
866 if (thread
->vm_privilege
)
867 enqueue_head(&wq
->wq_queue
, (queue_entry_t
) thread
);
869 enqueue_tail(&wq
->wq_queue
, (queue_entry_t
) thread
);
870 thread
->wait_event
= event
;
871 thread
->wait_queue
= wq
;
873 thread_unlock(thread
);
875 wait_queue_unlock(wq
);
880 * Routine: wait_queue_assert_wait
882 * Insert the current thread into the supplied wait queue
883 * waiting for a particular event to be posted to that queue.
886 * nothing of interest locked.
889 wait_queue_assert_wait(
892 wait_interrupt_t interruptible
)
897 /* If it is an invalid wait queue, you can't wait on it */
898 if (!wait_queue_is_valid(wq
)) {
899 thread_t thread
= current_thread();
900 return (thread
->wait_result
= THREAD_RESTART
);
905 ret
= wait_queue_assert_wait64_locked(
906 wq
, (event64_t
)((uint32_t)event
),
907 interruptible
, TRUE
);
908 /* wait queue unlocked */
914 * Routine: wait_queue_assert_wait64
916 * Insert the current thread into the supplied wait queue
917 * waiting for a particular event to be posted to that queue.
919 * nothing of interest locked.
922 wait_queue_assert_wait64(
925 wait_interrupt_t interruptible
)
930 /* If it is an invalid wait queue, you cant wait on it */
931 if (!wait_queue_is_valid(wq
)) {
932 thread_t thread
= current_thread();
933 return (thread
->wait_result
= THREAD_RESTART
);
938 ret
= wait_queue_assert_wait64_locked(wq
, event
, interruptible
, TRUE
);
939 /* wait queue unlocked */
946 * Routine: _wait_queue_select64_all
948 * Select all threads off a wait queue that meet the
953 * wake_queue initialized and ready for insertion
956 * a queue of locked threads
959 _wait_queue_select64_all(
964 wait_queue_element_t wq_element
;
965 wait_queue_element_t wqe_next
;
970 wq_element
= (wait_queue_element_t
) queue_first(q
);
971 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
972 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
973 wqe_next
= (wait_queue_element_t
)
974 queue_next((queue_t
) wq_element
);
977 * We may have to recurse if this is a compound wait queue.
979 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
980 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
981 wait_queue_t set_queue
;
984 * We have to check the set wait queue.
986 set_queue
= (wait_queue_t
)wql
->wql_setqueue
;
987 wait_queue_lock(set_queue
);
988 if (set_queue
->wq_isprepost
) {
989 wait_queue_set_t wqs
= (wait_queue_set_t
)set_queue
;
992 * Preposting is only for sets and wait queue
993 * is the first element of set
997 if (! wait_queue_empty(set_queue
))
998 _wait_queue_select64_all(set_queue
, event
, wake_queue
);
999 wait_queue_unlock(set_queue
);
1003 * Otherwise, its a thread. If it is waiting on
1004 * the event we are posting to this queue, pull
1005 * it off the queue and stick it in out wake_queue.
1007 thread_t t
= (thread_t
)wq_element
;
1009 if (t
->wait_event
== event
) {
1011 remqueue(q
, (queue_entry_t
) t
);
1012 enqueue (wake_queue
, (queue_entry_t
) t
);
1013 t
->wait_queue
= WAIT_QUEUE_NULL
;
1014 t
->wait_event
= NO_EVENT64
;
1015 t
->at_safe_point
= FALSE
;
1016 /* returned locked */
1019 wq_element
= wqe_next
;
1024 * Routine: wait_queue_wakeup64_all_locked
1026 * Wakeup some number of threads that are in the specified
1027 * wait queue and waiting on the specified event.
1029 * wait queue already locked (may be released).
1031 * KERN_SUCCESS - Threads were woken up
1032 * KERN_NOT_WAITING - No threads were waiting <wq,event> pair
1034 __private_extern__ kern_return_t
1035 wait_queue_wakeup64_all_locked(
1038 wait_result_t result
,
1041 queue_head_t wake_queue_head
;
1042 queue_t q
= &wake_queue_head
;
1045 assert(wait_queue_held(wq
));
1049 * Select the threads that we will wake up. The threads
1050 * are returned to us locked and cleanly removed from the
1053 _wait_queue_select64_all(wq
, event
, q
);
1055 wait_queue_unlock(wq
);
1058 * For each thread, set it running.
1060 res
= KERN_NOT_WAITING
;
1061 while (!queue_empty (q
)) {
1062 thread_t thread
= (thread_t
) dequeue(q
);
1063 res
= thread_go_locked(thread
, result
);
1064 assert(res
== KERN_SUCCESS
);
1065 thread_unlock(thread
);
1072 * Routine: wait_queue_wakeup_all
1074 * Wakeup some number of threads that are in the specified
1075 * wait queue and waiting on the specified event.
1079 * KERN_SUCCESS - Threads were woken up
1080 * KERN_NOT_WAITING - No threads were waiting <wq,event> pair
1083 wait_queue_wakeup_all(
1086 wait_result_t result
)
1091 if (!wait_queue_is_valid(wq
)) {
1092 return KERN_INVALID_ARGUMENT
;
1096 wait_queue_lock(wq
);
1097 ret
= wait_queue_wakeup64_all_locked(
1098 wq
, (event64_t
)((uint32_t)event
),
1106 * Routine: wait_queue_wakeup64_all
1108 * Wakeup some number of threads that are in the specified
1109 * wait queue and waiting on the specified event.
1113 * KERN_SUCCESS - Threads were woken up
1114 * KERN_NOT_WAITING - No threads were waiting <wq,event> pair
1117 wait_queue_wakeup64_all(
1120 wait_result_t result
)
1125 if (!wait_queue_is_valid(wq
)) {
1126 return KERN_INVALID_ARGUMENT
;
1130 wait_queue_lock(wq
);
1131 ret
= wait_queue_wakeup64_all_locked(wq
, event
, result
, TRUE
);
1138 * Routine: _wait_queue_select64_one
1140 * Select the best thread off a wait queue that meet the
1141 * supplied criteria.
1145 * possibly recursive
1147 * a locked thread - if one found
1149 * This is where the sync policy of the wait queue comes
1150 * into effect. For now, we just assume FIFO.
1153 _wait_queue_select64_one(
1157 wait_queue_element_t wq_element
;
1158 wait_queue_element_t wqe_next
;
1159 thread_t t
= THREAD_NULL
;
1162 assert(wq
->wq_fifo
);
1166 wq_element
= (wait_queue_element_t
) queue_first(q
);
1167 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
1168 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
1169 wqe_next
= (wait_queue_element_t
)
1170 queue_next((queue_t
) wq_element
);
1173 * We may have to recurse if this is a compound wait queue.
1175 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
1176 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
1177 wait_queue_t set_queue
;
1180 * We have to check the set wait queue.
1182 set_queue
= (wait_queue_t
)wql
->wql_setqueue
;
1183 wait_queue_lock(set_queue
);
1184 if (! wait_queue_empty(set_queue
)) {
1185 t
= _wait_queue_select64_one(set_queue
, event
);
1187 wait_queue_unlock(set_queue
);
1188 if (t
!= THREAD_NULL
)
1193 * Otherwise, its a thread. If it is waiting on
1194 * the event we are posting to this queue, pull
1195 * it off the queue and stick it in out wake_queue.
1197 thread_t t
= (thread_t
)wq_element
;
1199 if (t
->wait_event
== event
) {
1201 remqueue(q
, (queue_entry_t
) t
);
1202 t
->wait_queue
= WAIT_QUEUE_NULL
;
1203 t
->wait_event
= NO_EVENT64
;
1204 t
->at_safe_point
= FALSE
;
1205 return t
; /* still locked */
1208 wq_element
= wqe_next
;
1214 * Routine: wait_queue_peek64_locked
1216 * Select the best thread from a wait queue that meet the
1217 * supplied criteria, but leave it on the queue it was
1218 * found on. The thread, and the actual wait_queue the
1219 * thread was found on are identified.
1223 * possibly recursive
1225 * a locked thread - if one found
1226 * a locked waitq - the one the thread was found on
1228 * Both the waitq the thread was actually found on, and
1229 * the supplied wait queue, are locked after this.
1231 __private_extern__
void
1232 wait_queue_peek64_locked(
1238 wait_queue_element_t wq_element
;
1239 wait_queue_element_t wqe_next
;
1243 assert(wq
->wq_fifo
);
1249 wq_element
= (wait_queue_element_t
) queue_first(q
);
1250 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
1251 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
1252 wqe_next
= (wait_queue_element_t
)
1253 queue_next((queue_t
) wq_element
);
1256 * We may have to recurse if this is a compound wait queue.
1258 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
1259 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
1260 wait_queue_t set_queue
;
1263 * We have to check the set wait queue.
1265 set_queue
= (wait_queue_t
)wql
->wql_setqueue
;
1266 wait_queue_lock(set_queue
);
1267 if (! wait_queue_empty(set_queue
)) {
1268 wait_queue_peek64_locked(set_queue
, event
, tp
, wqp
);
1270 if (*tp
!= THREAD_NULL
) {
1271 if (*wqp
!= set_queue
)
1272 wait_queue_unlock(set_queue
);
1273 return; /* thread and its waitq locked */
1276 wait_queue_unlock(set_queue
);
1280 * Otherwise, its a thread. If it is waiting on
1281 * the event we are posting to this queue, return
1282 * it locked, but leave it on the queue.
1284 thread_t t
= (thread_t
)wq_element
;
1286 if (t
->wait_event
== event
) {
1293 wq_element
= wqe_next
;
1298 * Routine: wait_queue_pull_thread_locked
1300 * Pull a thread that was previously "peeked" off the wait
1301 * queue and (possibly) unlock the waitq.
1307 * with the thread still locked.
1310 wait_queue_pull_thread_locked(
1316 assert(thread
->wait_queue
== waitq
);
1318 remqueue(&waitq
->wq_queue
, (queue_entry_t
)thread
);
1319 thread
->wait_queue
= WAIT_QUEUE_NULL
;
1320 thread
->wait_event
= NO_EVENT64
;
1321 thread
->at_safe_point
= FALSE
;
1323 wait_queue_unlock(waitq
);
1328 * Routine: wait_queue_select64_thread
1330 * Look for a thread and remove it from the queues, if
1331 * (and only if) the thread is waiting on the supplied
1332 * <wait_queue, event> pair.
1336 * possibly recursive
1338 * KERN_NOT_WAITING: Thread is not waiting here.
1339 * KERN_SUCCESS: It was, and is now removed (returned locked)
1341 static kern_return_t
1342 _wait_queue_select64_thread(
1347 wait_queue_element_t wq_element
;
1348 wait_queue_element_t wqe_next
;
1349 kern_return_t res
= KERN_NOT_WAITING
;
1350 queue_t q
= &wq
->wq_queue
;
1352 thread_lock(thread
);
1353 if ((thread
->wait_queue
== wq
) && (thread
->wait_event
== event
)) {
1354 remqueue(q
, (queue_entry_t
) thread
);
1355 thread
->at_safe_point
= FALSE
;
1356 thread
->wait_event
= NO_EVENT64
;
1357 thread
->wait_queue
= WAIT_QUEUE_NULL
;
1358 /* thread still locked */
1359 return KERN_SUCCESS
;
1361 thread_unlock(thread
);
1364 * The wait_queue associated with the thread may be one of this
1365 * wait queue's sets. Go see. If so, removing it from
1366 * there is like removing it from here.
1368 wq_element
= (wait_queue_element_t
) queue_first(q
);
1369 while (!queue_end(q
, (queue_entry_t
)wq_element
)) {
1370 WAIT_QUEUE_ELEMENT_CHECK(wq
, wq_element
);
1371 wqe_next
= (wait_queue_element_t
)
1372 queue_next((queue_t
) wq_element
);
1374 if (wq_element
->wqe_type
== WAIT_QUEUE_LINK
) {
1375 wait_queue_link_t wql
= (wait_queue_link_t
)wq_element
;
1376 wait_queue_t set_queue
;
1378 set_queue
= (wait_queue_t
)wql
->wql_setqueue
;
1379 wait_queue_lock(set_queue
);
1380 if (! wait_queue_empty(set_queue
)) {
1381 res
= _wait_queue_select64_thread(set_queue
,
1385 wait_queue_unlock(set_queue
);
1386 if (res
== KERN_SUCCESS
)
1387 return KERN_SUCCESS
;
1389 wq_element
= wqe_next
;
1396 * Routine: wait_queue_wakeup64_identity_locked
1398 * Select a single thread that is most-eligible to run and set
1399 * set it running. But return the thread locked.
1404 * possibly recursive
1406 * a pointer to the locked thread that was awakened
1408 __private_extern__ thread_t
1409 wait_queue_wakeup64_identity_locked(
1412 wait_result_t result
,
1418 assert(wait_queue_held(wq
));
1421 thread
= _wait_queue_select64_one(wq
, event
);
1423 wait_queue_unlock(wq
);
1426 res
= thread_go_locked(thread
, result
);
1427 assert(res
== KERN_SUCCESS
);
1429 return thread
; /* still locked if not NULL */
1434 * Routine: wait_queue_wakeup64_one_locked
1436 * Select a single thread that is most-eligible to run and set
1442 * possibly recursive
1444 * KERN_SUCCESS: It was, and is, now removed.
1445 * KERN_NOT_WAITING - No thread was waiting <wq,event> pair
1447 __private_extern__ kern_return_t
1448 wait_queue_wakeup64_one_locked(
1451 wait_result_t result
,
1456 assert(wait_queue_held(wq
));
1458 thread
= _wait_queue_select64_one(wq
, event
);
1460 wait_queue_unlock(wq
);
1465 res
= thread_go_locked(thread
, result
);
1466 assert(res
== KERN_SUCCESS
);
1467 thread_unlock(thread
);
1471 return KERN_NOT_WAITING
;
1475 * Routine: wait_queue_wakeup_one
1477 * Wakeup the most appropriate thread that is in the specified
1478 * wait queue for the specified event.
1482 * KERN_SUCCESS - Thread was woken up
1483 * KERN_NOT_WAITING - No thread was waiting <wq,event> pair
1486 wait_queue_wakeup_one(
1489 wait_result_t result
)
1494 if (!wait_queue_is_valid(wq
)) {
1495 return KERN_INVALID_ARGUMENT
;
1499 wait_queue_lock(wq
);
1500 thread
= _wait_queue_select64_one(wq
, (event64_t
)((uint32_t)event
));
1501 wait_queue_unlock(wq
);
1506 res
= thread_go_locked(thread
, result
);
1507 assert(res
== KERN_SUCCESS
);
1508 thread_unlock(thread
);
1514 return KERN_NOT_WAITING
;
1518 * Routine: wait_queue_wakeup64_one
1520 * Wakeup the most appropriate thread that is in the specified
1521 * wait queue for the specified event.
1525 * KERN_SUCCESS - Thread was woken up
1526 * KERN_NOT_WAITING - No thread was waiting <wq,event> pair
1529 wait_queue_wakeup64_one(
1532 wait_result_t result
)
1537 if (!wait_queue_is_valid(wq
)) {
1538 return KERN_INVALID_ARGUMENT
;
1541 wait_queue_lock(wq
);
1542 thread
= _wait_queue_select64_one(wq
, event
);
1543 wait_queue_unlock(wq
);
1548 res
= thread_go_locked(thread
, result
);
1549 assert(res
== KERN_SUCCESS
);
1550 thread_unlock(thread
);
1556 return KERN_NOT_WAITING
;
1561 * Routine: wait_queue_wakeup64_thread_locked
1563 * Wakeup the particular thread that was specified if and only
1564 * it was in this wait queue (or one of it's set queues)
1565 * and waiting on the specified event.
1567 * This is much safer than just removing the thread from
1568 * whatever wait queue it happens to be on. For instance, it
1569 * may have already been awoken from the wait you intended to
1570 * interrupt and waited on something else (like another
1574 * wait queue already locked (may be released).
1576 * KERN_SUCCESS - the thread was found waiting and awakened
1577 * KERN_NOT_WAITING - the thread was not waiting here
1579 __private_extern__ kern_return_t
1580 wait_queue_wakeup64_thread_locked(
1584 wait_result_t result
,
1589 assert(wait_queue_held(wq
));
1592 * See if the thread was still waiting there. If so, it got
1593 * dequeued and returned locked.
1595 res
= _wait_queue_select64_thread(wq
, event
, thread
);
1597 wait_queue_unlock(wq
);
1599 if (res
!= KERN_SUCCESS
)
1600 return KERN_NOT_WAITING
;
1602 res
= thread_go_locked(thread
, result
);
1603 assert(res
== KERN_SUCCESS
);
1604 thread_unlock(thread
);
1609 * Routine: wait_queue_wakeup_thread
1611 * Wakeup the particular thread that was specified if and only
1612 * it was in this wait queue (or one of it's set queues)
1613 * and waiting on the specified event.
1615 * This is much safer than just removing the thread from
1616 * whatever wait queue it happens to be on. For instance, it
1617 * may have already been awoken from the wait you intended to
1618 * interrupt and waited on something else (like another
1621 * nothing of interest locked
1622 * we need to assume spl needs to be raised
1624 * KERN_SUCCESS - the thread was found waiting and awakened
1625 * KERN_NOT_WAITING - the thread was not waiting here
1628 wait_queue_wakeup_thread(
1632 wait_result_t result
)
1637 if (!wait_queue_is_valid(wq
)) {
1638 return KERN_INVALID_ARGUMENT
;
1642 wait_queue_lock(wq
);
1643 res
= _wait_queue_select64_thread(wq
, (event64_t
)((uint32_t)event
), thread
);
1644 wait_queue_unlock(wq
);
1646 if (res
== KERN_SUCCESS
) {
1647 res
= thread_go_locked(thread
, result
);
1648 assert(res
== KERN_SUCCESS
);
1649 thread_unlock(thread
);
1654 return KERN_NOT_WAITING
;
1658 * Routine: wait_queue_wakeup64_thread
1660 * Wakeup the particular thread that was specified if and only
1661 * it was in this wait queue (or one of it's set's queues)
1662 * and waiting on the specified event.
1664 * This is much safer than just removing the thread from
1665 * whatever wait queue it happens to be on. For instance, it
1666 * may have already been awoken from the wait you intended to
1667 * interrupt and waited on something else (like another
1670 * nothing of interest locked
1671 * we need to assume spl needs to be raised
1673 * KERN_SUCCESS - the thread was found waiting and awakened
1674 * KERN_NOT_WAITING - the thread was not waiting here
1677 wait_queue_wakeup64_thread(
1681 wait_result_t result
)
1686 if (!wait_queue_is_valid(wq
)) {
1687 return KERN_INVALID_ARGUMENT
;
1691 wait_queue_lock(wq
);
1692 res
= _wait_queue_select64_thread(wq
, event
, thread
);
1693 wait_queue_unlock(wq
);
1695 if (res
== KERN_SUCCESS
) {
1696 res
= thread_go_locked(thread
, result
);
1697 assert(res
== KERN_SUCCESS
);
1698 thread_unlock(thread
);
1703 return KERN_NOT_WAITING
;