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/intrusive_ptr.hpp>
15
#ifdef BOOST_THREAD_USES_CHRONO
16
#include <boost/chrono/system_clocks.hpp>
23
#include <boost/config/abi_prefix.hpp>
27
#pragma warning(disable:4251)
32
class condition_variable;
35
class thread_attributes {
37
thread_attributes() BOOST_NOEXCEPT {
39
//val_.lpThreadAttributes=0;
41
~thread_attributes() {
44
void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
45
val_.stack_size = size;
48
std::size_t get_stack_size() const BOOST_NOEXCEPT {
49
return val_.stack_size;
52
//void set_security(LPSECURITY_ATTRIBUTES lpThreadAttributes)
54
// val_.lpThreadAttributes=lpThreadAttributes;
56
//LPSECURITY_ATTRIBUTES get_security()
58
// return val_.lpThreadAttributes;
62
std::size_t stack_size;
63
//LPSECURITY_ATTRIBUTES lpThreadAttributes;
65
typedef win_attrs native_handle_type;
66
native_handle_type* native_handle() {return &val_;}
67
const native_handle_type* native_handle() const {return &val_;}
75
struct shared_state_base;
76
struct tss_cleanup_function;
77
struct thread_exit_callback_node;
80
boost::shared_ptr<boost::detail::tss_cleanup_function> func;
83
tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
85
func(func_),value(value_)
89
struct thread_data_base;
90
void intrusive_ptr_add_ref(thread_data_base * p);
91
void intrusive_ptr_release(thread_data_base * p);
93
struct BOOST_THREAD_DECL thread_data_base
96
detail::win32::handle_manager thread_handle;
97
boost::detail::thread_exit_callback_node* thread_exit_callbacks;
98
std::map<void const*,boost::detail::tss_data_node> tss_data;
100
typedef std::vector<std::pair<condition_variable*, mutex*>
101
//, hidden_allocator<std::pair<condition_variable*, mutex*> >
103
notify_list_t notify;
105
typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
106
async_states_t async_states_;
107
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
108
// These data must be at the end so that the access to the other fields doesn't change
109
// when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
110
// Another option is to have them always
111
detail::win32::handle_manager interruption_handle;
112
bool interruption_enabled;
116
count(0),thread_handle(detail::win32::invalid_handle_value),
117
thread_exit_callbacks(0),tss_data(),
121
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
122
, interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
123
, interruption_enabled(true)
126
virtual ~thread_data_base();
128
friend void intrusive_ptr_add_ref(thread_data_base * p)
130
BOOST_INTERLOCKED_INCREMENT(&p->count);
133
friend void intrusive_ptr_release(thread_data_base * p)
135
if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
137
detail::heap_delete(p);
141
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
144
BOOST_VERIFY(detail::win32::SetEvent(interruption_handle)!=0);
147
typedef detail::win32::handle native_handle_type;
149
virtual void run()=0;
151
virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
153
notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
156
void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
158
async_states_.push_back(as);
162
BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
164
typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
166
struct BOOST_SYMBOL_VISIBLE timeout
168
win32::ticks_type start;
169
uintmax_t milliseconds;
171
boost::system_time abs_time;
173
static unsigned long const max_non_infinite_wait=0xfffffffe;
175
timeout(uintmax_t milliseconds_):
176
start(win32::GetTickCount64()),
177
milliseconds(milliseconds_),
179
abs_time(boost::get_system_time())
182
timeout(boost::system_time const& abs_time_):
183
start(win32::GetTickCount64()),
189
struct BOOST_SYMBOL_VISIBLE remaining_time
192
unsigned long milliseconds;
194
remaining_time(uintmax_t remaining):
195
more(remaining>max_non_infinite_wait),
196
milliseconds(more?max_non_infinite_wait:(unsigned long)remaining)
200
remaining_time remaining_milliseconds() const
204
return remaining_time(win32::infinite);
208
win32::ticks_type const now=win32::GetTickCount64();
209
win32::ticks_type const elapsed=now-start;
210
return remaining_time((elapsed<milliseconds)?(milliseconds-elapsed):0);
214
system_time const now=get_system_time();
217
return remaining_time(0);
219
return remaining_time((abs_time-now).total_milliseconds()+1);
223
bool is_sentinel() const
225
return milliseconds==~uintmax_t(0);
229
static timeout sentinel()
231
return timeout(sentinel_type());
237
explicit timeout(sentinel_type):
238
start(0),milliseconds(~uintmax_t(0)),relative(true)
242
inline uintmax_t pin_to_zero(intmax_t value)
244
return (value<0)?0u:(uintmax_t)value;
248
namespace this_thread
250
void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
252
bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for,detail::timeout target_time);
253
inline void interruptible_wait(uintmax_t milliseconds)
255
interruptible_wait(detail::win32::invalid_handle_value,milliseconds);
257
inline BOOST_SYMBOL_VISIBLE void interruptible_wait(system_time const& abs_time)
259
interruptible_wait(detail::win32::invalid_handle_value,abs_time);
261
template<typename TimeDuration>
262
inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
264
interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
266
inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
268
interruptible_wait(abs_time);
270
#ifdef BOOST_THREAD_USES_CHRONO
271
inline void BOOST_SYMBOL_VISIBLE sleep_for(const chrono::nanoseconds& ns)
273
interruptible_wait(chrono::duration_cast<chrono::milliseconds>(ns).count());
284
#include <boost/config/abi_suffix.hpp>