diff --git a/uppsrc/Core/Mt.cpp b/uppsrc/Core/Mt.cpp index b84d243e5..0ca483cea 100644 --- a/uppsrc/Core/Mt.cpp +++ b/uppsrc/Core/Mt.cpp @@ -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, ¶m)) + 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, ¶m)) { + // 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, ¶m)) + return false; + } + return true; #endif } diff --git a/uppsrc/Core/Mt.h b/uppsrc/Core/Mt.h index c35f16b6e..6ce2d8284 100644 --- a/uppsrc/Core/Mt.h +++ b/uppsrc/Core/Mt.h @@ -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); diff --git a/uppsrc/Core/src.tpp/Thread$en-us.tpp b/uppsrc/Core/src.tpp/Thread$en-us.tpp index 6f29d5dbe..9e9b596b9 100644 --- a/uppsrc/Core/src.tpp/Thread$en-us.tpp +++ b/uppsrc/Core/src.tpp/Thread$en-us.tpp @@ -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; ] \ No newline at end of file