summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Event/Timer_Module.cpp
blob: de8d73e86250e226a1f0480aec4c10d866e6da7e (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
// $Id$

#include "ace/Functor.h"

#include "orbsvcs/orbsvcs/Scheduler_Factory.h"
#include "ReactorTask.h"
#include "Timer_Module.h"

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

ACE_RCSID (Event, 
           Timer_Module, 
           "$Id$")

#include "tao/Timeprobe.h"

#if defined (ACE_ENABLE_TIMEPROBES)
static const char *TAO_Timer_Module_Timeprobe_Description[] =
{
  "Timer_Module - start execute",
  "Timer_Module - end execute"
};

enum
{
  // Timeprobe description table start key
  TAO_EC_TIMER_MODULE_START_EXECUTE = 5400,
  TAO_EC_TIMER_MODULE_END_EXECUTE
};

// Setup Timeprobes
ACE_TIMEPROBE_EVENT_DESCRIPTIONS (TAO_Timer_Module_Timeprobe_Description,
                                  TAO_EC_TIMER_MODULE_START_EXECUTE);

#endif /* ACE_ENABLE_TIMEPROBES */

// ****************************************************************

TAO_EC_Timer_Module::~TAO_EC_Timer_Module (void)
{
}

// ****************************************************************

TAO_EC_ST_Timer_Module::TAO_EC_ST_Timer_Module (ACE_Reactor* r)
  :  reactor_ (r)
{
}

TAO_EC_ST_Timer_Module::~TAO_EC_ST_Timer_Module (void)
{
}

void
TAO_EC_ST_Timer_Module::activate (void)
{
}

void
TAO_EC_ST_Timer_Module::shutdown (void)
{
  this->reactor_->cancel_timer (&this->timeout_handler_);
}

RtecScheduler::handle_t
TAO_EC_ST_Timer_Module::rt_info (RtecScheduler::Preemption_Priority_t)
{
  // @@ TODO......
  return 0;
}

int
TAO_EC_ST_Timer_Module::schedule_timer (RtecScheduler::Preemption_Priority_t,
                                        ACE_Command_Base* act,
                                        const ACE_Time_Value& delta,
                                        const ACE_Time_Value& interval)
{
  return this->reactor_->schedule_timer (&this->timeout_handler_,
                                         ACE_static_cast(void*,act),
                                         delta, interval);
}

int
TAO_EC_ST_Timer_Module::cancel_timer (RtecScheduler::Preemption_Priority_t,
                                      int id,
                                      ACE_Command_Base*& act)
{
  const void *vp;

  int result =
    this->reactor_->cancel_timer (id, &vp);
  if (result == 0)
    {
      ACE_ERROR ((LM_ERROR, "TAO_EC_ST_Timer_Module::cancel_timer: "
                  "Tried to cancel nonexistent timer.\n"));
      act = 0;
    }
  else
    act = ACE_reinterpret_cast (ACE_Command_Base *,
                                ACE_const_cast (void *, vp));

  return result;
}

int
TAO_EC_ST_Timer_Module::register_handler (RtecScheduler::Preemption_Priority_t,
                                          ACE_Event_Handler* eh,
                                          ACE_HANDLE handle)
{
  return this->reactor_->register_handler (eh, handle);
}

ACE_Reactor*
TAO_EC_ST_Timer_Module::reactor (RtecScheduler::Preemption_Priority_t)
{
  return this->reactor_;
}

// ****************************************************************

TAO_EC_RPT_Timer_Module::
  TAO_EC_RPT_Timer_Module (RtecScheduler::Scheduler_ptr scheduler)
  : shutdown_ (0)
{
  if (CORBA::is_nil (scheduler))
    {
      this->scheduler_ =
        RtecScheduler::Scheduler::_duplicate (ACE_Scheduler_Factory::server ());
    }
  else
    {
      this->scheduler_ =
        RtecScheduler::Scheduler::_duplicate (scheduler);
    }
  for (int i = 0; i < ACE_Scheduler_MAX_PRIORITIES; ++i)
    this->reactorTasks[i] = 0;
}

TAO_EC_RPT_Timer_Module::~TAO_EC_RPT_Timer_Module (void)
{
  this->shutdown ();

  for (int i = 0; i < ACE_Scheduler_MAX_PRIORITIES; ++i)
    {
      if (this->reactorTasks[i] != 0)
        {
          delete this->reactorTasks[i];
          this->reactorTasks[i] = 0;
        }
    }
}

void TAO_EC_RPT_Timer_Module::activate (void)
{
  for (int i = 0; i < ACE_Scheduler_MAX_PRIORITIES; ++i)
    {
      if (this->reactorTasks[i] != 0)
        continue;

      // Convert ACE_Scheduler_Rate (it's really a period, not a rate!)
      // to a form we can easily work with.
      ACE_Time_Value period_tv;
      ORBSVCS_Time::TimeT_to_Time_Value (period_tv, ACE_Scheduler_Rates[i]);

      RtecScheduler::Period_t period = period_tv.sec () * 10000000 +
                                       period_tv.usec () * 10;

      ACE_NEW (this->reactorTasks[i],
               ReactorTask (this->scheduler_.in ()));

      if (!this->shutdown_)
        {
          this->reactorTasks[i]->thr_mgr (this->ThrMgr ());
          if (this->reactorTasks[i]->open_reactor (period) == -1)
            {
              ACE_ERROR ((LM_ERROR, "%p\n",
                          "EC (%t) Timer_Module - open reactor"));
            }
        }
    }
}

void
TAO_EC_RPT_Timer_Module::shutdown (void)
{
  if (this->shutdown_)
    return;

  this->shutdown_ = 1;
  for (int i = 0; i < ACE_Scheduler_MAX_PRIORITIES; ++i)
    {
      if (this->reactorTasks[i] != 0)
        this->reactorTasks[i]->shutdown_task ();
      this->reactorTasks[i]->get_reactor ().cancel_timer (&this->timeout_handler_);
    }

  if (this->ThrMgr ()->wait () == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "EC (%t) Timer_Module wait"));
}

