]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/win_thread.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 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 | ||
11 | #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP | |
12 | #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if defined(BOOST_ASIO_WINDOWS) \ | |
21 | && !defined(BOOST_ASIO_WINDOWS_APP) \ | |
22 | && !defined(UNDER_CE) | |
23 | ||
24 | #include <process.h> | |
25 | #include <boost/asio/detail/throw_error.hpp> | |
26 | #include <boost/asio/detail/win_thread.hpp> | |
27 | #include <boost/asio/error.hpp> | |
28 | ||
29 | #include <boost/asio/detail/push_options.hpp> | |
30 | ||
31 | namespace boost { | |
32 | namespace asio { | |
33 | namespace detail { | |
34 | ||
35 | win_thread::~win_thread() | |
36 | { | |
37 | ::CloseHandle(thread_); | |
38 | ||
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. | |
41 | } | |
42 | ||
43 | void win_thread::join() | |
44 | { | |
45 | HANDLE handles[2] = { exit_event_, thread_ }; | |
46 | ::WaitForMultipleObjects(2, handles, FALSE, INFINITE); | |
47 | ::CloseHandle(exit_event_); | |
48 | if (terminate_threads()) | |
49 | { | |
50 | ::TerminateThread(thread_, 0); | |
51 | } | |
52 | else | |
53 | { | |
54 | ::QueueUserAPC(apc_function, thread_, 0); | |
55 | ::WaitForSingleObject(thread_, INFINITE); | |
56 | } | |
57 | } | |
58 | ||
59 | void win_thread::start_thread(func_base* arg, unsigned int stack_size) | |
60 | { | |
61 | ::HANDLE entry_event = 0; | |
62 | arg->entry_event_ = entry_event = ::CreateEventW(0, true, false, 0); | |
63 | if (!entry_event) | |
64 | { | |
65 | DWORD last_error = ::GetLastError(); | |
66 | delete arg; | |
67 | boost::system::error_code ec(last_error, | |
68 | boost::asio::error::get_system_category()); | |
69 | boost::asio::detail::throw_error(ec, "thread.entry_event"); | |
70 | } | |
71 | ||
72 | arg->exit_event_ = exit_event_ = ::CreateEventW(0, true, false, 0); | |
73 | if (!exit_event_) | |
74 | { | |
75 | DWORD last_error = ::GetLastError(); | |
76 | delete arg; | |
77 | boost::system::error_code ec(last_error, | |
78 | boost::asio::error::get_system_category()); | |
79 | boost::asio::detail::throw_error(ec, "thread.exit_event"); | |
80 | } | |
81 | ||
82 | unsigned int thread_id = 0; | |
83 | thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0, | |
84 | stack_size, win_thread_function, arg, 0, &thread_id)); | |
85 | if (!thread_) | |
86 | { | |
87 | DWORD last_error = ::GetLastError(); | |
88 | delete arg; | |
89 | if (entry_event) | |
90 | ::CloseHandle(entry_event); | |
91 | if (exit_event_) | |
92 | ::CloseHandle(exit_event_); | |
93 | boost::system::error_code ec(last_error, | |
94 | boost::asio::error::get_system_category()); | |
95 | boost::asio::detail::throw_error(ec, "thread"); | |
96 | } | |
97 | ||
98 | if (entry_event) | |
99 | { | |
100 | ::WaitForSingleObject(entry_event, INFINITE); | |
101 | ::CloseHandle(entry_event); | |
102 | } | |
103 | } | |
104 | ||
105 | unsigned int __stdcall win_thread_function(void* arg) | |
106 | { | |
107 | win_thread::auto_func_base_ptr func = { | |
108 | static_cast<win_thread::func_base*>(arg) }; | |
109 | ||
110 | ::SetEvent(func.ptr->entry_event_); | |
111 | ||
112 | func.ptr->run(); | |
113 | ||
114 | // Signal that the thread has finished its work, but rather than returning go | |
115 | // to sleep to put the thread into a well known state. If the thread is being | |
116 | // joined during global object destruction then it may be killed using | |
117 | // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx | |
118 | // call will be interrupted using QueueUserAPC and the thread will shut down | |
119 | // cleanly. | |
120 | HANDLE exit_event = func.ptr->exit_event_; | |
121 | delete func.ptr; | |
122 | func.ptr = 0; | |
123 | ::SetEvent(exit_event); | |
124 | ::SleepEx(INFINITE, TRUE); | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
129 | #if defined(WINVER) && (WINVER < 0x0500) | |
130 | void __stdcall apc_function(ULONG) {} | |
131 | #else | |
132 | void __stdcall apc_function(ULONG_PTR) {} | |
133 | #endif | |
134 | ||
135 | } // namespace detail | |
136 | } // namespace asio | |
137 | } // namespace boost | |
138 | ||
139 | #include <boost/asio/detail/pop_options.hpp> | |
140 | ||
141 | #endif // defined(BOOST_ASIO_WINDOWS) | |
142 | // && !defined(BOOST_ASIO_WINDOWS_APP) | |
143 | // && !defined(UNDER_CE) | |
144 | ||
145 | #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP |