/src/contrib/boost/thread/pthread/thread_data.hpp

http://pythonocc.googlecode.com/ · C++ Header · 129 lines · 106 code · 19 blank · 4 comment · 5 complexity · a3b6599c96d23d156ed334b0467fd9e6 MD5 · raw file

  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 2007 Anthony Williams
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/exceptions.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. #include <boost/enable_shared_from_this.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include <boost/optional.hpp>
  13. #include <pthread.h>
  14. #include "condition_variable_fwd.hpp"
  15. #include <map>
  16. #include <boost/config/abi_prefix.hpp>
  17. namespace boost
  18. {
  19. class thread;
  20. namespace detail
  21. {
  22. struct tss_cleanup_function;
  23. struct thread_exit_callback_node;
  24. struct tss_data_node
  25. {
  26. boost::shared_ptr<boost::detail::tss_cleanup_function> func;
  27. void* value;
  28. tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
  29. void* value_):
  30. func(func_),value(value_)
  31. {}
  32. };
  33. struct thread_data_base;
  34. typedef boost::shared_ptr<thread_data_base> thread_data_ptr;
  35. struct BOOST_THREAD_DECL thread_data_base:
  36. enable_shared_from_this<thread_data_base>
  37. {
  38. thread_data_ptr self;
  39. pthread_t thread_handle;
  40. boost::mutex data_mutex;
  41. boost::condition_variable done_condition;
  42. boost::mutex sleep_mutex;
  43. boost::condition_variable sleep_condition;
  44. bool done;
  45. bool join_started;
  46. bool joined;
  47. boost::detail::thread_exit_callback_node* thread_exit_callbacks;
  48. std::map<void const*,boost::detail::tss_data_node> tss_data;
  49. bool interrupt_enabled;
  50. bool interrupt_requested;
  51. pthread_cond_t* current_cond;
  52. thread_data_base():
  53. done(false),join_started(false),joined(false),
  54. thread_exit_callbacks(0),
  55. interrupt_enabled(true),
  56. interrupt_requested(false),
  57. current_cond(0)
  58. {}
  59. virtual ~thread_data_base();
  60. typedef pthread_t native_handle_type;
  61. virtual void run()=0;
  62. };
  63. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  64. class interruption_checker
  65. {
  66. thread_data_base* const thread_info;
  67. void check_for_interruption()
  68. {
  69. if(thread_info->interrupt_requested)
  70. {
  71. thread_info->interrupt_requested=false;
  72. throw thread_interrupted();
  73. }
  74. }
  75. void operator=(interruption_checker&);
  76. public:
  77. explicit interruption_checker(pthread_cond_t* cond):
  78. thread_info(detail::get_current_thread_data())
  79. {
  80. if(thread_info && thread_info->interrupt_enabled)
  81. {
  82. lock_guard<mutex> guard(thread_info->data_mutex);
  83. check_for_interruption();
  84. thread_info->current_cond=cond;
  85. }
  86. }
  87. ~interruption_checker()
  88. {
  89. if(thread_info && thread_info->interrupt_enabled)
  90. {
  91. lock_guard<mutex> guard(thread_info->data_mutex);
  92. thread_info->current_cond=NULL;
  93. check_for_interruption();
  94. }
  95. }
  96. };
  97. }
  98. namespace this_thread
  99. {
  100. void BOOST_THREAD_DECL yield();
  101. void BOOST_THREAD_DECL sleep(system_time const& abs_time);
  102. template<typename TimeDuration>
  103. inline void sleep(TimeDuration const& rel_time)
  104. {
  105. this_thread::sleep(get_system_time()+rel_time);
  106. }
  107. }
  108. }
  109. #include <boost/config/abi_suffix.hpp>
  110. #endif