Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsthread.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #ifndef MMSTHREAD_H_
00034 #define MMSTHREAD_H_
00035 
00036 #include "mmstools/mmsmutex.h"
00037 #include "mmstools/mmslogger.h"
00038 #include <sched.h>
00039 
00040 
00041 //! This class is the base class for all threads.
00042 /*!
00043 This class includes the base functionality available for all threads within MMS/DISKO.
00044 This class cannot be constructed. Only classes which are derived from this class can be constructed.
00045 \author Jens Schneider
00046 */
00047 class MMSThread {
00048 
00049     private:
00050         //! helper mutex to perform a safe start
00051         MMSMutex    startlock;
00052 
00053         //! starting thread is in progress
00054         bool        starting;
00055 
00056         //! thread is running
00057         bool        running;
00058 
00059         //! if thread is detached, its resources are automatically released when it terminates
00060         bool        detached;
00061 
00062 
00063         //! thread attributes
00064         pthread_attr_t  tattr;
00065 
00066         //! scheduling parameter
00067         sched_param     param;
00068 
00069         //! id of the thread, valid for the running state
00070         pthread_t       id;
00071 
00072 
00073         //! requested priority of the thread
00074         int         priority;
00075 
00076         //! should thread automatically detached?
00077         bool        autodetach;
00078 
00079         //! requested stack size
00080         size_t      stacksize;
00081 
00082 
00083         //! static helper routine to call this->run()
00084         static void *runThread(void *thiz);
00085 
00086         //! the code of this method runs in the new thread and calls the virtual threadMain()
00087         void run();
00088 
00089     public:
00090         //! identification string
00091         string  identity;
00092 
00093 
00094     public:
00095 
00096         //! Constructor
00097         /*!
00098         \param identity    identification string
00099         \param priority    requested priority of the thread, default is 0
00100         \param autodetach  automatically detach the thread after starting, default is true
00101         */
00102         MMSThread(string identity = "MMSThread", int priority = 0, bool autodetach = true);
00103 
00104 
00105         //! Destructor
00106         virtual ~MMSThread();
00107 
00108 
00109         //! Virtual main method for the thread.
00110         /*!
00111         This virtual method is empty and have to be setup with code by a derived class.
00112         The MMSThread class is only the base class and cannot be constructed.
00113         */
00114         virtual void threadMain() = 0;
00115 
00116 
00117         //! Create and start a new thread.
00118         /*!
00119         \return true if thread is started
00120         \note The method returns false, if the thread is already running or cannot be started.
00121         \note If the method returns true, it is possible, that the new thread is already finished.
00122         \see isRunning()
00123         */
00124         virtual bool start();
00125 
00126 
00127         //! Check if the thread is running.
00128         /*!
00129         \return true if running
00130         \note This check is a combination between the "starting" and "running" states. This means
00131               that if the thread is currently starting this method also returns true.
00132         \see start()
00133         */
00134         virtual bool isRunning();
00135 
00136 
00137         //! Mark the thread as detached.
00138         /*!
00139         \note If a thread is detached, its resources are automatically released when it terminates.
00140         */
00141         void detach();
00142 
00143 
00144         //! Cancel execution of a thread.
00145         /*!
00146         \return true if successfully canceled
00147         \note The method returns false, if the thread is not running or cannot be canceled.
00148         */
00149         bool cancel();
00150 
00151 
00152         //! The caller of this method will wait of the termination of the thread.
00153         /*!
00154         \note This works only, if the thread is NOT detached.
00155         \see MMSThread(), detach()
00156         */
00157         void join();
00158 
00159 
00160         //! Set the size of the stack for the new thread.
00161         /*!
00162         The stack size determines the minimum size (in bytes) that will be allocated.
00163         \param stacksize  size of the stack in bytes
00164         \note The default stack size is 1000000 bytes.
00165         \note The stack size must be changed before the start() method will be called.
00166         */
00167         void setStacksize(size_t stacksize = 1000000);
00168 };
00169 
00170 
00171 void addGarbageHandler(void (*handlerfunc)(void *), void *data);
00172 
00173 void callGarbageHandler();
00174 
00175 void clearGarbageHandler();
00176 
00177 
00178 #endif /*MMSTHREAD_H_*/

Generated by doxygen