summaryrefslogtreecommitdiff
path: root/TAO/tao/RTPortableServer/RT_Servant_Dispatcher.cpp
blob: b7bb51f6879e5087c32547df99d8b4d1b54c24bc (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// @(#) $Id$

#include "RT_Servant_Dispatcher.h"
#include "RT_POA.h"
#include "tao/RTCORBA/Thread_Pool.h"
#include "tao/ORB_Core.h"
#include "tao/TAO_Server_Request.h"
#include "tao/Service_Context.h"
#include "tao/debug.h"

ACE_RCSID(tao, Default_Acceptor_Filter, "$Id$")

RT_Priority_Model_Processing::RT_Priority_Model_Processing (TAO_POA &poa,
                                                            CORBA::Short servant_priority)
  : state_ (NO_ACTION_REQUIRED),
    poa_ (poa),
    servant_priority_ (servant_priority),
    original_priority_ (TAO_INVALID_PRIORITY)
{
}

RT_Priority_Model_Processing::~RT_Priority_Model_Processing (void)
{
  if (this->state_ == PRIORITY_RESET_REQUIRED)
    {
      ACE_DECLARE_NEW_CORBA_ENV;

      ACE_TRY
        {
          this->post_invoke (ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
      ACE_CATCHANY
        {
          // Eat up the exception.
          ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                               "Exception caught: TAO (%P|%t) - Priority_Model_Processing::~Priority_Model_Processing");
        }
      ACE_ENDTRY;
    }
}

void
RT_Priority_Model_Processing::pre_invoke (
   TAO_Service_Context &request_service_context,
   TAO_Service_Context &reply_service_context,
   CORBA::Environment &ACE_TRY_ENV)
{
  TAO_Thread_Pool *thread_pool =
    (TAO_Thread_Pool *) this->poa_.thread_pool ();

  if (thread_pool != 0 &&
      thread_pool->with_lanes ())
    {
      // We don't mess with the priority of threads in lanes.

      if (TAO_debug_level > 0)
        {
          // Get the ORB_Core's TSS resources.
          TAO_ORB_Core_TSS_Resources *tss =
            this->poa_.orb_core ().get_tss_resources ();

          /// Get the lane attribute in TSS.
          TAO_Thread_Lane *lane =
            (TAO_Thread_Lane *) tss->lane_;

          ACE_ASSERT (lane->pool ().id () ==
                      thread_pool->id ());

          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("Using thread pool with lane ")
                      ACE_TEXT ("(%P|%t|%d|%d): original thread priority not changed\n"),
                      lane->pool ().id (),
                      lane->id ()));
        }

      return;
    }

  // Remember current thread's priority.
  TAO_Protocols_Hooks *tph =
    this->poa_.orb_core ().get_protocols_hooks (ACE_TRY_ENV);
  ACE_CHECK;

  if (tph->get_thread_priority (this->original_priority_,
                                ACE_TRY_ENV)
      == -1)
    ACE_THROW (CORBA::DATA_CONVERSION (1,
                                       CORBA::COMPLETED_NO));

  int client_propagated = 0;
  RTCORBA::Priority target_priority = TAO_INVALID_PRIORITY;
  TAO_POA_Cached_Policies &cached_policies =
    this->poa_.cached_policies ();

  // CLIENT_PROPAGATED PriorityModel processing.
  if (cached_policies.priority_model () ==
      TAO_POA_Cached_Policies::CLIENT_PROPAGATED)
    {
      client_propagated = 1;

      // Attempt to extract client-propagated priority from the
      // ServiceContextList of the request.
      const IOP::ServiceContext *context;

      if (request_service_context.get_context (IOP::RTCorbaPriority,
                                               &context) == 1)
        {
          // Extract the target priority
          TAO_InputCDR cdr (ACE_reinterpret_cast
                            (const char*,
                             context->context_data.get_buffer ()),
                            context->context_data.length ());
          CORBA::Boolean byte_order;
          if ((cdr >> ACE_InputCDR::to_boolean (byte_order)) == 0)
            ACE_THROW (CORBA::MARSHAL ());
          cdr.reset_byte_order (ACE_static_cast(int,byte_order));

          if ((cdr >> target_priority) == 0)
            ACE_THROW (CORBA::MARSHAL ());

          // Save the target priority in the response service
          // context to propagate back to the client as specified
          // by the RTCORBA specification.
          reply_service_context.set_context (*context);
        }
      else
        {
          // Use default priority if none came in the request.
          // (Request must have come from a non-RT ORB.)
          target_priority = cached_policies.server_priority ();
        }
    }
  else
    // SERVER_DECLARED PriorityModel processing.
    {
      // Use the request associated with the servant.
      target_priority = this->servant_priority_;
    }

  const char *priority_model;
  if (client_propagated)
    priority_model = "RTCORBA::CLIENT_PROPAGATED";
  else
    priority_model = "RTCORBA::SERVER_DECLARED";

  // Change the priority of the current thread for the duration of
  // request.
  if (target_priority != TAO_INVALID_PRIORITY &&
      target_priority != this->original_priority_)
    {
      if (TAO_debug_level > 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("%s processing ")
                      ACE_TEXT ("(%P|%t): original thread priority %d ")
                      ACE_TEXT ("temporarily changed to %d\n"),
                      priority_model,
                      original_priority_,
                      target_priority));
        }

      if (tph->set_thread_priority (target_priority,
                                    ACE_TRY_ENV)
          == -1)
        ACE_THROW (CORBA::DATA_CONVERSION (1, CORBA::COMPLETED_NO));

      this->state_ = PRIORITY_RESET_REQUIRED;
    }
  else if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("%s processing ")
                ACE_TEXT ("(%P|%t): original thread priority = ")
                ACE_TEXT ("target priority = %d\n"),
                priority_model,
                target_priority));
}

