Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/impl/win_thread.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_WIN_THREAD_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_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) \
0021   && !defined(BOOST_ASIO_WINDOWS_APP) \
0022   && !defined(UNDER_CE)
0023 
0024 #include <process.h>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/detail/win_thread.hpp>
0027 #include <boost/asio/error.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace detail {
0034 
0035 win_thread::~win_thread()
0036 {
0037   ::CloseHandle(thread_);
0038 
0039   // The exit_event_ handle is deliberately allowed to leak here since it
0040   // is an error for the owner of an internal thread not to join() it.
0041 }
0042 
0043 void win_thread::join()
0044 {
0045   HANDLE handles[2] = { exit_event_, thread_ };
0046   ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
0047   ::CloseHandle(exit_event_);
0048   if (terminate_threads())
0049   {
0050     ::TerminateThread(thread_, 0);
0051   }
0052   else
0053   {
0054     ::QueueUserAPC(apc_function, thread_, 0);
0055     ::WaitForSingleObject(thread_, INFINITE);
0056   }
0057 }
0058 
0059 std::size_t win_thread::hardware_concurrency()
0060 {
0061   SYSTEM_INFO system_info;
0062   ::GetSystemInfo(&system_info);
0063   return system_info.dwNumberOfProcessors;
0064 }
0065 
0066 void win_thread::start_thread(func_base* arg, unsigned int stack_size)
0067 {
0068   ::HANDLE entry_event = 0;
0069   arg->entry_event_ = entry_event = ::CreateEventW(0, true, false, 0);
0070   if (!entry_event)
0071   {
0072     DWORD last_error = ::GetLastError();
0073     delete arg;
0074     boost::system::error_code ec(last_error,
0075         boost::asio::error::get_system_category());
0076     boost::asio::detail::throw_error(ec, "thread.entry_event");
0077   }
0078 
0079   arg->exit_event_ = exit_event_ = ::CreateEventW(0, true, false, 0);
0080   if (!exit_event_)
0081   {
0082     DWORD last_error = ::GetLastError();
0083     delete arg;
0084     boost::system::error_code ec(last_error,
0085         boost::asio::error::get_system_category());
0086     boost::asio::detail::throw_error(ec, "thread.exit_event");
0087   }
0088 
0089   unsigned int thread_id = 0;
0090   thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
0091         stack_size, win_thread_function, arg, 0, &thread_id));
0092   if (!thread_)
0093   {
0094     DWORD last_error = ::GetLastError();
0095     delete arg;
0096     if (entry_event)
0097       ::CloseHandle(entry_event);
0098     if (exit_event_)
0099       ::CloseHandle(exit_event_);
0100     boost::system::error_code ec(last_error,
0101         boost::asio::error::get_system_category());
0102     boost::asio::detail::throw_error(ec, "thread");
0103   }
0104 
0105   if (entry_event)
0106   {
0107     ::WaitForSingleObject(entry_event, INFINITE);
0108     ::CloseHandle(entry_event);
0109   }
0110 }
0111 
0112 unsigned int __stdcall win_thread_function(void* arg)
0113 {
0114   win_thread::auto_func_base_ptr func = {
0115       static_cast<win_thread::func_base*>(arg) };
0116 
0117   ::SetEvent(func.ptr->entry_event_);
0118 
0119   func.ptr->run();
0120 
0121   // Signal that the thread has finished its work, but rather than returning go
0122   // to sleep to put the thread into a well known state. If the thread is being
0123   // joined during global object destruction then it may be killed using
0124   // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
0125   // call will be interrupted using QueueUserAPC and the thread will shut down
0126   // cleanly.
0127   HANDLE exit_event = func.ptr->exit_event_;
0128   delete func.ptr;
0129   func.ptr = 0;
0130   ::SetEvent(exit_event);
0131   ::SleepEx(INFINITE, TRUE);
0132 
0133   return 0;
0134 }
0135 
0136 #if defined(WINVER) && (WINVER < 0x0500)
0137 void __stdcall apc_function(ULONG) {}
0138 #else
0139 void __stdcall apc_function(ULONG_PTR) {}
0140 #endif
0141 
0142 } // namespace detail
0143 } // namespace asio
0144 } // namespace boost
0145 
0146 #include <boost/asio/detail/pop_options.hpp>
0147 
0148 #endif // defined(BOOST_ASIO_WINDOWS)
0149        // && !defined(BOOST_ASIO_WINDOWS_APP)
0150        // && !defined(UNDER_CE)
0151 
0152 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP