summaryrefslogtreecommitdiff
path: root/ace/Thread_Priority.cpp
blob: a3626782e6c24c52b6d3e62818c920479f4c814b (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// $Id$

// ============================================================================
//
// = LIBRARY
//    ACE
//
// = FILENAME
//    Thread_Priority.cpp
//
// = CREATION DATE
//    28 January 1997
//
// = AUTHOR
//    David Levine
//
// ============================================================================

#include "ace/ACE.h"
#include "ace/Thread_Priority.h"
#include "ace/OS.h"

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

#if defined (ACE_HAS_STHREADS)
#include <sys/priocntl.h>
#include <sys/rtpriocntl.h>
#include <sys/tspriocntl.h>

/* mapping of
      ACE_Thread_Priority::                    to        Solaris
   Priority_Class             Thread_Priority      class         priority
   ==============             ===============      =====         ========
   ACE_LOW_PRIORITY_CLASS         0 .. 6            TS            0 .. 6
   ACE_NORMAL_PRIORITY_CLASS      0 .. 6            TS            7 .. 13
   ACE_HIGH_PRIORITY_CLASS        0 .. 6            RT            0 .. 6
   ACE_REALTIME_PRIORITY_CLASS    0 .. 6            RT            7 .. 13
 */

long
ACE_Thread_Priority::normalize (void)
{
  // Get the priority class ID and attributes.
  pcinfo_t pcinfo;
  ACE_OS::strcpy (pcinfo.pc_clname,
                  priority_class_ == ACE_HIGH_PRIORITY_CLASS 
		  || priority_class_ == ACE_REALTIME_PRIORITY_CLASS
		  ?  "RT"
		  :  "TS");

  if (::priocntl (P_ALL /* ignored */, 
		  P_MYID /* ignored */, 
		  PC_GETCID,
                  (char *) &pcinfo) == -1)
    return -1;

  // OK, now we've got the class ID in pcinfo.pc_cid.  In addition,
  // the maximum configured real-time priority is in ((rtinfo_t *)
  // pcinfo.pc_clinfo)->rt_maxpri.

  os_priority_class_ = pcinfo.pc_cid;

  if (ACE_PRIORITY_MIN <= default_thread_priority_  
      && default_thread_priority_ <= ACE_PRIORITY_MAX)
    {
      os_default_thread_priority_ =
        priority_class_ == ACE_NORMAL_PRIORITY_CLASS 
	|| priority_class_ == ACE_REALTIME_PRIORITY_CLASS
	?  default_thread_priority_ + 7
	:  default_thread_priority_;
    }
  else
    // The user specified a thread priority outside the enum range, so
    // use it without modification.
    os_default_thread_priority_ = default_thread_priority_;

  return 0;
}

#elif defined (ACE_WIN32)

/* mapping of
      ACE_Thread_Priority::                    to        Win32
   Priority_Class             Thread_Priority      class      THREAD_PRIORITY_
   ==============             ===============      =====      ================
   ACE_LOW_PRIORITY_CLASS         0 .. 6           IDLE_...   IDLE...
                                                                 TIME_CRITICAL
   ACE_NORMAL_PRIORITY_CLASS      0 .. 6           NORMAL_...      ""
   ACE_HIGH_PRIORITY_CLASS        0 .. 6           HIGH_...        ""
   ACE_REALTIME_PRIORITY_CLASS    0 .. 6           REALTIME_...    ""
 */

long
ACE_Thread_Priority::normalize (void)
{
  switch (priority_class)
  {
    case ACE_LOW_PRIORITY_CLASS :
      os_priority_class_ = IDLE_PRIORITY_CLASS;
      break;
    case ACE_NORMAL_PRIORITY_CLASS :
      os_priority_class_ = NORMAL_PRIORITY_CLASS;
      break;
    case ACE_HIGH_PRIORITY_CLASS :
      os_priority_class_ = HIGH_PRIORITY_CLASS;
      break;
    case ACE_REALTIME_PRIORITY_CLASS :
      os_priority_class_ = REALTIME_PRIORITY_CLASS;
      break;
  }

  if (ACE_PRIORITY_MIN <= default_thread_priority_  
      && default_thread_priority_ <= ACE_PRIORITY_MAX)
    {
      switch (default_thread_priority_)
      {
        case ACE_PRIORITY_0 :
          os_default_thread_priority_ = THREAD_PRIORITY_IDLE;
          break;
        case ACE_PRIORITY_1 :
          os_default_thread_priority_ = THREAD_PRIORITY_LOWEST;
          break;
        case ACE_PRIORITY_2 :
          os_default_thread_priority_ = THREAD_PRIORITY_BELOW_NORMAL;
          break;
        case ACE_PRIORITY_3 :
          os_default_thread_priority_ = THREAD_PRIORITY_NORMAL;
          break;
        case ACE_PRIORITY_4 :
          os_default_thread_priority_ = THREAD_PRIORITY_ABOVE_NORMAL;
          break;
        case ACE_PRIORITY_5 :
          os_default_thread_priority_ = THREAD_PRIORITY_HIGHEST;
          break;
        case ACE_PRIORITY_6 :
          os_default_thread_priority_ = THREAD_PRIORITY_TIME_CRITICAL;
          break;
      }
    }
  else
    // The user specified a thread priority outside the enum range, so
    // use it without modification.  The user had better know what they're
    // doing on Win32 platforms.
    os_default_thread_priority_ = default_thread_priority_;

  return 0;
}

#elif defined (VXWORKS)

/* mapping of
      ACE_Thread_Priority::                    to        VxWorks
   Priority_Class             Thread_Priority      class         priority
   ==============             ===============      =====         ========
   ACE_LOW_PRIORITY_CLASS        0 .. 6             N/A           27 .. 21
   ACE_NORMAL_PRIORITY_CLASS     0 .. 6             N/A           20 .. 14
   ACE_HIGH_PRIORITY_CLASS       0 .. 6             N/A           13 .. 7
   ACE_REALTIME_PRIORITY_CLASS   0 .. 6             N/A            6 .. 0
 */

long
ACE_Thread_Priority::normalize (void)
{
  os_priority_class_ = -1;  /* unused on this platform */

  if (ACE_PRIORITY_MIN <= default_thread_priority_  
      && default_thread_priority_ <= ACE_PRIORITY_MAX)
    {
      switch (priority_class)
      {
        case ACE_LOW_PRIORITY_CLASS :
          os_default_thread_priority_ = 
	    ACE_PRIORITY_MAX - default_thread_priority_ + 21;
          break;
        case ACE_NORMAL_PRIORITY_CLASS :
          os_default_thread_priority_ = 
	    ACE_PRIORITY_MAX - default_thread_priority_ + 14;
          break;
        case ACE_HIGH_PRIORITY_CLASS :
          os_default_thread_priority_ = 
	    ACE_PRIORITY_MAX - default_thread_priority_ + 7;
          break;
        case ACE_REALTIME_PRIORITY_CLASS :
          os_default_thread_priority_ = 
	    ACE_PRIORITY_MAX - default_thread_priority_;
          break;
      }
    }
  else
    // The user specified a thread priority outside the enum range, so
    // use it without modification.
    os_default_thread_priority_ = default_thread_priority_;

  return 0;
}


#else

/* mapping of
      ACE_Thread_Priority::                    to        VxWorks
   Priority_Class             Thread_Priority      class         priority
   ==============             ===============      =====         ========
   ACE_LOW_PRIORITY_CLASS        0 .. 6             N/A           0 .. 6
   ACE_NORMAL_PRIORITY_CLASS     0 .. 6             N/A           7 .. 13
   ACE_HIGH_PRIORITY_CLASS       0 .. 6             N/A          14 .. 20
   ACE_REALTIME_PRIORITY_CLASS   0 .. 6             N/A          21 .. 27
 */

// assumes that priority increases with increasing ACE_pri_t value
long
ACE_Thread_Priority::normalize (void)
{
  os_priority_class_ = -1;  /* unused on this platform */

  if (ACE_PRIORITY_MIN <= default_thread_priority_ 
      && default_thread_priority_ <= ACE_PRIORITY_MAX)
    {
      switch (priority_class)
      {
        case ACE_LOW_PRIORITY_CLASS :
          os_default_thread_priority_ = default_thread_priority_;
          break;
        case ACE_NORMAL_PRIORITY_CLASS :
          os_default_thread_priority_ = default_thread_priority_ + 7;
          break;
        case ACE_HIGH_PRIORITY_CLASS :
          os_default_thread_priority_ = default_thread_priority_ + 14;
          break;
        case ACE_REALTIME_PRIORITY_CLASS :
          os_default_thread_priority_ = default_thread_priority_ + 21;
          break;
      }
    }
  else
    // The user specified a thread priority outside the enum range, so
    // use it without modification.
    os_default_thread_priority_ = default_thread_priority_;

  return 0;
}

#endif /* ACE_HAS_STHREADS */