//------------------------------------------------------------------------- void setThreadPriority( Thread::native_handle_type handle, ThreadPriorities threadPriority ) { #ifndef _WIN32 const int policy = SCHED_RR; const int minPrio = sched_get_priority_min(policy); const int maxPrio = sched_get_priority_max(policy); sched_param param; switch (threadPriority) { case ThreadPriority_LowPriority: param.sched_priority = minPrio + 1; break; case ThreadPriority_NormalPriority: param.sched_priority = (minPrio + maxPrio) / 2; break; case ThreadPriority_HighPriority: param.sched_priority = maxPrio - 3; break; case ThreadPriority_HighestPriority: param.sched_priority = maxPrio - 2; break; case ThreadPriority_RealtimePriority: param.sched_priority = maxPrio - 1; break; } pthread_setschedparam(handle, policy, ¶m); #else int priority = THREAD_PRIORITY_NORMAL; switch (threadPriority) { case ThreadPriority_LowPriority: priority = THREAD_PRIORITY_LOWEST; break; case ThreadPriority_NormalPriority: priority = THREAD_PRIORITY_NORMAL; break; case ThreadPriority_HighPriority: priority = THREAD_PRIORITY_ABOVE_NORMAL; break; case ThreadPriority_HighestPriority: priority = THREAD_PRIORITY_HIGHEST; break; case ThreadPriority_RealtimePriority: priority = THREAD_PRIORITY_TIME_CRITICAL; break; } #ifndef WINRT auto result = SetThreadPriority(handle, priority); assert(0 != result); #endif //ndef WINRT #endif //_WIN32 }
void AExecutable::SetThreadAffinity(boost::thread* daThread, unsigned short threadPriority, std::vector<short> CPUsToBind, int scheduler) { #ifndef __APPLE__ int policy; pthread_t threadID = (pthread_t) (daThread->native_handle()); if (scheduler > 0) { sched_param param; if (pthread_getschedparam(threadID, &policy, ¶m) != 0) { perror("pthread_getschedparam"); exit(EXIT_FAILURE); } /** * Set scheduling algorithm * Possible values: SCHED_FIFO, SCHED_RR, SCHED_OTHER */ policy = scheduler; param.sched_priority = threadPriority; if (pthread_setschedparam(threadID, policy, ¶m) != 0) { perror("pthread_setschedparam"); exit(EXIT_FAILURE); } } if (CPUsToBind.size() > 0) { /** * Bind the thread to CPUs from CPUsToBind */ cpu_set_t mask; CPU_ZERO(&mask); for (unsigned int i = 0; i < CPUsToBind.size(); i++) { if (CPUsToBind[i] == -1) { CPU_ZERO(&mask); break; } CPU_SET(CPUsToBind[i], &mask); } if (pthread_setaffinity_np(threadID, sizeof(mask), &mask) < 0) { throw NA62Error("Unable to bind threads to specific CPUs!"); } } #endif }
void myosd_init(void) { int res = 0; struct sched_param param; if (!lib_inited ) { printf("myosd_init\n"); //myosd_set_video_mode(320,240,320,240); printf("myosd_dbl_buffer %d\n",myosd_dbl_buffer); if(myosd_dbl_buffer) myosd_screen15 = myosd_screen; else myosd_screen15 = img_buffer; if(videot_running==0) { res = pthread_create(&main_tid, NULL, threaded_video, NULL); if(res!=0)printf("Error setting creating pthread %d \n",res); //param.sched_priority = 67; //param.sched_priority = 50; //param.sched_priority = 46; //param.sched_priority = 100; printf("video priority %d\n",video_thread_priority); param.sched_priority = video_thread_priority; int policy; if(video_thread_priority_type == 1) policy = SCHED_OTHER; else if(video_thread_priority_type == 2) policy = SCHED_RR; else policy = SCHED_FIFO; if(pthread_setschedparam(main_tid, policy, ¶m) != 0) printf("Error setting pthread priority\n"); videot_running = 1; } lib_inited = 1; } }
static void g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority) { #ifdef HAVE_PRIORITIES # ifdef G_THREADS_IMPL_POSIX struct sched_param sched; int policy; posix_check_for_error (pthread_getschedparam (*(pthread_t*)thread, &policy, &sched)); sched.sched_priority = g_thread_map_priority (priority); posix_check_for_error (pthread_setschedparam (*(pthread_t*)thread, policy, &sched)); # else /* G_THREADS_IMPL_DCE */ posix_check_for_error (pthread_setprio (*(pthread_t*)thread, g_thread_map_priority (priority))); # endif #endif /* HAVE_PRIORITIES */ }
int sal_splhi(void) { #ifdef SAL_SPL_NO_PREEMPT struct sched_param param; int policy; if (pthread_getschedparam(pthread_self(), &policy, ¶m) == 0) { /* Interrupt thread uses SCHED_RR and should be left alone */ if (policy != SCHED_RR) { param.sched_priority = 90; pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); } } #endif sal_mutex_take(spl_mutex, sal_mutex_FOREVER); return ++spl_level; }
void Core_ThreadImpl::startImpl(Runnable& target) { if (m_pData->pTarget) { return; } m_pData->pTarget = ⌖ m_pData->isRun=true; pthread_create(&m_pData->thread, NULL, entry, this); if (m_pData->prio != PRIO_NORMAL_IMPL) { struct sched_param par; par.sched_priority = mapPrio(m_pData->prio); pthread_setschedparam(m_pData->thread, SCHED_OTHER, &par); } }
/** Called during startup to increase thread priority. */ void Thread::high_priority() { struct sched_param param; int policy; pthread_t id = pthread_self(); // save original scheduling parameters pthread_getschedparam(id, &normal_sched_policy_, &normal_thread_param_); // set to high priority param = normal_thread_param_; param.sched_priority = SCHED_HIGH_PRIORITY; // magick number policy = SCHED_RR; // realtime, round robin if (pthread_setschedparam(id, policy, ¶m)) { fprintf(stderr, "Could not set thread priority to %i (%s). You might need to run the application as super user.\n", param.sched_priority, strerror(errno)); } }
void datalink_module::start(void) { // create thread thread_running = true; the_thread = boost::thread( boost::bind(&datalink_module::loop, this)); struct sched_param thread_param; thread_param.sched_priority = 5; pthread_t threadID = (pthread_t) the_thread.native_handle(); int retcode; if ((retcode = pthread_setschedparam(threadID, SCHED_FIFO, &thread_param)) != 0) { errno = retcode; perror("pthread_setschedparam"); } }
bool BoostThreadPriority(SDL_Thread* inThread) { #if defined(_POSIX_PRIORITY_SCHEDULING) pthread_t theTargetThread = (pthread_t) SDL_GetThreadID(inThread); int theSchedulingPolicy; struct sched_param theSchedulingParameters; if(pthread_getschedparam(theTargetThread, &theSchedulingPolicy, &theSchedulingParameters) != 0) return false; theSchedulingParameters.sched_priority = sched_get_priority_max(theSchedulingPolicy); if(pthread_setschedparam(theTargetThread, theSchedulingPolicy, &theSchedulingParameters) != 0) return false; #endif return true; }
void ThreadImpl::setPriorityImpl(int prio) { if (prio != _pData->prio) { _pData->prio = prio; _pData->policy = SCHED_OTHER; if (isRunningImpl()) { struct sched_param par; struct MyStruct { }; par.sched_priority = mapPrio(_pData->prio, SCHED_OTHER); if (pthread_setschedparam(_pData->thread, SCHED_OTHER, &par)) throw SystemException("cannot set thread priority"); } } }
static void set_realtime_prio () { #ifdef HAVE_SCHED_GET_PRIORITY_MAX int rc; if (options_get_int("UseRealtimePriority")) { struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_RR); rc = pthread_setschedparam (pthread_self (), SCHED_RR, ¶m); if (rc != 0) logit ("Can't set realtime priority: %s", strerror (rc)); } #else logit ("No sched_get_priority_max() function: realtime priority not " "used."); #endif }
static void SC_LinuxSetRealtimePriority(pthread_t thread, int priority) { int policy; struct sched_param param; pthread_getschedparam(thread, &policy, ¶m); policy = SCHED_FIFO; const int minprio = sched_get_priority_min(policy); const int maxprio = sched_get_priority_max(policy); param.sched_priority = sc_clip(priority, minprio, maxprio); int err = pthread_setschedparam(thread, policy, ¶m); if (err != 0) { post("Couldn't set realtime scheduling priority %d: %s\n", param.sched_priority, strerror(err)); } }
bool Thread::setPriority(int prior) { #ifdef _WIN32 if ( !handle ) return 0; if ( prior > 3 ) prior = 3; else if ( prior < - 4 ) prior = -4; int tp; switch( prior ) { case +3: tp = THREAD_PRIORITY_TIME_CRITICAL; break; case +2: tp = THREAD_PRIORITY_HIGHEST; break; case +1: tp = THREAD_PRIORITY_ABOVE_NORMAL; break; case 0: tp = THREAD_PRIORITY_NORMAL; break; case -1: tp = THREAD_PRIORITY_BELOW_NORMAL; break; case -2: tp = THREAD_PRIORITY_LOWEST; break; case -3: tp = THREAD_PRIORITY_IDLE; break; default: tp = THREAD_PRIORITY_NORMAL; } return SetThreadPriority( (HANDLE)handle, tp ) != FALSE; #else struct sched_param param; param.sched_priority = -prior; /*int ret =*/ pthread_setschedparam (*(pthread_t *)handle, SCHED_OTHER, ¶m); return 1; #endif }
/*! * \brief Sets the priority of the currently running thread. * * \internal * * \return * \li \c 0 on success. * \li \c EINVAL invalid priority or the result of GerLastError. */ static int SetPriority( /*! . */ ThreadPriority priority) { #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0 int retVal = 0; int currentPolicy; int minPriority = 0; int maxPriority = 0; int actPriority = 0; int midPriority = 0; struct sched_param newPriority; int sched_result; pthread_getschedparam(ithread_self(), ¤tPolicy, &newPriority); minPriority = sched_get_priority_min(currentPolicy); maxPriority = sched_get_priority_max(currentPolicy); midPriority = (maxPriority - minPriority) / 2; switch (priority) { case LOW_PRIORITY: actPriority = minPriority; break; case MED_PRIORITY: actPriority = midPriority; break; case HIGH_PRIORITY: actPriority = maxPriority; break; default: retVal = EINVAL; goto exit_function; }; newPriority.sched_priority = actPriority; sched_result = pthread_setschedparam(ithread_self(), currentPolicy, &newPriority); retVal = (sched_result == 0 || errno == EPERM) ? 0 : sched_result; exit_function: return retVal; #else return 0; priority = priority; #endif }
/* naming : pthread_create = start */ int mas_ticker_start( const mas_options_t * popts ) { CTRL_PREPARE; /* EVAL_PREPARE; */ int r = 0; MFP( "\x1b]2;starting ticker; mode:%d\x7", ctrl.ticker_mode ); if ( !ctrl.threads.n.ticker.thread ) { { ( void ) /* r = */ pthread_attr_getstack( &ctrl.thglob.ticker_attr, &ticker_stackaddr, &ticker_stacksize ); tMSG( "creating ticker thread stack:%lu @ %p", ( unsigned long ) ticker_stacksize, ticker_stackaddr ); HMSG( "+ TICKER mode %d", ctrl.ticker_mode ); } /* if ( !tmp ) */ /* tmp = mas_malloc( 4321 ); */ MAS_LOG( "starting ticker th." ); /* r = mas_xpthread_create( &ctrl.threads.n.ticker.thread, mas_ticker_th, MAS_THREAD_TICKER, NULL ); */ r = pthread_create( &ctrl.threads.n.ticker.thread, &ctrl.thglob.ticker_attr, mas_ticker_th, NULL ); #ifdef SCHED_IDLE { int policy, rs; struct sched_param sched; rs = pthread_getschedparam( ctrl.threads.n.ticker.thread, &policy, &sched ); /* SCHED_IDLE ... SCHED_RR */ rs = pthread_setschedparam( ctrl.threads.n.ticker.thread, SCHED_IDLE, &sched ); /* rs = pthread_getschedparam( ctrl.threads.n.ticker.thread, &policy, &sched ); */ MAS_LOG( "(%d) created(?) ticker thread [%lx] %d - %d (%d)", r, ctrl.threads.n.ticker.thread, policy, sched.sched_priority, rs ); tMSG( "(%d) created(?) ticker thread [%lx] %d - %d (%d)", r, ctrl.threads.n.ticker.thread, policy, sched.sched_priority, rs ); } #else MAS_LOG( "(%d) created(?) ticker thread [%lx]", r, ctrl.threads.n.ticker.thread ); #endif } else { MAS_LOG( "running w/o ticker th." ); } return r; }
void set_thread_priority(pthread_t thread, const gchar * name, gboolean realtime, gint priority) { struct sched_param param; int policy, priority_min, priority_max; int err; if ((err = pthread_getschedparam(thread, &policy, ¶m)) != 0) { g_debug("cannot get scheduling policy for %s thread: %s", name, g_strerror(err)); return; } if (realtime) { policy = SCHED_FIFO; } priority_min = sched_get_priority_min(policy); priority_max = sched_get_priority_max(policy); if (priority != -1) { if (priority < priority_min) { g_warning("%s thread priority (%d) too low, set to %d", name, priority, priority_min); priority = priority_min; } else if (priority > priority_max) { g_warning("%s thread priority (%d) too high, set to %d", name, priority, priority_max); priority = priority_max; } param.sched_priority = priority; } else { if (param.sched_priority < priority_min) { param.sched_priority = priority_min; } else if (param.sched_priority > priority_max) { param.sched_priority = priority_max; } } if ((err = pthread_setschedparam(thread, policy, ¶m)) != 0) { g_warning("cannot set scheduling policy for %s thread: %s", name, g_strerror(err)); } }
void Threads::setThreadPriority(BaseLib::Obj* baseLib, pthread_t thread, int32_t priority, int32_t policy) { try { if(!baseLib->settings.prioritizeThreads()) return; if(priority == -1) { baseLib->out.printWarning("Warning: Priority of -1 was passed to setThreadPriority."); priority = 0; policy = SCHED_OTHER; } if(policy == SCHED_OTHER) return; if((policy == SCHED_FIFO || policy == SCHED_RR) && (priority < 1 || priority > 99)) throw Exception("Invalid thread priority for SCHED_FIFO or SCHED_RR: " + std::to_string(priority)); else if((policy == SCHED_IDLE || policy == SCHED_BATCH) && priority != 0) throw Exception("Invalid thread priority for SCHED_IDLE: " + std::to_string(priority)); sched_param schedParam; schedParam.sched_priority = priority; int32_t error; //Only use SCHED_FIFO or SCHED_RR if((error = pthread_setschedparam(thread, policy, &schedParam)) != 0) { if(error == EPERM) { baseLib->out.printError("Could not set thread priority. The executing user does not have enough privileges. Please run \"ulimit -r 100\" before executing Homegear."); } else if(error == ESRCH) baseLib->out.printError("Could not set thread priority. Thread could not be found."); else if(error == EINVAL) baseLib->out.printError("Could not set thread priority: policy is not a recognized policy, or param does not make sense for the policy."); else baseLib->out.printError("Error: Could not set thread priority to " + std::to_string(priority) + " Error: " + std::to_string(error)); baseLib->settings.setPrioritizeThreads(false); } else baseLib->out.printDebug("Debug: Thread priority successfully set to: " + std::to_string(priority), 7); } catch(const std::exception& ex) { baseLib->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(const Exception& ex) { baseLib->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { baseLib->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
//------------------------------------------------------------------------------ tOplkError hrestimer_addInstance(void) { tOplkError ret = kErrorOk; UINT index; struct sched_param schedParam; tHresTimerInfo* pTimerInfo; OPLK_MEMSET(&hresTimerInstance_l, 0, sizeof(hresTimerInstance_l)); /* Initialize timer threads for all usable timers. */ for (index = 0; index < TIMER_COUNT; index++) { pTimerInfo = &hresTimerInstance_l.aTimerInfo[index]; pTimerInfo->fTerminate = FALSE; #ifdef HIGH_RESK_TIMER_LATENCY_DEBUG pTimerInfo->maxLatency = 0; pTimerInfo->minLatency = 999999999; #endif if (sem_init(&pTimerInfo->syncSem, 0, 0) != 0) { DEBUG_LVL_ERROR_TRACE("%s() Couldn't init semaphore!\n", __func__); return kErrorNoResource; } if (pthread_create(&pTimerInfo->timerThreadId, NULL, timerThread, pTimerInfo) != 0) { sem_destroy(&pTimerInfo->syncSem); return kErrorNoResource; } schedParam.__sched_priority = CONFIG_THREAD_PRIORITY_HIGH; if (pthread_setschedparam(pTimerInfo->timerThreadId, SCHED_FIFO, &schedParam) != 0) { DEBUG_LVL_ERROR_TRACE("%s() Couldn't set thread scheduling parameters!\n", __func__); sem_destroy(&pTimerInfo->syncSem); pthread_cancel(pTimerInfo->timerThreadId); return kErrorNoResource; } } return ret; }
int32_t vm_thread_set_scheduling(vm_thread* thread, void* params) { int32_t i_res = 1; struct sched_param param; vm_thread_linux_schedparams* linux_params = (vm_thread_linux_schedparams*)params; /* check error(s) */ if (!thread || !linux_params) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); param.sched_priority = linux_params->priority; i_res = !pthread_setschedparam(thread->handle, linux_params->schedtype, ¶m); vm_mutex_unlock(&thread->access_mut); } return i_res; }
void Thread::setPriority(Priority _priority) { #if ARX_HAVE_SCHED_GETSCHEDULER int policy = sched_getscheduler(0); #else int policy = SCHED_RR; #endif int min = sched_get_priority_min(policy); int max = sched_get_priority_max(policy); priority = min + ((_priority - Lowest) * (max - min) / (Highest - Lowest)); if(started && min != max) { sched_param param; param.sched_priority = priority; pthread_setschedparam(thread, policy, ¶m); } }
bool BoostThisThread() { #ifdef WIN32 std::cerr<<"MOOS::BoostThisThread is not supported in WIN32 (yet)\n"; return false; #else try { int policy; struct sched_param param; if(pthread_getschedparam(pthread_self(), &policy, ¶m)!=0) { throw std::runtime_error("MOOS::BoostThisThread() failed to get this thread's scheduling details"); } //std::cout<<"default priority"<< param.sched_priority<<"\n"; int max_priority; if((max_priority = sched_get_priority_max(policy))==-1) { throw std::runtime_error("MOOS::BoostThisThread() failed to get this thread's max priority"); } //std::cout<<"max priority"<< param.sched_priority<<"\n"; param.sched_priority+=(max_priority-param.sched_priority)/2; if(pthread_setschedparam(pthread_self(), policy, ¶m)!=0) { throw std::runtime_error("MOOS::BoostThisThread() failed to increase this thread's priority"); } //std::cerr<<"boosted IO of thread "<<pthread_self()<<"\n"; } catch(const std::runtime_error & e) { std::cerr<<e.what()<<" "<<strerror(errno)<<"\n"; return false; } return true; #endif }
//============================================================================== JUCE_API void JUCE_CALLTYPE Process::setPriority (const ProcessPriority prior) { const int policy = (prior <= NormalPriority) ? SCHED_OTHER : SCHED_RR; const int minp = sched_get_priority_min (policy); const int maxp = sched_get_priority_max (policy); struct sched_param param; switch (prior) { case LowPriority: case NormalPriority: param.sched_priority = 0; break; case HighPriority: param.sched_priority = minp + (maxp - minp) / 4; break; case RealtimePriority: param.sched_priority = minp + (3 * (maxp - minp) / 4); break; default: jassertfalse; break; } pthread_setschedparam (pthread_self(), policy, ¶m); }
int test_priority2(void) #endif { pthread_t t; void * result = NULL; int result2; struct sched_param param; assert((maxPrio = sched_get_priority_max(SCHED_OTHER)) != -1); assert((minPrio = sched_get_priority_min(SCHED_OTHER)) != -1); assert(pthread_create(&t, NULL, getValidPriorities, NULL) == 0); assert(pthread_join(t, &result) == 0); assert(pthread_barrier_init(&startBarrier, NULL, 2) == 0); assert(pthread_barrier_init(&endBarrier, NULL, 2) == 0); /* Set the thread's priority to a known initial value. * If the new priority is invalid then the threads priority * is unchanged from the previous value. */ SetThreadPriority(pthread_getw32threadhandle_np(pthread_self()), PTW32TEST_THREAD_INIT_PRIO); for (param.sched_priority = minPrio; param.sched_priority <= maxPrio; param.sched_priority++) { assert(pthread_create(&t, NULL, func, NULL) == 0); assert(pthread_setschedparam(t, SCHED_OTHER, ¶m) == 0); result2 = pthread_barrier_wait(&startBarrier); assert(result2 == 0 || result2 == PTHREAD_BARRIER_SERIAL_THREAD); result2 = pthread_barrier_wait(&endBarrier); assert(result2 == 0 || result2 == PTHREAD_BARRIER_SERIAL_THREAD); assert(GetThreadPriority(pthread_getw32threadhandle_np(t)) == validPriorities[param.sched_priority+(PTW32TEST_MAXPRIORITIES/2)]); pthread_join(t, &result); assert(param.sched_priority == (int)(size_t)result); } return 0; }
/* Un nouveau rédacteur arrive dans un thread séparé */ void* evenementNouveauRedacteur(void* nouveauRedacteurVoid) { int priority = 0; pthread_t self = pthread_self(); priority = sched_get_priority_min(SCHED_FIFO); struct sched_param param; param.sched_priority = priority; // modification de la priorité du lecteur a la priorité min pthread_setschedparam(self, SCHED_FIFO, ¶m); redacteur* nouveauRedacteur = (redacteur*) nouveauRedacteurVoid; pthread_mutex_lock(&mutConsole); // console protégée par un mutex std::cout << "Redacteur " << nouveauRedacteur->id << " demande la ressource" << std::endl; pthread_mutex_unlock(&mutConsole); ecrireRessource(nouveauRedacteur); pthread_exit(NULL); return NULL; // on n'utilisera pas le paramètre de retour }
void *inc_count(void *t) { int i, ret; long my_id = (long)t; struct timespec twait; struct sched_param param; cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0, &mask); ret = sched_setaffinity(0, sizeof(mask), &mask); if (ret != 0) { printf("pthread_setaffinity failed\n"); exit(EXIT_FAILURE); } param.sched_priority = 93; ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); if (ret != 0) { printf("pthread_setschedparam failed\n"); exit(EXIT_FAILURE); } printf("Starting inc_count(): thread %ld prio 93\n", my_id); pthread_mutex_lock(&count_mutex); /* Do some work (e.g., fill up the queue) */ twait = usec_to_timespec(6000000L); busywait(&twait); count++; printf("inc_count(): thread %ld, count = %d\n", my_id, count); pthread_cond_signal(&count_threshold_cv); printf("Just sent signal.\n"); printf("inc_count(): thread %ld, count = %d, unlocking mutex\n", my_id, count); pthread_mutex_unlock(&count_mutex); pthread_exit(NULL); }
static PaError BoostPriority( PaAlsaThreading *th ) { PaError result = paNoError; struct sched_param spm = { 0 }; spm.sched_priority = th->rtPrio; assert( th ); if( pthread_setschedparam( th->callbackThread, SCHED_FIFO, &spm ) != 0 ) { PA_UNLESS( errno == EPERM, paInternalError ); /* Lack permission to raise priority */ PA_DEBUG(( "Failed bumping priority\n" )); result = 0; } else result = 1; /* Success */ error: return result; }
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) { #ifdef __LINUX__ int value; if (priority == SDL_THREAD_PRIORITY_LOW) { value = 19; } else if (priority == SDL_THREAD_PRIORITY_HIGH) { value = -20; } else { value = 0; } if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) { /* Note that this fails if you're trying to set high priority and you don't have root permission. BUT DON'T RUN AS ROOT! */ return SDL_SetError("setpriority() failed"); } return 0; #else struct sched_param sched; int policy; pthread_t thread = pthread_self(); if (pthread_getschedparam(thread, &policy, &sched) < 0) { return SDL_SetError("pthread_getschedparam() failed"); } if (priority == SDL_THREAD_PRIORITY_LOW) { sched.sched_priority = sched_get_priority_min(policy); } else if (priority == SDL_THREAD_PRIORITY_HIGH) { sched.sched_priority = sched_get_priority_max(policy); } else { int min_priority = sched_get_priority_min(policy); int max_priority = sched_get_priority_max(policy); sched.sched_priority = (min_priority + (max_priority - min_priority) / 2); } if (pthread_setschedparam(thread, policy, &sched) < 0) { return SDL_SetError("pthread_setschedparam() failed"); } return 0; #endif /* linux */ }
int EXTThread::setPriority(int Priority, bool Realtime) { #ifdef _WIN32 auto thread = m_thread.native_handle(); #else auto thread = m_thread; #endif #ifdef __linux__ sched_param param; int policy; pthread_getschedparam(m_thread, &policy, ¶m); param.sched_priority = Priority; if (Realtime) { // for realtime threads, use SCHED_RR policy policy = SCHED_RR; } int result = pthread_setschedparam(thread, policy, ¶m); if (result) { printf("Error: failed to set thread priority: %s\n", strerror(result)); return 0; } return 1; #elif __APPLE__ struct thread_time_constraint_policy ttcpolicy; int result; // OSX magic numbers ttcpolicy.period = uint32_t(UNIV::SAMPLE_RATE / 100); // HZ/160 ttcpolicy.computation = uint32_t(UNIV::SAMPLE_RATE / 143); // HZ/3300; ttcpolicy.constraint = uint32_t(UNIV::SAMPLE_RATE / 143); // HZ/2200; ttcpolicy.preemptible = 1; // 1 result = thread_policy_set(pthread_mach_thread_np(thread), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&ttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT); if (result != KERN_SUCCESS) { printf("Error: failed to set thread priority: %s\n", strerror(result)); return 0; } return 1; #else printf("Error: cannot set thread priority on Windows\n"); return 0; #endif }
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_) { #if defined _POSIX_THREAD_PRIORITY_SCHEDULING && _POSIX_THREAD_PRIORITY_SCHEDULING >= 0 int policy = 0; struct sched_param param; #if _POSIX_THREAD_PRIORITY_SCHEDULING == 0 && defined _SC_THREAD_PRIORITY_SCHEDULING if (sysconf(_SC_THREAD_PRIORITY_SCHEDULING) < 0) { return; } #endif int rc = pthread_getschedparam(descriptor, &policy, ¶m); posix_assert (rc); if(priority_ != -1) { param.sched_priority = priority_; } if(schedulingPolicy_ != -1) { policy = schedulingPolicy_; } #ifdef __NetBSD__ if(policy == SCHED_OTHER) param.sched_priority = -1; #endif rc = pthread_setschedparam(descriptor, policy, ¶m); #if defined(__FreeBSD_kernel__) || defined (__FreeBSD__) // If this feature is unavailable at run-time, don't abort. if(rc == ENOSYS) return; #endif posix_assert (rc); #else LIBZMQ_UNUSED (priority_); LIBZMQ_UNUSED (schedulingPolicy_); #endif }
NvResult CNvThreadingLinux::ThreadPrioritySet(Handle uThreadHandle, S32 sPriority) { CNvThreadData *pThreadData = reinterpret_cast<CNvThreadData *>(uThreadHandle); if (pThreadData) { // Compute priority is relative to base if (m_iSchedPolicy == SCHED_OTHER) pThreadData->priority = m_iSchedPriorityBase - sPriority; else pThreadData->priority = m_iSchedPriorityBase + sPriority; // Ensure priority is within limits if (pThreadData->priority < m_iSchedPriorityMin) pThreadData->priority = m_iSchedPriorityMin; else if (pThreadData->priority > m_iSchedPriorityMax) pThreadData->priority = m_iSchedPriorityMax; if (m_iSchedPolicy == SCHED_OTHER) { if (pThreadData->pid == 0) return RESULT_FAIL; // Impossible since ThreadCreate waited on ThreadFunc if (setpriority(PRIO_PROCESS, pThreadData->pid, pThreadData->priority)) return RESULT_FAIL; } else { struct sched_param oSched; oSched.sched_priority = pThreadData->priority; if (pthread_setschedparam(pThreadData->thread, m_iSchedPolicy, &oSched)) return RESULT_FAIL; } return RESULT_OK; } else { return RESULT_INVALID_HANDLE; } }