File indexing completed on 2025-01-18 09:28:36
0001
0002
0003
0004
0005
0006
0007
0008
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
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
0040
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
0122
0123
0124
0125
0126
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 }
0143 }
0144 }
0145
0146 #include <boost/asio/detail/pop_options.hpp>
0147
0148 #endif
0149
0150
0151
0152 #endif