UniSet  2.0.0
ThreadCreator.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 //----------------------------------------------------------------------------------------
00024 //--------------------------------------------------------------------------------
00025 #ifndef ThreadCreator_h_
00026 #define ThreadCreator_h_
00027 //----------------------------------------------------------------------------------------
00028 #include <cc++/thread.h>
00029 #include <sys/resource.h>
00030 //----------------------------------------------------------------------------------------
00084 //----------------------------------------------------------------------------------------
00085 template<class ThreadMaster>
00086 class ThreadCreator:
00087     public ost::PosixThread
00088 {
00089     public:
00090 
00092         typedef void(ThreadMaster::* Action)(void);
00093 
00094         ThreadCreator( ThreadMaster* m, Action a );
00095         ~ThreadCreator();
00096 
00097         inline pid_t getTID(){ return pid; }
00098 
00100         int setPriority( int prior );
00101 
00103         int getPriority();
00104 
00105         void stop();
00106 
00107         inline void setName( const std::string& name )
00108         {
00109             ost::PosixThread::setName( name.c_str() );
00110         }
00111 
00112         inline void setName( const char* name )
00113         {
00114             ost::PosixThread::setName( name );
00115         }
00116 
00117         inline void setCancel( ost::Thread::Cancel mode )
00118         {
00119             ost::PosixThread::setCancel(mode);
00120         }
00121 
00122         inline void setFinalAction( ThreadMaster* m, Action a )
00123         {
00124             finm = m;
00125             finact = a;
00126         }
00127 
00128         inline void setInitialAction( ThreadMaster* m, Action a )
00129         {
00130             initm = m;
00131             initact = a;
00132         }
00133 
00134     protected:
00135         virtual void run();
00136         virtual void final()
00137         {
00138             if( finm )
00139                 (finm->*finact)();
00140         }
00141 
00142         virtual void initial()
00143         {
00144             if( initm )
00145                 (initm->*initact)();
00146         }
00147 
00148     private:
00149         ThreadCreator();
00150 
00151         pid_t pid;
00152 
00153         ThreadMaster* m;
00154         Action act;
00155 
00156         ThreadMaster* finm;
00157         Action finact;
00158 
00159         ThreadMaster* initm;
00160         Action initact;
00161 };
00162 
00163 //----------------------------------------------------------------------------------------
00164 template <class ThreadMaster>
00165 ThreadCreator<ThreadMaster>::ThreadCreator( ThreadMaster* m, Action a ):
00166     pid(-1),
00167     m(m),
00168     act(a),
00169     finm(0),
00170     finact(0),
00171     initm(0),
00172     initact(0)
00173 {
00174 }
00175 //----------------------------------------------------------------------------------------
00176 template <class ThreadMaster>
00177 void ThreadCreator<ThreadMaster>::run()
00178 {
00179     pid = getpid();
00180     if( m )
00181            (m->*act)();
00182 }
00183 //----------------------------------------------------------------------------------------
00184 template <class ThreadMaster>
00185 void ThreadCreator<ThreadMaster>::stop()
00186 {
00187     terminate();
00188 }
00189 //----------------------------------------------------------------------------------------
00190 template <class ThreadMaster>
00191 ThreadCreator<ThreadMaster>::ThreadCreator():
00192     pid(-1),
00193     m(0),
00194     act(0),
00195     finm(0),
00196     finact(0),
00197     initm(0),
00198     initact(0)
00199 {
00200 }
00201 //----------------------------------------------------------------------------------------
00202 template <class ThreadMaster>
00203 ThreadCreator<ThreadMaster>::~ThreadCreator()
00204 {
00205 }
00206 //----------------------------------------------------------------------------------------
00207 template <class ThreadMaster>
00208 int ThreadCreator<ThreadMaster>::setPriority( int prior )
00209 {
00210     return setpriority(PRIO_PROCESS, pid, prior );
00211 }
00212 //----------------------------------------------------------------------------------------
00213 template <class ThreadMaster>
00214 int ThreadCreator<ThreadMaster>::getPriority()
00215 {
00216     return getpriority(PRIO_PROCESS, pid);
00217 }
00218 //----------------------------------------------------------------------------------------
00219 #endif // ThreadCreator_h_