]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/kqueue_reactor.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 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 | ||
290 | void kqueue_reactor::deregister_descriptor(socket_type descriptor, | |
291 | kqueue_reactor::per_descriptor_data& descriptor_data, bool closing) | |
292 | { | |
293 | if (!descriptor_data) | |
294 | return; | |
295 | ||
296 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
297 | ||
298 | if (!descriptor_data->shutdown_) | |
299 | { | |
300 | if (closing) | |
301 | { | |
302 | // The descriptor will be automatically removed from the kqueue when it | |
303 | // is closed. | |
304 | } | |
305 | else | |
306 | { | |
307 | struct kevent events[2]; | |
308 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, | |
309 | EVFILT_READ, EV_DELETE, 0, 0, 0); | |
310 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, | |
311 | EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
312 | ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0); | |
313 | } | |
314 | ||
315 | op_queue<operation> ops; | |
316 | for (int i = 0; i < max_ops; ++i) | |
317 | { | |
318 | while (reactor_op* op = descriptor_data->op_queue_[i].front()) | |
319 | { | |
320 | op->ec_ = boost::asio::error::operation_aborted; | |
321 | descriptor_data->op_queue_[i].pop(); | |
322 | ops.push(op); | |
323 | } | |
324 | } | |
325 | ||
326 | descriptor_data->descriptor_ = -1; | |
327 | descriptor_data->shutdown_ = true; | |
328 | ||
329 | descriptor_lock.unlock(); | |
330 | ||
b32b8144 FG |
331 | BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(( |
332 | context(), static_cast<uintmax_t>(descriptor), | |
333 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
7c673cae | 334 | |
b32b8144 FG |
335 | scheduler_.post_deferred_completions(ops); |
336 | ||
337 | // Leave descriptor_data set so that it will be freed by the subsequent | |
338 | // call to cleanup_descriptor_data. | |
339 | } | |
340 | else | |
341 | { | |
342 | // We are shutting down, so prevent cleanup_descriptor_data from freeing | |
343 | // the descriptor_data object and let the destructor free it instead. | |
344 | descriptor_data = 0; | |
7c673cae FG |
345 | } |
346 | } | |
347 | ||
348 | void kqueue_reactor::deregister_internal_descriptor(socket_type descriptor, | |
349 | kqueue_reactor::per_descriptor_data& descriptor_data) | |
350 | { | |
351 | if (!descriptor_data) | |
352 | return; | |
353 | ||
354 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
355 | ||
356 | if (!descriptor_data->shutdown_) | |
357 | { | |
358 | struct kevent events[2]; | |
359 | BOOST_ASIO_KQUEUE_EV_SET(&events[0], descriptor, | |
360 | EVFILT_READ, EV_DELETE, 0, 0, 0); | |
361 | BOOST_ASIO_KQUEUE_EV_SET(&events[1], descriptor, | |
362 | EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
363 | ::kevent(kqueue_fd_, events, descriptor_data->num_kevents_, 0, 0, 0); | |
364 | ||
365 | op_queue<operation> ops; | |
366 | for (int i = 0; i < max_ops; ++i) | |
367 | ops.push(descriptor_data->op_queue_[i]); | |
368 | ||
369 | descriptor_data->descriptor_ = -1; | |
370 | descriptor_data->shutdown_ = true; | |
371 | ||
372 | descriptor_lock.unlock(); | |
373 | ||
b32b8144 FG |
374 | BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(( |
375 | context(), static_cast<uintmax_t>(descriptor), | |
376 | reinterpret_cast<uintmax_t>(descriptor_data))); | |
377 | ||
378 | // Leave descriptor_data set so that it will be freed by the subsequent | |
379 | // call to cleanup_descriptor_data. | |
380 | } | |
381 | else | |
382 | { | |
383 | // We are shutting down, so prevent cleanup_descriptor_data from freeing | |
384 | // the descriptor_data object and let the destructor free it instead. | |
385 | descriptor_data = 0; | |
386 | } | |
387 | } | |
388 | ||
389 | void kqueue_reactor::cleanup_descriptor_data( | |
390 | per_descriptor_data& descriptor_data) | |
391 | { | |
392 | if (descriptor_data) | |
393 | { | |
7c673cae FG |
394 | free_descriptor_state(descriptor_data); |
395 | descriptor_data = 0; | |
396 | } | |
397 | } | |
398 | ||
b32b8144 | 399 | void kqueue_reactor::run(long usec, op_queue<operation>& ops) |
7c673cae FG |
400 | { |
401 | mutex::scoped_lock lock(mutex_); | |
402 | ||
403 | // Determine how long to block while waiting for events. | |
404 | timespec timeout_buf = { 0, 0 }; | |
b32b8144 | 405 | timespec* timeout = usec ? get_timeout(usec, timeout_buf) : &timeout_buf; |
7c673cae FG |
406 | |
407 | lock.unlock(); | |
408 | ||
409 | // Block on the kqueue descriptor. | |
410 | struct kevent events[128]; | |
411 | int num_events = kevent(kqueue_fd_, 0, 0, events, 128, timeout); | |
412 | ||
b32b8144 FG |
413 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) |
414 | // Trace the waiting events. | |
415 | for (int i = 0; i < num_events; ++i) | |
416 | { | |
417 | void* ptr = reinterpret_cast<void*>(events[i].udata); | |
418 | if (ptr != &interrupter_) | |
419 | { | |
420 | unsigned event_mask = 0; | |
421 | switch (events[i].filter) | |
422 | { | |
423 | case EVFILT_READ: | |
424 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_READ_EVENT; | |
425 | break; | |
426 | case EVFILT_WRITE: | |
427 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT; | |
428 | break; | |
429 | } | |
430 | if ((events[i].flags & (EV_ERROR | EV_OOBAND)) != 0) | |
431 | event_mask |= BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT; | |
432 | BOOST_ASIO_HANDLER_REACTOR_EVENTS((context(), | |
433 | reinterpret_cast<uintmax_t>(ptr), event_mask)); | |
434 | } | |
435 | } | |
436 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
437 | ||
7c673cae FG |
438 | // Dispatch the waiting events. |
439 | for (int i = 0; i < num_events; ++i) | |
440 | { | |
441 | void* ptr = reinterpret_cast<void*>(events[i].udata); | |
442 | if (ptr == &interrupter_) | |
443 | { | |
444 | interrupter_.reset(); | |
445 | } | |
446 | else | |
447 | { | |
448 | descriptor_state* descriptor_data = static_cast<descriptor_state*>(ptr); | |
449 | mutex::scoped_lock descriptor_lock(descriptor_data->mutex_); | |
450 | ||
451 | if (events[i].filter == EVFILT_WRITE | |
452 | && descriptor_data->num_kevents_ == 2 | |
453 | && descriptor_data->op_queue_[write_op].empty()) | |
454 | { | |
455 | // Some descriptor types, like serial ports, don't seem to support | |
456 | // EV_CLEAR with EVFILT_WRITE. Since we have no pending write | |
457 | // operations we'll remove the EVFILT_WRITE registration here so that | |
458 | // we don't end up in a tight spin. | |
459 | struct kevent delete_events[1]; | |
460 | BOOST_ASIO_KQUEUE_EV_SET(&delete_events[0], | |
461 | descriptor_data->descriptor_, EVFILT_WRITE, EV_DELETE, 0, 0, 0); | |
462 | ::kevent(kqueue_fd_, delete_events, 1, 0, 0, 0); | |
463 | descriptor_data->num_kevents_ = 1; | |
464 | } | |
465 | ||
466 | // Exception operations must be processed first to ensure that any | |
467 | // out-of-band data is read before normal data. | |
468 | #if defined(__NetBSD__) | |
469 | static const unsigned int filter[max_ops] = | |
470 | #else | |
471 | static const int filter[max_ops] = | |
472 | #endif | |
473 | { EVFILT_READ, EVFILT_WRITE, EVFILT_READ }; | |
474 | for (int j = max_ops - 1; j >= 0; --j) | |
475 | { | |
476 | if (events[i].filter == filter[j]) | |
477 | { | |
478 | if (j != except_op || events[i].flags & EV_OOBAND) | |
479 | { | |
480 | while (reactor_op* op = descriptor_data->op_queue_[j].front()) | |
481 | { | |
482 | if (events[i].flags & EV_ERROR) | |
483 | { | |
484 | op->ec_ = boost::system::error_code( | |
485 | static_cast<int>(events[i].data), | |
486 | boost::asio::error::get_system_category()); | |
487 | descriptor_data->op_queue_[j].pop(); | |
488 | ops.push(op); | |
489 | } | |
490 | if (op->perform()) | |
491 | { | |
492 | descriptor_data->op_queue_[j].pop(); | |
493 | ops.push(op); | |
494 | } | |
495 | else | |
496 | break; | |
497 | } | |
498 | } | |
499 | } | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
504 | lock.lock(); | |
505 | timer_queues_.get_ready_timers(ops); | |
506 | } | |
507 | ||
508 | void kqueue_reactor::interrupt() | |
509 | { | |
510 | interrupter_.interrupt(); | |
511 | } | |
512 | ||
513 | int kqueue_reactor::do_kqueue_create() | |
514 | { | |
515 | int fd = ::kqueue(); | |
516 | if (fd == -1) | |
517 | { | |
518 | boost::system::error_code ec(errno, | |
519 | boost::asio::error::get_system_category()); | |
520 | boost::asio::detail::throw_error(ec, "kqueue"); | |
521 | } | |
522 | return fd; | |
523 | } | |
524 | ||
525 | kqueue_reactor::descriptor_state* kqueue_reactor::allocate_descriptor_state() | |
526 | { | |
527 | mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_); | |
b32b8144 FG |
528 | return registered_descriptors_.alloc(BOOST_ASIO_CONCURRENCY_HINT_IS_LOCKING( |
529 | REACTOR_IO, scheduler_.concurrency_hint())); | |
7c673cae FG |
530 | } |
531 | ||
532 | void kqueue_reactor::free_descriptor_state(kqueue_reactor::descriptor_state* s) | |
533 | { | |
534 | mutex::scoped_lock descriptors_lock(registered_descriptors_mutex_); | |
535 | registered_descriptors_.free(s); | |
536 | } | |
537 | ||
538 | void kqueue_reactor::do_add_timer_queue(timer_queue_base& queue) | |
539 | { | |
540 | mutex::scoped_lock lock(mutex_); | |
541 | timer_queues_.insert(&queue); | |
542 | } | |
543 | ||
544 | void kqueue_reactor::do_remove_timer_queue(timer_queue_base& queue) | |
545 | { | |
546 | mutex::scoped_lock lock(mutex_); | |
547 | timer_queues_.erase(&queue); | |
548 | } | |
549 | ||
b32b8144 | 550 | timespec* kqueue_reactor::get_timeout(long usec, timespec& ts) |
7c673cae FG |
551 | { |
552 | // By default we will wait no longer than 5 minutes. This will ensure that | |
553 | // any changes to the system clock are detected after no longer than this. | |
b32b8144 FG |
554 | const long max_usec = 5 * 60 * 1000 * 1000; |
555 | usec = timer_queues_.wait_duration_usec( | |
556 | (usec < 0 || max_usec < usec) ? max_usec : usec); | |
7c673cae FG |
557 | ts.tv_sec = usec / 1000000; |
558 | ts.tv_nsec = (usec % 1000000) * 1000; | |
559 | return &ts; | |
560 | } | |
561 | ||
562 | } // namespace detail | |
563 | } // namespace asio | |
564 | } // namespace boost | |
565 | ||
566 | #undef BOOST_ASIO_KQUEUE_EV_SET | |
567 | ||
568 | #include <boost/asio/detail/pop_options.hpp> | |
569 | ||
570 | #endif // defined(BOOST_ASIO_HAS_KQUEUE) | |
571 | ||
572 | #endif // BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_IPP |