summaryrefslogtreecommitdiff
path: root/ace/Timer_Queue_Adapters.h
blob: c19081646f3c0aa48b5846ff7f7515f4bb6db866 (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
// -*- C++ -*-
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
//
// = FILENAME
//    Timer_Queue_Adapters.h
//
// = AUTHOR
//    Douglas C. Schmidt and Carlos O'Ryan
//
// ============================================================================

#ifndef ACE_TIMER_QUEUE_ADAPTERS_H
# define ACE_TIMER_QUEUE_ADAPTERS_H

# include "ace/Task.h"

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

# include "ace/Signal.h"

template <class TQ>
class ACE_Export ACE_Async_Timer_Queue_Adapter : public ACE_Event_Handler
{
  // = TITLE
  //     Adapts a <TQ> to be run asynchronously.
  //
  // = DESCRIPTION
  //     This implementation uses the <ualarm> call, which generates
  //     the SIGARLM signal that is caught by this class.
public:
  typedef TQ TIMER_QUEUE;

  ACE_Async_Timer_Queue_Adapter (ACE_Sig_Set *mask = 0);
  // Register the SIGALRM handler.  If <mask> == 0 then block all
  // signals when <SIGALRM> is run.  Otherwise, just block the signals
  // indicated in <mask>.

  long schedule (ACE_Event_Handler *type,
                 const void *act,
                 const ACE_Time_Value &delay,
                 const ACE_Time_Value &interval = ACE_Time_Value::zero);
  // Schedule the timer according to the semantics of the
  // <ACE_Timer_List>.  However, this timer gets dispatched via a
  // signal, rather than by a user calling <expire>.

  int cancel (long timer_id, const void **act = 0);
  // Cancel the <timer_id> and pass back the <act> if an address is
  // passed in.

  int expire (void);
  // Dispatch all timers whose values are <= <cur_time>.  Returns the
  // number of timers canceled.

  TQ &timer_queue (void);
  // Access the underlying <TIMER_QUEUE>.

private:
  virtual int schedule_ualarm (void);
  // Perform the logic to compute the new ualarm(2) setting.

  virtual int handle_signal (int signum, siginfo_t *, ucontext_t *);
  // Called back by <SIGALRM> handler.

  ACE_Sig_Handler sig_handler_;
  // Handler for the <SIGALRM> signal, so that we can access our state
  // without requiring any global variables.

  TQ timer_queue_;
  // Implementation of the timer queue (e.g., <ACE_Timer_List>,
  // <ACE_Timer_Heap>, etc.).

  ACE_Sig_Set mask_;
  // Mask of signals to be blocked when we're servicing <SIGALRM>.
};

template <class TQ>
class ACE_Export ACE_Thread_Timer_Queue_Adapter : public ACE_Task_Base
{
  // = TITLE
  //   Adapts a Timer_Queue using a separate thread for dispatching.
  //
  // = DESCRIPTION
  //   This implementation of a Timer_Queue uses a separate thread to
  //   dispatch the timers. The base queue need not be thread safe,
  //   this class takes all the necessary locks.
  //
  // = NOTE
  //   This is a case were template parameters will be useful, but
  //   (IMHO) the effort and portability problems discourage their
  //   use.
public:

  typedef TQ TIMER_QUEUE;
  // Trait for the underlying queue type.

# if defined (ACE_HAS_DEFERRED_TIMER_COMMANDS)

  enum COMMAND_ENQUEUE_POSITION {HEAD, TAIL};
  // Typedef for the position at which to enqueue a deferred execution command.

# endif /* ACE_HAS_DEFERRED_TIMER_COMMANDS */

  ACE_Thread_Timer_Queue_Adapter (ACE_Thread_Manager * = ACE_Thread_Manager::instance ());
  // Creates the timer queue.  Activation of the task is the user's
  // responsibility.

  long schedule (ACE_Event_Handler* handler,
                 const void *act,
                 const ACE_Time_Value &delay,
                 const ACE_Time_Value &interval = ACE_Time_Value::zero);
  // Schedule the timer according to the semantics of the <TQ>; wakes
  // up the dispatching thread.

  int cancel (long timer_id, const void **act = 0);
  // Cancel the <timer_id> add return the <act> parameter if an
  // address is passed in. Also wakes up the dispatching thread.

  virtual int svc (void);
  // Runs the dispatching thread.

  virtual void deactivate (void);
  // Inform the dispatching thread that it should terminate.

  ACE_SYNCH_MUTEX &mutex (void);
  // Access the locking mechanism, useful for iteration.

  TQ &timer_queue (void);
  // Access the implementation queue, useful for iteration.

  ACE_thread_t thr_id (void);
  // Return the thread id of our active object.

  virtual int activate (long flags = THR_NEW_LWP | THR_JOINABLE,
                        int n_threads = 1,
                        int force_active = 0,
                        long priority = ACE_DEFAULT_THREAD_PRIORITY,
                        int grp_id = -1,
                        ACE_Task_Base *task = 0,
                        ACE_hthread_t thread_handles[] = 0,
                        void *stack[] = 0,
                        size_t stack_size[] = 0,
                        ACE_thread_t thread_names[] = 0);
  // We override the default <activate> method so that we can ensure
  // that only a single thread is ever spawned.  Otherwise, too many
  // weird things can happen...

# if defined (ACE_HAS_DEFERRED_TIMER_COMMANDS)

  int enqueue_command (ACE_Command_Base *command_,
                       COMMAND_ENQUEUE_POSITION pos = TAIL);
  // Enqueues a command object for execution just before waiting on the next
  // timer event. This allows deferred execution of commands that cannot
  // be performed in the timer event handler context, such as registering
  // or cancelling timers on platforms where the timer queue mutex is not
  // recursive.

# endif /* ACE_HAS_DEFERRED_TIMER_COMMANDS */

private:

# if defined (ACE_HAS_DEFERRED_TIMER_COMMANDS)

  int dispatch_commands (void);
  // Dispatches all command objects enqueued in the most
  // recent event handler context.

  ACE_Unbounded_Queue<ACE_Command_Base *> command_queue_;
  // Queue of commands for deferred execution.

  ACE_SYNCH_MUTEX command_mutex_;
  // The mutual exclusion mechanism for the command queue.

# endif /* ACE_HAS_DEFERRED_TIMER_COMMANDS */

  TQ timer_queue_;
  // The underlying Timer_Queue.

  ACE_SYNCH_CONDITION condition_;
  // The dispatching thread sleeps on this condition while waiting to
  // dispatch the next timer; it is used to wake it up if there is a
  // change on the timer queue.

  ACE_SYNCH_MUTEX mutex_;
  // The mutual exclusion mechanism which is required to use the
  // <condition_>.

  int active_;
  // When deactivate is called this variable turns to false and the
  // dispatching thread is signalled, to terminate its main loop.

  ACE_thread_t thr_id_;
  // Thread id of our active object task.
};

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

# if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#  include "ace/Timer_Queue_Adapters.cpp"
# endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

# if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#  pragma implementation ("Timer_Queue_Adapters.cpp")
# endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#endif /* ACE_TIMER_QUEUE_ADAPTERS_H */