// Linear_Priority_Mapping.cpp,v 1.4 1999/12/23 18:56:19 irfan Exp

#include "tao/orbconf.h"

#if (TAO_HAS_RT_CORBA == 1)

#include "tao/Linear_Priority_Mapping.h"
#include "tao/debug.h"
#include "ace/Sched_Params.h"

#if !defined (__ACE_INLINE__)
# include "tao/Linear_Priority_Mapping.i"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID(tao, Linear_Priority_Mapping, "Linear_Priority_Mapping.cpp,v 1.4 1999/12/23 18:56:19 irfan Exp")

TAO_Linear_Priority_Mapping::TAO_Linear_Priority_Mapping (int policy)
  :  policy_ (policy)
{
  this->min_ = ACE_Sched_Params::priority_min (this->policy_);
  this->max_ = ACE_Sched_Params::priority_max (this->policy_);
}

TAO_Linear_Priority_Mapping::~TAO_Linear_Priority_Mapping (void)
{
}

CORBA::Boolean
TAO_Linear_Priority_Mapping::to_native (RTCORBA::Priority corba_priority,
                                        RTCORBA::NativePriority &native_priority)
{
  if (corba_priority < 0 || corba_priority > RTCORBA::maxPriority)
    return 0;

#if defined (ACE_WIN32)
  // Count up the number of distinct native priorities on current
  // platform.
  int n;
  int current_priority = this->min_;
  for (n = 1; current_priority != this->max_; ++n)
    {
      current_priority = 
        ACE_Sched_Params::next_priority (this->policy_,
                                         current_priority);
    }
  int native_priority_index = 
    1 
    + ((n - 1) 
       * corba_priority 
       / (RTCORBA::maxPriority - RTCORBA::minPriority));

  // Now, find the value corresponding to this index.
  native_priority = this->min_;
  for (int i = 2; i <= native_priority_index; ++i)
    {
      native_priority = ACE_Sched_Params::next_priority (this->policy_,
                                                         native_priority);
    }
  return 1;

#else

  native_priority =
    this->min_
    + ((this->max_ - this->min_)
       * corba_priority
       / (RTCORBA::maxPriority - RTCORBA::minPriority));

  return 1;

#endif /* ACE_WIN32 */
}

CORBA::Boolean
TAO_Linear_Priority_Mapping::to_CORBA (RTCORBA::NativePriority native_priority,
                                       RTCORBA::Priority &corba_priority)
{
#if defined (ACE_WIN32)

  // Iterate over native priorities in order to 1) make sure
  // <native_priority> argument contains a valid value, 2) count its
  // index among all valid native pr. values, 3) get the total number
  // of native priority values for the current platform.

  int total;
  int native_priority_index = 0;
  int current_priority = this->min_;
  for (total = 1; ; ++total)
    {
      if (native_priority == current_priority)
        native_priority_index = total;
      
      if (current_priority == this->max_)
        break;

      current_priority =
        ACE_Sched_Params::next_priority (this->policy_,
                                         current_priority);
    }
  
  if (native_priority_index == 0)
    return 0;

  int delta = total - 1;
  if (delta != 0)
    {
      corba_priority =
        RTCORBA::minPriority
        + ((RTCORBA::maxPriority - RTCORBA::minPriority)
           * (native_priority_index - 1) / delta);
    }
  else
    {
      // There is only one native priority.
      corba_priority = RTCORBA::minPriority;
    }

  return 1;

#else

  if ((this->min_ < this->max_
       && (native_priority < this->min_
           || native_priority > this->max_))
      || (this->min_ > this->max_
          && (native_priority < this->max_
              || native_priority > this->min_)))
    {
      if (TAO_debug_level > 2)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - Linear_Priority_Mapping::to_CORBA: "
                    " priority %d out of range [%d,%d]\n",
                    native_priority, this->min_, this->max_));
      return 0;
    }

  int delta = this->max_ - this->min_;
  if (delta != 0)
    {
      corba_priority =
        RTCORBA::minPriority
        + ((RTCORBA::maxPriority - RTCORBA::minPriority)
           * (native_priority - this->min_) / delta);
    }
  else
    {
      // There is only one native priority.
      if (native_priority != this->min_)
        return 0;
      corba_priority = RTCORBA::minPriority;
    }

  return 1;

#endif /* ACE_WIN32 */
}

#endif /* TAO_HAS_RT_CORBA == 1 */
