Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / FlReactor / FlReactor.cpp
blob9c3cf5992de1cea802213f571e3224a3d510baf7
1 #include "ace/FlReactor/FlReactor.h"
3 #include /**/ <FL/Fl.H>
5 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
7 ACE_ALLOC_HOOK_DEFINE (ACE_FlReactor)
9 // Must be called with lock held
10 ACE_FlReactor::ACE_FlReactor (size_t size,
11 bool restart,
12 ACE_Sig_Handler *h)
13 : ACE_Select_Reactor (size, restart, h)
15 // When the ACE_Select_Reactor is constructed it creates the notify
16 // pipe and registers it with the register_handler_i() method. The
17 // FlReactor overloads this method BUT because the
18 // register_handler_i occurs when constructing the base class
19 // ACE_Select_Reactor, the ACE_Select_Reactor register_handler_i()
20 // is called not the FlReactor register_handler_i(). This means
21 // that the notify pipe is registered with the ACE_Select_Reactor
22 // event handling code not the FlReactor and so notfications don't
23 // work. To get around this we simply close and re-opened the
24 // notification handler in the constructor of the FlReactor.
26 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
27 this->notify_handler_->close ();
28 this->notify_handler_->open (this, 0);
29 #endif /* ACE_MT_SAFE */
32 ACE_FlReactor::~ACE_FlReactor ()
36 // This is just the <wait_for_multiple_events> from ace/Reactor.cpp
37 // but we use the Fl functions to wait for an event, not <select>
39 int
40 ACE_FlReactor::wait_for_multiple_events (ACE_Select_Reactor_Handle_Set &handle_set,
41 ACE_Time_Value *max_wait_time)
43 ACE_TRACE ("ACE_FlReactor::wait_for_multiple_events");
44 int nfound;
48 max_wait_time = this->timer_queue_->calculate_timeout (max_wait_time);
50 size_t width = this->handler_rep_.max_handlep1 ();
51 handle_set.rd_mask_ = this->wait_set_.rd_mask_;
52 handle_set.wr_mask_ = this->wait_set_.wr_mask_;
53 handle_set.ex_mask_ = this->wait_set_.ex_mask_;
55 // Check to make sure our handle's are all usable.
56 ACE_Select_Reactor_Handle_Set temp_set = handle_set;
58 ACE_Time_Value zero = ACE_Time_Value::zero;
59 if (ACE_OS::select (width,
60 temp_set.rd_mask_,
61 temp_set.wr_mask_,
62 temp_set.ex_mask_,
63 &zero) == -1)
64 return -1; // Bad file arguments...
66 // Instead of waiting using <select>, just use the Fl mechanism
67 // to wait for one or more events...
69 // Wait for something to happen.
70 double t = 0;
71 if (max_wait_time != 0)
72 t = max_wait_time->sec () + max_wait_time->usec () / 1000000.0F;
74 while (t > 0) {
75 t = Fl::wait (t);
78 // Reset the width, in case it changed during the upcalls.
79 width = this->handler_rep_.max_handlep1 ();
81 // Now actually read the result needed by the <Select_Reactor>
82 // using <select>.
83 zero = ACE_Time_Value::zero;
84 nfound = ACE_OS::select (width,
85 handle_set.rd_mask_,
86 handle_set.wr_mask_,
87 handle_set.ex_mask_,
88 &zero);
89 } while (nfound == -1 && this->handle_error () > 0);
91 if (nfound > 0)
93 #if !defined (ACE_WIN32)
94 handle_set.rd_mask_.sync (this->handler_rep_.max_handlep1 ());
95 handle_set.wr_mask_.sync (this->handler_rep_.max_handlep1 ());
96 handle_set.ex_mask_.sync (this->handler_rep_.max_handlep1 ());
97 #endif /* ACE_WIN32 */
99 return nfound; // Timed out or input available
102 void
103 ACE_FlReactor::fl_io_proc (int fd, void* reactor)
105 ACE_FlReactor *self = static_cast<ACE_FlReactor *> (reactor);
106 ACE_HANDLE handle = (ACE_HANDLE)fd; //reinterpret_cast<ACE_HANDLE> (fd);
108 // my copy isn't const.
109 ACE_Time_Value zero = ACE_Time_Value::zero;
111 ACE_Select_Reactor_Handle_Set wait_set;
113 // Deal with one file event.
115 // - read which kind of event
116 if (self->wait_set_.rd_mask_.is_set (handle))
117 wait_set.rd_mask_.set_bit (handle);
118 if (self->wait_set_.wr_mask_.is_set (handle))
119 wait_set.wr_mask_.set_bit (handle);
120 if (self->wait_set_.ex_mask_.is_set (handle))
121 wait_set.ex_mask_.set_bit (handle);
123 int result = ACE_OS::select (fd + 1,
124 wait_set.rd_mask_,
125 wait_set.wr_mask_,
126 wait_set.ex_mask_, &zero);
128 ACE_Select_Reactor_Handle_Set dispatch_set;
130 // - Use only that one file event (removes events for other files).
131 if (result > 0)
133 if (wait_set.rd_mask_.is_set (handle))
134 dispatch_set.rd_mask_.set_bit (handle);
135 if (wait_set.wr_mask_.is_set (handle))
136 dispatch_set.wr_mask_.set_bit (handle);
137 if (wait_set.ex_mask_.is_set (handle))
138 dispatch_set.ex_mask_.set_bit (handle);
140 self->dispatch (1, dispatch_set);
144 void
145 ACE_FlReactor::fl_timeout_proc (void* reactor)
147 ACE_FlReactor *self = static_cast<ACE_FlReactor *> (reactor);
149 // Deal with any timer events
150 ACE_Select_Reactor_Handle_Set handle_set;
151 self->dispatch (0, handle_set);
152 self->reset_timeout ();
157 ACE_FlReactor::register_handler_i (ACE_HANDLE handle,
158 ACE_Event_Handler *handler,
159 ACE_Reactor_Mask mask)
161 ACE_TRACE ("ACE_FlReactor::register_handler_i");
163 int result = ACE_Select_Reactor::register_handler_i (handle,
164 handler, mask);
165 if (result == -1)
166 return -1;
168 int condition = 0;
170 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::READ_MASK))
171 ACE_SET_BITS (condition, FL_READ);
172 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::WRITE_MASK))
173 ACE_SET_BITS (condition, FL_WRITE);
174 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::EXCEPT_MASK))
175 ACE_SET_BITS (condition, FL_EXCEPT);
176 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::ACCEPT_MASK))
177 ACE_SET_BITS (condition, FL_READ);
178 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::CONNECT_MASK))
180 ACE_SET_BITS (condition, FL_WRITE); // connected, you may write
181 ACE_SET_BITS (condition, FL_READ); // connected, you have data/err
184 if (condition != 0)
186 Fl::add_fd ((int)handle, // reinterpret_cast<int> (handle),
187 ACE_FlReactor::fl_io_proc,
188 this);
190 return 0;
194 ACE_FlReactor::register_handler_i (const ACE_Handle_Set &handles,
195 ACE_Event_Handler *handler,
196 ACE_Reactor_Mask mask)
198 return ACE_Select_Reactor::register_handler_i (handles,
199 handler,
200 mask);
204 ACE_FlReactor::remove_handler_i (ACE_HANDLE handle,
205 ACE_Reactor_Mask mask)
207 ACE_TRACE ("ACE_FlReactor::remove_handler_i");
209 // In the registration phase we registered first with
210 // ACE_Select_Reactor and then with X. Now we are now doing things
211 // in reverse order.
213 // First clean up the corresponding X11Input.
214 Fl::remove_fd ((int)handle); // reinterpret_cast<int> (handle);
216 // Now let the reactor do its work.
217 return ACE_Select_Reactor::remove_handler_i (handle,
218 mask);
222 ACE_FlReactor::remove_handler_i (const ACE_Handle_Set &handles,
223 ACE_Reactor_Mask mask)
225 return ACE_Select_Reactor::remove_handler_i (handles,
226 mask);
229 // The following function ensures there's an Fl timeout for the first
230 // timeout in the Reactor's Timer_Queue.
232 void
233 ACE_FlReactor::reset_timeout ()
235 ACE_Time_Value *max_wait_time =
236 this->timer_queue_->calculate_timeout (0);
238 if (max_wait_time != 0)
240 float t = max_wait_time->sec ()
241 + max_wait_time->usec () / 1000000.0F;
242 Fl::add_timeout (t,
243 ACE_FlReactor::fl_timeout_proc,
244 this);
249 ACE_FlReactor::reset_timer_interval
250 (long timer_id,
251 const ACE_Time_Value &interval)
253 ACE_TRACE ("ACE_FlReactor::reset_timer_interval");
254 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
256 int const result =
257 ACE_Select_Reactor::reset_timer_interval (timer_id,
258 interval);
260 if (result == -1)
261 return -1;
262 else
264 this->reset_timeout ();
265 return result;
269 long
270 ACE_FlReactor::schedule_timer (ACE_Event_Handler *event_handler,
271 const void *arg,
272 const ACE_Time_Value &delay,
273 const ACE_Time_Value &interval)
275 ACE_TRACE ("ACE_FlReactor::schedule_timer");
276 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
278 long result = ACE_Select_Reactor::schedule_timer (event_handler,
279 arg,
280 delay,
281 interval);
282 if (result == -1)
283 return -1;
284 else
286 this->reset_timeout ();
287 return result;
292 ACE_FlReactor::cancel_timer (ACE_Event_Handler *handler,
293 int dont_call_handle_close)
295 ACE_TRACE ("ACE_FlReactor::cancel_timer");
297 if (ACE_Select_Reactor::cancel_timer (handler,
298 dont_call_handle_close) == -1)
299 return -1;
300 else
302 this->reset_timeout ();
303 return 0;
308 ACE_FlReactor::cancel_timer (long timer_id,
309 const void **arg,
310 int dont_call_handle_close)
312 ACE_TRACE ("ACE_FlReactor::cancel_timer");
314 if (ACE_Select_Reactor::cancel_timer (timer_id,
315 arg,
316 dont_call_handle_close) == -1)
317 return -1;
318 else
320 this->reset_timeout ();
321 return 0;
325 ACE_END_VERSIONED_NAMESPACE_DECL