UniSet  2.6.0
ThreadCreator.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 //----------------------------------------------------------------------------------------
00021 //--------------------------------------------------------------------------------
00022 #ifndef ThreadCreator_h_
00023 #define ThreadCreator_h_
00024 //----------------------------------------------------------------------------------------
00025 #include <Poco/Thread.h>
00026 #include <sys/resource.h>
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029 //----------------------------------------------------------------------------------------
00030 namespace uniset
00031 {
00085 //----------------------------------------------------------------------------------------
00086 template<class ThreadMaster>
00087 class ThreadCreator:
00088     public Poco::Runnable
00089 {
00090     public:
00091 
00095         typedef void(ThreadMaster::* Action)(void);
00096 
00097         ThreadCreator( ThreadMaster* m, Action a );
00098         virtual ~ThreadCreator();
00099 
00100         inline Poco::Thread::TID getTID() const
00101         {
00102             return thr.tid();
00103         }
00104 
00106         void setPriority( Poco::Thread::Priority prior );
00107 
00109         Poco::Thread::Priority getPriority() const;
00110 
00111         void stop();
00112         void start();
00113 
00114         void sleep( long milliseconds );
00115 
00116         inline bool isRunning()
00117         {
00118             return thr.isRunning();
00119         }
00120 
00121         inline void join()
00122         {
00123             thr.join();
00124         }
00125 
00126         inline void setFinalAction( ThreadMaster* m, Action a )
00127         {
00128             finm = m;
00129             finact = a;
00130         }
00131 
00132         inline void setInitialAction( ThreadMaster* m, Action a )
00133         {
00134             initm = m;
00135             initact = a;
00136         }
00137 
00138     protected:
00139         virtual void run();
00140         virtual void final()
00141         {
00142             if( finm )
00143                 (finm->*finact)();
00144 
00145             //delete this;
00146         }
00147 
00148         virtual void initial()
00149         {
00150             if( initm )
00151                 (initm->*initact)();
00152         }
00153 
00154         virtual void terminate() {}
00155 
00156     private:
00157         ThreadCreator();
00158 
00159         ThreadMaster* m;
00160         Action act;
00161 
00162         ThreadMaster* finm;
00163         Action finact;
00164 
00165         ThreadMaster* initm;
00166         Action initact;
00167 
00168         Poco::Thread thr;
00169 };
00170 
00171 //----------------------------------------------------------------------------------------
00172 template <class ThreadMaster>
00173 ThreadCreator<ThreadMaster>::ThreadCreator( ThreadMaster* m, Action a ):
00174     m(m),
00175     act(a),
00176     finm(0),
00177     finact(0),
00178     initm(0),
00179     initact(0)
00180 {
00181 }
00182 //----------------------------------------------------------------------------------------
00183 template <class ThreadMaster>
00184 void ThreadCreator<ThreadMaster>::run()
00185 {
00186     initial();
00187 
00188     if( m )
00189         (m->*act)();
00190 
00191     final();
00192 }
00193 //----------------------------------------------------------------------------------------
00194 template <class ThreadMaster>
00195 void ThreadCreator<ThreadMaster>::stop()
00196 {
00197     terminate();
00198 }
00199 //----------------------------------------------------------------------------------------
00200 template <class ThreadMaster>
00201 void ThreadCreator<ThreadMaster>::start()
00202 {
00203     thr.start( *this );
00204 }
00205 //----------------------------------------------------------------------------------------
00206 template <class ThreadMaster>
00207 void ThreadCreator<ThreadMaster>::sleep( long milliseconds )
00208 {
00209     thr.sleep(milliseconds);
00210 }
00211 //----------------------------------------------------------------------------------------
00212 template <class ThreadMaster>
00213 ThreadCreator<ThreadMaster>::ThreadCreator():
00214     m(0),
00215     act(0),
00216     finm(0),
00217     finact(0),
00218     initm(0),
00219     initact(0)
00220 {
00221 }
00222 //----------------------------------------------------------------------------------------
00223 template <class ThreadMaster>
00224 ThreadCreator<ThreadMaster>::~ThreadCreator()
00225 {
00226 }
00227 //----------------------------------------------------------------------------------------
00228 template <class ThreadMaster>
00229 void ThreadCreator<ThreadMaster>::setPriority( Poco::Thread::Priority prior )
00230 {
00231     return thr.setPriority(prior);
00232 }
00233 //----------------------------------------------------------------------------------------
00234 template <class ThreadMaster>
00235 Poco::Thread::Priority ThreadCreator<ThreadMaster>::getPriority() const
00236 {
00237     return thr.getPriority();
00238 }
00239 // -------------------------------------------------------------------------
00240 } // end of uniset namespace
00241 //----------------------------------------------------------------------------------------
00242 #endif // ThreadCreator_h_