Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:36

0001 //
0002 // detail/impl/winrt_timer_scheduler.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 
0022 #include <boost/asio/detail/bind_handler.hpp>
0023 #include <boost/asio/detail/winrt_timer_scheduler.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 winrt_timer_scheduler::winrt_timer_scheduler(execution_context& context)
0032   : execution_context_service_base<winrt_timer_scheduler>(context),
0033     scheduler_(use_service<scheduler_impl>(context)),
0034     mutex_(),
0035     event_(),
0036     timer_queues_(),
0037     thread_(0),
0038     stop_thread_(false),
0039     shutdown_(false)
0040 {
0041   thread_ = new boost::asio::detail::thread(
0042       bind_handler(&winrt_timer_scheduler::call_run_thread, this));
0043 }
0044 
0045 winrt_timer_scheduler::~winrt_timer_scheduler()
0046 {
0047   shutdown();
0048 }
0049 
0050 void winrt_timer_scheduler::shutdown()
0051 {
0052   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0053   shutdown_ = true;
0054   stop_thread_ = true;
0055   event_.signal(lock);
0056   lock.unlock();
0057 
0058   if (thread_)
0059   {
0060     thread_->join();
0061     delete thread_;
0062     thread_ = 0;
0063   }
0064 
0065   op_queue<operation> ops;
0066   timer_queues_.get_all_timers(ops);
0067   scheduler_.abandon_operations(ops);
0068 }
0069 
0070 void winrt_timer_scheduler::notify_fork(execution_context::fork_event)
0071 {
0072 }
0073 
0074 void winrt_timer_scheduler::init_task()
0075 {
0076 }
0077 
0078 void winrt_timer_scheduler::run_thread()
0079 {
0080   boost::asio::detail::mutex::scoped_lock lock(mutex_);
0081   while (!stop_thread_)
0082   {
0083     const long max_wait_duration = 5 * 60 * 1000000;
0084     long wait_duration = timer_queues_.wait_duration_usec(max_wait_duration);
0085     event_.wait_for_usec(lock, wait_duration);
0086     event_.clear(lock);
0087     op_queue<operation> ops;
0088     timer_queues_.get_ready_timers(ops);
0089     if (!ops.empty())
0090     {
0091       lock.unlock();
0092       scheduler_.post_deferred_completions(ops);
0093       lock.lock();
0094     }
0095   }
0096 }
0097 
0098 void winrt_timer_scheduler::call_run_thread(winrt_timer_scheduler* scheduler)
0099 {
0100   scheduler->run_thread();
0101 }
0102 
0103 void winrt_timer_scheduler::do_add_timer_queue(timer_queue_base& queue)
0104 {
0105   mutex::scoped_lock lock(mutex_);
0106   timer_queues_.insert(&queue);
0107 }
0108 
0109 void winrt_timer_scheduler::do_remove_timer_queue(timer_queue_base& queue)
0110 {
0111   mutex::scoped_lock lock(mutex_);
0112   timer_queues_.erase(&queue);
0113 }
0114 
0115 } // namespace detail
0116 } // namespace asio
0117 } // namespace boost
0118 
0119 #include <boost/asio/detail/pop_options.hpp>
0120 
0121 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0122 
0123 #endif // BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP