Thread.hh

00001 //********************************************************************
00002 //*** Thread/Thread.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: Thread.hh,v 1.20 2009-03-02 14:16:47 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of 
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef INCLUDED_THREAD_H
00025 #define INCLUDED_THREAD_H
00026 
00027 #include <memory>
00028 #include <iosfwd>
00029 
00030 // TODO should be hidden, currently reqd for pthread_t member
00031 #include <pthread.h>
00032 
00033 #ifndef INCLUDED_TASK_HH
00034 #include "Thread/Task.hh"
00035 #endif
00036 
00037 #include "mm/gc_ptr.hh"
00038 #include "mm/CommonPtr.hh"
00039 
00040 namespace fatalmind {
00041 
00042 class Thread;
00043 std::ostream& operator<<(std::ostream&, const Thread&);
00044 
00054 class Thread {
00055     public:
00056         Thread();
00057         ~Thread();
00058 
00059         template<class D>
00060         inline void start(std::auto_ptr<D> a) {
00061             start(std::auto_ptr<Task>(a));
00062         }
00063         template<class D>
00064         inline void start(gc_ptr<D, MultiThreadedModel<> > a) {
00065             start(gc_ptr<Task, MultiThreadedModel<> >(a));
00066         }
00067         void start(std::auto_ptr<Task>);
00068         void start(gc_ptr<Task, MultiThreadedModel<> >);
00069 
00070         void detach();
00071         void join();
00072         static void yield();
00073 
00074         static std::ostream& printSelf(std::ostream&); 
00075 
00076         const std::string& getThreadID() const {
00077             return d_strthreadid;
00078         }
00079 
00080     protected:
00081         void start(Task*);
00082         static void* jumpinpoint(void* arg);
00083 
00084         CommonPtr<Task, MultiThreadedModel<> > d_task;
00085 
00086         bool        d_started;
00087         pthread_t   d_threadid;
00088         std::string d_strthreadid;
00089 
00090     friend 
00091         std::ostream& operator<<(std::ostream&, const Thread&);
00092 };
00093 
00094 
00095 }; // namespace fatalmind
00096 
00097 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3