Correct feature names
[ACE_TAO.git] / ACE / ace / FlReactor / FlReactor.cpp
blobedbac9be35ec74d5f3db2c1c190e3b8b718d39ca
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 (void)
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);
90 } while (nfound == -1 && this->handle_error () > 0);
92 if (nfound > 0)
94 #if !defined (ACE_WIN32)
95 handle_set.rd_mask_.sync (this->handler_rep_.max_handlep1 ());
96 handle_set.wr_mask_.sync (this->handler_rep_.max_handlep1 ());
97 handle_set.ex_mask_.sync (this->handler_rep_.max_handlep1 ());
98 #endif /* ACE_WIN32 */
100 return nfound; // Timed out or input available
103 void
104 ACE_FlReactor::fl_io_proc (int fd, void* reactor)
106 ACE_FlReactor *self = static_cast<ACE_FlReactor *> (reactor);
107 ACE_HANDLE handle = (ACE_HANDLE)fd; //reinterpret_cast<ACE_HANDLE> (fd);
109 // my copy isn't const.
110 ACE_Time_Value zero = ACE_Time_Value::zero;
112 ACE_Select_Reactor_Handle_Set wait_set;
114 // Deal with one file event.
116 // - read which kind of event
117 if (self->wait_set_.rd_mask_.is_set (handle))
118 wait_set.rd_mask_.set_bit (handle);
119 if (self->wait_set_.wr_mask_.is_set (handle))
120 wait_set.wr_mask_.set_bit (handle);
121 if (self->wait_set_.ex_mask_.is_set (handle))
122 wait_set.ex_mask_.set_bit (handle);
124 int result = ACE_OS::select (fd + 1,
125 wait_set.rd_mask_,
126 wait_set.wr_mask_,
127 wait_set.ex_mask_, &zero);
129 ACE_Select_Reactor_Handle_Set dispatch_set;
131 // - Use only that one file event (removes events for other files).
132 if (result > 0)
134 if (wait_set.rd_mask_.is_set (handle))
135 dispatch_set.rd_mask_.set_bit (handle);
136 if (wait_set.wr_mask_.is_set (handle))
137 dispatch_set.wr_mask_.set_bit (handle);
138 if (wait_set.ex_mask_.is_set (handle))
139 dispatch_set.ex_mask_.set_bit (handle);
141 self->dispatch (1, dispatch_set);
145 void
146 ACE_FlReactor::fl_timeout_proc (void* reactor)
148 ACE_FlReactor *self = static_cast<ACE_FlReactor *> (reactor);
150 // Deal with any timer events
151 ACE_Select_Reactor_Handle_Set handle_set;
152 self->dispatch (0, handle_set);
153 self->reset_timeout ();
158 ACE_FlReactor::register_handler_i (ACE_HANDLE handle,
159 ACE_Event_Handler *handler,
160 ACE_Reactor_Mask mask)
162 ACE_TRACE ("ACE_FlReactor::register_handler_i");
164 int result = ACE_Select_Reactor::register_handler_i (handle,
165 handler, mask);
166 if (result == -1)
167 return -1;
169 int condition = 0;
171 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::READ_MASK))
172 ACE_SET_BITS (condition, FL_READ);
173 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::WRITE_MASK))
174 ACE_SET_BITS (condition, FL_WRITE);
175 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::EXCEPT_MASK))
176 ACE_SET_BITS (condition, FL_EXCEPT);
177 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::ACCEPT_MASK))
178 ACE_SET_BITS (condition, FL_READ);
179 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::CONNECT_MASK))
181 ACE_SET_BITS (condition, FL_WRITE); // connected, you may write
182 ACE_SET_BITS (condition, FL_READ); // connected, you have data/err
185 if (condition != 0)
187 Fl::add_fd ((int)handle, // reinterpret_cast<int> (handle),
188 ACE_FlReactor::fl_io_proc,
189 this);
191 return 0;
195 ACE_FlReactor::register_handler_i (const ACE_Handle_Set &handles,
196 ACE_Event_Handler *handler,
197 ACE_Reactor_Mask mask)
199 return ACE_Select_Reactor::register_handler_i (handles,
200 handler,
201 mask);
205 ACE_FlReactor::remove_handler_i (ACE_HANDLE handle,
206 ACE_Reactor_Mask mask)
208 ACE_TRACE ("ACE_FlReactor::remove_handler_i");
210 // In the registration phase we registered first with
211 // ACE_Select_Reactor and then with X. Now we are now doing things
212 // in reverse order.
214 // First clean up the corresponding X11Input.
215 Fl::remove_fd ((int)handle); // reinterpret_cast<int> (handle);
217 // Now let the reactor do its work.
218 return ACE_Select_Reactor::remove_handler_i (handle,
219 mask);
223 ACE_FlReactor::remove_handler_i (const ACE_Handle_Set &handles,
224 ACE_Reactor_Mask mask)
226 return ACE_Select_Reactor::remove_handler_i (handles,
227 mask);
230 // The following function ensures there's an Fl timeout for the first
231 // timeout in the Reactor's Timer_Queue.
233 void
234 ACE_FlReactor::reset_timeout (void)
236 ACE_Time_Value *max_wait_time =
237 this->timer_queue_->calculate_timeout (0);
239 if (max_wait_time != 0)
241 float t = max_wait_time->sec ()
242 + max_wait_time->usec () / 1000000.0F;
243 Fl::add_timeout (t,
244 ACE_FlReactor::fl_timeout_proc,
245 this);
250 ACE_FlReactor::reset_timer_interval
251 (long timer_id,
252 const ACE_Time_Value &interval)
254 ACE_TRACE ("ACE_FlReactor::reset_timer_interval");
255 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
257 int const result =
258 ACE_Select_Reactor::reset_timer_interval (timer_id,
259 interval);
261 if (result == -1)
262 return -1;
263 else
265 this->reset_timeout ();
266 return result;
270 long
271 ACE_FlReactor::schedule_timer (ACE_Event_Handler *event_handler,
272 const void *arg,
273 const ACE_Time_Value &delay,
274 const ACE_Time_Value &interval)
276 ACE_TRACE ("ACE_FlReactor::schedule_timer");
277 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
279 long result = ACE_Select_Reactor::schedule_timer (event_handler,
280 arg,
281 delay,
282 interval);
283 if (result == -1)
284 return -1;
285 else
287 this->reset_timeout ();
288 return result;
293 ACE_FlReactor::cancel_timer (ACE_Event_Handler *handler,
294 int dont_call_handle_close)
296 ACE_TRACE ("ACE_FlReactor::cancel_timer");
298 if (ACE_Select_Reactor::cancel_timer (handler,
299 dont_call_handle_close) == -1)
300 return -1;
301 else
303 this->reset_timeout ();
304 return 0;
309 ACE_FlReactor::cancel_timer (long timer_id,
310 const void **arg,
311 int dont_call_handle_close)
313 ACE_TRACE ("ACE_FlReactor::cancel_timer");
315 if (ACE_Select_Reactor::cancel_timer (timer_id,
316 arg,
317 dont_call_handle_close) == -1)
318 return -1;
319 else
321 this->reset_timeout ();
322 return 0;
326 ACE_END_VERSIONED_NAMESPACE_DECL