void
RT_Priority_Model_Processing::post_invoke (
    CORBA::Environment &ACE_TRY_ENV)
{
  if (this->state_ == PRIORITY_RESET_REQUIRED)
    {
      this->state_ = NO_ACTION_REQUIRED;

      // Reset the priority of the current thread back to its original
      // value.
      TAO_Protocols_Hooks *tph =
        this->poa_.orb_core ().get_protocols_hooks (ACE_TRY_ENV);
      ACE_CHECK;

      if (tph->set_thread_priority (this->original_priority_,
                                    ACE_TRY_ENV)
          == -1)
        ACE_THROW (CORBA::DATA_CONVERSION (1, CORBA::COMPLETED_NO));
    }
}

/////////////////////////////////////////////////////////////////////////

TAO_RT_Servant_Dispatcher::~TAO_RT_Servant_Dispatcher (void)
{
}

void
TAO_RT_Servant_Dispatcher::dispatch (TAO_Object_Adapter::Servant_Upcall &servant_upcall,
                                     TAO_ServerRequest &req,
                                     CORBA::Environment &ACE_TRY_ENV)
{
  // RTCORBA PriorityModelPolicy processing (may need to be
  // moved/adjusted when POA threadpools are added).  This is the
  // earliest place we can do the processing, since only at this point
  // we know the target POA.
  RT_Priority_Model_Processing priority_processing (servant_upcall.poa (),
                                                    servant_upcall.priority ());

  // Set thread's priority.
  priority_processing.pre_invoke (req.request_service_context (),
                                  req.reply_service_context (),
                                  ACE_TRY_ENV);
  ACE_CHECK;

  // Servant dispatch.
  servant_upcall.servant ()->_dispatch (req,
                                        &servant_upcall,
                                        ACE_TRY_ENV);
  ACE_CHECK;

  // Reset thread's priority to its original value.  If this method
  // isn't reached, i.e., because of an exception, the reset takes
  // place in Priority_Model_Processing destructor.
  priority_processing.post_invoke (ACE_TRY_ENV);
  ACE_CHECK;
}

TAO_POA *
TAO_RT_Servant_Dispatcher::create_POA (const ACE_CString &name,
                                       TAO_POA_Manager &poa_manager,
                                       const TAO_POA_Policy_Set &policies,
                                       TAO_POA *parent,
                                       ACE_Lock &lock,
                                       TAO_SYNCH_MUTEX &thread_lock,
                                       TAO_ORB_Core &orb_core,
                                       TAO_Object_Adapter *object_adapter,
                                       CORBA_Environment &ACE_TRY_ENV)
{
  TAO_RT_POA *poa;

  ACE_NEW_THROW_EX (poa,
                    TAO_RT_POA (name,
                                poa_manager,
                                policies,
                                parent,
                                lock,
                                thread_lock,
                                orb_core,
                                object_adapter,
                                ACE_TRY_ENV),
                    CORBA::NO_MEMORY ());
  ACE_CHECK_RETURN (0);

  return poa;
}