/*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
*
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_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 file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
*
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ *
+ * The 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.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
*
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* @OSF_COPYRIGHT@
#include <mach/mach_host_server.h>
#include <mach/mach_syscalls.h>
+
+#ifdef MACH_BSD
+extern void workqueue_thread_yielded(void);
+#endif /* MACH_BSD */
+
+
+/* Called from commpage to take a delayed preemption when exiting
+ * the "Preemption Free Zone" (PFZ).
+ */
+kern_return_t
+pfz_exit(
+__unused struct pfz_exit_args *args)
+{
+ /* For now, nothing special to do. We'll pick up the ASTs on kernel exit. */
+
+ return (KERN_SUCCESS);
+}
+
+
/*
* swtch and swtch_pri both attempt to context switch (logic in
* thread_block no-ops the context switch if nothing would happen).
disable_preemption();
myprocessor = current_processor();
- result = myprocessor->runq.count > 0 ||
- myprocessor->processor_set->runq.count > 0;
+ result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
enable_preemption();
thread_syscall_return(result);
disable_preemption();
myprocessor = current_processor();
- if ( myprocessor->runq.count == 0 &&
- myprocessor->processor_set->runq.count == 0 ) {
+ if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
mp_enable_preemption();
return (FALSE);
disable_preemption();
myprocessor = current_processor();
- result = myprocessor->runq.count > 0 ||
- myprocessor->processor_set->runq.count > 0;
+ result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
enable_preemption();
return (result);
disable_preemption();
myprocessor = current_processor();
- result = myprocessor->runq.count > 0 ||
- myprocessor->processor_set->runq.count > 0;
+ result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
mp_enable_preemption();
thread_syscall_return(result);
disable_preemption();
myprocessor = current_processor();
- if ( myprocessor->runq.count == 0 &&
- myprocessor->processor_set->runq.count == 0 ) {
+ if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
mp_enable_preemption();
return (FALSE);
counter(c_swtch_pri_block++);
- thread_depress_abstime(std_quantum);
+ thread_depress_abstime(thread_depress_time);
thread_block_reason((thread_continue_t)swtch_pri_continue, NULL, AST_YIELD);
disable_preemption();
myprocessor = current_processor();
- result = myprocessor->runq.count > 0 ||
- myprocessor->processor_set->runq.count > 0;
+ result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
enable_preemption();
return (result);
return (KERN_INVALID_ARGUMENT);
}
+ workqueue_thread_yielded();
+
/*
* Translate the port name if supplied.
*/
ipc_port_release(port);
if (thread == self) {
- thread_deallocate_internal(thread);
+ (void)thread_deallocate_internal(thread);
thread = THREAD_NULL;
}
}
thread_lock(thread);
/*
- * Check if the thread is in the right pset,
- * is not bound to a different processor,
- * and that realtime is not involved.
+ * Check that the thread is not bound
+ * to a different processor, and that realtime
+ * is not involved.
*
* Next, pull it off its run queue. If it
* doesn't come, it's not eligible.
processor = current_processor();
if (processor->current_pri < BASEPRI_RTQUEUES &&
thread->sched_pri < BASEPRI_RTQUEUES &&
- thread->processor_set == processor->processor_set &&
(thread->bound_processor == PROCESSOR_NULL ||
thread->bound_processor == processor) &&
- run_queue_remove(thread) != RUN_QUEUE_NULL ) {
+ thread_run_queue_remove(thread) ) {
/*
* Hah, got it!!
*/
thread_unlock(thread);
- thread_deallocate_internal(thread);
+ (void)thread_deallocate_internal(thread);
if (option == SWITCH_OPTION_WAIT)
assert_wait_timeout((event_t)assert_wait_timeout, THREAD_ABORTSAFE,
s = splsched();
thread_lock(self);
- if (!(self->sched_mode & TH_MODE_ISDEPRESSED)) {
+ if (!(self->sched_flags & TH_SFLAG_DEPRESSED_MASK)) {
processor_t myprocessor = self->last_processor;
self->sched_pri = DEPRESSPRI;
myprocessor->current_pri = self->sched_pri;
- self->sched_mode &= ~TH_MODE_PREEMPT;
- self->sched_mode |= TH_MODE_DEPRESS;
+ self->sched_flags |= TH_SFLAG_DEPRESS;
if (interval != 0) {
clock_absolutetime_interval_to_deadline(interval, &deadline);
- if (!timer_call_enter(&self->depress_timer, deadline))
+ if (!timer_call_enter(&self->depress_timer, deadline, 0))
self->depress_timer_active++;
}
}
s = splsched();
thread_lock(thread);
if (--thread->depress_timer_active == 0) {
- thread->sched_mode &= ~TH_MODE_ISDEPRESSED;
- compute_priority(thread, FALSE);
+ thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
+ SCHED(compute_priority)(thread, FALSE);
}
thread_unlock(thread);
splx(s);
s = splsched();
thread_lock(thread);
- if (!(thread->sched_mode & TH_MODE_POLLDEPRESS)) {
- if (thread->sched_mode & TH_MODE_ISDEPRESSED) {
- thread->sched_mode &= ~TH_MODE_ISDEPRESSED;
- compute_priority(thread, FALSE);
+ if (!(thread->sched_flags & TH_SFLAG_POLLDEPRESS)) {
+ if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
+ thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
+ SCHED(compute_priority)(thread, FALSE);
result = KERN_SUCCESS;
}
assert(self == current_thread());
s = splsched();
- if (!(self->sched_mode & (TH_MODE_REALTIME|TH_MODE_TIMESHARE))) {
+ if (self->sched_mode == TH_MODE_FIXED) {
uint64_t total_computation, abstime;
abstime = mach_absolute_time();
ast_t preempt;
thread_lock(self);
- if (!(self->sched_mode & TH_MODE_ISDEPRESSED)) {
+ if (!(self->sched_flags & TH_SFLAG_DEPRESSED_MASK)) {
self->sched_pri = DEPRESSPRI;
myprocessor->current_pri = self->sched_pri;
- self->sched_mode &= ~TH_MODE_PREEMPT;
}
self->computation_epoch = abstime;
self->computation_metered = 0;
- self->sched_mode |= TH_MODE_POLLDEPRESS;
+ self->sched_flags |= TH_SFLAG_POLLDEPRESS;
abstime += (total_computation >> sched_poll_yield_shift);
- if (!timer_call_enter(&self->depress_timer, abstime))
+ if (!timer_call_enter(&self->depress_timer, abstime, 0))
self->depress_timer_active++;
thread_unlock(self);
- if ((preempt = csw_check(self, myprocessor)) != AST_NONE)
+ if ((preempt = csw_check(myprocessor)) != AST_NONE)
ast_on(preempt);
}
}
splx(s);
}
+
+
+void
+thread_yield_internal(
+ mach_msg_timeout_t ms)
+{
+ processor_t myprocessor;
+
+ disable_preemption();
+ myprocessor = current_processor();
+ if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
+ mp_enable_preemption();
+
+ return;
+ }
+ enable_preemption();
+
+ thread_depress_ms(ms);
+
+ thread_block_reason(THREAD_CONTINUE_NULL, NULL, AST_YIELD);
+
+ thread_depress_abort_internal(current_thread());
+}
+