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@
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.
56 * Scheduling primitive definitions file
60 #ifndef _KERN_SCHED_PRIM_H_
61 #define _KERN_SCHED_PRIM_H_
63 #include <mach/boolean.h>
64 #include <mach/machine/vm_types.h>
65 #include <mach/kern_return.h>
66 #include <kern/clock.h>
67 #include <kern/kern_types.h>
68 #include <kern/thread.h>
69 #include <kern/lock.h>
70 #include <kern/time_out.h> /*** ??? temp - remove me soon ***/
71 #include <kern/cpu_data.h>
73 #include <sys/appleapiopts.h>
75 #ifdef __APPLE_API_PRIVATE
77 #ifdef MACH_KERNEL_PRIVATE
79 #include <mach_ldebug.h>
81 * Exported interface to sched_prim.c.
82 * A few of these functions are actually defined in
83 * ipc_sched.c, for historical reasons.
86 /* Initialize scheduler module */
87 extern void sched_init(void);
89 extern void sched_timebase_init(void);
92 * Set up thread timeout element(s) when thread is created.
94 extern void thread_timer_setup(
97 extern void thread_timer_terminate(void);
100 * Stop a thread and wait for it to stop running.
102 extern boolean_t
thread_stop(
106 * Wait for a thread to stop running.
108 extern boolean_t
thread_wait(
111 /* Select a thread to run on a particular processor */
112 extern thread_t
thread_select(
113 processor_t myprocessor
);
115 extern kern_return_t
thread_go_locked(
117 wait_result_t result
);
119 /* Stop old thread and run new thread */
120 extern boolean_t
thread_invoke(
124 thread_continue_t continuation
);
126 /* Called when current thread is given new stack */
127 extern void thread_continue(
128 thread_t old_thread
);
130 /* Switch directly to a particular thread */
131 extern int thread_run(
133 thread_continue_t continuation
,
134 thread_t new_thread
);
136 /* Dispatch a thread not on a run queue */
137 extern void thread_dispatch(
140 /* Invoke continuation */
141 extern void call_continuation(
142 thread_continue_t continuation
);
144 /* Set the current scheduled priority */
145 extern void set_sched_pri(
149 /* Set base priority of the specified thread */
150 extern void set_priority(
154 /* Reset scheduled priority of thread */
155 extern void compute_priority(
157 boolean_t override_depress
);
159 /* Adjust scheduled priority of thread during execution */
160 extern void compute_my_priority(
163 /* Periodic scheduler activity */
164 extern void sched_tick_init(void);
167 * Update thread to the current scheduler tick.
169 extern void update_priority(
172 /* Idle thread loop */
173 extern void idle_thread(void);
176 * Machine-dependent code must define these functions.
179 /* Start thread running */
180 extern void thread_bootstrap_return(void);
182 /* Return from exception */
183 extern void thread_exception_return(void);
185 /* Continuation return from syscall */
186 extern void thread_syscall_return(
190 * These functions are either defined in kern/thread.c
191 * or are defined directly by machine-dependent code.
194 /* Block current thread, indicating reason */
195 extern wait_result_t
thread_block_reason(
196 thread_continue_t continuation
,
199 /* Dispatch a thread for execution */
200 extern void thread_setrun(
204 #define SCHED_TAILQ 0
205 #define SCHED_HEADQ 1
206 #define SCHED_PREEMPT 2
208 /* Bind thread to a particular processor */
209 extern processor_t
thread_bind(
211 processor_t processor
);
213 /* Set the maximum interrupt level for the thread */
214 __private_extern__ wait_interrupt_t
thread_interrupt_level(
215 wait_interrupt_t interruptible
);
217 __private_extern__ wait_result_t
thread_mark_wait_locked(
219 wait_interrupt_t interruptible
);
221 /* Sleep, unlocking and then relocking a usimple_lock in the process */
222 __private_extern__ wait_result_t
thread_sleep_fast_usimple_lock(
225 wait_interrupt_t interruptible
);
227 /* Wake up locked thread directly, passing result */
228 __private_extern__ kern_return_t
clear_wait_internal(
230 wait_result_t result
);
233 wait_queue_t
wait_event_wait_queue(
236 #endif /* MACH_KERNEL_PRIVATE */
238 extern wait_result_t
assert_wait_prim(
240 thread_roust_t roust_hint
,
242 wait_interrupt_t interruptible
);
245 ****************** Only exported until BSD stops using ********************
249 * Cancel a stop and unblock the thread if already stopped.
251 extern void thread_unstop(
254 /* Wake up thread directly, passing result */
255 extern kern_return_t
clear_wait(
257 wait_result_t result
);
259 #endif /* __APPLE_API_PRIVATE */
262 * ********************* PUBLIC APIs ************************************
265 /* Set timer for current thread */
266 extern void thread_set_timer(
268 uint32_t scale_factor
);
270 extern void thread_set_timer_deadline(
273 extern void thread_cancel_timer(void);
275 /* Declare thread will wait on a particular event */
276 extern wait_result_t
assert_wait(
278 wait_interrupt_t interruptflag
);
280 /* Assert that the thread intends to wait for a timeout */
281 extern wait_result_t
assert_wait_timeout(
283 wait_interrupt_t interruptflags
);
285 /* Sleep, unlocking and then relocking a usimple_lock in the process */
286 extern wait_result_t
thread_sleep_usimple_lock(
289 wait_interrupt_t interruptible
);
291 /* Sleep, unlocking and then relocking a mutex in the process */
292 extern wait_result_t
thread_sleep_mutex(
295 wait_interrupt_t interruptible
);
297 /* Sleep with a deadline, unlocking and then relocking a mutex in the process */
298 extern wait_result_t
thread_sleep_mutex_deadline(
302 wait_interrupt_t interruptible
);
304 /* Sleep, unlocking and then relocking a write lock in the process */
305 extern wait_result_t
thread_sleep_lock_write(
308 wait_interrupt_t interruptible
);
310 /* Sleep, hinting that a thread funnel may be involved in the process */
311 extern wait_result_t
thread_sleep_funnel(
313 wait_interrupt_t interruptible
);
315 /* Wake up thread (or threads) waiting on a particular event */
316 extern kern_return_t
thread_wakeup_prim(
318 boolean_t one_thread
,
319 wait_result_t result
);
321 #ifdef __APPLE_API_UNSTABLE
323 /* Block current thread (Block reason) */
324 extern wait_result_t
thread_block(
325 thread_continue_t continuation
);
327 #endif /* __APPLE_API_UNSTABLE */
330 * Routines defined as macros
333 #define thread_wakeup(x) \
334 thread_wakeup_prim((x), FALSE, THREAD_AWAKENED)
335 #define thread_wakeup_with_result(x, z) \
336 thread_wakeup_prim((x), FALSE, (z))
337 #define thread_wakeup_one(x) \
338 thread_wakeup_prim((x), TRUE, THREAD_AWAKENED)
340 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
342 #include <libkern/OSBase.h>
344 #define thread_set_timer_deadline(a) \
345 thread_set_timer_deadline(__OSAbsoluteTime(a))
349 #endif /* _KERN_SCHED_PRIM_H_ */