GpCoreTools/PThread.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2009-06-27
00021 **  Authors:
00022 **    Marc Wathelet
00023 **    Marc Wathelet (LGIT, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef PTHREAD_H
00028 #define PTHREAD_H
00029 
00030 #include <map>
00031 #include <list>
00032 #include <vector>
00033 
00034 #include "Trace.h"
00035 #include "TraceBug.h"
00036 #include "EventLoop.h"
00037 #include "GpCoreToolsDLLExport.h"
00038 
00039 namespace GpCoreTools {
00040 
00041   class EventLoop;
00042 
00043   class GPCORETOOLS_EXPORT PThread
00044   {
00045   public:
00046     PThread();
00047     virtual ~PThread() {}
00048 
00049     void start();
00050     void stop();
00051     void wait();
00052     pthread_t id() const {return _id;}
00053 
00054     void setEventLoop(EventLoop * l);
00055     EventLoop * eventLoop() const {return _eventLoop;}
00056 
00057     static PThread * currentThread();
00058     static void init();
00059     static void lockThreads() {pthread_mutex_lock(&_threadsMutex);}
00060     static void unlockThreads() {pthread_mutex_unlock(&_threadsMutex);}
00061 
00062     static std::list< std::vector<TraceBug *> * > bugStacks();
00063     std::vector<TraceBug  *> * bugStack() {return &_bugStack;}
00064   #ifdef TRACE_ENABLED
00065     static std::list< std::vector<const TraceStamp *> * > stacks();
00066     std::vector<const TraceStamp *> * stack() {return &_stack;}
00067   #endif
00068   protected:
00069     virtual void run()=0;
00070   private:
00071     static void * run(void * arg);
00072 
00073     pthread_t _id;
00074     static pthread_mutex_t _threadsMutex;
00075     static std::map<pthread_t, PThread *> _threads;
00076 
00077   #ifdef TRACE_ENABLED
00078     std::vector<const TraceStamp *> _stack;
00079   #endif
00080     std::vector<TraceBug *> _bugStack;
00081     EventLoop * _eventLoop;
00082   };
00083 
00084 } // namespace GpCoreTools
00085 
00086 #endif // PTHREAD_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines