priority.c   [plain text]


/*
 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * @OSF_COPYRIGHT@
 */
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
 * 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 ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS 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.
 */
/*
 */
/*
 *	File:	clock_prim.c
 *	Author:	Avadis Tevanian, Jr.
 *	Date:	1986
 *
 *	Clock primitives.
 */

#include <cpus.h>

#include <mach/boolean.h>
#include <mach/kern_return.h>
#include <mach/machine.h>
#include <kern/host.h>
#include <kern/mach_param.h>
#include <kern/sched.h>
#include <kern/spl.h>
#include <kern/thread.h>
#include <kern/processor.h>
#include <machine/machparam.h>

/*
 *	thread_quantum_expire:
 *
 *	Recalculate the quantum and priority for a thread.
 */

void
thread_quantum_expire(
	timer_call_param_t	p0,
	timer_call_param_t	p1)
{
	register processor_t		myprocessor = p0;
	register thread_t			thread = p1;
	register processor_set_t	pset;
	spl_t						s;

	pset = myprocessor->processor_set;

	/*
	 *	Update set_quanta for timesharing.
	 */
	pset->set_quanta = pset->machine_quanta[
							(pset->runq.count > pset->processor_count) ?
								  pset->processor_count : pset->runq.count];

	s = splsched();
	thread_lock(thread);

	/*
	 * Check for failsafe trip.
	 */
	if (!(thread->sched_mode & TH_MODE_TIMESHARE)) {
		extern uint64_t		max_unsafe_computation;
		uint64_t			new_computation;

		new_computation = myprocessor->quantum_end;
		new_computation -= thread->computation_epoch;
		if (new_computation + thread->metered_computation >
											max_unsafe_computation) {
			extern uint32_t		sched_safe_duration;

			if (thread->sched_mode & TH_MODE_REALTIME) {
				if (thread->depress_priority < 0)
					thread->priority = MINPRI;
				else
					thread->depress_priority = MINPRI;

				thread->safe_mode |= TH_MODE_REALTIME;
				thread->sched_mode &= ~TH_MODE_REALTIME;
			}

			thread->safe_release = sched_tick + sched_safe_duration;
			thread->sched_mode |= (TH_MODE_FAILSAFE|TH_MODE_TIMESHARE);
		}
	}
		
	/*
	 *	Now recompute the priority of the thread if appropriate.
	 */
	if (thread->sched_stamp != sched_tick)
		update_priority(thread);
	else
	if (	(thread->sched_mode & TH_MODE_TIMESHARE)	&&
				thread->depress_priority < 0				) {
		thread_timer_delta(thread);
		thread->sched_usage += thread->sched_delta;
		thread->sched_delta = 0;
		compute_my_priority(thread);
	}

	/*
	 *	This quantum is up, give this thread another.
	 */
	if (first_quantum(myprocessor))
		myprocessor->slice_quanta--;

	thread->current_quantum = (thread->sched_mode & TH_MODE_REALTIME)?
									thread->realtime.computation: std_quantum;
	myprocessor->quantum_end += thread->current_quantum;
	timer_call_enter1(&myprocessor->quantum_timer,
							thread, myprocessor->quantum_end);

	thread_unlock(thread);
	splx(s);

	/*
	 * Check for and schedule ast if needed.
	 */
	ast_check();
}