PVData C++
misc/pv/thread.h
00001 /* thread.h */
00002 /*
00003  * Copyright information and license terms for this software can be
00004  * found in the file LICENSE that is included with the distribution
00005  */
00009 #ifndef THREAD_H
00010 #define THREAD_H
00011 
00012 #include <memory>
00013 #include <sstream>
00014 #include <stdexcept>
00015 
00016 #if __cplusplus>=201103L
00017 #include <functional>
00018 #endif
00019 
00020 #include <epicsThread.h>
00021 #include <shareLib.h>
00022 
00023 #include <pv/noDefaultMethods.h>
00024 #include <pv/pvType.h>
00025 
00026 
00027 namespace epics { namespace pvData {
00028 
00029 enum ThreadPriority {
00030     lowestPriority  =epicsThreadPriorityLow,
00031     lowerPriority   =epicsThreadPriorityLow + 15,
00032     lowPriority     =epicsThreadPriorityMedium - 15,
00033     middlePriority  =epicsThreadPriorityMedium,
00034     highPriority    =epicsThreadPriorityMedium + 15,
00035     higherPriority  =epicsThreadPriorityHigh - 15,
00036     highestPriority =epicsThreadPriorityHigh
00037 };
00038 
00039 class Thread;
00040 typedef std::tr1::shared_ptr<Thread> ThreadPtr;
00041 typedef std::tr1::shared_ptr<epicsThread> EpicsThreadPtr;
00042 
00043 typedef epicsThreadRunable Runnable;
00044 
00045 namespace detail {
00046 template<typename C>
00047 struct MethRunner : public epicsThreadRunable
00048 {
00049     typedef void(C::*fn_t)();
00050     fn_t fn;
00051     C* inst;
00052     MethRunner(C* i, fn_t f) :fn(f), inst(i) {}
00053     virtual ~MethRunner() {}
00054     virtual void run()
00055     {
00056         (inst->*fn)();
00057     }
00058 };
00059 } // namespace detail
00060 
00065 class epicsShareClass Thread : public epicsThread {
00066     EPICS_NOT_COPYABLE(Thread)
00067 public:
00095     class epicsShareClass Config
00096     {
00097         unsigned int p_prio, p_stack;
00098         std::ostringstream p_strm;
00099         bool p_autostart;
00100         Runnable *p_runner;
00101         typedef epics::auto_ptr<Runnable> p_owned_runner_t;
00102         p_owned_runner_t p_owned_runner;
00103         friend class Thread;
00104         Runnable& x_getrunner();
00105         void x_setdefault();
00106 
00107     public:
00108         Config();
00109         Config(Runnable *r);
00110         Config(void(*fn)(void*), void *ptr);
00111         template<typename C>
00112         Config(C* inst, void(C::*meth)()) {this->x_setdefault();this->run(inst, meth);}
00113 #if __cplusplus>=201103L
00114         Config(std::function<void()>&& fn);
00115 #endif
00116 
00117         Config& name(const std::string& n);
00118         Config& prio(unsigned int p);
00119         Config& stack(epicsThreadStackSizeClass s);
00120         Config& autostart(bool a);
00121 
00123         Config& run(Runnable* r);
00125         Config& run(void(*fn)(void*), void *ptr);
00127         template<typename C>
00128         Config& run(C* inst, void(C::*meth)())
00129         {
00130             this->p_owned_runner.reset(new detail::MethRunner<C>(inst, meth));
00131             this->p_runner = this->p_owned_runner.get();
00132             return *this;
00133         }
00134 #if __cplusplus>=201103L
00135         Config& run(std::function<void()>&& fn);
00136 #endif
00137 
00139         template<typename T>
00140         Config& operator<<(T x) { this->p_strm<<x; return *this; }
00141     };
00142 
00158     Thread(std::string name,
00159            ThreadPriority priority,
00160            Runnable *runnable,
00161            epicsThreadStackSizeClass stkcls=epicsThreadStackSmall);
00162 
00178     Thread(Runnable &runnable,
00179            std::string name,
00180            unsigned int stksize,
00181            unsigned int priority=lowestPriority);
00182 
00185     Thread(Config& c);
00186 
00190     ~Thread();
00191 
00192     static size_t num_instances;
00193 private:
00194     Config::p_owned_runner_t p_owned;
00195 };
00196 
00197 
00198 }}
00199 #endif  /* THREAD_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator