PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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