Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
[droid] fixed linux thread priorities, do not mix xbmc priority level…
Browse files Browse the repository at this point in the history
…s with system priority levels
  • Loading branch information
davilla committed Jul 8, 2012
1 parent 4fd0e44 commit 4f0f6ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
14 changes: 0 additions & 14 deletions xbmc/linux/PlatformDefs.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,6 @@ typedef struct _TIME_ZONE_INFORMATION {
#define TIME_ZONE_ID_STANDARD 1
#define TIME_ZONE_ID_DAYLIGHT 2

// Thread
#define THREAD_BASE_PRIORITY_LOWRT 15
#define THREAD_BASE_PRIORITY_MAX 2
#define THREAD_BASE_PRIORITY_MIN -2
#define THREAD_BASE_PRIORITY_IDLE -15
#define THREAD_PRIORITY_LOWEST THREAD_BASE_PRIORITY_MIN
#define THREAD_PRIORITY_BELOW_NORMAL (THREAD_PRIORITY_LOWEST+1)
#define THREAD_PRIORITY_NORMAL 0
#define THREAD_PRIORITY_HIGHEST THREAD_BASE_PRIORITY_MAX
#define THREAD_PRIORITY_ABOVE_NORMAL (THREAD_PRIORITY_HIGHEST-1)
#define THREAD_PRIORITY_ERROR_RETURN (0x7fffffff)
#define THREAD_PRIORITY_TIME_CRITICAL THREAD_BASE_PRIORITY_LOWRT
#define THREAD_PRIORITY_IDLE THREAD_BASE_PRIORITY_IDLE

// Network
#define SOCKADDR_IN struct sockaddr_in
#define IN_ADDR struct in_addr
Expand Down
16 changes: 16 additions & 0 deletions xbmc/threads/Thread.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
#include <mach/mach.h>
#endif

// Thread
#if !defined(WIN32)
#define THREAD_BASE_PRIORITY_LOWRT 15
#define THREAD_BASE_PRIORITY_MAX 2
#define THREAD_BASE_PRIORITY_MIN -2
#define THREAD_BASE_PRIORITY_IDLE -15
#define THREAD_PRIORITY_LOWEST THREAD_BASE_PRIORITY_MIN
#define THREAD_PRIORITY_BELOW_NORMAL (THREAD_PRIORITY_LOWEST+1)
#define THREAD_PRIORITY_NORMAL 0
#define THREAD_PRIORITY_HIGHEST THREAD_BASE_PRIORITY_MAX
#define THREAD_PRIORITY_ABOVE_NORMAL (THREAD_PRIORITY_HIGHEST-1)
#define THREAD_PRIORITY_ERROR_RETURN (0x7fffffff)
#define THREAD_PRIORITY_TIME_CRITICAL THREAD_BASE_PRIORITY_LOWRT
#define THREAD_PRIORITY_IDLE THREAD_BASE_PRIORITY_IDLE
#endif

class IRunnable
{
public:
Expand Down
65 changes: 23 additions & 42 deletions xbmc/threads/platform/pthreads/ThreadImpl.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -90,69 +90,48 @@ bool CThread::IsCurrentThread(const ThreadIdentifier tid)

int CThread::GetMinPriority(void)
{
// one level lower than application
return -1;
return THREAD_PRIORITY_IDLE;
}

int CThread::GetMaxPriority(void)
{
// one level higher than application
return 1;
return THREAD_PRIORITY_HIGHEST;
}

int CThread::GetNormalPriority(void)
{
// same level as application
return 0;
return THREAD_PRIORITY_NORMAL;
}

bool CThread::SetPriority(const int iPriority)
{
// iPriority is with respect to that defined in Thread.h
bool bReturn = false;

// wait until thread is running, it needs to get its lwp id
m_StartEvent.Wait();

CSingleLock lock(m_CriticalSection);

// get min prio for SCHED_RR
int minRR = GetMaxPriority() + 1;

if (!m_ThreadId)
bReturn = false;
else if (iPriority >= minRR)
bReturn = SetPrioritySched_RR(iPriority);
#ifdef RLIMIT_NICE
return false;

// keep priority in bounds
int prio = iPriority;
if (prio >= GetMaxPriority())
prio = GetMinPriority();
if (prio < GetMinPriority())
prio = GetMinPriority();

// nice level of application
int appNice = getpriority(PRIO_PROCESS, getpid());
// flip it with respect to the 'nice' levels (-20 to 19)
prio = appNice - prio;

if (setpriority(PRIO_PROCESS, m_ThreadOpaque.LwpId, prio) == 0)
bReturn = true;
else
{
// get user max prio
struct rlimit limit;
int userMaxPrio;
if (getrlimit(RLIMIT_NICE, &limit) == 0)
{
userMaxPrio = limit.rlim_cur - 20;
}
else
userMaxPrio = 0;

// keep priority in bounds
int prio = iPriority;
if (prio >= GetMaxPriority())
prio = std::min(GetMaxPriority(), userMaxPrio);
if (prio < GetMinPriority())
prio = GetMinPriority();

// nice level of application
int appNice = getpriority(PRIO_PROCESS, getpid());
if (prio)
prio = prio > 0 ? appNice-1 : appNice+1;

if (setpriority(PRIO_PROCESS, m_ThreadOpaque.LwpId, prio) == 0)
bReturn = true;
else
if (logger) logger->Log(LOGERROR, "%s: error %s", __FUNCTION__, strerror(errno));
}
#endif
if (logger) logger->Log(LOGERROR, "%s: error %s", __FUNCTION__, strerror(errno));

return bReturn;
}
Expand All @@ -168,6 +147,8 @@ int CThread::GetPriority()

int appNice = getpriority(PRIO_PROCESS, getpid());
int prio = getpriority(PRIO_PROCESS, m_ThreadOpaque.LwpId);
// flip it with respect to the 'nice' levels (-20 to 19), so that
// what is returned is with repect to that defined in Thread.h
iReturn = appNice - prio;

return iReturn;
Expand Down

0 comments on commit 4f0f6ed

Please sign in to comment.