summaryrefslogtreecommitdiff
path: root/TAO/tao/RTCORBA/Linear_Priority_Mapping.cpp
blob: f45c7ae3ea0454194f7fdfe630d3f0cac5c39776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// $Id$

#include "tao/orbconf.h"

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

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

ACE_RCSID(Strategies, Linear_Priority_Mapping, "$Id$")

TAO_Linear_Priority_Mapping::TAO_Linear_Priority_Mapping (long 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 < RTCORBA::minPriority ||
      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 */
}