/* This file is part of Lcrzo.
*/

int lcrzo_process_current_priority_set(lcrzo_int8 priority)
{ 
#if defined LCRZODEF_SYSTEM_Unix
  lcrzo_int8 unixprio;
  int ret;
  /* priority between -127->+127 to +20->-20 */
  switch(priority)
  { case -128 : unixprio=+20; break;
    case -127 : unixprio=+20; break;
    case 0    : unixprio=0;   break;
    case +127 : unixprio=-20; break;
    default :
      unixprio=-(priority*20)/127;
  }
  /* change priority */
  ret=setpriority(PRIO_PROCESS, 0, unixprio);
  if (ret<0) return(LCRZO_ERR_FUSETPRIORITY);
#elif defined LCRZODEF_SYSTEM_Windows
  int winprio;
  BOOL err;
  /* priority between -127->+127 to ... */
  if      (priority < -100) winprio=THREAD_PRIORITY_IDLE;
  else if (priority <  -60) winprio=THREAD_PRIORITY_LOWEST;
  else if (priority <    0) winprio=THREAD_PRIORITY_BELOW_NORMAL;
  else if (priority ==   0) winprio=THREAD_PRIORITY_NORMAL;
  else if (priority <=  60) winprio=THREAD_PRIORITY_ABOVE_NORMAL;
  else if (priority <= 100) winprio=THREAD_PRIORITY_HIGHEST;
  else                      winprio=THREAD_PRIORITY_TIME_CRITICAL;
  /* change priority */
  err=SetThreadPriority(GetCurrentThread(), winprio);
  if (!err) return(LCRZO_ERR_FUSETTHREADPRIORITY);
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

  return(LCRZO_ERR_OK);
}

