Core: Thread::Priority now implemented for POSIX (thanks tojocky)

git-svn-id: svn://ultimatepp.org/upp/trunk@5045 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-06-08 15:45:02 +00:00
parent 9b1799d6b4
commit e1bb9c4e89
3 changed files with 76 additions and 6 deletions

View file

@ -227,7 +227,7 @@ int Thread::Wait()
return out;
}
void Thread::Priority(int percent)
bool Thread::Priority(int percent)
{
ASSERT(IsOpen());
#ifdef PLATFORM_WIN32
@ -242,10 +242,80 @@ void Thread::Priority(int percent)
prior = THREAD_PRIORITY_ABOVE_NORMAL;
else
prior = THREAD_PRIORITY_HIGHEST;
SetThreadPriority(handle, prior);
return SetThreadPriority(handle, prior);
#endif
#ifdef PLATFORM_POSIX
// ToDo
int policy;
struct sched_param param;
if(pthread_getschedparam(handle, &policy, &param))
return false;
int percen_min, percen_max;
if(percent <= 25) {
#if defined(SCHED_IDLE)
policy = SCHED_IDLE;
percen_min = 0;
percen_max = 25;
#elif defined(SCHED_BATCH)
policy = SCHED_BATCH;
percen_min = 0;
percen_max = 75;
#else
policy = SCHED_OTHER;
percen_min = 0;
percen_max = 125;
#endif
}
else
if(percent <= 75){
#if defined(SCHED_IDLE)
policy = SCHED_BATCH;
percen_min = 25;
percen_max = 75;
#elif defined(SCHED_BATCH)
policy = SCHED_BATCH;
percen_min = 0;
percen_max = 75;
#else
policy = SCHED_OTHER;
percen_min = 0;
percen_max = 125;
#endif
}
else
if(percent <= 125){
policy = SCHED_OTHER;
#if defined(SCHED_IDLE)
percen_min = 75;
percen_max = 125;
#elif defined(SCHED_BATCH)
percen_min = 25;
percen_max = 125;
#else
percen_min = 0;
percen_max = 125;
#endif
}
else
if(percent <= 175){ // should be the root
policy = SCHED_FIFO;
percen_min = 125;
percen_max = 175;
}
else
policy = SCHED_RR;
param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, percen_min, percen_max)-percen_min)/(percen_max - percen_min);
if (pthread_setschedparam(handle, policy, &param)) {
// No privileges? Try maximum possible! Do not use EPERM as not all os support this one
policy = SCHED_OTHER;
percen_max = 125;
percen_min = minmax(percen_min, 0, percen_max);
param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, percen_min, percen_max)-percen_min)/(percen_max - percen_min);
if (pthread_setschedparam(handle, policy, &param))
return false;
}
return true;
#endif
}

View file

@ -76,7 +76,7 @@ public:
Handle GetHandle() const { return handle; }
void Priority(int percent); // 0 = lowest, 100 = normal
bool Priority(int percent); // 0 = lowest, 100 = normal
static void Start(Callback cb);

View file

@ -97,11 +97,11 @@ client code should call the already installed function (return
value is not null)&]
[s3; &]
[s4; &]
[s5;:Thread`:`:Priority`(int`): [@(0.0.255) void]_[* Priority]([@(0.0.255) int]_[*@3 percent])
[s5;:Thread`:`:Priority`(int`): [@(0.0.255) bool]_[* Priority]([@(0.0.255) int]_[*@3 percent])
&]
[s2;%% Sets the treads priority to [%-*@3 percent ][%- (0 to 200)]. In
reality, current implementation supports only 5 levels, 25%,
75%, 125%, 175% and more than 175%; last two levels require root
priviledges.&]
priviledges. Returns true if setting the priority was successfull.&]
[s3;%% &]
[s0; ]