]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/include/boost/asio/detail/win_thread.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / asio / include / boost / asio / detail / win_thread.hpp
1 //
2 // detail/win_thread.hpp
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_WIN_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_WIN_THREAD_HPP
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 <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/detail/socket_types.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31 namespace detail {
32
33 BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
34
35 #if defined(WINVER) && (WINVER < 0x0500)
36 BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
37 #else
38 BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
39 #endif
40
41 template <typename T>
42 class win_thread_base
43 {
44 public:
45 static bool terminate_threads()
46 {
47 return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
48 }
49
50 static void set_terminate_threads(bool b)
51 {
52 ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
53 }
54
55 private:
56 static long terminate_threads_;
57 };
58
59 template <typename T>
60 long win_thread_base<T>::terminate_threads_ = 0;
61
62 class win_thread
63 : private noncopyable,
64 public win_thread_base<win_thread>
65 {
66 public:
67 // Constructor.
68 template <typename Function>
69 win_thread(Function f, unsigned int stack_size = 0)
70 : thread_(0),
71 exit_event_(0)
72 {
73 start_thread(new func<Function>(f), stack_size);
74 }
75
76 // Destructor.
77 BOOST_ASIO_DECL ~win_thread();
78
79 // Wait for the thread to exit.
80 BOOST_ASIO_DECL void join();
81
82 private:
83 friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
84
85 #if defined(WINVER) && (WINVER < 0x0500)
86 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
87 #else
88 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
89 #endif
90
91 class func_base
92 {
93 public:
94 virtual ~func_base() {}
95 virtual void run() = 0;
96 ::HANDLE entry_event_;
97 ::HANDLE exit_event_;
98 };
99
100 struct auto_func_base_ptr
101 {
102 func_base* ptr;
103 ~auto_func_base_ptr() { delete ptr; }
104 };
105
106 template <typename Function>
107 class func
108 : public func_base
109 {
110 public:
111 func(Function f)
112 : f_(f)
113 {
114 }
115
116 virtual void run()
117 {
118 f_();
119 }
120
121 private:
122 Function f_;
123 };
124
125 BOOST_ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
126
127 ::HANDLE thread_;
128 ::HANDLE exit_event_;
129 };
130
131 } // namespace detail
132 } // namespace asio
133 } // namespace boost
134
135 #include <boost/asio/detail/pop_options.hpp>
136
137 #if defined(BOOST_ASIO_HEADER_ONLY)
138 # include <boost/asio/detail/impl/win_thread.ipp>
139 #endif // defined(BOOST_ASIO_HEADER_ONLY)
140
141 #endif // defined(BOOST_ASIO_WINDOWS)
142 // && !defined(BOOST_ASIO_WINDOWS_APP)
143 // && !defined(UNDER_CE)
144
145 #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP