Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phThread.h

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 /* -------------------------------------------------------------------------- *
00027  * phThread.h: C++ Wrapper class for threading                                *
00028  * -------------------------------------------------------------------------- *
00029  * There were a number of useful resources out there on how to wrap pthreads
00030  * into a C++ class:
00031  *
00032  * http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-18.html
00033  * http://www.geocities.com/SiliconValley/Heights/6038/dthreads.html
00034  * http://www.codeguru.com/Cpp/misc/misc/threadsprocesses/article.php/c3793/
00035  *
00036  * These ultimately gave me guidance in how to set up the design to a point.
00037  * The point after which I decided to add more functionality and flexibility.
00038  * I include these resources for my own reference purposes and to show I 
00039  * didn't simply copy them.
00040  * -------------------------------------------------------------------------- */
00045 /* ---------------------------------------------------------------------- */
00046 #ifndef _PHTHREAD_H
00047 #define _PHTHREAD_H
00048 
00049 #include <phStandard.h>
00050 #include <phCondition.h>
00051 
00052 /* ------------------------------------------------------------------------- */
00058 /* ------------------------------------------------------------------------- */
00059 /* The following methods are methods that a spawned thread is not allowed to 
00060  * call on itself:
00061  *
00062  *  start
00063  *  stop
00064  *  getReturnCode
00065  *  wakeup
00066  *  error
00067  *
00068  *  Only a thread that spawns the thread is allowed to call those methods.
00069  *  
00070  *  'wakeup' is used by any thread to wake the thread up.
00071  *  
00072  *  'error' is used by the spawner to perform any error recovery the user
00073  *      would like to do.
00074  *      
00075  *  getReturnCode only makes sense from a separate thread, because
00076  *      the current thread has to return for the return value of this method
00077  *      to be meaningful.
00078  *
00079  *  There is no thread around until after 'start' is called, so it's 
00080  *  practically impossible for a spawned thread to call start on itself.
00081  *  'start' is protected by a check to make sure a thread hasn't already
00082  *  been started.
00083  *  
00084  *  'stop' can't be called by the spawned thread because 'stop' calls 'wakeup'
00085  *  and must join with the thread.
00086  * ------------------------------------------------------------------------- *
00087  * ------------------------------------------------------------------------- */
00088 
00089 #define phNOFLAG 0
00090 #define phDETACHED (1<<1)
00091 
00092 /* ------------------------------------------------------------------------- */
00093 #if defined(__ADSPBLACKFIN__)
00094 
00103 class phWrapperThread;
00104 #endif
00105 
00106 /* ------------------------------------------------------------------------- */
00107 typedef struct ph_thread_info_t *ph_thread_info;
00108 
00109 /* ------------------------------------------------------------------------- */
00154 PHAPI(class) phThread : public phCondition
00155 {
00156 #if defined(__ADSPBLACKFIN__)
00157 friend class phWrapperThread;
00158 #endif
00159 private:
00164     ph_thread_info m_info;
00165 
00170     int             m_ready;
00175     int32_t        m_returned;
00181     int             m_detached;
00183     volatile int m_running;
00189     volatile int m_error;
00190     
00196     volatile int m_joining;
00203     static void *entry( void *param );
00204     
00205    
00206 public:
00213     phThread();
00214     
00235     virtual ~phThread();
00236 
00243     int start(int flag = phNOFLAG);
00244 
00250     int stop();    
00251     
00263     int threadStopped();
00264     
00272     int isRunning();
00273  
00280     void setRunning(int running);
00281     
00286     uint32_t id();
00287     
00302     int32_t getReturnCode();
00303         
00312     int detach();
00313     
00319     int detached();
00320 
00356     int setPriority(int priority, int flag);
00357     
00361     int getPriority();
00362     /* pthread_getschedparam - control thread scheduling parameters */
00366     int getPriorityFlag();
00367     
00380     void exit(uint32_t value = 0);
00381     
00403     int equal( phThread &thread );
00407     static int same( phThread &thread_one, phThread &thread_two );
00408 
00412     static void forcedExit();
00413  
00422     int signal_running();
00426     int signal_error();
00427     
00428    
00429 protected:
00430     /* OVERLOADABLE METHODS -
00431      * these can only be called by the public methods implemented in this
00432      * class. */
00433     
00439     virtual int run();
00440     
00451     virtual int setup();
00452     
00458     virtual int wakeup();
00459    
00463     virtual int cleanup();
00464     
00469     virtual int error();
00470 };
00471  /* end \addtogroup ThreadSync */
00473 
00474 #endif /* PHTHREAD_H */




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:02 2007 for phission by  doxygen 1.4.4