00001
00002
00003 #include "tao/orbconf.h"
00004
00005 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
00006
00007 #include "tao/RTCORBA/Multi_Priority_Mapping.h"
00008 #include "tao/debug.h"
00009 #include "ace/Log_Msg.h"
00010 #include "ace/Sched_Params.h"
00011
00012 ACE_RCSID (RTCORBA,
00013 Multi_Priority_Mapping,
00014 "$Id: Multi_Priority_Mapping.cpp 79339 2007-08-14 17:49:17Z sowayaa $")
00015
00016 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00017
00018 TAO_Multi_Priority_Mapping::TAO_Multi_Priority_Mapping (
00019 int base_native_priority,
00020 int base_corba_priority,
00021 int priority_spacing,
00022 int priorities_contiguous,
00023 int policy)
00024 : base_native_priority_ (base_native_priority)
00025 , base_corba_priority_ (base_corba_priority)
00026 , priority_spacing_ (priority_spacing)
00027 , priorities_contiguous_(priorities_contiguous)
00028 , policy_ (policy)
00029 , min_ (ACE_Sched_Params::priority_min (this->policy_))
00030 , max_ (ACE_Sched_Params::priority_max (this->policy_))
00031 {
00032 if ( this->min_ < this->max_ )
00033 {
00034 if (base_native_priority_ < this->min_)
00035 {
00036 if (TAO_debug_level > 2)
00037 {
00038 ACE_DEBUG ((LM_DEBUG,
00039 "TAO (%P|%t) - Multi_Priority_Mapping::ctor: "
00040 " base_native_priority %d out of range [%d,%d]\n",
00041 base_native_priority_, this->min_, this->max_));
00042 }
00043 }
00044 }
00045 else
00046 {
00047 if (base_native_priority_ > this->min_)
00048 {
00049 if (TAO_debug_level > 2)
00050 {
00051 ACE_DEBUG ((LM_DEBUG,
00052 "TAO (%P|%t) - Multi_Priority_Mapping::ctor: "
00053 " base_native_priority %d out of range [%d,%d]\n",
00054 base_native_priority_, this->min_, this->max_));
00055 }
00056 }
00057 }
00058
00059 if (base_corba_priority_ > RTCORBA::maxPriority)
00060 {
00061 if (TAO_debug_level > 2)
00062 {
00063 ACE_DEBUG ((LM_DEBUG,
00064 "TAO (%P|%t) - Multi_Priority_Mapping::ctor: "
00065 " base_corba_priority %d out of range [%d,%d]\n",
00066 base_corba_priority_, RTCORBA::minPriority, RTCORBA::maxPriority));
00067 }
00068 }
00069 }
00070
00071 TAO_Multi_Priority_Mapping::~TAO_Multi_Priority_Mapping (void)
00072 {
00073 }
00074
00075 CORBA::Boolean
00076 TAO_Multi_Priority_Mapping::to_native (RTCORBA::Priority corba_priority,
00077 RTCORBA::NativePriority &native_priority)
00078 {
00079
00080 if (corba_priority < RTCORBA::minPriority || corba_priority > this->base_corba_priority_ )
00081 {
00082 if (TAO_debug_level > 2)
00083 {
00084 ACE_DEBUG ((LM_DEBUG,
00085 "TAO (%P|%t) - Multi_Priority_Mapping::to_native: "
00086 " corba priority %d out of range [%d,%d]\n",
00087 corba_priority, RTCORBA::minPriority, this->base_corba_priority_));
00088 }
00089 return 0;
00090 }
00091
00092 if (corba_priority == base_corba_priority_)
00093 {
00094
00095 native_priority = static_cast<RTCORBA::NativePriority> (base_native_priority_);
00096 }
00097 else
00098 {
00099 if (priorities_contiguous_ == 1)
00100 {
00101 if ( this->min_ < this->max_ )
00102 {
00103 native_priority = static_cast<RTCORBA::NativePriority>
00104 (((corba_priority - base_corba_priority_) / priority_spacing_) + base_native_priority_);
00105 }
00106 else
00107 {
00108 native_priority = static_cast<RTCORBA::NativePriority>
00109 (((base_corba_priority_ - corba_priority) / priority_spacing_) + base_native_priority_);
00110 }
00111 }
00112 else
00113 {
00114
00115 int last_priority = this->base_corba_priority_;
00116 while (true)
00117 {
00118 if (last_priority == RTCORBA::minPriority) break;
00119 if (base_corba_priority_ >= --last_priority) break;
00120 }
00121
00122 int priority_ndx = 0;
00123 while (true)
00124 {
00125 if (last_priority == RTCORBA::minPriority) break;
00126 if (corba_priority >= --last_priority) break;
00127 priority_ndx++;
00128 }
00129
00130
00131 last_priority = this->base_native_priority_;
00132 for (int current_ndx = 0; current_ndx < priority_ndx; current_ndx++)
00133 {
00134 native_priority = static_cast<RTCORBA::NativePriority>
00135 (ACE_Sched_Params::previous_priority (this->policy_,
00136 last_priority,
00137 ACE_SCOPE_THREAD));
00138 }
00139 }
00140 }
00141
00142 return 1;
00143 }
00144
00145 CORBA::Boolean
00146 TAO_Multi_Priority_Mapping::to_CORBA (RTCORBA::NativePriority native_priority,
00147 RTCORBA::Priority &corba_priority)
00148 {
00149
00150 if ((this->min_ < this->max_ && (native_priority < this->min_ || native_priority > this->base_native_priority_ )) ||
00151 (this->min_ > this->max_ && (native_priority < this->base_corba_priority_ || native_priority > this->min_)))
00152 {
00153 if (TAO_debug_level > 2)
00154 {
00155 ACE_DEBUG ((LM_DEBUG,
00156 "TAO (%P|%t) - Multi_Priority_Mapping::to_CORBA: "
00157 " priority %d out of range [%d,%d]\n",
00158 native_priority, this->min_, this->base_corba_priority_));
00159 }
00160 return 0;
00161 }
00162
00163 if (native_priority == base_native_priority_)
00164 {
00165
00166 corba_priority = static_cast<RTCORBA::Priority> (base_corba_priority_);
00167 }
00168 else
00169 {
00170 if (priorities_contiguous_ == 1)
00171 {
00172 if ( this->min_ < this->max_ )
00173 {
00174 corba_priority = static_cast<RTCORBA::Priority>
00175 (((native_priority - base_native_priority_) * priority_spacing_) + base_corba_priority_);
00176 }
00177 else
00178 {
00179 corba_priority = static_cast<RTCORBA::Priority>
00180 (((base_native_priority_ - native_priority) * priority_spacing_) + base_corba_priority_);
00181 }
00182 }
00183 else
00184 {
00185
00186 int last_priority = this->base_native_priority_;
00187 while (true)
00188 {
00189 int previous_priority = ACE_Sched_Params::previous_priority (this->policy_,
00190 last_priority,
00191 ACE_SCOPE_THREAD);
00192 last_priority = previous_priority;
00193
00194 if (last_priority == this->min_)
00195 {
00196 break;
00197 }
00198
00199 if ( this->min_ < this->max_ )
00200 {
00201 if (base_native_priority_ >= previous_priority) break;
00202 }
00203 else
00204 {
00205 if (base_native_priority_ <= previous_priority) break;
00206 }
00207 }
00208
00209 int priority_ndx = 1;
00210 while (true)
00211 {
00212 if (last_priority == this->min_)
00213 {
00214 break;
00215 }
00216
00217 if ( this->min_ < this->max_ )
00218 {
00219 if (native_priority >= last_priority) break;
00220 }
00221 else
00222 {
00223 if (native_priority <= last_priority) break;
00224 }
00225
00226 int previous_priority = ACE_Sched_Params::previous_priority (this->policy_,
00227 last_priority,
00228 ACE_SCOPE_THREAD);
00229 last_priority = previous_priority;
00230 priority_ndx++;
00231 }
00232
00233 corba_priority = static_cast<RTCORBA::Priority>
00234 (base_corba_priority_ - priority_ndx);
00235 }
00236 }
00237
00238 return 1;
00239 }
00240
00241 TAO_END_VERSIONED_NAMESPACE_DECL
00242
00243 #endif