summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Event/Timer_Module.h
blob: e28f0cc74ea79cfb1c9aee4720376cdf339c43fa (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
/* -*- C++ -*- */
//
// $Id$
//

#ifndef TAO_EC_TIMER_MODULE_H
#define TAO_EC_TIMER_MODULE_H
#include /**/ "ace/pre.h"

#include "ace/ACE.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "orbsvcs/orbsvcs/Event_Service_Constants.h"
#include "orbsvcs/orbsvcs/Event/RT_Task.h"

class ACE_Command_Base;

/**
 * @class TAO_EC_Timer_Module
 *
 * @brief The timer managment module.
 *
 * The Event Channel can use several strategies to dispatch timers,
 * for instance, it can use the ORB reactor or a pool of reactors running at
 * different priorities or a pool of Thread_Timer_Queue_Adapters running at
 * different priorities also. This class is the abstract base class to
 * abstract this strategies.
 */
class TAO_RTOLDEvent_Export TAO_EC_Timer_Module
{
public:
  /// The dtor
  virtual ~TAO_EC_Timer_Module (void);

  /// Activate the threads, it waits until the threads are up and
  /// running.
  virtual void activate (void) = 0;

  /// Deactivate the threads, it waits until all the threads have
  /// terminated.
  virtual void shutdown (void) = 0;

  /// The RT_Info handle for the "task" at <priority>
  virtual RtecScheduler::handle_t
     rt_info (RtecScheduler::OS_Priority priority) = 0;

  /// Add a timer at the given priority, returns the timer ID.
  virtual int schedule_timer (RtecScheduler::Preemption_Priority_t priority,
                              ACE_Command_Base* act,
                              const ACE_Time_Value& delta,
                              const ACE_Time_Value& interval) = 0;

  /// Add a timer at the given priority.
  virtual int cancel_timer (RtecScheduler::Preemption_Priority_t priority,
                            int id,
                            ACE_Command_Base*& act) = 0;

  /// Register a handler?????
  virtual int register_handler (RtecScheduler::Preemption_Priority_t priority,
                                ACE_Event_Handler* eh,
                                ACE_HANDLE handle) = 0;

  /// Obtain the reactor for the given priority.
  /// @@ This may prove tricky to implement with timer queues not based
  /// on reactors.
  virtual ACE_Reactor* reactor (RtecScheduler::Preemption_Priority_t priority) = 0;
};

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

/**
 * @class TAO_EC_Timeout_Handler
 *
 * @brief Event Service Timeout handler.
 *
 * This is used by the Timer_Modules as an adaptor between the
 * reactor (Event_Handler) and the Command objects.
 */
class TAO_RTOLDEvent_Export TAO_EC_Timeout_Handler : public ACE_Event_Handler
{
public:
  /// Default construction.
  TAO_EC_Timeout_Handler (void);

private:
  /// Casts @ act to ACE_Command_Base and calls execute.
  virtual int handle_timeout (const ACE_Time_Value &tv,
                              const void *act);
};

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

/**
 * @class TAO_EC_ST_Timer_Module
 *
 * @brief A single threaded implementation for the timer module.
 *
 * This timer module uses a single Reactor to implement the timer,
 * usually the ORB reactor is used for this purposes.
 */
class TAO_RTOLDEvent_Export TAO_EC_ST_Timer_Module : public TAO_EC_Timer_Module
{
public:
  /// The ctor.
  TAO_EC_ST_Timer_Module (ACE_Reactor* reactor);

  /// The dtor
  virtual ~TAO_EC_ST_Timer_Module (void);

  // = The TAO_EC_Timer_Module methods.
  virtual void activate (void);
  virtual void shutdown (void);
  virtual RtecScheduler::handle_t
     rt_info (RtecScheduler::Preemption_Priority_t priority);
  virtual int schedule_timer (RtecScheduler::Preemption_Priority_t priority,
                              ACE_Command_Base* act,
                              const ACE_Time_Value& delta,
                              const ACE_Time_Value& interval);
  virtual int cancel_timer (RtecScheduler::Preemption_Priority_t priority,
                            int id,
                            ACE_Command_Base*& act);
  virtual int register_handler (RtecScheduler::Preemption_Priority_t priority,
                                ACE_Event_Handler* eh,
                                ACE_HANDLE handle);
  virtual ACE_Reactor* reactor (RtecScheduler::Preemption_Priority_t priority);

private:
  /// The reactor.
  ACE_Reactor* reactor_;

  /// To receive the timeouts.
  TAO_EC_Timeout_Handler timeout_handler_;
};

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

class ACE_ES_Reactor_Task;

/**
 * @class TAO_EC_RPT_Timer_Module
 *
 * @brief A timer module using reactor-per-thread.
 *
 * This Timer Module uses a pool of ACE_ReactorTask to handle the
 * dispatching of timeouts. In real-time multi-threaded enviroments
 * each Reactor runs at a different priority.
 */
class TAO_RTOLDEvent_Export TAO_EC_RPT_Timer_Module : public TAO_EC_Timer_Module
{
public:
  /// Create the Timer Module
  TAO_EC_RPT_Timer_Module (RtecScheduler::Scheduler_ptr scheduler);

  /// The dtor also shutdowns the Task_Manager.
  virtual ~TAO_EC_RPT_Timer_Module (void);

  typedef ACE_ES_Reactor_Task ReactorTask;

  /// Obtain the ReactorTask for the given priority.
  /// The Task must have been created already.
  ReactorTask* GetReactorTask(RtecScheduler::Preemption_Priority_t priority);

  /// Returns a global ThreadManager for the Task pool.
  ACE_RT_Thread_Manager* ThrMgr();

  // = The TAO_EC_Timer_Module methods.
  virtual void activate (void);
  virtual void shutdown (void);
  virtual RtecScheduler::handle_t
     rt_info (RtecScheduler::Preemption_Priority_t priority);
  virtual int schedule_timer (RtecScheduler::Preemption_Priority_t priority,
                              ACE_Command_Base* act,
                              const ACE_Time_Value& delta,
                              const ACE_Time_Value& interval);
  virtual int cancel_timer (RtecScheduler::Preemption_Priority_t priority,
                            int id,
                            ACE_Command_Base*& act);
  virtual int register_handler (RtecScheduler::Preemption_Priority_t priority,
                                ACE_Event_Handler* eh,
                                ACE_HANDLE handle);
  virtual ACE_Reactor* reactor (RtecScheduler::Preemption_Priority_t priority);

private:
  /// The reactors are shutdown, do not attempt to restart them.
  int shutdown_;

  /// The set of ReactorTasks
  ReactorTask *reactorTasks[ACE_Scheduler_MAX_PRIORITIES];

  /// The thread manager.
  ACE_RT_Thread_Manager thr_mgr;

  /// To receive the timeouts.
  TAO_EC_Timeout_Handler timeout_handler_;

  /// The scheduler.
  RtecScheduler::Scheduler_var scheduler_;
};

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

#include /**/ "ace/post.h"
#endif /* TAO_EC_TIMER_MODULE_H */