|
PVData C++
|
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 00046 template<typename C> 00047 class RunnableMethod : public Runnable 00048 { 00049 EPICS_NOT_COPYABLE(RunnableMethod) 00050 typedef void (C::*meth_t)(); 00051 C *inst; 00052 meth_t meth; 00053 00054 virtual void run() 00055 { 00056 (inst->*meth)(); 00057 } 00058 public: 00059 RunnableMethod(C* inst, void (C::*meth)()) 00060 :inst(inst), meth(meth) 00061 {} 00062 }; 00063 00064 namespace detail { 00065 struct FuncRunner : public epicsThreadRunable 00066 { 00067 typedef void (*fn_t)(void*); 00068 fn_t fn; 00069 void *arg; 00070 FuncRunner(fn_t f, void *a) :fn(f), arg(a) {} 00071 virtual ~FuncRunner(){} 00072 virtual void run() 00073 { 00074 (*fn)(arg); 00075 } 00076 }; 00077 template<typename C> 00078 struct MethRunner : public epicsThreadRunable 00079 { 00080 typedef void(C::*fn_t)(); 00081 fn_t fn; 00082 C* inst; 00083 MethRunner(C* i, fn_t f) :fn(f), inst(i) {} 00084 virtual ~MethRunner() {} 00085 virtual void run() 00086 { 00087 (inst->*fn)(); 00088 } 00089 }; 00090 #if __cplusplus>=201103L 00091 struct BindRunner : public epicsThreadRunable 00092 { 00093 typedef std::function<void()> fn_t; 00094 fn_t fn; 00095 BindRunner(const fn_t f) : fn(f) {} 00096 virtual ~BindRunner() {} 00097 virtual void run() 00098 { 00099 fn(); 00100 } 00101 }; 00102 #endif 00103 } // namespace detail 00104 00109 class Thread : public epicsThread { 00110 EPICS_NOT_COPYABLE(Thread) 00111 public: 00139 class Config 00140 { 00141 unsigned int p_prio, p_stack; 00142 std::ostringstream p_strm; 00143 bool p_autostart; 00144 Runnable *p_runner; 00145 typedef epics::auto_ptr<Runnable> p_owned_runner_t; 00146 p_owned_runner_t p_owned_runner; 00147 friend class Thread; 00148 Runnable& x_getrunner() 00149 { 00150 if(!this->p_runner) 00151 throw std::logic_error("Thread::Config missing run()"); 00152 return *this->p_runner; 00153 } 00154 void x_setdefault() 00155 { 00156 this->p_prio = epicsThreadPriorityLow; 00157 this->p_autostart = true; 00158 this->p_runner = NULL; 00159 (*this).stack(epicsThreadStackSmall); 00160 } 00161 00162 public: 00163 Config() {this->x_setdefault();} 00164 Config(Runnable *r) {this->x_setdefault();this->run(r);} 00165 Config(void(*fn)(void*), void *ptr) {this->x_setdefault();this->run(fn, ptr);} 00166 template<typename C> 00167 Config(C* inst, void(C::*meth)()) {this->x_setdefault();this->run(inst, meth);} 00168 #if __cplusplus>=201103L 00169 Config(const std::function<void()>& fn) {this->x_setdefault();this->run(fn);} 00170 #endif 00171 00172 inline Config& name(const std::string& n) 00173 { this->p_strm.str(n); return *this; } 00174 inline Config& prio(unsigned int p) 00175 { this->p_prio = p; return *this; } 00176 inline Config& stack(epicsThreadStackSizeClass s) 00177 { this->p_stack = epicsThreadGetStackSize(s); return *this; } 00178 inline Config& autostart(bool a) 00179 { this->p_autostart = a; return *this; } 00180 00182 Config& run(Runnable* r) 00183 { this->p_runner = r; return *this; } 00185 Config& run(void(*fn)(void*), void *ptr) 00186 { 00187 this->p_owned_runner.reset(new detail::FuncRunner(fn, ptr)); 00188 this->p_runner = this->p_owned_runner.get(); 00189 return *this; 00190 } 00192 template<typename C> 00193 Config& run(C* inst, void(C::*meth)()) 00194 { 00195 this->p_owned_runner.reset(new detail::MethRunner<C>(inst, meth)); 00196 this->p_runner = this->p_owned_runner.get(); 00197 return *this; 00198 } 00199 #if __cplusplus>=201103L 00200 Config& run(const std::function<void()>& fn) 00201 { 00202 this->p_owned_runner.reset(new detail::BindRunner(fn)); 00203 this->p_runner = this->p_owned_runner.get(); 00204 return *this; 00205 } 00206 #endif 00207 00209 template<typename T> 00210 Config& operator<<(T x) { this->p_strm<<x; return *this; } 00211 }; 00212 00228 Thread(std::string name, 00229 ThreadPriority priority, 00230 Runnable *runnable, 00231 epicsThreadStackSizeClass stkcls=epicsThreadStackSmall) 00232 :epicsThread(*runnable, 00233 name.c_str(), 00234 epicsThreadGetStackSize(stkcls), 00235 priority) 00236 { 00237 this->start(); 00238 } 00239 00255 Thread(Runnable &runnable, 00256 std::string name, 00257 unsigned int stksize, 00258 unsigned int priority=lowestPriority) 00259 :epicsThread(runnable, 00260 name.c_str(), 00261 stksize, 00262 priority) 00263 { 00264 this->start(); 00265 } 00266 00269 Thread(Config& c) 00270 :epicsThread(c.x_getrunner(), c.p_strm.str().c_str(), 00271 c.p_stack, c.p_prio) 00272 { 00273 #if __cplusplus>=201103L 00274 p_owned = std::move(c.p_owned_runner); 00275 #else 00276 p_owned = c.p_owned_runner; 00277 #endif 00278 if(c.p_autostart) 00279 this->start(); 00280 } 00281 00285 ~Thread() 00286 { 00287 this->exitWait(); 00288 } 00289 00290 Config::p_owned_runner_t p_owned; 00291 }; 00292 00293 00294 }} 00295 #endif /* THREAD_H */
1.7.4