]> git.saurik.com Git - apple/xnu.git/blame - osfmk/chud/chud_thread.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / chud / chud_thread.c
CommitLineData
0c530ab8 1/*
b0d623f7 2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
0c530ab8 3 *
2d21ac55
A
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
0c530ab8
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
0c530ab8
A
27 */
28
29#include <mach/mach_types.h>
30#include <mach/task.h>
31#include <mach/thread_act.h>
32
33#include <kern/kern_types.h>
34#include <kern/processor.h>
35#include <kern/thread.h>
36#include <kern/kalloc.h>
37
38#include <chud/chud_xnu.h>
39#include <chud/chud_xnu_private.h>
2d21ac55 40#include <chud/chud_thread.h>
0c530ab8
A
41
42#include <machine/machine_routines.h>
43
2d21ac55
A
44#include <libkern/OSAtomic.h>
45
39236c6e
A
46#if KPC
47#include <kern/kpc.h>
48#endif
49
50#if KPERF
51#include <kperf/kperf.h>
52#endif
53
0c530ab8
A
54// include the correct file to find real_ncpus
55#if defined(__i386__) || defined(__x86_64__)
56# include <i386/mp.h>
2d21ac55
A
57#else
58// fall back on declaring it extern. The linker will sort us out.
59extern unsigned int real_ncpus;
60#endif
61
62// Mask for supported options
63#define T_CHUD_BIND_OPT_MASK (-1UL)
0c530ab8 64
b0d623f7 65#if 0
0c530ab8 66#pragma mark **** thread binding ****
b0d623f7 67#endif
0c530ab8 68
2d21ac55
A
69/*
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.
74 */
0c530ab8 75__private_extern__ kern_return_t
2d21ac55 76chudxnu_bind_thread(thread_t thread, int cpu, __unused int options)
0c530ab8
A
77{
78 processor_t proc = NULL;
2d21ac55
A
79
80 if(cpu < 0 || (unsigned int)cpu >= real_ncpus) // sanity check
0c530ab8 81 return KERN_FAILURE;
2d21ac55
A
82
83 // temporary restriction until after phase 2 of the scheduler
84 if(thread != current_thread())
85 return KERN_FAILURE;
0c530ab8
A
86
87 proc = cpu_to_processor(cpu);
88
2d21ac55
A
89 /*
90 * Potentially racey, but mainly to prevent bind to shutdown
91 * processor.
92 */
0c530ab8 93 if(proc && !(proc->state == PROCESSOR_OFF_LINE) &&
2d21ac55
A
94 !(proc->state == PROCESSOR_SHUTDOWN)) {
95
96 thread_bind(proc);
97
98 /*
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.
103 */
104 if(thread == current_thread() &&
b0d623f7 105 !ml_at_interrupt_context() && cpu_number() != cpu) {
0c530ab8
A
106 (void)thread_block(THREAD_CONTINUE_NULL);
107 }
108 return KERN_SUCCESS;
109 }
110 return KERN_FAILURE;
111}
112
113__private_extern__ kern_return_t
2d21ac55 114chudxnu_unbind_thread(thread_t thread, __unused int options)
0c530ab8 115{
2d21ac55
A
116 if(thread == current_thread())
117 thread_bind(PROCESSOR_NULL);
0c530ab8
A
118 return KERN_SUCCESS;
119}