]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/kqueue_reactor.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com) |
7 | // | |
8 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
9 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | ||
12 | #ifndef BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP | |
13 | #define BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP | |
14 | ||
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
16 | # pragma once | |
17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
18 | ||
19 | #include <boost/asio/detail/config.hpp> | |
20 | ||
21 | #if defined(BOOST_ASIO_HAS_KQUEUE) | |
22 | ||
23 | #include <boost/asio/detail/kqueue_reactor.hpp> | |
b32b8144 | 24 | #include <boost/asio/detail/scheduler.hpp> |
7c673cae FG |
25 | #include <boost/asio/detail/throw_error.hpp> |
26 | #include <boost/asio/error.hpp> | |
27 | ||
f67539c2 TL |
28 | #if defined(__NetBSD__) |
29 | # include <sys/param.h> | |
30 | #endif | |
31 | ||
7c673cae FG |
32 | #include <boost/asio/detail/push_options.hpp> |
33 | ||
f67539c2 | 34 | #if defined(__NetBSD__) && __NetBSD_Version__ < 999001500 |
7c673cae FG |
35 | # define BOOST_ASIO_KQUEUE_EV_SET(ev, ident, filt, flags, fflags, data, udata) \ |
36 | EV_SET(ev, ident, filt, flags, fflags, data, \ | |
37 | reinterpret_cast<intptr_t>(static_cast<void*>(udata))) | |
38 | #else | |
39 | # define BOOST_ASIO_KQUEUE_EV_SET(ev, ident, filt, flags, fflags, data, udata) \ | |
40 | EV_SET(ev, ident, filt, flags, fflags, data, udata) | |
41 | #endif | |
42 | ||
43 | namespace boost { | |
44 | namespace asio { | |
45 | namespace detail { | |
46 | ||
b32b8144 FG |
47 | kqueue_reactor::kqueue_reactor(boost::asio::execution_context& ctx) |
48 | : execution_context_service_base<kqueue_reactor>(ctx), | |
49 | scheduler_(use_service<scheduler>(ctx)), | |
50 | mutex_(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING( | |
51 | REACTOR_REGISTRATION, scheduler_.concurrency_hint())), | |
7c673cae FG |
52 | kqueue_fd_(do_kqueue_create()), |
53 | interrupter_(), | |
b32b8144 FG |
54 | shutdown_(false), |
55 | registered_descriptors_mutex_(mutex_.enabled()) | |
7c673cae FG |
56 | { |
57 | struct kevent events[1]; | |
58 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], interrupter_.read_descriptor(), | |
59 | EVFILT_READ, EV_ADD, 0, 0, &interrupter_); | |
60 | if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1) | |
61 | { | |
62 | boost::system::error_code error(errno, | |
63 | boost::asio::error::get_system_category()); | |
64 | boost::asio::detail::throw_error(error); | |
65 | } | |
66 | } | |
67 | ||
68 | kqueue_reactor::~kqueue_reactor() | |
69 | { | |
70 | close(kqueue_fd_); | |
71 | } | |
72 | ||
b32b8144 | 73 | void kqueue_reactor::shutdown() |
7c673cae FG |
74 | { |
75 | mutex::scoped_lock lock(mutex_); | |
76 | shutdown_ = true; | |
77 | lock.unlock(); | |
78 | ||
79 | op_queue<operation> ops; | |
80 | ||
81 | while (descriptor_state* state = registered_descriptors_.first()) | |
82 | { | |
83 | for (int i = 0; i < max_ops; ++i) | |
84 | ops.push(state->op_queue_[i]); | |
85 | state->shutdown_ = true; | |
86 | registered_descriptors_.free(state); | |
87 | } | |
88 | ||
89 | timer_queues_.get_all_timers(ops); | |
90 | ||
b32b8144 | 91 | scheduler_.abandon_operations(ops); |
7c673cae FG |
92 | } |
93 | ||
b32b8144 FG |
94 | void kqueue_reactor::notify_fork( |
95 | boost::asio::execution_context::fork_event fork_ev) | |
7c673cae | 96 | { |
b32b8144 | 97 | if (fork_ev == boost::asio::execution_context::fork_child) |
7c673cae FG |
98 | { |
99 | // The kqueue descriptor is automatically closed in the child. | |
100 | kqueue_fd_ = -1; | |
101 | kqueue_fd_ = do_kqueue_create(); | |
102 | ||
103 | interrupter_.recreate(); | |
104 | ||
105 | struct kevent events[2]; | |
106 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], interrupter_.read_descriptor(), | |
107 | EVFILT_READ, EV_ADD, 0, 0, &interrupter_); | |
108 | if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1) | |
109 | { | |
110 | boost::system::error_code ec(errno, | |
111 | boost::asio::error::get_system_category()); | |
112 | boost::asio::detail::throw_error(ec, "kqueue interrupter registration"); | |
113 | } | |
114 | ||
115 | // Re-register all descriptors with kqueue. | |
116 | mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_); | |
117 | for (descriptor_state* state = registered_descriptors_.first(); | |
118 | state != 0; state = state->next_) | |
119 | { | |
120 | if (state->num_kevents_ > 0) | |
121 | { | |
122 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], state->descriptor_, | |
123 | EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, state); | |
124 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], state->descriptor_, | |
125 | EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, state); | |
126 | if (::kevent(kqueue_fd_, events, state->num_kevents_, 0, 0, 0) == -1) | |
127 | { | |
128 | boost::system::error_code ec(errno, | |
129 | boost::asio::error::get_system_category()); | |
130 | boost::asio::detail::throw_error(ec, "kqueue re-registration"); | |
131 | } | |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | void kqueue_reactor::init_task() | |
138 | { | |
b32b8144 | 139 | scheduler_.init_task(); |
7c673cae FG |
140 | } |
141 | ||
142 | int kqueue_reactor::register_descriptor(socket_type descriptor, | |
143 | kqueue_reactor::per_descriptor_data& descriptor_data) | |
144 | { | |
145 | descriptor_data = allocate_descriptor_state(); | |
146 | ||
b32b8144 FG |
147 | BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(( |
148 | context(), static_cast<uintmax_t>(descriptor), | |
149 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
150 | ||
7c673cae FG |
151 | mutex::scoped_lock lock(descriptor_data->mutex_); |
152 | ||
153 | descriptor_data->descriptor_ = descriptor; | |
154 | descriptor_data->num_kevents_ = 0; | |
155 | descriptor_data->shutdown_ = false; | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | int kqueue_reactor::register_internal_descriptor( | |
161 | int op_type, socket_type descriptor, | |
162 | kqueue_reactor::per_descriptor_data& descriptor_data, reactor_op* op) | |
163 | { | |
164 | descriptor_data = allocate_descriptor_state(); | |
165 | ||
b32b8144 FG |
166 | BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(( |
167 | context(), static_cast<uintmax_t>(descriptor), | |
168 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
169 | ||
7c673cae FG |
170 | mutex::scoped_lock lock(descriptor_data->mutex_); |
171 | ||
172 | descriptor_data->descriptor_ = descriptor; | |
173 | descriptor_data->num_kevents_ = 1; | |
174 | descriptor_data->shutdown_ = false; | |
175 | descriptor_data->op_queue_[op_type].push(op); | |
176 | ||
177 | struct kevent events[1]; | |
178 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ, | |
179 | EV_ADD | EV_CLEAR, 0, 0, descriptor_data); | |
180 | if (::kevent(kqueue_fd_, events, 1, 0, 0, 0) == -1) | |
181 | return errno; | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
186 | void kqueue_reactor::move_descriptor(socket_type, | |
187 | kqueue_reactor::per_descriptor_data& target_descriptor_data, | |
188 | kqueue_reactor::per_descriptor_data& source_descriptor_data) | |
189 | { | |
190 | target_descriptor_data = source_descriptor_data; | |
191 | source_descriptor_data = 0; | |
192 | } | |
193 | ||
194 | void kqueue_reactor::start_op(int op_type, socket_type descriptor, | |
195 | kqueue_reactor::per_descriptor_data& descriptor_data, reactor_op* op, | |
196 | bool is_continuation, bool allow_speculative) | |
197 | { | |
198 | if (!descriptor_data) | |
199 | { | |
200 | op->ec_ = boost::asio::error::bad_descriptor; | |
201 | post_immediate_completion(op, is_continuation); | |
202 | return; | |
203 | } | |
204 | ||
205 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
206 | ||
207 | if (descriptor_data->shutdown_) | |
208 | { | |
209 | post_immediate_completion(op, is_continuation); | |
210 | return; | |
211 | } | |
212 | ||
213 | if (descriptor_data->op_queue_[op_type].empty()) | |
214 | { | |
215 | static const int num_kevents[max_ops] = { 1, 2, 1 }; | |
216 | ||
217 | if (allow_speculative | |
218 | && (op_type != read_op | |
219 | || descriptor_data->op_queue_[except_op].empty())) | |
220 | { | |
221 | if (op->perform()) | |
222 | { | |
223 | descriptor_lock.unlock(); | |
b32b8144 | 224 | scheduler_.post_immediate_completion(op, is_continuation); |
7c673cae FG |
225 | return; |
226 | } | |
227 | ||
228 | if (descriptor_data->num_kevents_ < num_kevents[op_type]) | |
229 | { | |
230 | struct kevent events[2]; | |
231 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ, | |
232 | EV_ADD | EV_CLEAR, 0, 0, descriptor_data); | |
233 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, EVFILT_WRITE, | |
234 | EV_ADD | EV_CLEAR, 0, 0, descriptor_data); | |
235 | if (::kevent(kqueue_fd_, events, num_kevents[op_type], 0, 0, 0) != -1) | |
236 | { | |
237 | descriptor_data->num_kevents_ = num_kevents[op_type]; | |
238 | } | |
239 | else | |
240 | { | |
241 | op->ec_ = boost::system::error_code(errno, | |
242 | boost::asio::error::get_system_category()); | |
b32b8144 | 243 | scheduler_.post_immediate_completion(op, is_continuation); |
7c673cae FG |
244 | return; |
245 | } | |
246 | } | |
247 | } | |
248 | else | |
249 | { | |
250 | if (descriptor_data->num_kevents_ < num_kevents[op_type]) | |
251 | descriptor_data->num_kevents_ = num_kevents[op_type]; | |
252 | ||
253 | struct kevent events[2]; | |
254 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, EVFILT_READ, | |
255 | EV_ADD | EV_CLEAR, 0, 0, descriptor_data); | |
256 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, EVFILT_WRITE, | |
257 | EV_ADD | EV_CLEAR, 0, 0, descriptor_data); | |
258 | ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0); | |
259 | } | |
260 | } | |
261 | ||
262 | descriptor_data->op_queue_[op_type].push(op); | |
b32b8144 | 263 | scheduler_.work_started(); |
7c673cae FG |
264 | } |
265 | ||
266 | void kqueue_reactor::cancel_ops(socket_type, | |
267 | kqueue_reactor::per_descriptor_data& descriptor_data) | |
268 | { | |
269 | if (!descriptor_data) | |
270 | return; | |
271 | ||
272 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
273 | ||
274 | op_queue<operation> ops; | |
275 | for (int i = 0; i < max_ops; ++i) | |
276 | { | |
277 | while (reactor_op* op = descriptor_data->op_queue_[i].front()) | |
278 | { | |
279 | op->ec_ = boost::asio::error::operation_aborted; | |
280 | descriptor_data->op_queue_[i].pop(); | |
281 | ops.push(op); | |
282 | } | |
283 | } | |
284 | ||
285 | descriptor_lock.unlock(); | |
286 | ||
b32b8144 | 287 | scheduler_.post_deferred_completions(ops); |
7c673cae FG |
288 | } |
289 | ||
1e59de90 TL |
290 | void kqueue_reactor::cancel_ops_by_key(socket_type, |
291 | kqueue_reactor::per_descriptor_data& descriptor_data, | |
292 | int op_type, void* cancellation_key) | |
293 | { | |
294 | if (!descriptor_data) | |
295 | return; | |
296 | ||
297 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
298 | ||
299 | op_queue<operation> ops; | |
300 | op_queue<reactor_op> other_ops; | |
301 | while (reactor_op* op = descriptor_data->op_queue_[op_type].front()) | |
302 | { | |
303 | descriptor_data->op_queue_[op_type].pop(); | |
304 | if (op->cancellation_key_ == cancellation_key) | |
305 | { | |
306 | op->ec_ = boost::asio::error::operation_aborted; | |
307 | ops.push(op); | |
308 | } | |
309 | else | |
310 | other_ops.push(op); | |
311 | } | |
312 | descriptor_data->op_queue_[op_type].push(other_ops); | |
313 | ||
314 | descriptor_lock.unlock(); | |
315 | ||
316 | scheduler_.post_deferred_completions(ops); | |
317 | } | |
318 | ||
7c673cae FG |
319 | void kqueue_reactor::deregister_descriptor(socket_type descriptor, |
320 | kqueue_reactor::per_descriptor_data& descriptor_data, bool closing) | |
321 | { | |
322 | if (!descriptor_data) | |
323 | return; | |
324 | ||
325 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
326 | ||
327 | if (!descriptor_data->shutdown_) | |
328 | { | |
329 | if (closing) | |
330 | { | |
331 | // The descriptor will be automatically removed from the kqueue when it | |
332 | // is closed. | |
333 | } | |
334 | else | |
335 | { | |
336 | struct kevent events[2]; | |
337 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, | |
338 | EVFILT_READ, EV_DELETE, 0, 0, 0); | |
339 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, | |
340 | EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
341 | ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0); | |
342 | } | |
343 | ||
344 | op_queue<operation> ops; | |
345 | for (int i = 0; i < max_ops; ++i) | |
346 | { | |
347 | while (reactor_op* op = descriptor_data->op_queue_[i].front()) | |
348 | { | |
349 | op->ec_ = boost::asio::error::operation_aborted; | |
350 | descriptor_data->op_queue_[i].pop(); | |
351 | ops.push(op); | |
352 | } | |
353 | } | |
354 | ||
355 | descriptor_data->descriptor_ = -1; | |
356 | descriptor_data->shutdown_ = true; | |
357 | ||
358 | descriptor_lock.unlock(); | |
359 | ||
b32b8144 FG |
360 | BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(( |
361 | context(), static_cast<uintmax_t>(descriptor), | |
362 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
7c673cae | 363 | |
b32b8144 FG |
364 | scheduler_.post_deferred_completions(ops); |
365 | ||
366 | // Leave descriptor_data set so that it will be freed by the subsequent | |
367 | // call to cleanup_descriptor_data. | |
368 | } | |
369 | else | |
370 | { | |
371 | // We are shutting down, so prevent cleanup_descriptor_data from freeing | |
372 | // the descriptor_data object and let the destructor free it instead. | |
373 | descriptor_data = 0; | |
7c673cae FG |
374 | } |
375 | } | |
376 | ||
377 | void kqueue_reactor::deregister_internal_descriptor(socket_type descriptor, | |
378 | kqueue_reactor::per_descriptor_data& descriptor_data) | |
379 | { | |
380 | if (!descriptor_data) | |
381 | return; | |
382 | ||
383 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
384 | ||
385 | if (!descriptor_data->shutdown_) | |
386 | { | |
387 | struct kevent events[2]; | |
388 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, | |
389 | EVFILT_READ, EV_DELETE, 0, 0, 0); | |
390 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, | |
391 | EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
392 | ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0); | |
393 | ||
394 | op_queue<operation> ops; | |
395 | for (int i = 0; i < max_ops; ++i) | |
396 | ops.push(descriptor_data->op_queue_[i]); | |
397 | ||
398 | descriptor_data->descriptor_ = -1; | |
399 | descriptor_data->shutdown_ = true; | |
400 | ||
401 | descriptor_lock.unlock(); | |
402 | ||
b32b8144 FG |
403 | BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(( |
404 | context(), static_cast<uintmax_t>(descriptor), | |
405 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
406 | ||
407 | // Leave descriptor_data set so that it will be freed by the subsequent | |
408 | // call to cleanup_descriptor_data. | |
409 | } | |
410 | else | |
411 | { | |
412 | // We are shutting down, so prevent cleanup_descriptor_data from freeing | |
413 | // the descriptor_data object and let the destructor free it instead. | |
414 | descriptor_data = 0; | |
415 | } | |
416 | } | |
417 | ||
418 | void kqueue_reactor::cleanup_descriptor_data( | |
419 | per_descriptor_data& descriptor_data) | |
420 | { | |
421 | if (descriptor_data) | |
422 | { | |
7c673cae FG |
423 | free_descriptor_state(descriptor_data); |
424 | descriptor_data = 0; | |
425 | } | |
426 | } | |
427 | ||
b32b8144 | 428 | void kqueue_reactor::run(long usec, op_queue<operation>& ops) |
7c673cae FG |
429 | { |
430 | mutex::scoped_lock lock(mutex_); | |
431 | ||
432 | // Determine how long to block while waiting for events. | |
433 | timespec timeout_buf = { 0, 0 }; | |
b32b8144 | 434 | timespec* timeout = usec ? get_timeout(usec, timeout_buf) : &timeout_buf; |
7c673cae FG |
435 | |
436 | lock.unlock(); | |
437 | ||
438 | // Block on the kqueue descriptor. | |
439 | struct kevent events[128]; | |
440 | int num_events = kevent(kqueue_fd_, 0, 0, events, 128, timeout); | |
441 | ||
b32b8144 FG |
442 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) |
443 | // Trace the waiting events. | |
444 | for (int i = 0; i < num_events; ++i) | |
445 | { | |
446 | void* ptr = reinterpret_cast<void*>(events[i].udata); | |
447 | if (ptr != &interrupter_) | |
448 | { | |
449 | unsigned event_mask = 0; | |
450 | switch (events[i].filter) | |
451 | { | |
452 | case EVFILT_READ: | |
453 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_READ_EVENT; | |
454 | break; | |
455 | case EVFILT_WRITE: | |
456 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT; | |
457 | break; | |
458 | } | |
459 | if ((events[i].flags & (EV_ERROR | EV_OOBAND)) != 0) | |
460 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT; | |
461 | BOOST_ASIO_HANDLER_REACTOR_EVENTS((context(), | |
462 | reinterpret_cast<uintmax_t>(ptr), event_mask)); | |
463 | } | |
464 | } | |
465 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
466 | ||
7c673cae FG |
467 | // Dispatch the waiting events. |
468 | for (int i = 0; i < num_events; ++i) | |
469 | { | |
470 | void* ptr = reinterpret_cast<void*>(events[i].udata); | |
471 | if (ptr == &interrupter_) | |
472 | { | |
473 | interrupter_.reset(); | |
474 | } | |
475 | else | |
476 | { | |
477 | descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr); | |
478 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
479 | ||
480 | if (events[i].filter == EVFILT_WRITE | |
481 | && descriptor_data->num_kevents_ == 2 | |
482 | && descriptor_data->op_queue_[write_op].empty()) | |
483 | { | |
484 | // Some descriptor types, like serial ports, don't seem to support | |
485 | // EV_CLEAR with EVFILT_WRITE. Since we have no pending write | |
486 | // operations we'll remove the EVFILT_WRITE registration here so that | |
487 | // we don't end up in a tight spin. | |
488 | struct kevent delete_events[1]; | |
489 | BOOST_ASIO_KQUEUE_EV_SET(&delete_events[0], | |
490 | descriptor_data->descriptor_, EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
491 | ::kevent(kqueue_fd_, delete_events, 1, 0, 0, 0); | |
492 | descriptor_data->num_kevents_ = 1; | |
493 | } | |
494 | ||
495 | // Exception operations must be processed first to ensure that any | |
496 | // out-of-band data is read before normal data. | |
497 | #if defined(__NetBSD__) | |
498 | static const unsigned int filter[max_ops] = | |
499 | #else | |
500 | static const int filter[max_ops] = | |
501 | #endif | |
502 | { EVFILT_READ, EVFILT_WRITE, EVFILT_READ }; | |
503 | for (int j = max_ops - 1; j >= 0; --j) | |
504 | { | |
505 | if (events[i].filter == filter[j]) | |
506 | { | |
507 | if (j != except_op || events[i].flags & EV_OOBAND) | |
508 | { | |
509 | while (reactor_op* op = descriptor_data->op_queue_[j].front()) | |
510 | { | |
511 | if (events[i].flags & EV_ERROR) | |
512 | { | |
513 | op->ec_ = boost::system::error_code( | |
514 | static_cast<int>(events[i].data), | |
515 | boost::asio::error::get_system_category()); | |
516 | descriptor_data->op_queue_[j].pop(); | |
517 | ops.push(op); | |
518 | } | |
519 | if (op->perform()) | |
520 | { | |
521 | descriptor_data->op_queue_[j].pop(); | |
522 | ops.push(op); | |
523 | } | |
524 | else | |
525 | break; | |
526 | } | |
527 | } | |
528 | } | |
529 | } | |
530 | } | |
531 | } | |
532 | ||
533 | lock.lock(); | |
534 | timer_queues_.get_ready_timers(ops); | |
535 | } | |
536 | ||
537 | void kqueue_reactor::interrupt() | |
538 | { | |
539 | interrupter_.interrupt(); | |
540 | } | |
541 | ||
542 | int kqueue_reactor::do_kqueue_create() | |
543 | { | |
544 | int fd = ::kqueue(); | |
545 | if (fd == -1) | |
546 | { | |
547 | boost::system::error_code ec(errno, | |
548 | boost::asio::error::get_system_category()); | |
549 | boost::asio::detail::throw_error(ec, "kqueue"); | |
550 | } | |
551 | return fd; | |
552 | } | |
553 | ||
554 | kqueue_reactor::descriptor_state* kqueue_reactor::allocate_descriptor_state() | |
555 | { | |
556 | mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_); | |
b32b8144 FG |
557 | return registered_descriptors_.alloc(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING( |
558 | REACTOR_IO, scheduler_.concurrency_hint())); | |
7c673cae FG |
559 | } |
560 | ||
561 | void kqueue_reactor::free_descriptor_state(kqueue_reactor::descriptor_state* s) | |
562 | { | |
563 | mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_); | |
564 | registered_descriptors_.free(s); | |
565 | } | |
566 | ||
567 | void kqueue_reactor::do_add_timer_queue(timer_queue_base& queue) | |
568 | { | |
569 | mutex::scoped_lock lock(mutex_); | |
570 | timer_queues_.insert(&queue); | |
571 | } | |
572 | ||
573 | void kqueue_reactor::do_remove_timer_queue(timer_queue_base& queue) | |
574 | { | |
575 | mutex::scoped_lock lock(mutex_); | |
576 | timer_queues_.erase(&queue); | |
577 | } | |
578 | ||
b32b8144 | 579 | timespec* kqueue_reactor::get_timeout(long usec, timespec& ts) |
7c673cae FG |
580 | { |
581 | // By default we will wait no longer than 5 minutes. This will ensure that | |
582 | // any changes to the system clock are detected after no longer than this. | |
b32b8144 FG |
583 | const long max_usec = 5 * 60 * 1000 * 1000; |
584 | usec = timer_queues_.wait_duration_usec( | |
585 | (usec < 0 || max_usec < usec) ? max_usec : usec); | |
7c673cae FG |
586 | ts.tv_sec = usec / 1000000; |
587 | ts.tv_nsec = (usec % 1000000) * 1000; | |
588 | return &ts; | |
589 | } | |
590 | ||
591 | } // namespace detail | |
592 | } // namespace asio | |
593 | } // namespace boost | |
594 | ||
595 | #undef BOOST_ASIO_KQUEUE_EV_SET | |
596 | ||
597 | #include <boost/asio/detail/pop_options.hpp> | |
598 | ||
599 | #endif // defined(BOOST_ASIO_HAS_KQUEUE) | |
600 | ||
601 | #endif // BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP |