2 * Copyright (c) 2000 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@
22 #ifndef _KERN_WAIT_QUEUE_H_
23 #define _KERN_WAIT_QUEUE_H_
25 #include <kern/kern_types.h> /* for wait_queue_t */
26 #include <mach/sync_policy.h>
28 #ifdef MACH_KERNEL_PRIVATE
30 #include <kern/lock.h>
31 #include <kern/queue.h>
35 * This is the definition of the common event wait queue
36 * that the scheduler APIs understand. It is used
37 * internally by the gerneralized event waiting mechanism
38 * (assert_wait), and also for items that maintain their
39 * own wait queues (such as ports and semaphores).
41 * It is not published to other kernel components. They
42 * can create wait queues by calling wait_queue_alloc.
44 * NOTE: Hardware locks are used to protect event wait
45 * queues since interrupt code is free to post events to
48 typedef struct wait_queue
{
49 hw_lock_data_t wq_interlock
; /* interlock */
50 unsigned int /* flags */
51 /* boolean_t */ wq_fifo
:1, /* fifo wakeup policy? */
52 wq_issub
:1, /* is waitq linked? */
53 :0; /* force to long boundary */
54 queue_head_t wq_queue
; /* queue of elements */
59 * This is the common definition for a subordinate wait queue.
60 * These can be linked as members/elements of multiple regular
61 * wait queues. They have an additional set of linkages to
62 * identify the linkage structures that point to them.
64 typedef struct wait_queue_sub
{
65 WaitQueue wqs_wait_queue
; /* our wait queue */
66 queue_head_t wqs_sublinks
; /* links from sub perspective */
69 typedef WaitQueueSub
*wait_queue_sub_t
;
71 #define WAIT_QUEUE_SUB_NULL ((wait_queue_sub_t)0)
75 * wait_queue_element_t
76 * This structure describes the elements on an event wait
77 * queue. It is the common first fields in a thread shuttle
78 * and wait_queue_link_t. In that way, a wait queue can
79 * consist of both thread shuttle elements and links off of
80 * to other (subordinate) wait queues.
82 * WARNING: The first three fields of the thread shuttle
83 * definition does not use this definition yet. Any change in
84 * the layout here will have to be matched with a change there.
86 typedef struct wait_queue_element
{
87 queue_chain_t wqe_links
; /* link of elements on this queue */
88 wait_queue_t wqe_queue
; /* queue this element is on */
89 event_t wqe_event
; /* event this element is waiting for */
90 } *wait_queue_element_t
;
94 * Specialized wait queue element type for linking subordinate
95 * event waits queues onto a wait queue. In this way, an event
96 * can be constructed so that any thread waiting on any number
97 * of associated wait queues can handle the event, while letting
98 * the thread only be linked on the single wait queue it blocked on.
100 * One use: ports in multiple portsets. Each thread is queued up
101 * on the portset that it specifically blocked on during a receive
102 * operation. Each port's event queue links in all the portset
103 * event queues of which it is a member. An IPC event post associated
104 * with that port may wake up any thread from any of those portsets,
105 * or one that was waiting locally on the port itself.
107 typedef struct wait_queue_link
{
108 struct wait_queue_element wql_element
; /* element on master */
109 wait_queue_sub_t wql_subqueue
; /* sub queue */
110 queue_chain_t wql_sublinks
; /* element on sub */
111 } *wait_queue_link_t
;
113 #define WAIT_QUEUE_LINK_NULL ((wait_queue_link_t)0)
115 #define wql_links wql_element.wqe_links
116 #define wql_queue wql_element.wqe_queue
117 #define wql_event wql_element.wqe_event
119 extern int wait_queue_subordinate
;
120 #define WAIT_QUEUE_SUBORDINATE &_wait_queue_subordinate
122 extern void wait_queue_init(
123 wait_queue_t wait_queue
,
126 extern kern_return_t
wait_queue_link(
127 wait_queue_t wait_queue
,
128 wait_queue_sub_t subordinate_queue
);
130 extern kern_return_t
wait_queue_unlink(
131 wait_queue_t wait_queue
,
132 wait_queue_sub_t subordinate_queue
);
133 extern void wait_queue_unlink_one(
134 wait_queue_t wait_queue
,
135 wait_queue_sub_t
*subordinate_queue_pointer
);
137 extern boolean_t
wait_queue_member_queue(
138 wait_queue_t wait_queue
,
139 wait_queue_sub_t subordinate_queue
);
141 extern kern_return_t
clear_wait_queue_internal(
145 extern kern_return_t
wait_queue_remove(
148 #define wait_queue_assert_possible(thread) \
149 ((thread)->wait_queue == WAIT_QUEUE_NULL)
152 #define wait_queue_empty(wq) (queue_empty(&(wq)->wq_queue))
154 #define wait_queue_held(wq) (hw_lock_held(&(wq)->wq_interlock))
156 #define wait_queue_is_sub(wqs) ((wqs)->wqs_wait_queue.wq_issub)
157 #define wqs_lock(wqs) wait_queue_lock(&(wqs)->wqs_wait_queue)
158 #define wqs_unlock(wqs) wait_queue_unlock(&(wqs)->wqs_wait_queue)
159 #define wqs_lock_try(wqs) wait_queue__try_lock(&(wqs)->wqs_wait_queue)
161 /******** Decomposed interfaces (to build higher level constructs) ***********/
163 extern void wait_queue_lock(
164 wait_queue_t wait_queue
);
166 extern void wait_queue_unlock(
167 wait_queue_t wait_queue
);
169 extern boolean_t
wait_queue_lock_try(
170 wait_queue_t wait_queue
);
172 /* assert intent to wait on a locked wait queue */
173 extern void wait_queue_assert_wait_locked(
174 wait_queue_t wait_queue
,
179 /* peek to see which thread would be chosen for a wakeup - but keep on queue */
180 extern void wait_queue_peek_locked(
181 wait_queue_t wait_queue
,
184 wait_queue_t
*found_queue
);
186 /* peek to see which thread would be chosen for a wakeup - but keep on queue */
187 extern void wait_queue_pull_thread_locked(
188 wait_queue_t wait_queue
,
192 /* wakeup all threads waiting for a particular event on locked queue */
193 extern kern_return_t
wait_queue_wakeup_one_locked(
194 wait_queue_t wait_queue
,
199 /* wakeup one thread waiting for a particular event on locked queue */
200 extern kern_return_t
wait_queue_wakeup_one_locked(
201 wait_queue_t wait_queue
,
206 /* return the identity of a thread that is waiting for <wait_queue, event> */
207 extern thread_t
wait_queue_recommend_locked(
208 wait_queue_t wait_queue
,
211 /* return identity of a thread awakened for a particular <wait_queue,event> */
212 extern thread_t
wait_queue_wakeup_identity_locked(
213 wait_queue_t wait_queue
,
218 /* wakeup thread iff its still waiting for a particular event on locked queue */
219 extern kern_return_t
wait_queue_wakeup_thread_locked(
220 wait_queue_t wait_queue
,
226 #endif /* MACH_KERNEL_PRIVATE */
228 extern wait_queue_t
wait_queue_alloc(
231 extern void wait_queue_free(
232 wait_queue_t wait_queue
);
234 /******** Standalone interfaces (not a part of a higher construct) ************/
236 /* assert intent to wait on <wait_queue,event> pair */
237 extern void wait_queue_assert_wait(
238 wait_queue_t wait_queue
,
242 /* wakeup the most appropriate thread waiting on <wait_queue,event> pair */
243 extern kern_return_t
wait_queue_wakeup_one(
244 wait_queue_t wait_queue
,
248 /* wakeup all the threads waiting on <wait_queue,event> pair */
249 extern kern_return_t
wait_queue_wakeup_all(
250 wait_queue_t wait_queue
,
254 /* wakeup a specified thread waiting iff waiting on <wait_queue,event> pair */
255 extern kern_return_t
wait_queue_wakeup_thread(
256 wait_queue_t wait_queue
,
261 #endif /* _KERN_WAIT_QUEUE_H_ */