summaryrefslogtreecommitdiff
path: root/TAO/tao/RTCORBA/Continuous_Priority_Mapping.cpp
blob: 870a14485e85776ddc1eaee340bc25e8210edb00 (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
// $Id$

#include "tao/RTCORBA/Continuous_Priority_Mapping.h"

#if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0

#include "ace/Sched_Params.h"

ACE_RCSID(RTCORBA, Continuous_Priority_Mapping, "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_Continuous_Priority_Mapping::TAO_Continuous_Priority_Mapping (int policy)
  :  policy_ (policy)
   , min_ (ACE_Sched_Params::priority_min (this->policy_))
   , max_ (ACE_Sched_Params::priority_max (this->policy_))
{
}

TAO_Continuous_Priority_Mapping::~TAO_Continuous_Priority_Mapping (void)
{
}

CORBA::Boolean
TAO_Continuous_Priority_Mapping::to_native (RTCORBA::Priority corba_priority,
                                            RTCORBA::NativePriority &native_priority)
{
  if (corba_priority < 0)
    return false;

#if defined (ACE_WIN32)

  int current_native_priority = this->min_;
  int next_native_priority;
  for (int i = 1; i <= corba_priority; ++i)
    {
      next_native_priority =
        ACE_Sched_Params::next_priority (this->policy_,
                                         current_native_priority);

      if (next_native_priority == current_native_priority)
        return false;

      current_native_priority = next_native_priority;
    }

  native_priority = static_cast<RTCORBA::NativePriority> (current_native_priority);
  return true;

#else

  if (this->min_ < this->max_)
    {
      native_priority = corba_priority + this->min_;
      if (native_priority > this->max_)
        return false;
    }
  else if (this->min_ > this->max_)
    {
      native_priority = this->min_ - corba_priority;
      if (native_priority < this->max_)
        return false;
    }
  else
    {
      // There is only one native priority.
      if (corba_priority != 0)
        return false;

      native_priority = this->min_;
    }

  return true;

#endif /* ACE_WIN32 */

}

CORBA::Boolean
TAO_Continuous_Priority_Mapping::to_CORBA (RTCORBA::NativePriority native_priority,
                                           RTCORBA::Priority &corba_priority)
{

#if defined (ACE_WIN32)

  int current_native_priority = this->min_;
  for (corba_priority = 0; ; ++corba_priority)
    {
      if (current_native_priority == native_priority)
        return true;

      else if (current_native_priority == this->max_)
        return false;

      else
        current_native_priority =
          ACE_Sched_Params::next_priority (this->policy_,
                                           current_native_priority);
    }

#else

  if (this->min_ < this->max_)
    {
      if (native_priority < this->min_
          || native_priority > this->max_)
        return false;
      corba_priority = native_priority - this->min_;
    }
  else if (this->min_ > this->max_)
    {
      if (native_priority > this->min_
          || native_priority < this->max_)
        return false;
      corba_priority = this->min_ - native_priority;
    }
  else if (this->min_ == this->max_)
    {
      if (native_priority != this->min_)
        return false;
      corba_priority = 0;
    }

  return true;

#endif /* ACE_WIN32 */

}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */