annotate DEPENDENCIES/generic/include/boost/asio/detail/impl/win_thread.ipp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //
Chris@16 2 // detail/impl/win_thread.ipp
Chris@16 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris@16 4 //
Chris@101 5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
Chris@16 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 //
Chris@16 10
Chris@16 11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
Chris@16 12 #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
Chris@16 13
Chris@16 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 17
Chris@16 18 #include <boost/asio/detail/config.hpp>
Chris@16 19
Chris@16 20 #if defined(BOOST_ASIO_WINDOWS) && !defined(UNDER_CE)
Chris@16 21
Chris@16 22 #include <process.h>
Chris@16 23 #include <boost/asio/detail/throw_error.hpp>
Chris@16 24 #include <boost/asio/detail/win_thread.hpp>
Chris@16 25 #include <boost/asio/error.hpp>
Chris@16 26
Chris@16 27 #include <boost/asio/detail/push_options.hpp>
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace asio {
Chris@16 31 namespace detail {
Chris@16 32
Chris@16 33 win_thread::~win_thread()
Chris@16 34 {
Chris@16 35 ::CloseHandle(thread_);
Chris@16 36
Chris@16 37 // The exit_event_ handle is deliberately allowed to leak here since it
Chris@16 38 // is an error for the owner of an internal thread not to join() it.
Chris@16 39 }
Chris@16 40
Chris@16 41 void win_thread::join()
Chris@16 42 {
Chris@16 43 HANDLE handles[2] = { exit_event_, thread_ };
Chris@16 44 ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
Chris@16 45 ::CloseHandle(exit_event_);
Chris@16 46 if (terminate_threads())
Chris@16 47 {
Chris@16 48 ::TerminateThread(thread_, 0);
Chris@16 49 }
Chris@16 50 else
Chris@16 51 {
Chris@16 52 ::QueueUserAPC(apc_function, thread_, 0);
Chris@16 53 ::WaitForSingleObject(thread_, INFINITE);
Chris@16 54 }
Chris@16 55 }
Chris@16 56
Chris@16 57 void win_thread::start_thread(func_base* arg, unsigned int stack_size)
Chris@16 58 {
Chris@16 59 ::HANDLE entry_event = 0;
Chris@16 60 arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
Chris@16 61 if (!entry_event)
Chris@16 62 {
Chris@16 63 DWORD last_error = ::GetLastError();
Chris@16 64 delete arg;
Chris@16 65 boost::system::error_code ec(last_error,
Chris@16 66 boost::asio::error::get_system_category());
Chris@16 67 boost::asio::detail::throw_error(ec, "thread.entry_event");
Chris@16 68 }
Chris@16 69
Chris@16 70 arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
Chris@16 71 if (!exit_event_)
Chris@16 72 {
Chris@16 73 DWORD last_error = ::GetLastError();
Chris@16 74 delete arg;
Chris@16 75 boost::system::error_code ec(last_error,
Chris@16 76 boost::asio::error::get_system_category());
Chris@16 77 boost::asio::detail::throw_error(ec, "thread.exit_event");
Chris@16 78 }
Chris@16 79
Chris@16 80 unsigned int thread_id = 0;
Chris@16 81 thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
Chris@16 82 stack_size, win_thread_function, arg, 0, &thread_id));
Chris@16 83 if (!thread_)
Chris@16 84 {
Chris@16 85 DWORD last_error = ::GetLastError();
Chris@16 86 delete arg;
Chris@16 87 if (entry_event)
Chris@16 88 ::CloseHandle(entry_event);
Chris@16 89 if (exit_event_)
Chris@16 90 ::CloseHandle(exit_event_);
Chris@16 91 boost::system::error_code ec(last_error,
Chris@16 92 boost::asio::error::get_system_category());
Chris@16 93 boost::asio::detail::throw_error(ec, "thread");
Chris@16 94 }
Chris@16 95
Chris@16 96 if (entry_event)
Chris@16 97 {
Chris@16 98 ::WaitForSingleObject(entry_event, INFINITE);
Chris@16 99 ::CloseHandle(entry_event);
Chris@16 100 }
Chris@16 101 }
Chris@16 102
Chris@16 103 unsigned int __stdcall win_thread_function(void* arg)
Chris@16 104 {
Chris@16 105 win_thread::auto_func_base_ptr func = {
Chris@16 106 static_cast<win_thread::func_base*>(arg) };
Chris@16 107
Chris@16 108 ::SetEvent(func.ptr->entry_event_);
Chris@16 109
Chris@16 110 func.ptr->run();
Chris@16 111
Chris@16 112 // Signal that the thread has finished its work, but rather than returning go
Chris@16 113 // to sleep to put the thread into a well known state. If the thread is being
Chris@16 114 // joined during global object destruction then it may be killed using
Chris@16 115 // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
Chris@16 116 // call will be interrupted using QueueUserAPC and the thread will shut down
Chris@16 117 // cleanly.
Chris@16 118 HANDLE exit_event = func.ptr->exit_event_;
Chris@16 119 delete func.ptr;
Chris@16 120 func.ptr = 0;
Chris@16 121 ::SetEvent(exit_event);
Chris@16 122 ::SleepEx(INFINITE, TRUE);
Chris@16 123
Chris@16 124 return 0;
Chris@16 125 }
Chris@16 126
Chris@16 127 #if defined(WINVER) && (WINVER < 0x0500)
Chris@16 128 void __stdcall apc_function(ULONG) {}
Chris@16 129 #else
Chris@16 130 void __stdcall apc_function(ULONG_PTR) {}
Chris@16 131 #endif
Chris@16 132
Chris@16 133 } // namespace detail
Chris@16 134 } // namespace asio
Chris@16 135 } // namespace boost
Chris@16 136
Chris@16 137 #include <boost/asio/detail/pop_options.hpp>
Chris@16 138
Chris@16 139 #endif // defined(BOOST_ASIO_WINDOWS) && !defined(UNDER_CE)
Chris@16 140
Chris@16 141 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP