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.
55 #include <mach/boolean.h>
56 #include <mach/thread_switch.h>
57 #include <ipc/ipc_port.h>
58 #include <ipc/ipc_space.h>
59 #include <kern/counters.h>
60 #include <kern/etap_macros.h>
61 #include <kern/ipc_kobject.h>
62 #include <kern/processor.h>
63 #include <kern/sched.h>
64 #include <kern/sched_prim.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
69 #include <mach/policy.h>
71 #include <kern/syscall_subr.h>
72 #include <mach/mach_host_server.h>
73 #include <mach/mach_syscalls.h>
78 * swtch and swtch_pri both attempt to context switch (logic in
79 * thread_block no-ops the context switch if nothing would happen).
80 * A boolean is returned that indicates whether there is anything
83 * This boolean can be used by a thread waiting on a
84 * lock or condition: If FALSE is returned, the thread is justified
85 * in becoming a resource hog by continuing to spin because there's
86 * nothing else useful that the processor could do. If TRUE is
87 * returned, the thread should make one more check on the
88 * lock and then be a good citizen and really suspend.
93 /* broken..do not enable */
98 register processor_t myprocessor
;
100 mp_disable_preemption();
101 myprocessor
= current_processor();
104 myprocessor
->runq
.count
> 0 ||
106 myprocessor
->processor_set
->runq
.count
> 0);
107 mp_enable_preemption();
116 register processor_t myprocessor
;
119 mp_disable_preemption();
120 myprocessor
= current_processor();
123 myprocessor
->runq
.count
== 0 &&
124 #endif /* NCPUS > 1 */
125 myprocessor
->processor_set
->runq
.count
== 0 ) {
126 mp_enable_preemption();
130 mp_enable_preemption();
132 counter(c_swtch_block
++);
134 thread_block((void (*)(void)) 0);
136 mp_disable_preemption();
137 myprocessor
= current_processor();
140 myprocessor
->runq
.count
> 0 ||
142 myprocessor
->processor_set
->runq
.count
> 0;
143 mp_enable_preemption();
152 thread_t self
= current_thread();
153 register processor_t myprocessor
;
155 sched_policy_t
*policy
;
160 myprocessor
= current_processor();
163 myprocessor
->runq
.count
== 0 &&
164 #endif /* NCPUS > 1 */
165 myprocessor
->processor_set
->runq
.count
== 0 ) {
172 policy
= &sched_policy
[self
->policy
];
176 policy
->sp_ops
.sp_swtch_pri(policy
, pri
);
178 mp_disable_preemption();
179 myprocessor
= current_processor();
182 myprocessor
->runq
.count
> 0 ||
184 myprocessor
->processor_set
->runq
.count
> 0;
185 mp_enable_preemption();
193 * Context switch. User may supply thread hint.
197 mach_port_name_t thread_name
,
199 mach_msg_timeout_t option_time
)
201 register thread_t self
= current_thread();
202 register thread_act_t hint_act
= THR_ACT_NULL
;
203 sched_policy_t
*policy
;
211 case SWITCH_OPTION_NONE
:
212 case SWITCH_OPTION_DEPRESS
:
213 case SWITCH_OPTION_WAIT
:
217 return (KERN_INVALID_ARGUMENT
);
220 if (thread_name
!= MACH_PORT_NULL
) {
223 if (ipc_port_translate_send(self
->top_act
->task
->itk_space
,
224 thread_name
, &port
) == KERN_SUCCESS
) {
228 hint_act
= convert_port_to_act(port
);
229 ipc_port_release(port
);
235 policy
= &sched_policy
[self
->policy
];
240 * This is a scheduling policy-dependent operation.
241 * Call the routine associated with the thread's
244 return (policy
->sp_ops
.
245 sp_thread_switch(policy
, hint_act
, option
, option_time
));