/* This file is part of Lcrzo.
*/

int lcrzo_process_current_priority_get(lcrzo_int8 *ppriority)
{
#if defined LCRZODEF_SYSTEM_Unix
  lcrzo_int8 unixprio;
  /* get priority */
  errno=0;
  unixprio=getpriority(PRIO_PROCESS, 0);
  if (unixprio==-1 && errno) return(LCRZO_ERR_FUGETPRIORITY);
  /* priority between +20->-20 to -127->+127 */
  if (ppriority!=NULL)
  { *ppriority=-(unixprio*127)/20;
  }
#elif defined LCRZODEF_SYSTEM_Windows
  int winprio;
  /* get priority */
  winprio=GetThreadPriority(GetCurrentThread());
  if (winprio==THREAD_PRIORITY_ERROR_RETURN) return(LCRZO_ERR_FUGETTHREADPRIORITY);
  /* priority between ... to -127->+127 */
  if (ppriority!=NULL)
  { switch (winprio)
    { case THREAD_PRIORITY_IDLE:          *ppriority=-127; break;
      case THREAD_PRIORITY_LOWEST:        *ppriority=-100; break;
      case THREAD_PRIORITY_BELOW_NORMAL:  *ppriority= -60; break;
      case THREAD_PRIORITY_NORMAL:        *ppriority=   0; break;
      case THREAD_PRIORITY_ABOVE_NORMAL:  *ppriority= +60; break;
      case THREAD_PRIORITY_HIGHEST:       *ppriority=+100; break;
      case THREAD_PRIORITY_TIME_CRITICAL: *ppriority=+127; break;
    }
  }
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif
  return(LCRZO_ERR_OK);
}