RtecScheduler::handle_t
TAO_EC_RPT_Timer_Module::rt_info (RtecScheduler::Preemption_Priority_t priority)
{
  return this->GetReactorTask (priority)->rt_info ();
}

int
TAO_EC_RPT_Timer_Module::schedule_timer (RtecScheduler::Preemption_Priority_t priority,
                                         ACE_Command_Base* act,
                                         const ACE_Time_Value& delta,
                                         const ACE_Time_Value& interval)
{
  ACE_Reactor& reactor = this->GetReactorTask (priority)->get_reactor ();
  return reactor.schedule_timer (&this->timeout_handler_,
                                 ACE_static_cast(void*,act),
                                 delta, interval);
}

int
TAO_EC_RPT_Timer_Module::cancel_timer (RtecScheduler::Preemption_Priority_t priority,
                                       int id,
                                       ACE_Command_Base*& act)
{
  const void* vp;
  ACE_Reactor& reactor = this->GetReactorTask (priority)->get_reactor ();

  int result =
    reactor.cancel_timer (id, &vp);
  if (result == 0)
    {
      ACE_ERROR ((LM_ERROR, "TAO_EC_ST_Timer_Module::cancel_timer: "
                  "Tried to cancel nonexistent timer.\n"));
      act = 0;
    }
  else
    act = ACE_reinterpret_cast (ACE_Command_Base *,
                                ACE_const_cast (void *, vp));

  return result;
}

int
TAO_EC_RPT_Timer_Module::register_handler (RtecScheduler::Preemption_Priority_t priority,
                                           ACE_Event_Handler* eh,
                                           ACE_HANDLE handle)
{
  return this->GetReactorTask (priority)->get_reactor ().register_handler (eh, handle);
}

ACE_Reactor*
TAO_EC_RPT_Timer_Module::reactor (RtecScheduler::Preemption_Priority_t priority)
{
  return &this->GetReactorTask (priority)->get_reactor ();
}

// ****************************************************************

int
TAO_EC_Timeout_Handler::handle_timeout (const ACE_Time_Value &,
                                        const void *vp)
{
  ACE_Command_Base *act = ACE_static_cast(ACE_Command_Base*,
                                          ACE_const_cast(void*,vp));

  if (act == 0)
    ACE_ERROR_RETURN ((LM_ERROR, "ACE_ES_Priority_Timer::handle_timeout: "
                       "received act == 0!!!.\n"), 0);

  {
    ACE_FUNCTION_TIMEPROBE (TAO_EC_TIMER_MODULE_START_EXECUTE);
    act->execute ();
  }

  return 0;
}