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>
14 #include <boost/predef/platform.h>
16 #include <boost/intrusive_ptr.hpp>
17 #ifdef BOOST_THREAD_USES_CHRONO
18 #include <boost/chrono/system_clocks.hpp>
25 #include <boost/config/abi_prefix.hpp>
29 #pragma warning(disable:4251)
34 class condition_variable;
37 class thread_attributes {
39 thread_attributes() BOOST_NOEXCEPT {
41 //val_.lpThreadAttributes=0;
43 ~thread_attributes() {
46 void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
47 val_.stack_size = size;
50 std::size_t get_stack_size() const BOOST_NOEXCEPT {
51 return val_.stack_size;
54 //void set_security(LPSECURITY_ATTRIBUTES lpThreadAttributes)
56 // val_.lpThreadAttributes=lpThreadAttributes;
58 //LPSECURITY_ATTRIBUTES get_security()
60 // return val_.lpThreadAttributes;
64 std::size_t stack_size;
65 //LPSECURITY_ATTRIBUTES lpThreadAttributes;
67 typedef win_attrs native_handle_type;
68 native_handle_type* native_handle() {return &val_;}
69 const native_handle_type* native_handle() const {return &val_;}
77 struct shared_state_base;
78 struct tss_cleanup_function;
79 struct thread_exit_callback_node;
82 boost::shared_ptr<boost::detail::tss_cleanup_function> func;
85 tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
87 func(func_),value(value_)
91 struct thread_data_base;
92 void intrusive_ptr_add_ref(thread_data_base * p);
93 void intrusive_ptr_release(thread_data_base * p);
95 struct BOOST_THREAD_DECL thread_data_base
99 // Win32 threading APIs are not available in store apps so
100 // use abstraction on top of Windows::System::Threading.
101 #if BOOST_PLAT_WINDOWS_RUNTIME
102 detail::win32::scoped_winrt_thread thread_handle;
104 detail::win32::handle_manager thread_handle;
107 boost::detail::thread_exit_callback_node* thread_exit_callbacks;
109 std::map<void const*,boost::detail::tss_data_node> tss_data;
110 typedef std::vector<std::pair<condition_variable*, mutex*>
111 //, hidden_allocator<std::pair<condition_variable*, mutex*> >
113 notify_list_t notify;
115 typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
116 async_states_t async_states_;
117 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
118 // These data must be at the end so that the access to the other fields doesn't change
119 // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
120 // Another option is to have them always
121 detail::win32::handle_manager interruption_handle;
122 bool interruption_enabled;
128 thread_exit_callbacks(0),
133 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
134 , interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
135 , interruption_enabled(true)
138 virtual ~thread_data_base();
140 friend void intrusive_ptr_add_ref(thread_data_base * p)
142 BOOST_INTERLOCKED_INCREMENT(&p->count);
145 friend void intrusive_ptr_release(thread_data_base * p)
147 if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
149 detail::heap_delete(p);
153 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
156 BOOST_VERIFY(detail::win32::SetEvent(interruption_handle)!=0);
159 typedef detail::win32::handle native_handle_type;
161 virtual void run()=0;
163 virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
165 notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
168 void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
170 async_states_.push_back(as);
174 BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
176 typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
178 struct BOOST_SYMBOL_VISIBLE timeout
180 win32::ticks_type start;
181 uintmax_t milliseconds;
183 boost::system_time abs_time;
185 static unsigned long const max_non_infinite_wait=0xfffffffe;
187 timeout(uintmax_t milliseconds_):
188 start(win32::GetTickCount64_()()),
189 milliseconds(milliseconds_),
192 // abs_time(boost::get_system_time())
195 timeout(boost::system_time const& abs_time_):
196 start(win32::GetTickCount64_()()),
202 struct BOOST_SYMBOL_VISIBLE remaining_time
205 unsigned long milliseconds;
207 remaining_time(uintmax_t remaining):
208 more(remaining>max_non_infinite_wait),
209 milliseconds(more?max_non_infinite_wait:(unsigned long)remaining)
213 remaining_time remaining_milliseconds() const
217 return remaining_time(win32::infinite);
221 win32::ticks_type const now=win32::GetTickCount64_()();
222 win32::ticks_type const elapsed=now-start;
223 return remaining_time((elapsed<milliseconds)?(milliseconds-elapsed):0);
227 system_time const now=get_system_time();
230 return remaining_time(0);
232 return remaining_time((abs_time-now).total_milliseconds()+1);
236 bool is_sentinel() const
238 return milliseconds==~uintmax_t(0);
242 static timeout sentinel()
244 return timeout(sentinel_type());
250 explicit timeout(sentinel_type):
251 start(0),milliseconds(~uintmax_t(0)),relative(true)
255 inline uintmax_t pin_to_zero(intmax_t value)
257 return (value<0)?0u:(uintmax_t)value;
261 namespace this_thread
263 void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
265 bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for,detail::timeout target_time);
266 inline void interruptible_wait(uintmax_t milliseconds)
268 interruptible_wait(detail::win32::invalid_handle_value,milliseconds);
270 inline BOOST_SYMBOL_VISIBLE void interruptible_wait(system_time const& abs_time)
272 interruptible_wait(detail::win32::invalid_handle_value,abs_time);
274 template<typename TimeDuration>
275 inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
277 interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
279 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
281 interruptible_wait(abs_time);
283 // #11322 sleep_for() nanoseconds overload will always return too early on windows
284 //#ifdef BOOST_THREAD_USES_CHRONO
285 // inline void BOOST_SYMBOL_VISIBLE sleep_for(const chrono::nanoseconds& ns)
287 // interruptible_wait(chrono::duration_cast<chrono::milliseconds>(ns).count());
290 namespace no_interruption_point
292 bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for,detail::timeout target_time);
293 inline void non_interruptible_wait(uintmax_t milliseconds)
295 non_interruptible_wait(detail::win32::invalid_handle_value,milliseconds);
297 inline BOOST_SYMBOL_VISIBLE void non_interruptible_wait(system_time const& abs_time)
299 non_interruptible_wait(detail::win32::invalid_handle_value,abs_time);
301 template<typename TimeDuration>
302 inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
304 non_interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
306 inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
308 non_interruptible_wait(abs_time);
310 // #11322 sleep_for() nanoseconds overload will always return too early on windows
311 //#ifdef BOOST_THREAD_USES_CHRONO
312 // inline void BOOST_SYMBOL_VISIBLE sleep_for(const chrono::nanoseconds& ns)
314 // non_interruptible_wait(chrono::duration_cast<chrono::milliseconds>(ns).count());
326 #include <boost/config/abi_suffix.hpp>