leapmotion/autowiring

Implement SetThreadPriority on Unix

Closed this issue · 2 comments

I'm tired of looking at the warning in PriorityBoost.cpp, it's about time we got around to implementing this.

It's likely all we need to do is to call the nice function or near equivalent, we just need to make sure we do so in a way that will also work properly on Mac.

This is some initial code I had on a branch from 20 months ago in CoreThreadUnix.cpp:

#include <pthread.h>
#if __APPLE__
// Missing definitions from pthreads.h
#if !defined(PTHREAD_MIN_PRIORITY)
#define PTHREAD_MIN_PRIORITY  0
#endif
#if !defined(PTHREAD_MAX_PRIORITY)
#define PTHREAD_MAX_PRIORITY 31
#endif
#endif
void CoreThread::SetThreadPriority(ThreadPriority threadPriority) {
  struct sched_param param = { 0 };
  int policy = SCHED_RR;
  int percent = 0;

  switch(threadPriority) {
  case ThreadPriority::Idle:
#if !__APPLE__
    policy = SCHED_IDLE;
#endif
    percent = 0;
    break;
  case ThreadPriority::Lowest:
    percent = 1;
    break;
  case ThreadPriority::BelowNormal:
    percent = 20;
    break;
  case ThreadPriority::Normal:
    percent = 50;
    break;
  case ThreadPriority::AboveNormal:
    percent = 66;
    break;
  case ThreadPriority::Highest:
    percent = 83;
    break;
  case ThreadPriority::TimeCritical:
    percent = 100;
    break;
  default:
    throw std::runtime_error("Attempted to assign an unrecognized thread priority");
  }
  param.sched_priority = PTHREAD_MIN_PRIORITY + (percent*(PTHREAD_MAX_PRIORITY - PTHREAD_MIN_PRIORITY) + 50)/100;

  pthread_setschedparam(m_thisThread.native_handle(), policy, &param);
}

Lol well shit. 20 months ago.