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.
53 #include <mach/boolean.h>
54 #include <mach/thread_switch.h>
55 #include <ipc/ipc_port.h>
56 #include <ipc/ipc_space.h>
57 #include <kern/counters.h>
58 #include <kern/etap_macros.h>
59 #include <kern/ipc_kobject.h>
60 #include <kern/processor.h>
61 #include <kern/sched.h>
62 #include <kern/sched_prim.h>
64 #include <kern/task.h>
65 #include <kern/thread.h>
67 #include <mach/policy.h>
69 #include <kern/syscall_subr.h>
70 #include <mach/mach_host_server.h>
71 #include <mach/mach_syscalls.h>
73 #include <kern/mk_sp.h>
76 * swtch and swtch_pri both attempt to context switch (logic in
77 * thread_block no-ops the context switch if nothing would happen).
78 * A boolean is returned that indicates whether there is anything
81 * This boolean can be used by a thread waiting on a
82 * lock or condition: If FALSE is returned, the thread is justified
83 * in becoming a resource hog by continuing to spin because there's
84 * nothing else useful that the processor could do. If TRUE is
85 * returned, the thread should make one more check on the
86 * lock and then be a good citizen and really suspend.
92 register processor_t myprocessor
;
95 mp_disable_preemption();
96 myprocessor
= current_processor();
97 result
= myprocessor
->runq
.count
> 0 ||
98 myprocessor
->processor_set
->runq
.count
> 0;
99 mp_enable_preemption();
101 thread_syscall_return(result
);
108 register processor_t myprocessor
;
111 mp_disable_preemption();
112 myprocessor
= current_processor();
113 if ( myprocessor
->runq
.count
== 0 &&
114 myprocessor
->processor_set
->runq
.count
== 0 ) {
115 mp_enable_preemption();
119 mp_enable_preemption();
121 counter(c_swtch_block
++);
123 thread_block(swtch_continue
);
125 mp_disable_preemption();
126 myprocessor
= current_processor();
127 result
= myprocessor
->runq
.count
> 0 ||
128 myprocessor
->processor_set
->runq
.count
> 0;
129 mp_enable_preemption();
135 swtch_pri_continue(void)
137 register processor_t myprocessor
;
140 _mk_sp_thread_depress_abort(current_thread(), FALSE
);
142 mp_disable_preemption();
143 myprocessor
= current_processor();
144 result
= myprocessor
->runq
.count
> 0 ||
145 myprocessor
->processor_set
->runq
.count
> 0;
146 mp_enable_preemption();
148 thread_syscall_return(result
);
156 register processor_t myprocessor
;
159 mp_disable_preemption();
160 myprocessor
= current_processor();
161 if ( myprocessor
->runq
.count
== 0 &&
162 myprocessor
->processor_set
->runq
.count
== 0 ) {
163 mp_enable_preemption();
167 mp_enable_preemption();
169 counter(c_swtch_pri_block
++);
171 _mk_sp_thread_depress_abstime(std_quantum
);
173 thread_block(swtch_pri_continue
);
175 _mk_sp_thread_depress_abort(current_thread(), FALSE
);
177 mp_disable_preemption();
178 myprocessor
= current_processor();
179 result
= myprocessor
->runq
.count
> 0 ||
180 myprocessor
->processor_set
->runq
.count
> 0;
181 mp_enable_preemption();
189 * Context switch. User may supply thread hint.
193 mach_port_name_t thread_name
,
195 mach_msg_timeout_t option_time
)
197 register thread_t self
= current_thread();
198 register thread_act_t hint_act
= THR_ACT_NULL
;
205 case SWITCH_OPTION_NONE
:
206 case SWITCH_OPTION_DEPRESS
:
207 case SWITCH_OPTION_WAIT
:
211 return (KERN_INVALID_ARGUMENT
);
214 if (thread_name
!= MACH_PORT_NULL
) {
217 if (ipc_port_translate_send(self
->top_act
->task
->itk_space
,
218 thread_name
, &port
) == KERN_SUCCESS
) {
222 hint_act
= convert_port_to_act(port
);
223 ipc_port_release(port
);
227 return _mk_sp_thread_switch(hint_act
, option
, option_time
);