Also use Objects as part of an operation but as a result don't generate Any operation...
[ACE_TAO.git] / ACE / ace / TP_Reactor.h
blobc9be2cf15f565408890eb1ef11952dc6ffd4c3f8
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file TP_Reactor.h
7 * The ACE_TP_Reactor (aka, Thread Pool Reactor) uses the
8 * Leader/Followers pattern to demultiplex events among a pool of
9 * threads. When using a thread pool reactor, an application
10 * pre-spawns a fixed number of threads. When these threads
11 * invoke the ACE_TP_Reactor's handle_events() method, one thread
12 * will become the leader and wait for an event. The other
13 * follower threads will queue up waiting for their turn to become
14 * the leader. When an event occurs, the leader will pick a
15 * follower to become the leader and go on to handle the event.
16 * The consequence of using ACE_TP_Reactor is the amortization of
17 * the costs used to create threads. The context switching cost
18 * will also reduce. Moreover, the total resources used by
19 * threads are bounded because there are a fixed number of threads.
21 * @author Irfan Pyarali <irfan@cs.wustl.edu>
22 * @author Nanbor Wang <nanbor@cs.wustl.edu>
24 //=============================================================================
27 #ifndef ACE_TP_REACTOR_H
28 #define ACE_TP_REACTOR_H
30 #include /**/ "ace/pre.h"
32 #include "ace/Select_Reactor.h"
33 #include "ace/Timer_Queue.h" /* Simple forward decl won't work... */
35 #if !defined (ACE_LACKS_PRAGMA_ONCE)
36 # pragma once
37 #endif /* ACE_LACKS_PRAGMA_ONCE */
39 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
41 /**
42 * @class ACE_EH_Dispatch_Info
44 * @brief This structure contains information of the activated event
45 * handler.
47 class ACE_EH_Dispatch_Info
49 public:
50 ACE_EH_Dispatch_Info (void);
52 void set (ACE_HANDLE handle,
53 ACE_Event_Handler *event_handler,
54 ACE_Reactor_Mask mask,
55 ACE_EH_PTMF callback);
57 bool dispatch (void) const;
59 ACE_HANDLE handle_;
60 ACE_Event_Handler *event_handler_;
61 ACE_Reactor_Mask mask_;
62 ACE_EH_PTMF callback_;
63 int resume_flag_;
64 bool reference_counting_required_;
66 private:
67 bool dispatch_;
69 // Disallow copying and assignment.
70 ACE_EH_Dispatch_Info (const ACE_EH_Dispatch_Info &);
71 ACE_EH_Dispatch_Info &operator= (const ACE_EH_Dispatch_Info &);
75 /**
76 * @class ACE_TP_Token_Guard
78 * @brief A helper class that helps grabbing, releasing and waiting
79 * on tokens for a thread that tries calling handle_events ().
81 * In short, this class will be owned by one thread by creating on the
82 * stack. This class gives the status of the ownership of the token
83 * and manages the ownership
86 class ACE_TP_Token_Guard
88 public:
90 /// Constructor that will grab the token for us
91 ACE_TP_Token_Guard (ACE_Select_Reactor_Token &token);
93 /// Destructor. This will release the token if it hasnt been
94 /// released till this point
95 ~ACE_TP_Token_Guard (void);
97 /// Release the token ..
98 void release_token (void);
100 /// Returns whether the thread that created this object ownes the
101 /// token or not.
102 bool is_owner (void);
104 /// A helper method that grabs the token for us, after which the
105 /// thread that owns that can do some actual work.
106 int acquire_read_token (ACE_Time_Value *max_wait_time = 0);
109 * A helper method that grabs the token for us, after which the
110 * thread that owns that can do some actual work. This differs from
111 * acquire_read_token() as it uses acquire () to get the token instead of
112 * acquire_read ()
114 int acquire_token (ACE_Time_Value *max_wait_time = 0);
116 private:
118 // Disallow default construction.
119 ACE_TP_Token_Guard (void);
121 // Disallow copying and assignment.
122 ACE_TP_Token_Guard (const ACE_TP_Token_Guard &);
123 ACE_TP_Token_Guard &operator= (const ACE_TP_Token_Guard &);
125 private:
127 /// The Select Reactor token.
128 ACE_Select_Reactor_Token &token_;
130 /// Flag that indicate whether the thread that created this object
131 /// owns the token or not. A value of false indicates that this class
132 /// hasn't got the token (and hence the thread) and a value of true
133 /// vice-versa.
134 bool owner_;
138 * @class ACE_TP_Reactor
140 * @brief Specialization of ACE_Select_Reactor to support thread-pool
141 * based event dispatching.
143 * One of the shortcomings of the ACE_Select_Reactor is that it
144 * does not support a thread pool-based event dispatching model,
145 * similar to the one in ACE_WFMO_Reactor. In ACE_Select_Reactor, only
146 * thread can call handle_events() at any given time. ACE_TP_Reactor
147 * removes this short-coming.
149 * ACE_TP_Reactor is a specialization of ACE_Select_Reactor to support
150 * thread pool-based event dispatching. This reactor takes advantage
151 * of the fact that events reported by @c select() are persistent if not
152 * acted upon immediately. It works by remembering the event handler
153 * which was just activated, suspending it for further I/O activities,
154 * releasing the internal lock (so that another thread can start waiting
155 * in the event loop) and then dispatching the event's handler outside the
156 * scope of the reactor lock. After the event handler has been dispatched
157 * the event handler is resumed for further I/O activity.
159 * This reactor implementation is best suited for situations when the
160 * callbacks to event handlers can take arbitrarily long and/or a number
161 * of threads are available to run the event loop. Note that I/O-processing
162 * callback code in event handlers (e.g. handle_input()) does not have to
163 * be modified or made thread-safe for this reactor. This is because
164 * before an I/O event is dispatched to an event handler, the handler is
165 * suspended; it is resumed by the reactor after the upcall completes.
166 * Therefore, multiple I/O events will not be made to one event handler
167 * multiple threads simultaneously. This suspend/resume protection does not
168 * apply to either timers scheduled with the reactor or to notifications
169 * requested via the reactor. When using timers and/or notifications you
170 * must provide proper protection for your class in the context of multiple
171 * threads.
173 class ACE_Export ACE_TP_Reactor : public ACE_Select_Reactor
175 public:
177 /// Initialize ACE_TP_Reactor with the default size.
178 ACE_TP_Reactor (ACE_Sig_Handler * = 0,
179 ACE_Timer_Queue * = 0,
180 bool mask_signals = true,
181 int s_queue = ACE_Select_Reactor_Token::FIFO);
184 * Initialize the ACE_TP_Reactor to manage
185 * @a max_number_of_handles. If @a restart is non-0 then the
186 * ACE_Reactor's @c handle_events() method will be restarted
187 * automatically when @c EINTR occurs. If @a sh or
188 * @a tq are non-0 they are used as the signal handler and
189 * timer queue, respectively.
191 ACE_TP_Reactor (size_t max_number_of_handles,
192 bool restart = false,
193 ACE_Sig_Handler *sh = 0,
194 ACE_Timer_Queue *tq = 0,
195 bool mask_signals = true,
196 int s_queue = ACE_Select_Reactor_Token::FIFO);
199 * This event loop driver that blocks for @a max_wait_time before
200 * returning. It will return earlier if timer events, I/O events,
201 * or signal events occur. Note that @a max_wait_time can be 0, in
202 * which case this method blocks indefinitely until events occur.
204 * @a max_wait_time is decremented to reflect how much time this call
205 * took. For instance, if a time value of 3 seconds is passed to
206 * handle_events and an event occurs after 2 seconds,
207 * @a max_wait_time will equal 1 second. This can be used if an
208 * application wishes to handle events for some fixed amount of
209 * time.
211 * @return The total number of events that were dispatched; 0 if the
212 * @a max_wait_time elapsed without dispatching any handlers, or -1
213 * if an error occurs (check @c errno for more information).
215 virtual int handle_events (ACE_Time_Value *max_wait_time = 0);
217 virtual int handle_events (ACE_Time_Value &max_wait_time);
219 /// Does the reactor allow the application to resume the handle on
220 /// its own ie. can it pass on the control of handle resumption to
221 /// the application. The TP reactor has can allow applications to
222 /// resume handles. So return a positive value.
223 virtual int resumable_handler (void);
225 /// Called from handle events
226 static void no_op_sleep_hook (void *);
228 /// The ACE_TP_Reactor implementation does not have a single owner thread.
229 /// Attempts to set the owner explicitly are ignored. The reported owner
230 /// thread is the current Leader in the pattern.
231 virtual int owner (ACE_thread_t n_id, ACE_thread_t *o_id = 0);
233 /// Return the thread ID of the current Leader.
234 virtual int owner (ACE_thread_t *t_id);
236 /// Declare the dynamic allocation hooks.
237 ACE_ALLOC_HOOK_DECLARE;
239 protected:
240 // = Internal methods that do the actual work.
242 /// Template method from the base class.
243 virtual void clear_dispatch_mask (ACE_HANDLE handle,
244 ACE_Reactor_Mask mask);
246 /// Dispatch just 1 signal, timer, notification handlers
247 int dispatch_i (ACE_Time_Value *max_wait_time,
248 ACE_TP_Token_Guard &guard);
250 /// Get the event that needs dispatching. It could be either a
251 /// signal, timer, notification handlers or return possibly 1 I/O
252 /// handler for dispatching. In the most common use case, this would
253 /// return 1 I/O handler for dispatching
254 int get_event_for_dispatching (ACE_Time_Value *max_wait_time);
256 #if 0
257 // @Ciju
258 // signal handling isn't in a production state yet.
259 // Commenting it out for now.
261 /// Method to handle signals
262 /// @note It is just busted at this point in time.
263 int handle_signals (int &event_count,
264 ACE_TP_Token_Guard &g);
265 #endif // #if 0
267 /// Handle timer events
268 int handle_timer_events (int &event_count,
269 ACE_TP_Token_Guard &g);
271 /// Handle notify events
272 int handle_notify_events (int &event_count,
273 ACE_TP_Token_Guard &g);
275 /// handle socket events
276 int handle_socket_events (int &event_count,
277 ACE_TP_Token_Guard &g);
279 /// This method shouldn't get called.
280 virtual void notify_handle (ACE_HANDLE handle,
281 ACE_Reactor_Mask mask,
282 ACE_Handle_Set &,
283 ACE_Event_Handler *eh,
284 ACE_EH_PTMF callback);
285 private:
287 /// Get the handle of the notify pipe from the ready set if there is
288 /// an event in the notify pipe.
289 ACE_HANDLE get_notify_handle (void);
291 /// Get socket event dispatch information.
292 int get_socket_event_info (ACE_EH_Dispatch_Info &info);
294 /// Notify the appropriate <callback> in the context of the <eh>
295 /// associated with <handle> that a particular event has occurred.
296 int dispatch_socket_event (ACE_EH_Dispatch_Info &dispatch_info);
298 /// Clear the @a handle from the read_set
299 void clear_handle_read_set (ACE_HANDLE handle);
301 int post_process_socket_event (ACE_EH_Dispatch_Info &dispatch_info,int status);
303 private:
304 /// Deny access since member-wise won't work...
305 ACE_TP_Reactor (const ACE_TP_Reactor &);
306 ACE_TP_Reactor &operator = (const ACE_TP_Reactor &);
309 ACE_END_VERSIONED_NAMESPACE_DECL
311 #if defined (__ACE_INLINE__)
312 #include "ace/TP_Reactor.inl"
313 #endif /* __ACE_INLINE__ */
315 #include /**/ "ace/post.h"
317 #endif /* ACE_TP_REACTOR_H */