summaryrefslogtreecommitdiff
path: root/TAO/tao/PICurrent.cpp
blob: d41839d8f549df2ad163f74a66d6e57625e99eb6 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// -*- C++ -*-

#include "PICurrent.h"

#if TAO_HAS_INTERCEPTORS == 1

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


#if !defined (__ACE_INLINE__)
# include "PICurrent.inl"
#endif /* __ACE_INLINE__ */


#include "ORB_Core.h"
#include "Stub.h"
#include "TAO_Server_Request.h"


TAO_PICurrent::TAO_PICurrent (TAO_ORB_Core *orb_core)
  : orb_core_ (orb_core),
    slot_count_ (0)
{
}

TAO_PICurrent::~TAO_PICurrent (void)
{
}

CORBA::Any *
TAO_PICurrent::get_slot (PortableInterceptor::SlotId id,
			 CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
		   PortableInterceptor::InvalidSlot))
{
  this->check_validity (id, ACE_TRY_ENV);
  ACE_CHECK_RETURN (0);

  TAO_PICurrent_Impl *impl = this->tsc ();

  if (impl == 0)
    {
      ACE_THROW_RETURN (CORBA::BAD_INV_ORDER (TAO_OMG_VMCID | 14,
                                              CORBA::COMPLETED_NO),
                        0);
    }

  return impl->get_slot (id, ACE_TRY_ENV);
}

void
TAO_PICurrent::set_slot (PortableInterceptor::SlotId id,
			 const CORBA::Any & data,
			 CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
		   PortableInterceptor::InvalidSlot))
{
  this->check_validity (id, ACE_TRY_ENV);
  ACE_CHECK;

  TAO_PICurrent_Impl *impl = this->tsc ();

  if (impl == 0)
    {
      ACE_THROW (CORBA::BAD_INV_ORDER (TAO_OMG_VMCID | 14,
                                       CORBA::COMPLETED_NO));
    }

  impl->set_slot (id, data, ACE_TRY_ENV);
  ACE_CHECK;
}

PortableInterceptor::SlotId
TAO_PICurrent::allocate_slot_id (void)
{
  // No need to acquire a lock.  This only gets called during ORB
  // initialization.  ORB initialization is already atomic.
  return this->slot_count_++;
}

TAO_PICurrent_Impl *
TAO_PICurrent::tsc (void)
{
  TAO_ORB_Core_TSS_Resources *tss =
    this->orb_core_->get_tss_resources ();

  return &tss->pi_current_;
}

// ------------------------------------------------------------------

TAO_PICurrent_Impl::TAO_PICurrent_Impl (void)
  : pi_peer_ (0),
    slot_table_ (),
    lc_slot_table_ (0),
    dirty_ (0)
{
}

TAO_PICurrent_Impl::~TAO_PICurrent_Impl (void)
{
  size_t len = this->slot_table_.size ();
  for (size_t i = 0; i < len; ++i)
    delete ACE_static_cast (CORBA::Any *, this->slot_table_[i]);
}

CORBA::Any *
TAO_PICurrent_Impl::get_slot (PortableInterceptor::SlotId id,
			      CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
		   PortableInterceptor::InvalidSlot))
{
  // No need to check validity of SlotId.  It is validated before this
  // method is invoked.

  TAO_PICurrent_Impl::Table &table =
    this->lc_slot_table_ == 0 ? this->slot_table_ : *this->lc_slot_table_;

  CORBA::Any * any = 0;

  if (id >= table.size () || table[id] == 0)
    {
      // In accordance with the Portable Interceptor specification,
      // return an Any with a TCKind of tk_null.  A default
      // constructed Any has that TCKind.
      ACE_NEW_THROW_EX (any,
			CORBA::Any,
			CORBA::NO_MEMORY (
			  CORBA_SystemException::_tao_minor_code (
                            TAO_DEFAULT_MINOR_CODE,
			    ENOMEM),
			  CORBA::COMPLETED_NO));
      ACE_CHECK_RETURN (0);

      return any;
    }

  const CORBA::Any *data =
    ACE_static_cast (CORBA::Any *, table[id]);

  ACE_NEW_THROW_EX (any,
                    CORBA::Any (*data), // Make a copy.
                    CORBA::NO_MEMORY (
                      CORBA_SystemException::_tao_minor_code (
                        TAO_DEFAULT_MINOR_CODE,
                        ENOMEM),
                      CORBA::COMPLETED_NO));
  ACE_CHECK_RETURN (0);

  return any;
}

void
TAO_PICurrent_Impl::set_slot (PortableInterceptor::SlotId id,
			      const CORBA::Any & data,
			      CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException,
		   PortableInterceptor::InvalidSlot))
{
  // No need to check validity of SlotId.  It is validated before this
  // method is invoked.

  // Copy the contents of the logically copied slot table before
  // modifying our own slot table.
  if (this->lc_slot_table_ != 0)
    {
      // Deep copy

      Table &table = *this->lc_slot_table_;

      size_t new_size = table.size ();
      this->slot_table_.size (new_size);

      for (size_t i = 0; i < new_size; ++i)
        {
          if (i == id)
            continue;  // Avoid copying data twice.

          const CORBA::Any *lc_data =
            ACE_static_cast (CORBA::Any *, table[i]);

          CORBA::Any *any = 0;
          ACE_NEW_THROW_EX (any,
                            CORBA::Any (*lc_data), // Make a copy.
                            CORBA::NO_MEMORY (
                              CORBA_SystemException::_tao_minor_code (
                                TAO_DEFAULT_MINOR_CODE,
                                ENOMEM),
                              CORBA::COMPLETED_NO));
          ACE_CHECK;

          this->slot_table_[i] = any;
        }

      // Break all ties with the logically copied slot table.
      this->lc_slot_table_ = 0;
    }

  // If the slot table array isn't large enough, then increase its
  // size.  We're guaranteed not to exceed the number of allocated
  // slots for the reason stated above.
  size_t old_size = this->slot_table_.size ();
  size_t new_size = id + 1;
  if (id >= old_size && this->slot_table_.size (new_size) != 0)
    ACE_THROW (CORBA::INTERNAL ());

  // Initialize intermediate array elements to zero, since they
  // haven't been initialized yet.  This ensures that garbage is not
  // returned when accessing any of those elements at a later point in
  // time.
  for (size_t i = old_size; i < id; ++i)
    this->slot_table_[i] = 0;

  // Now copy the data into the slot table.
  CORBA::Any * any = 0;
  ACE_NEW_THROW_EX (any,
                    CORBA::Any (data),  // Make a copy.
                    CORBA::NO_MEMORY (
		      CORBA_SystemException::_tao_minor_code (
                        TAO_DEFAULT_MINOR_CODE,
                        ENOMEM),
                      CORBA::COMPLETED_NO));
  ACE_CHECK;

  this->slot_table_[id] = any;

  // Mark the table as being modified.
  this->dirty_ = 1;
}

void
TAO_PICurrent_Impl::copy (TAO_PICurrent_Impl &rhs, CORBA::Boolean deep_copy)
{
  if (rhs.dirty () && deep_copy)
    {
      size_t new_size  = rhs.slot_table ().size ();

      this->slot_table_.size (new_size);

      ACE_DECLARE_NEW_CORBA_ENV;
      ACE_TRY
        {
          for (size_t i = 0; i < new_size; ++i)
            {
              this->slot_table_[i] =
                rhs.get_slot (i, ACE_TRY_ENV);  // Deep copy
              ACE_TRY_CHECK;
            }
        }
      ACE_CATCHANY
        {
          if (TAO_debug_level > 0)
            ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                                 "(%P|%t) Error during PICurrent "
                                 "slot table copy.");
        }
      ACE_ENDTRY;
      
      rhs.dirty (0);

      rhs.pi_peer (0); // Break all ties with the PICurrent peer.
    }
  else
    {
      this->lc_slot_table_ = &rhs.slot_table ();  // Shallow copy

      this->pi_peer_ = &rhs;
    }
}

// ------------------------------------------------------------------

TAO_PICurrent_Guard::TAO_PICurrent_Guard (TAO_Stub *stub,
                                          TAO_PICurrent_Impl &rsc)
  : src_ (0),
    dest_ (0)
{
  // This constructor is used on the client side.

  // Retrieve the thread scope current (no TSS access incurred yet).
  TAO_PICurrent *pi_current = stub->orb_core ()->pi_current ();

  // If the slot count is zero, then there is nothing to copy.
  // Prevent any copying (and hence TSS accesses) from occurring.
  if (pi_current->slot_count () != 0)
    {
      // Retrieve the thread scope current.
      TAO_PICurrent_Impl *tsc = pi_current->tsc ();

      // Copy the TSC to the RSC.
      rsc.copy (*tsc, 0);  // Shallow copy
    }

  // Notice that a TAO_PICurrent_Guard instantiated with this
  // constructor will basically have a no-op destructor.
}

TAO_PICurrent_Guard::TAO_PICurrent_Guard (TAO_ServerRequest &server_request,
                                          CORBA::Boolean tsc_to_rsc)
  : src_ (0),
    dest_ (0)
{
  // This constructor is used on the server side.

  // Retrieve the thread scope current (no TSS access incurred yet).
  TAO_PICurrent *pi_current = server_request.orb_core ()->pi_current ();

  // If the slot count is zero, then there is nothing to copy.
  // Prevent any copying (and hence TSS accesses) from occurring.
  if (pi_current->slot_count () != 0)
    {
      // Retrieve the request scope current.
      TAO_PICurrent_Impl *rsc = &server_request.rs_pi_current ();

      // Retrieve the thread scope current.
      TAO_PICurrent_Impl *tsc = pi_current->tsc ();

      if (tsc_to_rsc)
        {
          // TSC to RSC copy.
          // Occurs after receive_request() interception point and
          // upcall.
          this->src_  = tsc;
          this->dest_ = rsc;
        }
      else
        {
          // RSC to TSC copy.
          // Occurs after receive_request_service_contexts()
          // interception point.
          this->src_  = rsc;
          this->dest_ = tsc;
        }
    }
}

TAO_PICurrent_Guard::~TAO_PICurrent_Guard (void)
{
  if (this->src_ != 0 && this->dest_ != 0)
    {
      // This copy better be exception-safe!
      this->dest_->copy (*this->src_, 0);    // Logical copy
    }
}

#endif  /* TAO_HAS_INTERCEPTORS == 1 */