1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
* Mach Operating System
* Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University.
* Copyright (c) 1993,1994 The University of Utah and
* the Computer Systems Laboratory (CSL).
* All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
* THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
* OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
* THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
#include <mach/boolean.h>
#include <mach/thread_switch.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_space.h>
#include <kern/counters.h>
#include <kern/ipc_kobject.h>
#include <kern/mach_clock.h>
#include <kern/printf.h>
#include <kern/processor.h>
#include <kern/sched.h>
#include <kern/sched_prim.h>
#include <kern/syscall_subr.h>
#include <kern/ipc_sched.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <machine/machspl.h> /* for splsched */
#if MACH_FIXPRI
#include <mach/policy.h>
#endif /* MACH_FIXPRI */
/*
* swtch and swtch_pri both attempt to context switch (logic in
* thread_block no-ops the context switch if nothing would happen).
* A boolean is returned that indicates whether there is anything
* else runnable.
*
* This boolean can be used by a thread waiting on a
* lock or condition: If FALSE is returned, the thread is justified
* in becoming a resource hog by continuing to spin because there's
* nothing else useful that the processor could do. If TRUE is
* returned, the thread should make one more check on the
* lock and then be a good citizen and really suspend.
*/
void swtch_continue(void)
{
processor_t myprocessor;
myprocessor = current_processor();
thread_syscall_return(myprocessor->runq.count > 0 ||
myprocessor->processor_set->runq.count > 0);
/*NOTREACHED*/
}
boolean_t swtch(void)
{
processor_t myprocessor;
#if NCPUS > 1
myprocessor = current_processor();
if (myprocessor->runq.count == 0 &&
myprocessor->processor_set->runq.count == 0)
return(FALSE);
#endif /* NCPUS > 1 */
counter(c_swtch_block++);
thread_block(swtch_continue);
myprocessor = current_processor();
return(myprocessor->runq.count > 0 ||
myprocessor->processor_set->runq.count > 0);
}
void swtch_pri_continue(void)
{
thread_t thread = current_thread();
processor_t myprocessor;
if (thread->depress_priority >= 0)
(void) thread_depress_abort(thread);
myprocessor = current_processor();
thread_syscall_return(myprocessor->runq.count > 0 ||
myprocessor->processor_set->runq.count > 0);
/*NOTREACHED*/
}
boolean_t swtch_pri(int pri)
{
thread_t thread = current_thread();
processor_t myprocessor;
#if NCPUS > 1
myprocessor = current_processor();
if (myprocessor->runq.count == 0 &&
myprocessor->processor_set->runq.count == 0)
return(FALSE);
#endif /* NCPUS > 1 */
/*
* XXX need to think about depression duration.
* XXX currently using min quantum.
*/
thread_depress_priority(thread, min_quantum);
counter(c_swtch_pri_block++);
thread_block(swtch_pri_continue);
if (thread->depress_priority >= 0)
(void) thread_depress_abort(thread);
myprocessor = current_processor();
return(myprocessor->runq.count > 0 ||
myprocessor->processor_set->runq.count > 0);
}
void thread_switch_continue(void)
{
thread_t cur_thread = current_thread();
/*
* Restore depressed priority
*/
if (cur_thread->depress_priority >= 0)
(void) thread_depress_abort(cur_thread);
thread_syscall_return(KERN_SUCCESS);
/*NOTREACHED*/
}
/*
* thread_switch:
*
* Context switch. User may supply thread hint.
*
* Fixed priority threads that call this get what they asked for
* even if that violates priority order.
*/
kern_return_t thread_switch(
mach_port_t thread_name,
int option,
mach_msg_timeout_t option_time)
{
thread_t cur_thread = current_thread();
processor_t myprocessor;
ipc_port_t port;
/*
* Process option.
*/
switch (option) {
case SWITCH_OPTION_NONE:
/*
* Nothing to do.
*/
break;
case SWITCH_OPTION_DEPRESS:
/*
* Depress priority for given time.
*/
thread_depress_priority(cur_thread, option_time);
break;
case SWITCH_OPTION_WAIT:
thread_will_wait_with_timeout(cur_thread, option_time);
break;
default:
return(KERN_INVALID_ARGUMENT);
}
#ifndef MIGRATING_THREADS /* XXX thread_run defunct */
/*
* Check and act on thread hint if appropriate.
*/
if ((thread_name != 0) &&
(ipc_port_translate_send(cur_thread->task->itk_space,
thread_name, &port) == KERN_SUCCESS)) {
/* port is locked, but it might not be active */
/*
* Get corresponding thread.
*/
if (ip_active(port) && (ip_kotype(port) == IKOT_THREAD)) {
thread_t thread;
spl_t s;
thread = (thread_t) port->ip_kobject;
/*
* Check if the thread is in the right pset. Then
* pull it off its run queue. If it
* doesn't come, then it's not eligible.
*/
s = splsched();
thread_lock(thread);
if ((thread->processor_set == cur_thread->processor_set)
&& (rem_runq(thread) != RUN_QUEUE_NULL)) {
/*
* Hah, got it!!
*/
thread_unlock(thread);
(void) splx(s);
ip_unlock(port);
/* XXX thread might disappear on us now? */
#if MACH_FIXPRI
if (thread->policy == POLICY_FIXEDPRI) {
myprocessor = current_processor();
myprocessor->quantum = thread->sched_data;
myprocessor->first_quantum = TRUE;
}
#endif /* MACH_FIXPRI */
counter(c_thread_switch_handoff++);
thread_run(thread_switch_continue, thread);
/*
* Restore depressed priority
*/
if (cur_thread->depress_priority >= 0)
(void) thread_depress_abort(cur_thread);
return(KERN_SUCCESS);
}
thread_unlock(thread);
(void) splx(s);
}
ip_unlock(port);
}
#endif /* not MIGRATING_THREADS */
/*
* No handoff hint supplied, or hint was wrong. Call thread_block() in
* hopes of running something else. If nothing else is runnable,
* thread_block will detect this. WARNING: thread_switch with no
* option will not do anything useful if the thread calling it is the
* highest priority thread (can easily happen with a collection
* of timesharing threads).
*/
#if NCPUS > 1
myprocessor = current_processor();
if (myprocessor->processor_set->runq.count > 0 ||
myprocessor->runq.count > 0)
#endif /* NCPUS > 1 */
{
counter(c_thread_switch_block++);
thread_block(thread_switch_continue);
}
/*
* Restore depressed priority
*/
if (cur_thread->depress_priority >= 0)
(void) thread_depress_abort(cur_thread);
return(KERN_SUCCESS);
}
/*
* thread_depress_priority
*
* Depress thread's priority to lowest possible for specified period.
* Intended for use when thread wants a lock but doesn't know which
* other thread is holding it. As with thread_switch, fixed
* priority threads get exactly what they asked for. Users access
* this by the SWITCH_OPTION_DEPRESS option to thread_switch. A Time
* of zero will result in no timeout being scheduled.
*/
void
thread_depress_priority(
thread_t thread,
mach_msg_timeout_t depress_time)
{
unsigned int ticks;
spl_t s;
/* convert from milliseconds to ticks */
ticks = convert_ipc_timeout_to_ticks(depress_time);
s = splsched();
thread_lock(thread);
/*
* If thread is already depressed, override previous depression.
*/
reset_timeout_check(&thread->depress_timer);
/*
* Save current priority, then set priority and
* sched_pri to their lowest possible values.
*/
thread->depress_priority = thread->priority;
thread->priority = NRQS-1;
thread->sched_pri = NRQS-1;
if (ticks != 0)
set_timeout(&thread->depress_timer, ticks);
thread_unlock(thread);
(void) splx(s);
}
/*
* thread_depress_timeout:
*
* Timeout routine for priority depression.
*/
void
thread_depress_timeout(thread_t thread)
{
spl_t s;
s = splsched();
thread_lock(thread);
/*
* If we lose a race with thread_depress_abort,
* then depress_priority might be -1.
*/
if (thread->depress_priority >= 0) {
thread->priority = thread->depress_priority;
thread->depress_priority = -1;
compute_priority(thread, FALSE);
}
thread_unlock(thread);
(void) splx(s);
}
/*
* thread_depress_abort:
*
* Prematurely abort priority depression if there is one.
*/
kern_return_t
thread_depress_abort(thread_t thread)
{
spl_t s;
if (thread == THREAD_NULL)
return(KERN_INVALID_ARGUMENT);
s = splsched();
thread_lock(thread);
/*
* Only restore priority if thread is depressed.
*/
if (thread->depress_priority >= 0) {
reset_timeout_check(&thread->depress_timer);
thread->priority = thread->depress_priority;
thread->depress_priority = -1;
compute_priority(thread, FALSE);
}
thread_unlock(thread);
(void) splx(s);
return(KERN_SUCCESS);
}
/*
* mach_print
*
* Display a null-terminated character string on the Mach console.
* This system call is meant as a debugging tool useful to circumvent
* messaging altogether.
*/
#ifdef MACH_KDB
void
mach_print(const char *s)
{
printf("%s", s);
}
#endif /* MACH_KDB */
|