]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
b32b8144 FG |
2 | // detail/wince_thread.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~ | |
7c673cae | 4 | // |
92f5a8d4 | 5 | // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | ||
b32b8144 FG |
11 | #ifndef BOOST_ASIO_DETAIL_WINCE_THREAD_HPP |
12 | #define BOOST_ASIO_DETAIL_WINCE_THREAD_HPP | |
7c673cae FG |
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 | ||
b32b8144 | 20 | #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE) |
7c673cae | 21 | |
7c673cae | 22 | #include <boost/asio/detail/noncopyable.hpp> |
b32b8144 | 23 | #include <boost/asio/detail/scoped_ptr.hpp> |
7c673cae FG |
24 | #include <boost/asio/detail/socket_types.hpp> |
25 | #include <boost/asio/detail/throw_error.hpp> | |
26 | #include <boost/asio/error.hpp> | |
27 | ||
28 | #include <boost/asio/detail/push_options.hpp> | |
29 | ||
30 | namespace boost { | |
31 | namespace asio { | |
32 | namespace detail { | |
33 | ||
b32b8144 | 34 | DWORD WINAPI wince_thread_function(LPVOID arg); |
7c673cae | 35 | |
b32b8144 | 36 | class wince_thread |
7c673cae FG |
37 | : private noncopyable |
38 | { | |
39 | public: | |
40 | // Constructor. | |
41 | template <typename Function> | |
b32b8144 | 42 | wince_thread(Function f, unsigned int = 0) |
7c673cae | 43 | { |
b32b8144 | 44 | scoped_ptr<func_base> arg(new func<Function>(f)); |
7c673cae | 45 | DWORD thread_id = 0; |
b32b8144 | 46 | thread_ = ::CreateThread(0, 0, wince_thread_function, |
7c673cae FG |
47 | arg.get(), 0, &thread_id); |
48 | if (!thread_) | |
49 | { | |
50 | DWORD last_error = ::GetLastError(); | |
51 | boost::system::error_code ec(last_error, | |
52 | boost::asio::error::get_system_category()); | |
53 | boost::asio::detail::throw_error(ec, "thread"); | |
54 | } | |
55 | arg.release(); | |
56 | } | |
57 | ||
58 | // Destructor. | |
b32b8144 | 59 | ~wince_thread() |
7c673cae FG |
60 | { |
61 | ::CloseHandle(thread_); | |
62 | } | |
63 | ||
64 | // Wait for the thread to exit. | |
65 | void join() | |
66 | { | |
7c673cae | 67 | ::WaitForSingleObject(thread_, INFINITE); |
b32b8144 FG |
68 | } |
69 | ||
70 | // Get number of CPUs. | |
71 | static std::size_t hardware_concurrency() | |
72 | { | |
73 | SYSTEM_INFO system_info; | |
74 | ::GetSystemInfo(&system_info); | |
75 | return system_info.dwNumberOfProcessors; | |
7c673cae FG |
76 | } |
77 | ||
78 | private: | |
b32b8144 | 79 | friend DWORD WINAPI wince_thread_function(LPVOID arg); |
7c673cae FG |
80 | |
81 | class func_base | |
82 | { | |
83 | public: | |
84 | virtual ~func_base() {} | |
85 | virtual void run() = 0; | |
86 | }; | |
87 | ||
88 | template <typename Function> | |
89 | class func | |
90 | : public func_base | |
91 | { | |
92 | public: | |
93 | func(Function f) | |
94 | : f_(f) | |
95 | { | |
96 | } | |
97 | ||
98 | virtual void run() | |
99 | { | |
100 | f_(); | |
101 | } | |
102 | ||
103 | private: | |
104 | Function f_; | |
105 | }; | |
106 | ||
107 | ::HANDLE thread_; | |
108 | }; | |
109 | ||
b32b8144 | 110 | inline DWORD WINAPI wince_thread_function(LPVOID arg) |
7c673cae | 111 | { |
b32b8144 FG |
112 | scoped_ptr<wince_thread::func_base> func( |
113 | static_cast<wince_thread::func_base*>(arg)); | |
7c673cae FG |
114 | func->run(); |
115 | return 0; | |
116 | } | |
117 | ||
118 | } // namespace detail | |
119 | } // namespace asio | |
120 | } // namespace boost | |
121 | ||
122 | #include <boost/asio/detail/pop_options.hpp> | |
123 | ||
b32b8144 | 124 | #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE) |
7c673cae | 125 | |
b32b8144 | 126 | #endif // BOOST_ASIO_DETAIL_WINCE_THREAD_HPP |