summaryrefslogtreecommitdiff
path: root/TAO/tao/Thread_Lane_Resources.cpp
blob: a934c64286bbcedc8e3e4d9629177220dc54a5b7 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// $Id$

#include "tao/Thread_Lane_Resources.h"

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

#include "tao/ORB_Core.h"
#include "tao/Acceptor_Registry.h"
#include "tao/Transport_Cache_Manager.h"
#include "tao/Leader_Follower.h"
#include "tao/Connection_Handler.h"
#include "tao/Transport.h"
#include "tao/Connector_Registry.h"
#include "ace/Reactor.h"


#if !defined (__ACE_INLINE__)
# include "tao/Thread_Lane_Resources.i"
#endif /* ! __ACE_INLINE__ */

TAO_Thread_Lane_Resources::TAO_Thread_Lane_Resources (TAO_ORB_Core &orb_core,
                                                      TAO_New_Leader_Generator *new_leader_generator)
  : orb_core_ (orb_core)
    , acceptor_registry_ (0)
    , connector_registry_ (0)
    , transport_cache_ (0)
    , leader_follower_ (0)
    , new_leader_generator_ (new_leader_generator)
    , input_cdr_dblock_allocator_ (0)
    , input_cdr_buffer_allocator_ (0)
    , input_cdr_msgblock_allocator_ (0)
    , transport_message_buffer_allocator_ (0)
    , output_cdr_dblock_allocator_ (0)
    , output_cdr_buffer_allocator_ (0)
    , output_cdr_msgblock_allocator_ (0)
{
  // Create the transport cache.
  ACE_NEW (this->transport_cache_,
           TAO_Transport_Cache_Manager (orb_core));

}

TAO_Thread_Lane_Resources::~TAO_Thread_Lane_Resources (void)
{

}

TAO_Transport_Cache_Manager &
TAO_Thread_Lane_Resources::transport_cache (void)
{
  return *this->transport_cache_;
}

int
TAO_Thread_Lane_Resources::has_acceptor_registry_been_created (void) const
{
  return this->acceptor_registry_ != 0;
}

int
TAO_Thread_Lane_Resources::is_collocated (const TAO_MProfile& mprofile)
{
  if (!this->has_acceptor_registry_been_created ())
    return 0;

  return this->acceptor_registry ().is_collocated (mprofile);
}

TAO_Acceptor_Registry &
TAO_Thread_Lane_Resources::acceptor_registry (void)
{
  // Double check.
  if (this->acceptor_registry_ == 0)
    {
      // @@todo: Wouldnt this crash big time if you happen to
      // dereference a  null-pointer? Needs fixing.
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                        ace_mon,
                        this->lock_,
                        *this->acceptor_registry_);
      if (this->acceptor_registry_ == 0)
        {
          // @@ Not exception safe code
          // Get the resource factory.
          TAO_Resource_Factory &resource_factory =
            *this->orb_core_.resource_factory ();

          // Ask it to create a new acceptor registry.
          this->acceptor_registry_ =
            resource_factory.get_acceptor_registry ();
        }
    }

  return *this->acceptor_registry_;
}

TAO_Connector_Registry *
TAO_Thread_Lane_Resources::connector_registry (ACE_ENV_SINGLE_ARG_DECL)
{
  // Double check.
  if (this->connector_registry_ == 0)
    {
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                        ace_mon,
                        this->lock_,
                        0);

      if (this->connector_registry_ == 0)
        {
          // Ask it to create a new acceptor registry.
          TAO_Connector_Registry *connector_registry =
            this->orb_core_.resource_factory ()->get_connector_registry ();

          if (connector_registry == 0)
            ACE_THROW_RETURN (CORBA::INITIALIZE (
                                CORBA::SystemException::_tao_minor_code (
                                  TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
                                  0),
                                CORBA::COMPLETED_NO),
                      0);

          if (connector_registry->open (&this->orb_core_) != 0)
            ACE_THROW_RETURN (CORBA::INITIALIZE (
                                CORBA::SystemException::_tao_minor_code (
                                  TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
                                  0),
                                CORBA::COMPLETED_NO),
                              0);

          // Finally, everything is created and opened successfully:
          // now we can assign to the member.  Otherwise, the
          // assignment would be premature.
          this->connector_registry_ =
            connector_registry;
        }
    }

  return this->connector_registry_;
}


TAO_Leader_Follower &
TAO_Thread_Lane_Resources::leader_follower (void)
{
  // Double check.
  if (this->leader_follower_ == 0)
    {
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, *this->leader_follower_);
      if (this->leader_follower_ == 0)
        {
          // Create a new Leader Follower object.
          ACE_NEW_RETURN (this->leader_follower_,
                          TAO_Leader_Follower (&this->orb_core_,
                                               this->new_leader_generator_),
                          *this->leader_follower_);
        }
    }

  return *this->leader_follower_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::input_cdr_dblock_allocator (void)
{
  if (this->input_cdr_dblock_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->input_cdr_dblock_allocator_ == 0)
        this->input_cdr_dblock_allocator_ =
          this->resource_factory ()->input_cdr_dblock_allocator ();
    }

  return this->input_cdr_dblock_allocator_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::input_cdr_buffer_allocator (void)
{
  if (this->input_cdr_buffer_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->input_cdr_buffer_allocator_ == 0)
        this->input_cdr_buffer_allocator_ =
          this->resource_factory ()->input_cdr_buffer_allocator ();
    }

  return this->input_cdr_buffer_allocator_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::input_cdr_msgblock_allocator (void)
{
  if (this->input_cdr_msgblock_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->input_cdr_msgblock_allocator_ == 0)
        this->input_cdr_msgblock_allocator_ =
          this->resource_factory ()->input_cdr_msgblock_allocator ();
    }

  return this->input_cdr_msgblock_allocator_;
}

