2 // detail/impl/win_thread.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS) \
21 && !defined(BOOST_ASIO_WINDOWS_APP) \
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/detail/win_thread.hpp>
27 #include <boost/asio/error.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 win_thread::~win_thread()
37 ::CloseHandle(thread_);
39 // The exit_event_ handle is deliberately allowed to leak here since it
40 // is an error for the owner of an internal thread not to join() it.
43 void win_thread::join()
45 HANDLE handles[2] = { exit_event_, thread_ };
46 ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
47 ::CloseHandle(exit_event_);
48 if (terminate_threads())
50 ::TerminateThread(thread_, 0);
54 ::QueueUserAPC(apc_function, thread_, 0);
55 ::WaitForSingleObject(thread_, INFINITE);
59 std::size_t win_thread::hardware_concurrency()
61 SYSTEM_INFO system_info;
62 ::GetSystemInfo(&system_info);
63 return system_info.dwNumberOfProcessors;
66 void win_thread::start_thread(func_base* arg, unsigned int stack_size)
68 ::HANDLE entry_event = 0;
69 arg->entry_event_ = entry_event = ::CreateEventW(0, true, false, 0);
72 DWORD last_error = ::GetLastError();
74 boost::system::error_code ec(last_error,
75 boost::asio::error::get_system_category());
76 boost::asio::detail::throw_error(ec, "thread.entry_event");
79 arg->exit_event_ = exit_event_ = ::CreateEventW(0, true, false, 0);
82 DWORD last_error = ::GetLastError();
84 boost::system::error_code ec(last_error,
85 boost::asio::error::get_system_category());
86 boost::asio::detail::throw_error(ec, "thread.exit_event");
89 unsigned int thread_id = 0;
90 thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
91 stack_size, win_thread_function, arg, 0, &thread_id));
94 DWORD last_error = ::GetLastError();
97 ::CloseHandle(entry_event);
99 ::CloseHandle(exit_event_);
100 boost::system::error_code ec(last_error,
101 boost::asio::error::get_system_category());
102 boost::asio::detail::throw_error(ec, "thread");
107 ::WaitForSingleObject(entry_event, INFINITE);
108 ::CloseHandle(entry_event);
112 unsigned int __stdcall win_thread_function(void* arg)
114 win_thread::auto_func_base_ptr func = {
115 static_cast<win_thread::func_base*>(arg) };
117 ::SetEvent(func.ptr->entry_event_);
121 // Signal that the thread has finished its work, but rather than returning go
122 // to sleep to put the thread into a well known state. If the thread is being
123 // joined during global object destruction then it may be killed using
124 // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
125 // call will be interrupted using QueueUserAPC and the thread will shut down
127 HANDLE exit_event = func.ptr->exit_event_;
130 ::SetEvent(exit_event);
131 ::SleepEx(INFINITE, TRUE);
136 #if defined(WINVER) && (WINVER < 0x0500)
137 void __stdcall apc_function(ULONG) {}
139 void __stdcall apc_function(ULONG_PTR) {}
142 } // namespace detail
146 #include <boost/asio/detail/pop_options.hpp>
148 #endif // defined(BOOST_ASIO_WINDOWS)
149 // && !defined(BOOST_ASIO_WINDOWS_APP)
150 // && !defined(UNDER_CE)
152 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP