summaryrefslogtreecommitdiff
path: root/TAO/tao/Reactor_Per_Priority.cpp
blob: e55729b322828cab48e421192d8bd188a5c84c52 (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
// $Id$

#include "tao/Reactor_Per_Priority.h"
#include "tao/ORB_Core.h"
#include "tao/Resource_Factory.h"
#include "tao/Leader_Follower.h"
#include "tao/Pluggable.h"
#include "tao/debug.h"
#include "ace/Reactor.h"

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

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

TAO_Reactor_Per_Priority::~TAO_Reactor_Per_Priority (void)
{
  for (Map_Iterator i = this->map_.begin ();
       i != this->map_.end ();
       ++i)
    {
      delete (*i).int_id_;
    }
}

ACE_Reactor *
TAO_Reactor_Per_Priority::reactor (void)
{
  TAO_ORB_Core_TSS_Resources *tss =
    this->orb_core ()->get_tss_resources ();

  TAO_Leader_Follower *leader_follower =
    ACE_static_cast (TAO_Leader_Follower*,
                     tss->reactor_registry_cookie_);

  if (leader_follower != 0)
    return leader_follower->reactor ();

  CORBA::Short priority = 0;
  if (this->orb_core ()->get_thread_priority (priority) == -1)
    {
      if (TAO_debug_level > 3)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - Reactor_Per_Priority::reactor: "
                    " cannot get priority for this thread\n"));
      return 0;
    }

  leader_follower = this->leader_follower_i (priority);

  return leader_follower->reactor ();
}

ACE_Reactor *
TAO_Reactor_Per_Priority::reactor (TAO_Acceptor *acceptor)
{
  CORBA::Short priority =
    acceptor->priority ();

  TAO_Leader_Follower *leader_follower =
    this->leader_follower_i (priority);

  return leader_follower->reactor ();
}

TAO_Leader_Follower &
TAO_Reactor_Per_Priority::leader_follower (void)
{
  TAO_ORB_Core_TSS_Resources *tss =
    this->orb_core ()->get_tss_resources ();

  TAO_Leader_Follower *leader_follower =
    ACE_static_cast (TAO_Leader_Follower*,
                     tss->reactor_registry_cookie_);

  if (leader_follower != 0)
    return *leader_follower;

  CORBA::Short priority = 0;
  if (this->orb_core ()->get_thread_priority (priority) == -1)
    return *leader_follower;

  return *this->leader_follower_i (priority);
}

TAO_Leader_Follower &
TAO_Reactor_Per_Priority::leader_follower (TAO_Acceptor *acceptor)
{
  CORBA::Short priority =
    acceptor->priority ();

  return *this->leader_follower_i (priority);
}

void
TAO_Reactor_Per_Priority::destroy_tss_cookie (void*)
{
  // Do nothing, data is destroyed in the map...
}

TAO_Leader_Follower *
TAO_Reactor_Per_Priority::leader_follower_i (CORBA::Short priority)
{
  // If the priority of the current thread is not right we return.
  TAO_Leader_Follower *leader_follower = 0;
  if (this->map_.find (priority, leader_follower) == -1)
    {
      if (TAO_debug_level > 3)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - new priority %d\n",
                    priority));
      // The priority is new, create an entry in the table.
      ACE_NEW_RETURN (leader_follower,
                      TAO_Leader_Follower (this->orb_core ()),
                      leader_follower);
      this->map_.bind (priority, leader_follower);
    }

  return leader_follower;
}

int
TAO_Reactor_Per_Priority::shutdown_all (void)
{
  for (Map_Iterator i = this->map_.begin ();
       i != this->map_.end ();
       ++i)
    {
      TAO_Leader_Follower &leader_follower =
        *((*i).int_id_);

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
                        ace_mon,
                        leader_follower.lock (),
                        -1);

      // 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 ();
        }
    }

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class ACE_Map_Manager<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>;
template class ACE_Map_Iterator<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>;
template class ACE_Map_Iterator_Base<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>;
template class ACE_Map_Reverse_Iterator<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>;
template class ACE_Map_Entry<CORBA::Short,TAO_Leader_Follower*>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate ACE_Map_Manager<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>
#pragma instantiate ACE_Map_Iterator<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>
#pragma instantiate ACE_Map_Iterator_Base<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>
#pragma instantiate ACE_Map_Reverse_Iterator<CORBA::Short,TAO_Leader_Follower*,ACE_SYNCH_MUTEX>
#pragma instantiate ACE_Map_Entry<CORBA::Short,TAO_Leader_Follower*>

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */