2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
59 * Scheduling primitive definitions file
63 #ifndef _KERN_SCHED_PRIM_H_
64 #define _KERN_SCHED_PRIM_H_
66 #include <mach/boolean.h>
67 #include <mach/machine/vm_types.h>
68 #include <mach/kern_return.h>
69 #include <kern/clock.h>
70 #include <kern/kern_types.h>
71 #include <kern/thread.h>
72 #include <kern/lock.h>
73 #include <kern/time_out.h> /*** ??? temp - remove me soon ***/
74 #include <kern/cpu_data.h>
76 #include <sys/appleapiopts.h>
78 #ifdef __APPLE_API_PRIVATE
80 #ifdef MACH_KERNEL_PRIVATE
82 #include <mach_ldebug.h>
84 * Exported interface to sched_prim.c.
85 * A few of these functions are actually defined in
86 * ipc_sched.c, for historical reasons.
89 /* Initialize scheduler module */
90 extern void sched_init(void);
92 extern void sched_timebase_init(void);
95 * Set up thread timeout element(s) when thread is created.
97 extern void thread_timer_setup(
100 extern void thread_timer_terminate(void);
103 * Stop a thread and wait for it to stop running.
105 extern boolean_t
thread_stop(
109 * Wait for a thread to stop running.
111 extern boolean_t
thread_wait(
114 /* Select a thread to run on a particular processor */
115 extern thread_t
thread_select(
116 processor_t myprocessor
);
118 extern kern_return_t
thread_go_locked(
120 wait_result_t result
);
122 /* Stop old thread and run new thread */
123 extern boolean_t
thread_invoke(
127 thread_continue_t continuation
);
129 /* Called when current thread is given new stack */
130 extern void thread_continue(
131 thread_t old_thread
);
133 /* Switch directly to a particular thread */
134 extern int thread_run(
136 thread_continue_t continuation
,
137 thread_t new_thread
);
139 /* Dispatch a thread not on a run queue */
140 extern void thread_dispatch(
143 /* Invoke continuation */
144 extern void call_continuation(
145 thread_continue_t continuation
);
147 /* Set the current scheduled priority */
148 extern void set_sched_pri(
152 /* Set base priority of the specified thread */
153 extern void set_priority(
157 /* Reset scheduled priority of thread */
158 extern void compute_priority(
160 boolean_t override_depress
);
162 /* Adjust scheduled priority of thread during execution */
163 extern void compute_my_priority(
166 /* Periodic scheduler activity */
167 extern void sched_tick_init(void);
170 * Update thread to the current scheduler tick.
172 extern void update_priority(
175 /* Idle thread loop */
176 extern void idle_thread(void);
179 * Machine-dependent code must define these functions.
182 /* Start thread running */
183 extern void thread_bootstrap_return(void);
185 /* Return from exception */
186 extern void thread_exception_return(void);
188 /* Continuation return from syscall */
189 extern void thread_syscall_return(
193 * These functions are either defined in kern/thread.c
194 * or are defined directly by machine-dependent code.
197 /* Block current thread, indicating reason */
198 extern wait_result_t
thread_block_reason(
199 thread_continue_t continuation
,
202 /* Dispatch a thread for execution */
203 extern void thread_setrun(
207 #define SCHED_TAILQ 0
208 #define SCHED_HEADQ 1
209 #define SCHED_PREEMPT 2
211 /* Bind thread to a particular processor */
212 extern processor_t
thread_bind(
214 processor_t processor
);
216 /* Set the maximum interrupt level for the thread */
217 __private_extern__ wait_interrupt_t
thread_interrupt_level(
218 wait_interrupt_t interruptible
);
220 __private_extern__ wait_result_t
thread_mark_wait_locked(
222 wait_interrupt_t interruptible
);
224 /* Sleep, unlocking and then relocking a usimple_lock in the process */
225 __private_extern__ wait_result_t
thread_sleep_fast_usimple_lock(
228 wait_interrupt_t interruptible
);
230 /* Wake up locked thread directly, passing result */
231 __private_extern__ kern_return_t
clear_wait_internal(
233 wait_result_t result
);
236 wait_queue_t
wait_event_wait_queue(
239 #endif /* MACH_KERNEL_PRIVATE */
241 extern wait_result_t
assert_wait_prim(
243 thread_roust_t roust_hint
,
245 wait_interrupt_t interruptible
);
248 ****************** Only exported until BSD stops using ********************
252 * Cancel a stop and unblock the thread if already stopped.
254 extern void thread_unstop(
257 /* Wake up thread directly, passing result */
258 extern kern_return_t
clear_wait(
260 wait_result_t result
);
262 #endif /* __APPLE_API_PRIVATE */
265 * ********************* PUBLIC APIs ************************************
268 /* Set timer for current thread */
269 extern void thread_set_timer(
271 uint32_t scale_factor
);
273 extern void thread_set_timer_deadline(
276 extern void thread_cancel_timer(void);
278 /* Declare thread will wait on a particular event */
279 extern wait_result_t
assert_wait(
281 wait_interrupt_t interruptflag
);
283 /* Assert that the thread intends to wait for a timeout */
284 extern wait_result_t
assert_wait_timeout(
286 wait_interrupt_t interruptflags
);
288 /* Sleep, unlocking and then relocking a usimple_lock in the process */
289 extern wait_result_t
thread_sleep_usimple_lock(
292 wait_interrupt_t interruptible
);
294 /* Sleep, unlocking and then relocking a mutex in the process */
295 extern wait_result_t
thread_sleep_mutex(
298 wait_interrupt_t interruptible
);
300 /* Sleep with a deadline, unlocking and then relocking a mutex in the process */
301 extern wait_result_t
thread_sleep_mutex_deadline(
305 wait_interrupt_t interruptible
);
307 /* Sleep, unlocking and then relocking a write lock in the process */
308 extern wait_result_t
thread_sleep_lock_write(
311 wait_interrupt_t interruptible
);
313 /* Sleep, hinting that a thread funnel may be involved in the process */
314 extern wait_result_t
thread_sleep_funnel(
316 wait_interrupt_t interruptible
);
318 /* Wake up thread (or threads) waiting on a particular event */
319 extern kern_return_t
thread_wakeup_prim(
321 boolean_t one_thread
,
322 wait_result_t result
);
324 #ifdef __APPLE_API_UNSTABLE
326 /* Block current thread (Block reason) */
327 extern wait_result_t
thread_block(
328 thread_continue_t continuation
);
330 #endif /* __APPLE_API_UNSTABLE */
333 * Routines defined as macros
336 #define thread_wakeup(x) \
337 thread_wakeup_prim((x), FALSE, THREAD_AWAKENED)
338 #define thread_wakeup_with_result(x, z) \
339 thread_wakeup_prim((x), FALSE, (z))
340 #define thread_wakeup_one(x) \
341 thread_wakeup_prim((x), TRUE, THREAD_AWAKENED)
343 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
345 #include <libkern/OSBase.h>
347 #define thread_set_timer_deadline(a) \
348 thread_set_timer_deadline(__OSAbsoluteTime(a))
352 #endif /* _KERN_SCHED_PRIM_H_ */