Bytemaster's Boost Libraries
/Users/dlarimer/dev/libs/cmt/include/boost/cmt/thread.hpp
Go to the documentation of this file.
00001 
00004 #ifndef BOOST_CMT_HPP
00005 #define BOOST_CMT_HPP
00006 #include <vector>
00007 #include <boost/cmt/task.hpp>
00008 #include <boost/cmt/retainable.hpp>
00009 #include <boost/chrono.hpp>
00010 
00011 namespace boost { namespace cmt {
00012    using boost::chrono::microseconds;
00013 
00014    class abstract_thread : public retainable {
00015         public:
00016             typedef retainable_ptr<abstract_thread> ptr;
00017 
00018             virtual ~abstract_thread(){};
00019         protected:
00020             friend class promise_base;
00021             virtual void wait( const promise_base::ptr& p, const boost::chrono::microseconds& timeout_us ) = 0;
00022             virtual void notify( const promise_base::ptr& p ) = 0;
00023    };
00024    priority current_priority();
00025 
00029    class thread : public abstract_thread {
00030         public:
00031             static thread& current();
00032 
00033             void async( const boost::function<void()>& t, priority p = priority() );
00034 
00035             static thread* create();
00036 
00037             template<typename T>
00038             future<T> async( const boost::function<T()>& t, priority prio = priority(), const char* n= "" ) {
00039                typename promise<T>::ptr p(new promise<T>());
00040                task::ptr tsk( new rtask<T>(t,p,std::max(current_priority(),prio),n) );
00041                async(tsk);
00042                return p;
00043             }
00044             template<typename T>
00045             T sync( const boost::function<T()>& t, priority prio, const microseconds& timeout_us=microseconds::max(), const char* n= "" ) {
00046                stack_retainable<promise<T> > prom; prom.retain(); prom.retain();
00047                typename promise<T>::ptr p((promise<T>*)&prom);
00048                stack_retainable<rtask<T> > tsk(t,p,(std::max)(current_priority(),prio),n); tsk.retain();
00049                async(&tsk);
00050                return p->wait(timeout_us);
00051             }
00052             template<typename T>
00053             T sync( const boost::function<T()>& t, const microseconds& timeout_us=microseconds::max(), const char* n= "" ) {
00054                stack_retainable<promise<T> > prom; prom.retain(); prom.retain();
00055                typename promise<T>::ptr p((promise<T>*)&prom);
00056                stack_retainable<rtask<T> > tsk(t,p,current_priority(),n); tsk.retain();
00057                async(&tsk);
00058                return p->wait(timeout_us);
00059             }
00060 
00061             void yield();
00062             void usleep( uint64_t us );
00063 
00064             void quit( );
00065             void exec();
00066 
00067             priority current_priority()const;
00068             ~thread();
00069         protected:
00070             void wait( const promise_base::ptr& p, const microseconds& timeout_us );
00071             void notify( const promise_base::ptr& p );
00072             void exec_fiber();
00073 
00074         private:
00075             thread();
00076 
00077             friend class promise_base;
00078             void async( const task::ptr& t );
00079             class thread_private* my;
00080    };
00081 
00082    template<typename T>
00083    future<T> async( const boost::function<T()>& t, const char* n, priority prio=priority()) {
00084         return cmt::thread::current().async<T>(t,(std::max)(current_priority(),prio),n);
00085    }
00086    template<typename T>
00087    future<T> async( const boost::function<T()>& t, priority prio=priority(), const char* n = "") {
00088         return cmt::thread::current().async<T>(t,(std::max)(current_priority(),prio),n);
00089    }
00090    template<typename T>
00091    T sync( const boost::function<T()>& t, const char* n, priority prio = current_priority(), const microseconds& timeout_us = microseconds::max()) {
00092         return cmt::thread::current().sync<T>(t,prio,timeout_us,n);
00093    }
00094    template<typename T>
00095    T sync( const boost::function<T()>& t, priority prio, const microseconds& timeout_us = microseconds::max(), const char* n = "") {
00096         return cmt::thread::current().sync<T>(t,(std::max)(current_priority(),prio),timeout_us,n);
00097    }
00098    template<typename T>
00099    T sync( const boost::function<T()>& t, const microseconds& timeout_us = microseconds::max(), const char* n = "") {
00100         return cmt::thread::current().sync<T>(t,current_priority(),timeout_us,n);
00101    }
00102    void async( const boost::function<void()>& t, priority prio=priority() ); 
00103    int  exec();
00104 
00108    inline void usleep( uint64_t us ) {
00109         boost::cmt::thread::current().usleep(us);
00110    }
00111 
00112    void yield();
00113 } } // boost::cmt
00114 
00115 #endif
 All Classes Namespaces Files Functions Variables Typedefs Defines