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 typedef void(*cleanup_func_t)(void*);
84 typedef void(*cleanup_caller_t)(cleanup_func_t, void*);
86 cleanup_caller_t caller;
90 tss_data_node(cleanup_caller_t caller_,cleanup_func_t func_,void* value_):
91 caller(caller_),func(func_),value(value_)
95 struct thread_data_base;
96 void intrusive_ptr_add_ref(thread_data_base * p);
97 void intrusive_ptr_release(thread_data_base * p);
99 struct BOOST_THREAD_DECL thread_data_base
103 // Win32 threading APIs are not available in store apps so
104 // use abstraction on top of Windows::System::Threading.
105 #if BOOST_PLAT_WINDOWS_RUNTIME
106 detail::win32::scoped_winrt_thread thread_handle;
108 detail::win32::handle_manager thread_handle;
111 boost::detail::thread_exit_callback_node* thread_exit_callbacks;
113 std::map<void const*,boost::detail::tss_data_node> tss_data;
114 typedef std::vector<std::pair<condition_variable*, mutex*>
115 //, hidden_allocator<std::pair<condition_variable*, mutex*> >
117 notify_list_t notify;
119 //#ifndef BOOST_NO_EXCEPTIONS
120 typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
121 async_states_t async_states_;
123 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
124 // These data must be at the end so that the access to the other fields doesn't change
125 // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
126 // Another option is to have them always
127 detail::win32::handle_manager interruption_handle;
128 bool interruption_enabled;
134 thread_exit_callbacks(0),
138 //#ifndef BOOST_NO_EXCEPTIONS
141 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
142 , interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
143 , interruption_enabled(true)
146 virtual ~thread_data_base();
148 friend void intrusive_ptr_add_ref(thread_data_base * p)
150 BOOST_INTERLOCKED_INCREMENT(&p->count);
153 friend void intrusive_ptr_release(thread_data_base * p)
155 if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
157 detail::heap_delete(p);
161 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
164 BOOST_VERIFY(winapi::SetEvent(interruption_handle)!=0);
167 typedef detail::win32::handle native_handle_type;
169 virtual void run()=0;
171 virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
173 notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
176 //#ifndef BOOST_NO_EXCEPTIONS
177 void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
179 async_states_.push_back(as);
183 BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
185 typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
188 namespace this_thread
190 void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
192 bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
194 #if defined BOOST_THREAD_USES_DATETIME
195 template<typename TimeDuration>
196 BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
198 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
201 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
203 const detail::real_platform_timepoint ts(abs_time);
204 detail::platform_duration d(ts - detail::real_platform_clock::now());
205 while (d > detail::platform_duration::zero())
207 d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
208 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
209 d = ts - detail::real_platform_clock::now();
214 #ifdef BOOST_THREAD_USES_CHRONO
215 template <class Rep, class Period>
216 void sleep_for(const chrono::duration<Rep, Period>& d)
218 interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
221 template <class Duration>
222 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
224 sleep_for(t - chrono::steady_clock::now());
227 template <class Clock, class Duration>
228 void sleep_until(const chrono::time_point<Clock, Duration>& t)
230 typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
231 common_duration d(t - Clock::now());
232 while (d > common_duration::zero())
234 d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
236 d = t - Clock::now();
241 namespace no_interruption_point
243 bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
245 #if defined BOOST_THREAD_USES_DATETIME
246 template<typename TimeDuration>
247 BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
249 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
252 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
254 const detail::real_platform_timepoint ts(abs_time);
255 detail::platform_duration d(ts - detail::real_platform_clock::now());
256 while (d > detail::platform_duration::zero())
258 d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
259 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
260 d = ts - detail::real_platform_clock::now();
265 #ifdef BOOST_THREAD_USES_CHRONO
266 template <class Rep, class Period>
267 void sleep_for(const chrono::duration<Rep, Period>& d)
269 non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
272 template <class Duration>
273 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
275 sleep_for(t - chrono::steady_clock::now());
278 template <class Clock, class Duration>
279 void sleep_until(const chrono::time_point<Clock, Duration>& t)
281 typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
282 common_duration d(t - Clock::now());
283 while (d > common_duration::zero())
285 d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
287 d = t - Clock::now();
300 #include <boost/config/abi_suffix.hpp>