Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

DynThreads.hh

Go to the documentation of this file.
00001 /*
00002 
00003     Class/template system for threading using pthreads
00004     Copyright (C) 2002-2003 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <unistd.h>
00024 #include <sys/types.h>
00025 #ifndef USE_NPTL
00026 #include <pthread.h>
00027 #else
00028 #include <nptl/pthread.h>
00029 #endif
00030 #include <map>
00031 
00032 #include <Mutex.hh>
00033 
00034 
00035 #ifndef DYNTHREADS_HH
00036 #define DYNTHREADS_HH
00037 
00038 
00039     typedef std::map<int, pthread_t> ThreadMap_t;
00040 
00041 
00042     class clDynThreadsBase
00043     {
00044             int iThreadCount;
00045             ThreadMap_t mapThreads;
00046             clMutex MtxBase;
00047         public:
00048             typedef struct _stParams
00049             {
00050                 clDynThreadsBase *Klass;
00051                 void *vpParam;
00052             } stParams, *stpParams;
00053             clDynThreadsBase ();
00054             virtual ~clDynThreadsBase ();
00055             int Create (void *, bool);
00056             void *Wait (int);
00057             pthread_t Self ()
00058                 { return pthread_self(); }
00059             bool SetSched (pthread_t, int, int);
00060             virtual void *InternalCaller (void *) = 0;
00061     };
00062     
00063     
00064     template <class TThreads> class clDynThreads :
00065         public clDynThreadsBase
00066     {
00067         public:
00068             typedef void *(TThreads::*Method_t)(void *);
00069             typedef struct _stParams2
00070             {
00071                 Method_t Method;
00072                 void *vpParam;
00073             } stParams2, *stpParams2;
00074             TThreads *Klass;
00075             clDynThreads (TThreads &KlassInst)
00076                 { Klass = &KlassInst; }
00077             int Create (Method_t Method, void *vpParam, bool bDetached = false)
00078                 {
00079                     stpParams2 spParams;
00080                     
00081                     spParams = new stParams2;
00082                     spParams->Method = Method;
00083                     spParams->vpParam = vpParam;
00084                     
00085                     return clDynThreadsBase::Create((void *) spParams, 
00086                         bDetached);
00087                 }
00088             virtual void *InternalCaller (void *vpParam)
00089                 {
00090                     stpParams2 spParams = (stpParams2) vpParam;
00091                     Method_t Method = spParams->Method;
00092                     void *vpThreadParam = spParams->vpParam;
00093                     
00094                     delete spParams;
00095                     try
00096                     {
00097                         return (Klass->*Method)(vpThreadParam);
00098                     }
00099                     catch (...)
00100                     {
00101                         pthread_exit(NULL);
00102                     }
00103                     return NULL;  // dummy for Intel C++
00104                 }
00105     };
00106 
00107 #endif

Generated on Tue Mar 2 19:46:44 2004 for libDSP by doxygen 1.3.6