ACE_Allocator*
TAO_Thread_Lane_Resources::transport_message_buffer_allocator (void)
{
  if (this->transport_message_buffer_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->transport_message_buffer_allocator_ == 0)
        this->transport_message_buffer_allocator_ =
          this->resource_factory ()->input_cdr_dblock_allocator ();
    }

  return this->transport_message_buffer_allocator_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::output_cdr_dblock_allocator (void)
{
  if (this->output_cdr_dblock_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->output_cdr_dblock_allocator_ == 0)
        this->output_cdr_dblock_allocator_ =
          this->resource_factory ()->output_cdr_dblock_allocator ();
    }

  return this->output_cdr_dblock_allocator_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::output_cdr_buffer_allocator (void)
{
  if (this->output_cdr_buffer_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->output_cdr_buffer_allocator_ == 0)
        this->output_cdr_buffer_allocator_ =
          this->resource_factory ()->output_cdr_buffer_allocator ();
    }

  return this->output_cdr_buffer_allocator_;
}


ACE_Allocator*
TAO_Thread_Lane_Resources::output_cdr_msgblock_allocator (void)
{
  if (this->output_cdr_msgblock_allocator_ == 0)
    {
      // Double checked locking
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
      if (this->output_cdr_msgblock_allocator_ == 0)
        this->output_cdr_msgblock_allocator_ =
          this->resource_factory ()->output_cdr_msgblock_allocator ();
    }

  return this->output_cdr_msgblock_allocator_;
}

int
TAO_Thread_Lane_Resources::open_acceptor_registry (int ignore_address
                                                   ACE_ENV_ARG_DECL)
{
  /// Access the acceptor registry.
  TAO_Acceptor_Registry &ar =
    this->acceptor_registry ();

  // Open it.
  int result =
    ar.open (&this->orb_core_,
             this->leader_follower ().reactor (),
             ignore_address
              ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return result;
}

TAO_Resource_Factory *
TAO_Thread_Lane_Resources::resource_factory (void)
{
  return this->orb_core_.resource_factory ();
}

void
TAO_Thread_Lane_Resources::finalize (void)
{
  // Close connectors before acceptors!
  // Ask the registry to close all registered connectors.
  if (this->connector_registry_ != 0)
    {
      this->connector_registry_->close_all ();
      delete this->connector_registry_;
    }

  // Ask the registry to close all registered acceptors.
  if (this->acceptor_registry_ != 0)
    {
      this->acceptor_registry_->close_all ();
      delete this->acceptor_registry_;
    }

  // Set of handlers still in the connection cache.
  TAO_Connection_Handler_Set handlers;

  // Close the transport cache and return the handlers that were still
  // registered.  The cache will decrease the #REFCOUNT# on the
  // handler when it removes the handler from cache.  However,
  // #REFCOUNT# is increased when the handler is placed in the handler
  // set.
  this->transport_cache_->close (handlers);

  // Go through the handler set, closing the connections and removing
  // the references.
  TAO_Connection_Handler **handler = 0;
  for (TAO_Connection_Handler_Set::iterator iter (handlers);
       iter.next (handler);
       iter.advance ())
    {
      // Connection is closed.  Potential removal from the Reactor.
      (*handler)->close_connection ();

      // #REFCOUNT# related to the handler set decreases.
      (*handler)->transport ()->remove_reference ();
    }

  delete this->transport_cache_;
  delete this->leader_follower_;

  // Delete all the allocators here.. They shouldnt be done earlier,
  // lest some of the contents in the abve, say reactor or acceptor
  // may use memory from the pool..
  if (this->input_cdr_dblock_allocator_ != 0)
    this->input_cdr_dblock_allocator_->remove ();
  delete this->input_cdr_dblock_allocator_;

  if (this->input_cdr_buffer_allocator_ != 0)
    this->input_cdr_buffer_allocator_->remove ();
  delete this->input_cdr_buffer_allocator_;

  if (this->input_cdr_msgblock_allocator_ != 0)
    this->input_cdr_msgblock_allocator_->remove ();
  delete this->input_cdr_msgblock_allocator_;

  if (this->transport_message_buffer_allocator_ != 0)
    this->transport_message_buffer_allocator_->remove ();
  delete this->transport_message_buffer_allocator_;

  if (this->output_cdr_dblock_allocator_ != 0)
    this->output_cdr_dblock_allocator_->remove ();
  delete this->output_cdr_dblock_allocator_;

  if (this->output_cdr_buffer_allocator_ != 0)
    this->output_cdr_buffer_allocator_->remove ();
  delete this->output_cdr_buffer_allocator_;

  if (this->output_cdr_msgblock_allocator_ != 0)
    this->output_cdr_msgblock_allocator_->remove ();
  delete this->output_cdr_msgblock_allocator_;
}

void
TAO_Thread_Lane_Resources::shutdown_reactor (void)
{
  TAO_Leader_Follower &leader_follower =
    this->leader_follower ();

  ACE_GUARD (TAO_SYNCH_MUTEX,
             ace_mon,
             leader_follower.lock ());

  // Wakeup all the threads waiting blocked in the event loop, this
  // does not guarantee that they will all go away, but reduces the
  // load on the POA....
  ACE_Reactor *reactor =
    leader_follower.reactor ();

  reactor->wakeup_all_threads ();

  // If there are some client threads running we have to wait until
  // they finish, when the last one does it will shutdown the reactor
  // for us.  Meanwhile no new requests will be accepted because the
  // POA will not process them.
  if (!leader_follower.has_clients ())
    {
      // Wake up all waiting threads in the reactor.
      reactor->end_reactor_event_loop ();
    }
}