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 <sys/appleapiopts.h>
27 #ifdef __APPLE_API_PRIVATE
29 #include <mach/sync_policy.h>
30 #include <mach/kern_return.h> /* for kern_return_t */
32 #include <kern/kern_types.h> /* for wait_queue_t */
34 #ifdef MACH_KERNEL_PRIVATE
36 #include <kern/lock.h>
37 #include <kern/queue.h>
42 * This is the definition of the common event wait queue
43 * that the scheduler APIs understand. It is used
44 * internally by the gerneralized event waiting mechanism
45 * (assert_wait), and also for items that maintain their
46 * own wait queues (such as ports and semaphores).
48 * It is not published to other kernel components. They
49 * can create wait queues by calling wait_queue_alloc.
51 * NOTE: Hardware locks are used to protect event wait
52 * queues since interrupt code is free to post events to
55 typedef struct wait_queue
{
56 unsigned int /* flags */
57 /* boolean_t */ wq_type
:16, /* only public field */
58 wq_fifo
:1, /* fifo wakeup policy? */
59 wq_isprepost
:1, /* is waitq preposted? set only */
60 :0; /* force to long boundary */
61 hw_lock_data_t wq_interlock
; /* interlock */
62 queue_head_t wq_queue
; /* queue of elements */
67 * This is the common definition for a set wait queue.
68 * These can be linked as members/elements of multiple regular
69 * wait queues. They have an additional set of linkages to
70 * identify the linkage structures that point to them.
72 typedef struct wait_queue_set
{
73 WaitQueue wqs_wait_queue
; /* our wait queue */
74 queue_head_t wqs_setlinks
; /* links from set perspective */
75 unsigned int wqs_refcount
; /* refcount for preposting */
78 #define wqs_type wqs_wait_queue.wq_type
79 #define wqs_fifo wqs_wait_queue.wq_fifo
80 #define wqs_isprepost wqs_wait_queue.wq_isprepost
81 #define wqs_queue wqs_wait_queue.wq_queue
84 * wait_queue_element_t
85 * This structure describes the elements on an event wait
86 * queue. It is the common first fields in a thread shuttle
87 * and wait_queue_link_t. In that way, a wait queue can
88 * consist of both thread shuttle elements and links off of
89 * to other (set) wait queues.
91 * WARNING: These fields correspond to fields in the thread
92 * shuttle (run queue links and run queue pointer). Any change in
93 * the layout here will have to be matched with a change there.
95 typedef struct wait_queue_element
{
96 queue_chain_t wqe_links
; /* link of elements on this queue */
97 void * wqe_type
; /* Identifies link vs. thread */
98 wait_queue_t wqe_queue
; /* queue this element is on */
101 typedef WaitQueueElement
*wait_queue_element_t
;
105 * Specialized wait queue element type for linking set
106 * event waits queues onto a wait queue. In this way, an event
107 * can be constructed so that any thread waiting on any number
108 * of associated wait queues can handle the event, while letting
109 * the thread only be linked on the single wait queue it blocked on.
111 * One use: ports in multiple portsets. Each thread is queued up
112 * on the portset that it specifically blocked on during a receive
113 * operation. Each port's event queue links in all the portset
114 * event queues of which it is a member. An IPC event post associated
115 * with that port may wake up any thread from any of those portsets,
116 * or one that was waiting locally on the port itself.
118 typedef struct wait_queue_link
{
119 WaitQueueElement wql_element
; /* element on master */
120 queue_chain_t wql_setlinks
; /* element on set */
121 wait_queue_set_t wql_setqueue
; /* set queue */
124 #define wql_links wql_element.wqe_links
125 #define wql_type wql_element.wqe_type
126 #define wql_queue wql_element.wqe_queue
128 #define _WAIT_QUEUE_inited 0xf1d0
129 #define _WAIT_QUEUE_SET_inited 0xf1d1
131 #define wait_queue_is_queue(wq) \
132 ((wq)->wq_type == _WAIT_QUEUE_inited)
134 #define wait_queue_is_set(wqs) \
135 ((wqs)->wqs_type == _WAIT_QUEUE_SET_inited)
137 #define wait_queue_is_valid(wq) \
138 (((wq)->wq_type & ~1) == _WAIT_QUEUE_inited)
140 #define wait_queue_empty(wq) (queue_empty(&(wq)->wq_queue))
141 #define wait_queue_held(wq) (hw_lock_held(&(wq)->wq_interlock))
142 #define wait_queue_lock_try(wq) (hw_lock_try(&(wq)->wq_interlock))
145 * Double the standard lock timeout, because wait queues tend
146 * to iterate over a number of threads - locking each. If there is
147 * a problem with a thread lock, it normally times out at the wait
148 * queue level first, hiding the real problem.
150 #define wait_queue_lock(wq) \
151 ((void) (!hw_lock_to(&(wq)->wq_interlock, LockTimeOut * 2) ? \
152 panic("wait queue deadlock - wq=0x%x, cpu=%d\n", \
153 wq, cpu_number()) : 0))
155 #define wait_queue_unlock(wq) \
156 (assert(wait_queue_held(wq)), hw_lock_unlock(&(wq)->wq_interlock))
158 #define wqs_lock(wqs) wait_queue_lock(&(wqs)->wqs_wait_queue)
159 #define wqs_unlock(wqs) wait_queue_unlock(&(wqs)->wqs_wait_queue)
160 #define wqs_lock_try(wqs) wait_queue__try_lock(&(wqs)->wqs_wait_queue)
162 #define wait_queue_assert_possible(thread) \
163 ((thread)->wait_queue == WAIT_QUEUE_NULL)
165 /******** Decomposed interfaces (to build higher level constructs) ***********/
167 /* assert intent to wait on a locked wait queue */
168 __private_extern__ wait_result_t
wait_queue_assert_wait64_locked(
169 wait_queue_t wait_queue
,
170 event64_t wait_event
,
171 wait_interrupt_t interruptible
,
174 /* peek to see which thread would be chosen for a wakeup - but keep on queue */
175 __private_extern__
void wait_queue_peek64_locked(
176 wait_queue_t wait_queue
,
179 wait_queue_t
*found_queue
);
181 /* peek to see which thread would be chosen for a wakeup - but keep on queue */
182 __private_extern__
void wait_queue_pull_thread_locked(
183 wait_queue_t wait_queue
,
187 /* wakeup all threads waiting for a particular event on locked queue */
188 __private_extern__ kern_return_t
wait_queue_wakeup64_all_locked(
189 wait_queue_t wait_queue
,
190 event64_t wake_event
,
191 wait_result_t result
,
194 /* wakeup one thread waiting for a particular event on locked queue */
195 __private_extern__ kern_return_t
wait_queue_wakeup64_one_locked(
196 wait_queue_t wait_queue
,
197 event64_t wake_event
,
198 wait_result_t result
,
201 /* return identity of a thread awakened for a particular <wait_queue,event> */
202 __private_extern__ thread_t
wait_queue_wakeup64_identity_locked(
203 wait_queue_t wait_queue
,
204 event64_t wake_event
,
205 wait_result_t result
,
208 /* wakeup thread iff its still waiting for a particular event on locked queue */
209 __private_extern__ kern_return_t
wait_queue_wakeup64_thread_locked(
210 wait_queue_t wait_queue
,
211 event64_t wake_event
,
213 wait_result_t result
,
216 #endif /* MACH_KERNEL_PRIVATE */
218 #ifdef __APPLE_API_UNSTABLE
219 /******** Semi-Public interfaces (not a part of a higher construct) ************/
221 extern kern_return_t
wait_queue_init(
222 wait_queue_t wait_queue
,
225 extern wait_queue_set_t
wait_queue_set_alloc(
228 extern kern_return_t
wait_queue_set_free(
229 wait_queue_set_t set_queue
);
231 extern wait_queue_link_t
wait_queue_link_alloc(
234 extern kern_return_t
wait_queue_link_free(
235 wait_queue_link_t link_element
);
237 #endif /* __APPLE_API_UNSTABLE */
239 #ifdef __APPLE_API_EVOLVING
241 extern wait_queue_t
wait_queue_alloc(
244 extern kern_return_t
wait_queue_free(
245 wait_queue_t wait_queue
);
247 extern kern_return_t
wait_queue_link(
248 wait_queue_t wait_queue
,
249 wait_queue_set_t set_queue
);
251 extern kern_return_t
wait_queue_unlink(
252 wait_queue_t wait_queue
,
253 wait_queue_set_t set_queue
);
255 extern kern_return_t
wait_queue_unlink_all(
256 wait_queue_t wait_queue
);
258 extern kern_return_t
wait_queue_set_unlink_all(
259 wait_queue_set_t set_queue
);
261 /* assert intent to wait on <wait_queue,event64> pair */
262 extern wait_result_t
wait_queue_assert_wait64(
263 wait_queue_t wait_queue
,
264 event64_t wait_event
,
265 wait_interrupt_t interruptible
);
267 /* wakeup the most appropriate thread waiting on <wait_queue,event64> pair */
268 extern kern_return_t
wait_queue_wakeup64_one(
269 wait_queue_t wait_queue
,
270 event64_t wake_event
,
271 wait_result_t result
);
273 /* wakeup all the threads waiting on <wait_queue,event64> pair */
274 extern kern_return_t
wait_queue_wakeup64_all(
275 wait_queue_t wait_queue
,
276 event64_t wake_event
,
277 wait_result_t result
);
279 /* wakeup a specified thread waiting iff waiting on <wait_queue,event64> pair */
280 extern kern_return_t
wait_queue_wakeup64_thread(
281 wait_queue_t wait_queue
,
282 event64_t wake_event
,
284 wait_result_t result
);
286 #endif /* __APPLE_API_EVOLVING */
289 * Compatibility Wait Queue APIs based on pointer events instead of 64bit
293 /* assert intent to wait on <wait_queue,event> pair */
294 extern wait_result_t
wait_queue_assert_wait(
295 wait_queue_t wait_queue
,
297 wait_interrupt_t interruptible
);
299 /* wakeup the most appropriate thread waiting on <wait_queue,event> pair */
300 extern kern_return_t
wait_queue_wakeup_one(
301 wait_queue_t wait_queue
,
303 wait_result_t result
);
305 /* wakeup all the threads waiting on <wait_queue,event> pair */
306 extern kern_return_t
wait_queue_wakeup_all(
307 wait_queue_t wait_queue
,
309 wait_result_t result
);
311 /* wakeup a specified thread waiting iff waiting on <wait_queue,event> pair */
312 extern kern_return_t
wait_queue_wakeup_thread(
313 wait_queue_t wait_queue
,
316 wait_result_t result
);
318 #endif /* __APPLE_API_PRIVATE */
320 #endif /* _KERN_WAIT_QUEUE_H_ */