]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/wait_queue.h
xnu-792.6.22.tar.gz
[apple/xnu.git] / osfmk / kern / wait_queue.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
1c79356b 22
91447636 23#ifdef KERNEL_PRIVATE
9bccf70c 24
91447636
A
25#ifndef _KERN_WAIT_QUEUE_H_
26#define _KERN_WAIT_QUEUE_H_
9bccf70c 27
91447636 28#include <mach/mach_types.h>
1c79356b 29#include <mach/sync_policy.h>
0b4e3aa0 30#include <mach/kern_return.h> /* for kern_return_t */
1c79356b 31
9bccf70c
A
32#include <kern/kern_types.h> /* for wait_queue_t */
33
91447636
A
34#include <sys/cdefs.h>
35
36#ifdef MACH_KERNEL_PRIVATE
1c79356b
A
37
38#include <kern/lock.h>
39#include <kern/queue.h>
91447636 40#include <machine/cpu_number.h>
9bccf70c 41
1c79356b
A
42/*
43 * wait_queue_t
44 * This is the definition of the common event wait queue
45 * that the scheduler APIs understand. It is used
46 * internally by the gerneralized event waiting mechanism
47 * (assert_wait), and also for items that maintain their
48 * own wait queues (such as ports and semaphores).
49 *
50 * It is not published to other kernel components. They
51 * can create wait queues by calling wait_queue_alloc.
52 *
53 * NOTE: Hardware locks are used to protect event wait
54 * queues since interrupt code is free to post events to
55 * them.
56 */
57typedef struct wait_queue {
9bccf70c
A
58 unsigned int /* flags */
59 /* boolean_t */ wq_type:16, /* only public field */
60 wq_fifo:1, /* fifo wakeup policy? */
61 wq_isprepost:1, /* is waitq preposted? set only */
62 :0; /* force to long boundary */
1c79356b 63 hw_lock_data_t wq_interlock; /* interlock */
9bccf70c 64 queue_head_t wq_queue; /* queue of elements */
1c79356b
A
65} WaitQueue;
66
67/*
9bccf70c
A
68 * wait_queue_set_t
69 * This is the common definition for a set wait queue.
1c79356b
A
70 * These can be linked as members/elements of multiple regular
71 * wait queues. They have an additional set of linkages to
72 * identify the linkage structures that point to them.
73 */
9bccf70c
A
74typedef struct wait_queue_set {
75 WaitQueue wqs_wait_queue; /* our wait queue */
76 queue_head_t wqs_setlinks; /* links from set perspective */
0b4e3aa0 77 unsigned int wqs_refcount; /* refcount for preposting */
9bccf70c 78} WaitQueueSet;
1c79356b 79
9bccf70c
A
80#define wqs_type wqs_wait_queue.wq_type
81#define wqs_fifo wqs_wait_queue.wq_fifo
82#define wqs_isprepost wqs_wait_queue.wq_isprepost
83#define wqs_queue wqs_wait_queue.wq_queue
1c79356b
A
84
85/*
86 * wait_queue_element_t
87 * This structure describes the elements on an event wait
88 * queue. It is the common first fields in a thread shuttle
89 * and wait_queue_link_t. In that way, a wait queue can
90 * consist of both thread shuttle elements and links off of
9bccf70c 91 * to other (set) wait queues.
1c79356b 92 *
9bccf70c
A
93 * WARNING: These fields correspond to fields in the thread
94 * shuttle (run queue links and run queue pointer). Any change in
1c79356b
A
95 * the layout here will have to be matched with a change there.
96 */
97typedef struct wait_queue_element {
98 queue_chain_t wqe_links; /* link of elements on this queue */
9bccf70c 99 void * wqe_type; /* Identifies link vs. thread */
1c79356b 100 wait_queue_t wqe_queue; /* queue this element is on */
9bccf70c 101} WaitQueueElement;
1c79356b 102
9bccf70c 103typedef WaitQueueElement *wait_queue_element_t;
0b4e3aa0 104
1c79356b
A
105/*
106 * wait_queue_link_t
9bccf70c 107 * Specialized wait queue element type for linking set
1c79356b
A
108 * event waits queues onto a wait queue. In this way, an event
109 * can be constructed so that any thread waiting on any number
110 * of associated wait queues can handle the event, while letting
111 * the thread only be linked on the single wait queue it blocked on.
112 *
113 * One use: ports in multiple portsets. Each thread is queued up
114 * on the portset that it specifically blocked on during a receive
115 * operation. Each port's event queue links in all the portset
116 * event queues of which it is a member. An IPC event post associated
117 * with that port may wake up any thread from any of those portsets,
118 * or one that was waiting locally on the port itself.
119 */
120typedef struct wait_queue_link {
9bccf70c
A
121 WaitQueueElement wql_element; /* element on master */
122 queue_chain_t wql_setlinks; /* element on set */
123 wait_queue_set_t wql_setqueue; /* set queue */
0b4e3aa0
A
124} WaitQueueLink;
125
1c79356b 126#define wql_links wql_element.wqe_links
9bccf70c 127#define wql_type wql_element.wqe_type
1c79356b 128#define wql_queue wql_element.wqe_queue
0b4e3aa0 129
9bccf70c
A
130#define _WAIT_QUEUE_inited 0xf1d0
131#define _WAIT_QUEUE_SET_inited 0xf1d1
0b4e3aa0 132
9bccf70c
A
133#define wait_queue_is_queue(wq) \
134 ((wq)->wq_type == _WAIT_QUEUE_inited)
1c79356b 135
9bccf70c
A
136#define wait_queue_is_set(wqs) \
137 ((wqs)->wqs_type == _WAIT_QUEUE_SET_inited)
1c79356b 138
9bccf70c
A
139#define wait_queue_is_valid(wq) \
140 (((wq)->wq_type & ~1) == _WAIT_QUEUE_inited)
1c79356b 141
9bccf70c
A
142#define wait_queue_empty(wq) (queue_empty(&(wq)->wq_queue))
143#define wait_queue_held(wq) (hw_lock_held(&(wq)->wq_interlock))
144#define wait_queue_lock_try(wq) (hw_lock_try(&(wq)->wq_interlock))
1c79356b 145
9bccf70c
A
146/*
147 * Double the standard lock timeout, because wait queues tend
148 * to iterate over a number of threads - locking each. If there is
149 * a problem with a thread lock, it normally times out at the wait
150 * queue level first, hiding the real problem.
151 */
152#define wait_queue_lock(wq) \
153 ((void) (!hw_lock_to(&(wq)->wq_interlock, LockTimeOut * 2) ? \
154 panic("wait queue deadlock - wq=0x%x, cpu=%d\n", \
155 wq, cpu_number()) : 0))
1c79356b 156
9bccf70c
A
157#define wait_queue_unlock(wq) \
158 (assert(wait_queue_held(wq)), hw_lock_unlock(&(wq)->wq_interlock))
1c79356b 159
9bccf70c
A
160#define wqs_lock(wqs) wait_queue_lock(&(wqs)->wqs_wait_queue)
161#define wqs_unlock(wqs) wait_queue_unlock(&(wqs)->wqs_wait_queue)
162#define wqs_lock_try(wqs) wait_queue__try_lock(&(wqs)->wqs_wait_queue)
1c79356b
A
163
164#define wait_queue_assert_possible(thread) \
165 ((thread)->wait_queue == WAIT_QUEUE_NULL)
166
1c79356b
A
167/******** Decomposed interfaces (to build higher level constructs) ***********/
168
1c79356b 169/* assert intent to wait on a locked wait queue */
9bccf70c 170__private_extern__ wait_result_t wait_queue_assert_wait64_locked(
1c79356b 171 wait_queue_t wait_queue,
9bccf70c
A
172 event64_t wait_event,
173 wait_interrupt_t interruptible,
91447636 174 uint64_t deadline,
55e303ae 175 thread_t thread);
1c79356b
A
176
177/* peek to see which thread would be chosen for a wakeup - but keep on queue */
9bccf70c 178__private_extern__ void wait_queue_peek64_locked(
1c79356b 179 wait_queue_t wait_queue,
9bccf70c 180 event64_t event,
1c79356b
A
181 thread_t *thread,
182 wait_queue_t *found_queue);
183
184/* peek to see which thread would be chosen for a wakeup - but keep on queue */
9bccf70c 185__private_extern__ void wait_queue_pull_thread_locked(
1c79356b
A
186 wait_queue_t wait_queue,
187 thread_t thread,
188 boolean_t unlock);
189
190/* wakeup all threads waiting for a particular event on locked queue */
9bccf70c 191__private_extern__ kern_return_t wait_queue_wakeup64_all_locked(
1c79356b 192 wait_queue_t wait_queue,
9bccf70c
A
193 event64_t wake_event,
194 wait_result_t result,
1c79356b
A
195 boolean_t unlock);
196
197/* wakeup one thread waiting for a particular event on locked queue */
9bccf70c 198__private_extern__ kern_return_t wait_queue_wakeup64_one_locked(
1c79356b 199 wait_queue_t wait_queue,
9bccf70c
A
200 event64_t wake_event,
201 wait_result_t result,
1c79356b
A
202 boolean_t unlock);
203
1c79356b 204/* return identity of a thread awakened for a particular <wait_queue,event> */
9bccf70c 205__private_extern__ thread_t wait_queue_wakeup64_identity_locked(
1c79356b 206 wait_queue_t wait_queue,
9bccf70c
A
207 event64_t wake_event,
208 wait_result_t result,
1c79356b
A
209 boolean_t unlock);
210
211/* wakeup thread iff its still waiting for a particular event on locked queue */
9bccf70c 212__private_extern__ kern_return_t wait_queue_wakeup64_thread_locked(
1c79356b 213 wait_queue_t wait_queue,
9bccf70c 214 event64_t wake_event,
1c79356b 215 thread_t thread,
9bccf70c 216 wait_result_t result,
1c79356b
A
217 boolean_t unlock);
218
91447636
A
219#endif /* MACH_KERNEL_PRIVATE */
220
221__BEGIN_DECLS
1c79356b 222
9bccf70c
A
223/******** Semi-Public interfaces (not a part of a higher construct) ************/
224
91447636
A
225extern unsigned int wait_queue_set_size(void);
226extern unsigned int wait_queue_link_size(void);
227
9bccf70c
A
228extern kern_return_t wait_queue_init(
229 wait_queue_t wait_queue,
230 int policy);
231
232extern wait_queue_set_t wait_queue_set_alloc(
233 int policy);
234
91447636
A
235extern kern_return_t wait_queue_set_init(
236 wait_queue_set_t set_queue,
237 int policy);
238
9bccf70c
A
239extern kern_return_t wait_queue_set_free(
240 wait_queue_set_t set_queue);
241
242extern wait_queue_link_t wait_queue_link_alloc(
243 int policy);
244
245extern kern_return_t wait_queue_link_free(
246 wait_queue_link_t link_element);
247
91447636
A
248extern kern_return_t wait_queue_link(
249 wait_queue_t wait_queue,
250 wait_queue_set_t set_queue);
9bccf70c 251
91447636
A
252extern kern_return_t wait_queue_link_noalloc(
253 wait_queue_t wait_queue,
254 wait_queue_set_t set_queue,
255 wait_queue_link_t link);
1c79356b 256
91447636 257extern boolean_t wait_queue_member(
9bccf70c
A
258 wait_queue_t wait_queue,
259 wait_queue_set_t set_queue);
260
261extern kern_return_t wait_queue_unlink(
262 wait_queue_t wait_queue,
263 wait_queue_set_t set_queue);
264
265extern kern_return_t wait_queue_unlink_all(
1c79356b
A
266 wait_queue_t wait_queue);
267
91447636
A
268extern kern_return_t wait_queue_unlinkall_nofree(
269 wait_queue_t wait_queue);
270
9bccf70c
A
271extern kern_return_t wait_queue_set_unlink_all(
272 wait_queue_set_t set_queue);
273
91447636
A
274/* legacy API */
275kern_return_t wait_queue_sub_init(
276 wait_queue_set_t set_queue,
277 int policy);
278
279kern_return_t wait_queue_sub_clearrefs(
280 wait_queue_set_t wq_set);
281
282extern kern_return_t wait_subqueue_unlink_all(
283 wait_queue_set_t set_queue);
284
285extern wait_queue_t wait_queue_alloc(
286 int policy);
287
288extern kern_return_t wait_queue_free(
289 wait_queue_t wait_queue);
290
9bccf70c
A
291/* assert intent to wait on <wait_queue,event64> pair */
292extern wait_result_t wait_queue_assert_wait64(
293 wait_queue_t wait_queue,
294 event64_t wait_event,
91447636
A
295 wait_interrupt_t interruptible,
296 uint64_t deadline);
9bccf70c
A
297
298/* wakeup the most appropriate thread waiting on <wait_queue,event64> pair */
299extern kern_return_t wait_queue_wakeup64_one(
300 wait_queue_t wait_queue,
301 event64_t wake_event,
302 wait_result_t result);
303
304/* wakeup all the threads waiting on <wait_queue,event64> pair */
305extern kern_return_t wait_queue_wakeup64_all(
306 wait_queue_t wait_queue,
307 event64_t wake_event,
308 wait_result_t result);
309
310/* wakeup a specified thread waiting iff waiting on <wait_queue,event64> pair */
311extern kern_return_t wait_queue_wakeup64_thread(
312 wait_queue_t wait_queue,
313 event64_t wake_event,
314 thread_t thread,
315 wait_result_t result);
316
9bccf70c
A
317/*
318 * Compatibility Wait Queue APIs based on pointer events instead of 64bit
319 * integer events.
320 */
1c79356b
A
321
322/* assert intent to wait on <wait_queue,event> pair */
9bccf70c 323extern wait_result_t wait_queue_assert_wait(
1c79356b
A
324 wait_queue_t wait_queue,
325 event_t wait_event,
91447636
A
326 wait_interrupt_t interruptible,
327 uint64_t deadline);
1c79356b
A
328
329/* wakeup the most appropriate thread waiting on <wait_queue,event> pair */
9bccf70c 330extern kern_return_t wait_queue_wakeup_one(
1c79356b
A
331 wait_queue_t wait_queue,
332 event_t wake_event,
9bccf70c 333 wait_result_t result);
1c79356b
A
334
335/* wakeup all the threads waiting on <wait_queue,event> pair */
9bccf70c 336extern kern_return_t wait_queue_wakeup_all(
1c79356b
A
337 wait_queue_t wait_queue,
338 event_t wake_event,
9bccf70c 339 wait_result_t result);
1c79356b
A
340
341/* wakeup a specified thread waiting iff waiting on <wait_queue,event> pair */
9bccf70c 342extern kern_return_t wait_queue_wakeup_thread(
1c79356b
A
343 wait_queue_t wait_queue,
344 event_t wake_event,
345 thread_t thread,
9bccf70c
A
346 wait_result_t result);
347
91447636
A
348__END_DECLS
349
350#endif /* _KERN_WAIT_QUEUE_H_ */
1c79356b 351
91447636 352#endif /* KERNEL_PRIVATE */