5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
// Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
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)
12
#ifndef ASIO_DETAIL_KQUEUE_REACTOR_HPP
13
#define ASIO_DETAIL_KQUEUE_REACTOR_HPP
15
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
17
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19
#include "asio/detail/push_options.hpp"
21
#include "asio/detail/kqueue_reactor_fwd.hpp"
23
#if defined(ASIO_HAS_KQUEUE)
25
#include "asio/detail/push_options.hpp"
28
#include <sys/types.h>
29
#include <sys/event.h>
31
#include <boost/config.hpp>
32
#include <boost/date_time/posix_time/posix_time_types.hpp>
33
#include <boost/throw_exception.hpp>
34
#include "asio/detail/pop_options.hpp"
36
#include "asio/error.hpp"
37
#include "asio/io_service.hpp"
38
#include "asio/system_error.hpp"
39
#include "asio/detail/bind_handler.hpp"
40
#include "asio/detail/mutex.hpp"
41
#include "asio/detail/task_io_service.hpp"
42
#include "asio/detail/thread.hpp"
43
#include "asio/detail/reactor_op_queue.hpp"
44
#include "asio/detail/select_interrupter.hpp"
45
#include "asio/detail/service_base.hpp"
46
#include "asio/detail/signal_blocker.hpp"
47
#include "asio/detail/socket_types.hpp"
48
#include "asio/detail/timer_queue.hpp"
50
// Older versions of Mac OS X may not define EV_OOBAND.
51
#if !defined(EV_OOBAND)
52
# define EV_OOBAND EV_FLAG1
53
#endif // !defined(EV_OOBAND)
58
template <bool Own_Thread>
60
: public asio::detail::service_base<kqueue_reactor<Own_Thread> >
63
// Per-descriptor data.
64
struct per_descriptor_data
66
bool allow_speculative_read;
67
bool allow_speculative_write;
71
kqueue_reactor(asio::io_service& io_service)
72
: asio::detail::service_base<
73
kqueue_reactor<Own_Thread> >(io_service),
75
kqueue_fd_(do_kqueue_create()),
76
wait_in_progress_(false),
81
pending_cancellations_(),
85
need_kqueue_wait_(true)
87
// Start the reactor's internal thread only if needed.
90
asio::detail::signal_blocker sb;
91
thread_ = new asio::detail::thread(
92
bind_handler(&kqueue_reactor::call_run_thread, this));
95
// Add the interrupter's descriptor to the kqueue.
97
EV_SET(&event, interrupter_.read_descriptor(),
98
EVFILT_READ, EV_ADD, 0, 0, 0);
99
::kevent(kqueue_fd_, &event, 1, 0, 0, 0);
109
// Destroy all user-defined handler objects owned by the service.
110
void shutdown_service()
112
asio::detail::mutex::scoped_lock lock(mutex_);
119
interrupter_.interrupt();
125
read_op_queue_.destroy_operations();
126
write_op_queue_.destroy_operations();
127
except_op_queue_.destroy_operations();
129
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
130
timer_queues_[i]->destroy_timers();
131
timer_queues_.clear();
134
// Register a socket with the reactor. Returns 0 on success, system error
136
int register_descriptor(socket_type, per_descriptor_data& descriptor_data)
138
descriptor_data.allow_speculative_read = true;
139
descriptor_data.allow_speculative_write = true;
144
// Start a new read operation. The handler object will be invoked when the
145
// given descriptor is ready to be read, or an error has occurred.
146
template <typename Handler>
147
void start_read_op(socket_type descriptor,
148
per_descriptor_data& descriptor_data, Handler handler,
149
bool allow_speculative_read = true)
151
if (allow_speculative_read && descriptor_data.allow_speculative_read)
154
std::size_t bytes_transferred = 0;
155
if (handler.perform(ec, bytes_transferred))
157
handler.complete(ec, bytes_transferred);
161
// We only get one shot at a speculative read in this function.
162
allow_speculative_read = false;
165
asio::detail::mutex::scoped_lock lock(mutex_);
170
if (!allow_speculative_read)
171
need_kqueue_wait_ = true;
172
else if (!read_op_queue_.has_operation(descriptor))
174
// Speculative reads are ok as there are no queued read operations.
175
descriptor_data.allow_speculative_read = true;
178
std::size_t bytes_transferred = 0;
179
if (handler.perform(ec, bytes_transferred))
181
handler.complete(ec, bytes_transferred);
186
// Speculative reads are not ok as there will be queued read operations.
187
descriptor_data.allow_speculative_read = false;
189
if (read_op_queue_.enqueue_operation(descriptor, handler))
192
EV_SET(&event, descriptor, EVFILT_READ, EV_ADD, 0, 0, 0);
193
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
195
asio::error_code ec(errno,
196
asio::error::get_system_category());
197
read_op_queue_.perform_all_operations(descriptor, ec);
202
// Start a new write operation. The handler object will be invoked when the
203
// given descriptor is ready to be written, or an error has occurred.
204
template <typename Handler>
205
void start_write_op(socket_type descriptor,
206
per_descriptor_data& descriptor_data, Handler handler,
207
bool allow_speculative_write = true)
209
if (allow_speculative_write && descriptor_data.allow_speculative_write)
212
std::size_t bytes_transferred = 0;
213
if (handler.perform(ec, bytes_transferred))
215
handler.complete(ec, bytes_transferred);
219
// We only get one shot at a speculative write in this function.
220
allow_speculative_write = false;
223
asio::detail::mutex::scoped_lock lock(mutex_);
228
if (!allow_speculative_write)
229
need_kqueue_wait_ = true;
230
else if (!write_op_queue_.has_operation(descriptor))
232
// Speculative writes are ok as there are no queued write operations.
233
descriptor_data.allow_speculative_write = true;
236
std::size_t bytes_transferred = 0;
237
if (handler.perform(ec, bytes_transferred))
239
handler.complete(ec, bytes_transferred);
244
// Speculative writes are not ok as there will be queued write operations.
245
descriptor_data.allow_speculative_write = false;
247
if (write_op_queue_.enqueue_operation(descriptor, handler))
250
EV_SET(&event, descriptor, EVFILT_WRITE, EV_ADD, 0, 0, 0);
251
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
253
asio::error_code ec(errno,
254
asio::error::get_system_category());
255
write_op_queue_.perform_all_operations(descriptor, ec);
260
// Start a new exception operation. The handler object will be invoked when
261
// the given descriptor has exception information, or an error has occurred.
262
template <typename Handler>
263
void start_except_op(socket_type descriptor,
264
per_descriptor_data&, Handler handler)
266
asio::detail::mutex::scoped_lock lock(mutex_);
271
if (except_op_queue_.enqueue_operation(descriptor, handler))
274
if (read_op_queue_.has_operation(descriptor))
275
EV_SET(&event, descriptor, EVFILT_READ, EV_ADD, 0, 0, 0);
277
EV_SET(&event, descriptor, EVFILT_READ, EV_ADD, EV_OOBAND, 0, 0);
278
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
280
asio::error_code ec(errno,
281
asio::error::get_system_category());
282
except_op_queue_.perform_all_operations(descriptor, ec);
287
// Start a new write operation. The handler object will be invoked when the
288
// given descriptor is ready to be written, or an error has occurred.
289
template <typename Handler>
290
void start_connect_op(socket_type descriptor,
291
per_descriptor_data& descriptor_data, Handler handler)
293
asio::detail::mutex::scoped_lock lock(mutex_);
298
// Speculative writes are not ok as there will be queued write operations.
299
descriptor_data.allow_speculative_write = false;
301
if (write_op_queue_.enqueue_operation(descriptor, handler))
304
EV_SET(&event, descriptor, EVFILT_WRITE, EV_ADD, 0, 0, 0);
305
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
307
asio::error_code ec(errno,
308
asio::error::get_system_category());
309
write_op_queue_.perform_all_operations(descriptor, ec);
314
// Cancel all operations associated with the given descriptor. The
315
// handlers associated with the descriptor will be invoked with the
316
// operation_aborted error.
317
void cancel_ops(socket_type descriptor, per_descriptor_data&)
319
asio::detail::mutex::scoped_lock lock(mutex_);
320
cancel_ops_unlocked(descriptor);
323
// Cancel any operations that are running against the descriptor and remove
324
// its registration from the reactor.
325
void close_descriptor(socket_type descriptor, per_descriptor_data&)
327
asio::detail::mutex::scoped_lock lock(mutex_);
329
// Remove the descriptor from kqueue.
330
struct kevent event[2];
331
EV_SET(&event[0], descriptor, EVFILT_READ, EV_DELETE, 0, 0, 0);
332
EV_SET(&event[1], descriptor, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
333
::kevent(kqueue_fd_, event, 2, 0, 0, 0);
335
// Cancel any outstanding operations associated with the descriptor.
336
cancel_ops_unlocked(descriptor);
339
// Add a new timer queue to the reactor.
340
template <typename Time_Traits>
341
void add_timer_queue(timer_queue<Time_Traits>& timer_queue)
343
asio::detail::mutex::scoped_lock lock(mutex_);
344
timer_queues_.push_back(&timer_queue);
347
// Remove a timer queue from the reactor.
348
template <typename Time_Traits>
349
void remove_timer_queue(timer_queue<Time_Traits>& timer_queue)
351
asio::detail::mutex::scoped_lock lock(mutex_);
352
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
354
if (timer_queues_[i] == &timer_queue)
356
timer_queues_.erase(timer_queues_.begin() + i);
362
// Schedule a timer in the given timer queue to expire at the specified
363
// absolute time. The handler object will be invoked when the timer expires.
364
template <typename Time_Traits, typename Handler>
365
void schedule_timer(timer_queue<Time_Traits>& timer_queue,
366
const typename Time_Traits::time_type& time, Handler handler, void* token)
368
asio::detail::mutex::scoped_lock lock(mutex_);
370
if (timer_queue.enqueue_timer(time, handler, token))
371
interrupter_.interrupt();
374
// Cancel the timer associated with the given token. Returns the number of
375
// handlers that have been posted or dispatched.
376
template <typename Time_Traits>
377
std::size_t cancel_timer(timer_queue<Time_Traits>& timer_queue, void* token)
379
asio::detail::mutex::scoped_lock lock(mutex_);
380
std::size_t n = timer_queue.cancel_timer(token);
382
interrupter_.interrupt();
387
friend class task_io_service<kqueue_reactor<Own_Thread> >;
389
// Run the kqueue loop.
392
asio::detail::mutex::scoped_lock lock(mutex_);
394
// Dispatch any operation cancellations that were made while the select
395
// loop was not running.
396
read_op_queue_.perform_cancellations();
397
write_op_queue_.perform_cancellations();
398
except_op_queue_.perform_cancellations();
399
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
400
timer_queues_[i]->dispatch_cancellations();
402
// Check if the thread is supposed to stop.
405
complete_operations_and_timers(lock);
409
// We can return immediately if there's no work to do and the reactor is
410
// not supposed to block.
411
if (!block && read_op_queue_.empty() && write_op_queue_.empty()
412
&& except_op_queue_.empty() && all_timer_queues_are_empty())
414
complete_operations_and_timers(lock);
418
// Determine how long to block while waiting for events.
419
timespec timeout_buf = { 0, 0 };
420
timespec* timeout = block ? get_timeout(timeout_buf) : &timeout_buf;
422
wait_in_progress_ = true;
425
// Block on the kqueue descriptor.
426
struct kevent events[128];
427
int num_events = (block || need_kqueue_wait_)
428
? kevent(kqueue_fd_, 0, 0, events, 128, timeout)
432
wait_in_progress_ = false;
434
// Block signals while performing operations.
435
asio::detail::signal_blocker sb;
437
// Dispatch the waiting events.
438
for (int i = 0; i < num_events; ++i)
440
int descriptor = events[i].ident;
441
if (descriptor == interrupter_.read_descriptor())
443
interrupter_.reset();
445
else if (events[i].filter == EVFILT_READ)
447
// Dispatch operations associated with the descriptor.
448
bool more_reads = false;
449
bool more_except = false;
450
if (events[i].flags & EV_ERROR)
452
asio::error_code error(
453
events[i].data, asio::error::get_system_category());
454
except_op_queue_.perform_all_operations(descriptor, error);
455
read_op_queue_.perform_all_operations(descriptor, error);
457
else if (events[i].flags & EV_OOBAND)
459
asio::error_code error;
460
more_except = except_op_queue_.perform_operation(descriptor, error);
461
if (events[i].data > 0)
462
more_reads = read_op_queue_.perform_operation(descriptor, error);
464
more_reads = read_op_queue_.has_operation(descriptor);
468
asio::error_code error;
469
more_reads = read_op_queue_.perform_operation(descriptor, error);
470
more_except = except_op_queue_.has_operation(descriptor);
473
// Update the descriptor in the kqueue.
476
EV_SET(&event, descriptor, EVFILT_READ, EV_ADD, 0, 0, 0);
477
else if (more_except)
478
EV_SET(&event, descriptor, EVFILT_READ, EV_ADD, EV_OOBAND, 0, 0);
480
EV_SET(&event, descriptor, EVFILT_READ, EV_DELETE, 0, 0, 0);
481
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
483
asio::error_code error(errno,
484
asio::error::get_system_category());
485
except_op_queue_.perform_all_operations(descriptor, error);
486
read_op_queue_.perform_all_operations(descriptor, error);
489
else if (events[i].filter == EVFILT_WRITE)
491
// Dispatch operations associated with the descriptor.
492
bool more_writes = false;
493
if (events[i].flags & EV_ERROR)
495
asio::error_code error(
496
events[i].data, asio::error::get_system_category());
497
write_op_queue_.perform_all_operations(descriptor, error);
501
asio::error_code error;
502
more_writes = write_op_queue_.perform_operation(descriptor, error);
505
// Update the descriptor in the kqueue.
508
EV_SET(&event, descriptor, EVFILT_WRITE, EV_ADD, 0, 0, 0);
510
EV_SET(&event, descriptor, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
511
if (::kevent(kqueue_fd_, &event, 1, 0, 0, 0) == -1)
513
asio::error_code error(errno,
514
asio::error::get_system_category());
515
write_op_queue_.perform_all_operations(descriptor, error);
520
read_op_queue_.perform_cancellations();
521
write_op_queue_.perform_cancellations();
522
except_op_queue_.perform_cancellations();
523
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
525
timer_queues_[i]->dispatch_timers();
526
timer_queues_[i]->dispatch_cancellations();
529
// Issue any pending cancellations.
530
for (std::size_t i = 0; i < pending_cancellations_.size(); ++i)
531
cancel_ops_unlocked(pending_cancellations_[i]);
532
pending_cancellations_.clear();
534
// Determine whether kqueue needs to be called next time the reactor is run.
535
need_kqueue_wait_ = !read_op_queue_.empty()
536
|| !write_op_queue_.empty() || !except_op_queue_.empty();
538
complete_operations_and_timers(lock);
541
// Run the select loop in the thread.
544
asio::detail::mutex::scoped_lock lock(mutex_);
545
while (!stop_thread_)
553
// Entry point for the select loop thread.
554
static void call_run_thread(kqueue_reactor* reactor)
556
reactor->run_thread();
559
// Interrupt the select loop.
562
interrupter_.interrupt();
565
// Create the kqueue file descriptor. Throws an exception if the descriptor
566
// cannot be created.
567
static int do_kqueue_create()
572
boost::throw_exception(
574
asio::error_code(errno,
575
asio::error::get_system_category()),
581
// Check if all timer queues are empty.
582
bool all_timer_queues_are_empty() const
584
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
585
if (!timer_queues_[i]->empty())
590
// Get the timeout value for the kevent call.
591
timespec* get_timeout(timespec& ts)
593
if (all_timer_queues_are_empty())
596
// By default we will wait no longer than 5 minutes. This will ensure that
597
// any changes to the system clock are detected after no longer than this.
598
boost::posix_time::time_duration minimum_wait_duration
599
= boost::posix_time::minutes(5);
601
for (std::size_t i = 0; i < timer_queues_.size(); ++i)
603
boost::posix_time::time_duration wait_duration
604
= timer_queues_[i]->wait_duration();
605
if (wait_duration < minimum_wait_duration)
606
minimum_wait_duration = wait_duration;
609
if (minimum_wait_duration > boost::posix_time::time_duration())
611
ts.tv_sec = minimum_wait_duration.total_seconds();
612
ts.tv_nsec = minimum_wait_duration.total_nanoseconds() % 1000000000;
623
// Cancel all operations associated with the given descriptor. The do_cancel
624
// function of the handler objects will be invoked. This function does not
625
// acquire the kqueue_reactor's mutex.
626
void cancel_ops_unlocked(socket_type descriptor)
628
bool interrupt = read_op_queue_.cancel_operations(descriptor);
629
interrupt = write_op_queue_.cancel_operations(descriptor) || interrupt;
630
interrupt = except_op_queue_.cancel_operations(descriptor) || interrupt;
632
interrupter_.interrupt();
635
// Clean up operations and timers. We must not hold the lock since the
636
// destructors may make calls back into this reactor. We make a copy of the
637
// vector of timer queues since the original may be modified while the lock
639
void complete_operations_and_timers(
640
asio::detail::mutex::scoped_lock& lock)
642
timer_queues_for_cleanup_ = timer_queues_;
644
read_op_queue_.complete_operations();
645
write_op_queue_.complete_operations();
646
except_op_queue_.complete_operations();
647
for (std::size_t i = 0; i < timer_queues_for_cleanup_.size(); ++i)
648
timer_queues_for_cleanup_[i]->complete_timers();
651
// Mutex to protect access to internal data.
652
asio::detail::mutex mutex_;
654
// The kqueue file descriptor.
657
// Whether the kqueue wait call is currently in progress
658
bool wait_in_progress_;
660
// The interrupter is used to break a blocking kevent call.
661
select_interrupter interrupter_;
663
// The queue of read operations.
664
reactor_op_queue<socket_type> read_op_queue_;
666
// The queue of write operations.
667
reactor_op_queue<socket_type> write_op_queue_;
669
// The queue of except operations.
670
reactor_op_queue<socket_type> except_op_queue_;
673
std::vector<timer_queue_base*> timer_queues_;
675
// A copy of the timer queues, used when cleaning up timers. The copy is
676
// stored as a class data member to avoid unnecessary memory allocation.
677
std::vector<timer_queue_base*> timer_queues_for_cleanup_;
679
// The descriptors that are pending cancellation.
680
std::vector<socket_type> pending_cancellations_;
682
// Does the reactor loop thread need to stop.
685
// The thread that is running the reactor loop.
686
asio::detail::thread* thread_;
688
// Whether the service has been shut down.
691
// Whether we need to call kqueue the next time the reactor is run.
692
bool need_kqueue_wait_;
695
} // namespace detail
698
#endif // defined(ASIO_HAS_KQUEUE)
700
#include "asio/detail/pop_options.hpp"
702
#endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP