1 #ifndef BOOST_THREAD_PTHREAD_THREAD_DATA_HPP
2 #define BOOST_THREAD_PTHREAD_THREAD_DATA_HPP
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // (C) Copyright 2008 Anthony Williams
7 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
9 #include <boost/thread/detail/config.hpp>
10 #include <boost/thread/thread_time.hpp>
11 #include <boost/thread/win32/thread_primitives.hpp>
12 #include <boost/thread/win32/thread_heap_alloc.hpp>
13 #include <boost/thread/detail/platform_time.hpp>
15 #include <boost/predef/platform.h>
17 #include <boost/intrusive_ptr.hpp>
18 #ifdef BOOST_THREAD_USES_CHRONO
19 #include <boost/chrono/system_clocks.hpp>
26 #include <boost/config/abi_prefix.hpp>
30 #pragma warning(disable:4251)
35 class condition_variable;
38 class thread_attributes {
40 thread_attributes() BOOST_NOEXCEPT {
42 //val_.lpThreadAttributes=0;
44 ~thread_attributes() {
47 void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
48 val_.stack_size = size;
51 std::size_t get_stack_size() const BOOST_NOEXCEPT {
52 return val_.stack_size;
55 //void set_security(LPSECURITY_ATTRIBUTES lpThreadAttributes)
57 // val_.lpThreadAttributes=lpThreadAttributes;
59 //LPSECURITY_ATTRIBUTES get_security()
61 // return val_.lpThreadAttributes;
65 std::size_t stack_size;
66 //LPSECURITY_ATTRIBUTES lpThreadAttributes;
68 typedef win_attrs native_handle_type;
69 native_handle_type* native_handle() {return &val_;}
70 const native_handle_type* native_handle() const {return &val_;}
78 struct shared_state_base;
79 struct tss_cleanup_function;
80 struct thread_exit_callback_node;
83 boost::shared_ptr<boost::detail::tss_cleanup_function> func;
86 tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
88 func(func_),value(value_)
92 struct thread_data_base;
93 void intrusive_ptr_add_ref(thread_data_base * p);
94 void intrusive_ptr_release(thread_data_base * p);
96 struct BOOST_THREAD_DECL thread_data_base
100 // Win32 threading APIs are not available in store apps so
101 // use abstraction on top of Windows::System::Threading.
102 #if BOOST_PLAT_WINDOWS_RUNTIME
103 detail::win32::scoped_winrt_thread thread_handle;
105 detail::win32::handle_manager thread_handle;
108 boost::detail::thread_exit_callback_node* thread_exit_callbacks;
110 std::map<void const*,boost::detail::tss_data_node> tss_data;
111 typedef std::vector<std::pair<condition_variable*, mutex*>
112 //, hidden_allocator<std::pair<condition_variable*, mutex*> >
114 notify_list_t notify;
116 typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
117 async_states_t async_states_;
118 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
119 // These data must be at the end so that the access to the other fields doesn't change
120 // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
121 // Another option is to have them always
122 detail::win32::handle_manager interruption_handle;
123 bool interruption_enabled;
129 thread_exit_callbacks(0),
134 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
135 , interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
136 , interruption_enabled(true)
139 virtual ~thread_data_base();
141 friend void intrusive_ptr_add_ref(thread_data_base * p)
143 BOOST_INTERLOCKED_INCREMENT(&p->count);
146 friend void intrusive_ptr_release(thread_data_base * p)
148 if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
150 detail::heap_delete(p);
154 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
157 BOOST_VERIFY(winapi::SetEvent(interruption_handle)!=0);
160 typedef detail::win32::handle native_handle_type;
162 virtual void run()=0;
164 virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
166 notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
169 void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
171 async_states_.push_back(as);
175 BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
177 typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
180 namespace this_thread
182 void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
184 bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
186 #if defined BOOST_THREAD_USES_DATETIME
187 template<typename TimeDuration>
188 BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
190 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
193 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
195 const detail::real_platform_timepoint ts(abs_time);
196 detail::platform_duration d(ts - detail::real_platform_clock::now());
197 while (d > detail::platform_duration::zero())
199 d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
200 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
201 d = ts - detail::real_platform_clock::now();
206 #ifdef BOOST_THREAD_USES_CHRONO
207 template <class Rep, class Period>
208 void sleep_for(const chrono::duration<Rep, Period>& d)
210 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
213 template <class Duration>
214 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
216 sleep_for(t - chrono::steady_clock::now());
219 template <class Clock, class Duration>
220 void sleep_until(const chrono::time_point<Clock, Duration>& t)
222 typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
223 common_duration d(t - Clock::now());
224 while (d > common_duration::zero())
226 d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
228 d = t - Clock::now();
233 namespace no_interruption_point
235 bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
237 #if defined BOOST_THREAD_USES_DATETIME
238 template<typename TimeDuration>
239 BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
241 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
244 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
246 const detail::real_platform_timepoint ts(abs_time);
247 detail::platform_duration d(ts - detail::real_platform_clock::now());
248 while (d > detail::platform_duration::zero())
250 d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
251 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
252 d = ts - detail::real_platform_clock::now();
257 #ifdef BOOST_THREAD_USES_CHRONO
258 template <class Rep, class Period>
259 void sleep_for(const chrono::duration<Rep, Period>& d)
261 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
264 template <class Duration>
265 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
267 sleep_for(t - chrono::steady_clock::now());
270 template <class Clock, class Duration>
271 void sleep_until(const chrono::time_point<Clock, Duration>& t)
273 typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
274 common_duration d(t - Clock::now());
275 while (d > common_duration::zero())
277 d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
279 d = t - Clock::now();
292 #include <boost/config/abi_suffix.hpp>