Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Optimizing thread performance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optimizing thread performance



Hello Shaun,

Well I think I understand what you are saying. This is probably not the problem because my secondary thread is essentially a ring buffer type routine. It modifies the tail only. The head is modified by the other thread. This routine then runs until the head == tail and then sleeps. So if the semaphore fails, the head will have already advanced.

Jeremy

On Sep 11, 2006, at 6:12 PM, Shaun Wexler wrote:

On Sep 11, 2006, at 2:12 PM, Shaun Wexler wrote:

On Sep 11, 2006, at 9:42 AM, Jeremy Sagan wrote:

I am trying to optimize thread performance and it almost works perfectly. What I am trying to do is to signal a high-priority secondary thread to wakeup and execute as soon as possible when certain criteria is met on a core-audio thread. I am using semaphore_signal to signal to the secondary thread. I also am having the secondary thread sleep only for the appropriate number of nanoseconds with semaphore_timedwait in an attempt to have just wake-up on time. The problem I am running into is that the secondary thread will sleep sometimes for more than 30 milliseconds even after being signaled by the semaphore mechanism.

Is there a way to tell the scheduler to immediately place the secondary thread at the front of the queue?

I have tried setting real-time priority with THREAD_TIME_CONSTRAINT_POLICY but it does not seem to make any difference. In fact it actually works better if I simply set a high priority using THREAD_PRECEDENCE_POLICY.

kern_return_t semaphore_signal_thread(sempahore_t sem, thread_t thread);

Ah, another caveat here. This function only works if the thread is already waiting on the semaphore; NOTE: it does not prepost to the semaphore (increase the semaphore's "signal" count). If semaphore_signal_thread returns KERN_SUCCESS, that means the thread has been made runnable, and it has already begun running if there was an idle processor available, or it has been queued according to normal scheduler behavior if all CPU's were busy or it's a UP.


This means that if semaphore_signal_thread returns KERN_NOT_WAITING, then you will have to set an atomic flag so that your secondary thread which is either (A) currently running or (B) not runnable will test the atomic flag to perform its work immediately instead of waiting on the semaphore, or immediately call semaphore_signal to prepost, otherwise you'll have to handle the secondary thread processing in the next IO cycle.
--
Shaun Wexler
MacFOH
http://www.macfoh.com




_______________________________________________ Do not post admin requests to the list. They will be ignored. PerfOptimization-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Optimizing thread performance (From: Jeremy Sagan <email@hidden>)
 >Re: Optimizing thread performance (From: Shaun Wexler <email@hidden>)
 >Re: Optimizing thread performance (From: Shaun Wexler <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.