UniSet  2.2.1
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()
00098         {
00099             return pid;
00100         }
00101 
00103         int setPriority( int prior );
00104 
00106         int getPriority();
00107 
00108         void stop();
00109 
00110         inline void setName( const std::string& name )
00111         {
00112             ost::PosixThread::setName( name.c_str() );
00113         }
00114 
00115         inline void setCancel( ost::Thread::Cancel mode )
00116         {
00117             ost::PosixThread::setCancel(mode);
00118         }
00119 
00120         inline void setFinalAction( ThreadMaster* m, Action a )
00121         {
00122             finm = m;
00123             finact = a;
00124         }
00125 
00126         inline void setInitialAction( ThreadMaster* m, Action a )
00127         {
00128             initm = m;
00129             initact = a;
00130         }
00131 
00132     protected:
00133         virtual void run();
00134         virtual void final()
00135         {
00136             if( finm )
00137                 (finm->*finact)();
00138 
00139             //delete this;
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 
00181     if( m )
00182         (m->*act)();
00183 }
00184 //----------------------------------------------------------------------------------------
00185 template <class ThreadMaster>
00186 void ThreadCreator<ThreadMaster>::stop()
00187 {
00188     terminate();
00189 }
00190 //----------------------------------------------------------------------------------------
00191 template <class ThreadMaster>
00192 ThreadCreator<ThreadMaster>::ThreadCreator():
00193     pid(-1),
00194     m(0),
00195     act(0),
00196     finm(0),
00197     finact(0),
00198     initm(0),
00199     initact(0)
00200 {
00201 }
00202 //----------------------------------------------------------------------------------------
00203 template <class ThreadMaster>
00204 ThreadCreator<ThreadMaster>::~ThreadCreator()
00205 {
00206 }
00207 //----------------------------------------------------------------------------------------
00208 template <class ThreadMaster>
00209 int ThreadCreator<ThreadMaster>::setPriority( int prior )
00210 {
00211     return setpriority(PRIO_PROCESS, pid, prior );
00212 }
00213 //----------------------------------------------------------------------------------------
00214 template <class ThreadMaster>
00215 int ThreadCreator<ThreadMaster>::getPriority()
00216 {
00217     return getpriority(PRIO_PROCESS, pid);
00218 }
00219 //----------------------------------------------------------------------------------------
00220 #endif // ThreadCreator_h_