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
00010 namespace boost { namespace cmt {
00011 class abstract_thread : public retainable {
00012 public:
00013 typedef retainable_ptr<abstract_thread> ptr;
00014
00015 virtual ~abstract_thread(){};
00016 protected:
00017 friend class promise_base;
00018 virtual void wait( const promise_base::ptr& p, uint64_t timeout_us ) = 0;
00019 virtual void notify( const promise_base::ptr& p ) = 0;
00020 };
00021 priority current_priority();
00022
00026 class thread : public abstract_thread {
00027 public:
00028 static thread& current();
00029
00030 void async( const boost::function<void()>& t, priority p = priority() );
00031
00032 static thread* create();
00033
00034 template<typename T>
00035 future<T> async( const boost::function<T()>& t, priority prio = priority(), const char* n= "" ) {
00036 typename promise<T>::ptr p(new promise<T>());
00037 task::ptr tsk( new rtask<T>(t,p,std::max(current_priority(),prio),n) );
00038 async(tsk);
00039 return p;
00040 }
00041 template<typename T>
00042 T sync( const boost::function<T()>& t, priority prio, uint64_t timeout_us=-1, const char* n= "" ) {
00043 stack_retainable<promise<T> > prom; prom.retain(); prom.retain();
00044 typename promise<T>::ptr p((promise<T>*)&prom);
00045 stack_retainable<rtask<T> > tsk(t,p,(std::max)(current_priority(),prio),n); tsk.retain();
00046 async(&tsk);
00047 return p->wait(timeout_us);
00048 }
00049 template<typename T>
00050 T sync( const boost::function<T()>& t, uint64_t timeout_us=-1, const char* n= "" ) {
00051 stack_retainable<promise<T> > prom; prom.retain(); prom.retain();
00052 typename promise<T>::ptr p((promise<T>*)&prom);
00053 stack_retainable<rtask<T> > tsk(t,p,current_priority(),n); tsk.retain();
00054 async(&tsk);
00055 return p->wait(timeout_us);
00056 }
00057
00058 void yield();
00059 void usleep( uint64_t us );
00060
00061 void quit( );
00062 void exec();
00063
00064 priority current_priority()const;
00065 ~thread();
00066 protected:
00067 void wait( const promise_base::ptr& p, uint64_t timeout_us );
00068 void notify( const promise_base::ptr& p );
00069 void exec_fiber();
00070
00071 private:
00072 thread();
00073
00074 friend class promise_base;
00075 void async( const task::ptr& t );
00076 class thread_private* my;
00077 };
00078
00079 template<typename T>
00080 future<T> async( const boost::function<T()>& t, const char* n, priority prio=priority()) {
00081 return cmt::thread::current().async<T>(t,(std::max)(current_priority(),prio),n);
00082 }
00083 template<typename T>
00084 future<T> async( const boost::function<T()>& t, priority prio=priority(), const char* n = "") {
00085 return cmt::thread::current().async<T>(t,(std::max)(current_priority(),prio),n);
00086 }
00087 template<typename T>
00088 T sync( const boost::function<T()>& t, const char* n, priority prio = current_priority(), uint64_t timeout_us = -1) {
00089 return cmt::thread::current().sync<T>(t,prio,timeout_us,n);
00090 }
00091 template<typename T>
00092 T sync( const boost::function<T()>& t, priority prio, uint64_t timeout_us = -1, const char* n = "") {
00093 return cmt::thread::current().sync<T>(t,(std::max)(current_priority(),prio),timeout_us,n);
00094 }
00095 template<typename T>
00096 T sync( const boost::function<T()>& t, uint64_t timeout_us = -1, const char* n = "") {
00097 return cmt::thread::current().sync<T>(t,current_priority(),timeout_us,n);
00098 }
00099 void async( const boost::function<void()>& t, priority prio=priority() );
00100 int exec();
00101
00105 inline void usleep( uint64_t us ) {
00106 boost::cmt::thread::current().usleep(us);
00107 }
00108
00109 void yield();
00110 } }
00111
00112 #endif