2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <mach/task.h>
31 #include <mach/thread_act.h>
33 #include <kern/kern_types.h>
34 #include <kern/processor.h>
35 #include <kern/thread.h>
36 #include <kern/kalloc.h>
38 #include <chud/chud_xnu.h>
39 #include <chud/chud_xnu_private.h>
40 #include <chud/chud_thread.h>
42 #include <machine/machine_routines.h>
44 #include <libkern/OSAtomic.h>
51 #include <kperf/kperf.h>
54 // include the correct file to find real_ncpus
55 #if defined(__i386__) || defined(__x86_64__)
58 // fall back on declaring it extern. The linker will sort us out.
59 extern unsigned int real_ncpus
;
62 // Mask for supported options
63 #define T_CHUD_BIND_OPT_MASK (-1UL)
66 #pragma mark **** thread binding ****
70 * This method will bind a given thread to the requested CPU starting at the
71 * next time quantum. If the thread is the current thread, this method will
72 * force a thread_block(). The result is that if you call this method on the
73 * current thread, you will be on the requested CPU when this method returns.
75 __private_extern__ kern_return_t
76 chudxnu_bind_thread(thread_t thread
, int cpu
, __unused
int options
)
78 processor_t proc
= NULL
;
80 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) // sanity check
83 // temporary restriction until after phase 2 of the scheduler
84 if(thread
!= current_thread())
87 proc
= cpu_to_processor(cpu
);
90 * Potentially racey, but mainly to prevent bind to shutdown
93 if(proc
&& !(proc
->state
== PROCESSOR_OFF_LINE
) &&
94 !(proc
->state
== PROCESSOR_SHUTDOWN
)) {
99 * If we're trying to bind the current thread, and
100 * we're not on the target cpu, and not at interrupt
101 * context, block the current thread to force a
102 * reschedule on the target CPU.
104 if(thread
== current_thread() &&
105 !ml_at_interrupt_context() && cpu_number() != cpu
) {
106 (void)thread_block(THREAD_CONTINUE_NULL
);
113 __private_extern__ kern_return_t
114 chudxnu_unbind_thread(thread_t thread
, __unused
int options
)
116 if(thread
== current_thread())
117 thread_bind(PROCESSOR_NULL
);