win_iocp_io_service.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // detail/impl/win_iocp_io_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP
  11. #define ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IOCP)
  17. #include "asio/detail/call_stack.hpp"
  18. #include "asio/detail/completion_handler.hpp"
  19. #include "asio/detail/fenced_block.hpp"
  20. #include "asio/detail/handler_alloc_helpers.hpp"
  21. #include "asio/detail/handler_invoke_helpers.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. template <typename Handler>
  26. void win_iocp_io_service::dispatch(Handler handler)
  27. {
  28. if (call_stack<win_iocp_io_service>::contains(this))
  29. {
  30. asio::detail::fenced_block b;
  31. asio_handler_invoke_helpers::invoke(handler, handler);
  32. }
  33. else
  34. post(handler);
  35. }
  36. template <typename Handler>
  37. void win_iocp_io_service::post(Handler handler)
  38. {
  39. // Allocate and construct an operation to wrap the handler.
  40. typedef completion_handler<Handler> op;
  41. typename op::ptr p = { boost::addressof(handler),
  42. asio_handler_alloc_helpers::allocate(
  43. sizeof(op), handler), 0 };
  44. p.p = new (p.v) op(handler);
  45. post_immediate_completion(p.p);
  46. p.v = p.p = 0;
  47. }
  48. template <typename Time_Traits>
  49. void win_iocp_io_service::add_timer_queue(
  50. timer_queue<Time_Traits>& queue)
  51. {
  52. do_add_timer_queue(queue);
  53. }
  54. template <typename Time_Traits>
  55. void win_iocp_io_service::remove_timer_queue(
  56. timer_queue<Time_Traits>& queue)
  57. {
  58. do_remove_timer_queue(queue);
  59. }
  60. template <typename Time_Traits>
  61. void win_iocp_io_service::schedule_timer(timer_queue<Time_Traits>& queue,
  62. const typename Time_Traits::time_type& time,
  63. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op)
  64. {
  65. // If the service has been shut down we silently discard the timer.
  66. if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
  67. {
  68. post_immediate_completion(op);
  69. return;
  70. }
  71. mutex::scoped_lock lock(dispatch_mutex_);
  72. bool earliest = queue.enqueue_timer(time, timer, op);
  73. work_started();
  74. if (earliest)
  75. update_timeout();
  76. }
  77. template <typename Time_Traits>
  78. std::size_t win_iocp_io_service::cancel_timer(timer_queue<Time_Traits>& queue,
  79. typename timer_queue<Time_Traits>::per_timer_data& timer)
  80. {
  81. // If the service has been shut down we silently ignore the cancellation.
  82. if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
  83. return 0;
  84. mutex::scoped_lock lock(dispatch_mutex_);
  85. op_queue<win_iocp_operation> ops;
  86. std::size_t n = queue.cancel_timer(timer, ops);
  87. post_deferred_completions(ops);
  88. return n;
  89. }
  90. } // namespace detail
  91. } // namespace asio
  92. #include "asio/detail/pop_options.hpp"
  93. #endif // defined(ASIO_HAS_IOCP)
  94. #endif // ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP