PixelLightAPI  .
Thread.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Thread.h                                       *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_THREAD_H__
00024 #define __PLCORE_THREAD_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/String/String.h"
00032 #include "PLCore/System/ThreadFunction.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLCore {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Forward declarations                                  ]
00043 //[-------------------------------------------------------]
00044 class ThreadImpl;
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Data types                                            ]
00049 //[-------------------------------------------------------]
00050 typedef int (*THREADFUNCTION)(void*);   /**< Static thread function */
00051 
00052 
00053 //[-------------------------------------------------------]
00054 //[ Classes                                               ]
00055 //[-------------------------------------------------------]
00056 /**
00057 *  @brief
00058 *    Thread class
00059 *
00060 *  @note
00061 *    - Implementation of the bridge design pattern, this class is the abstraction
00062 */
00063 class Thread : public ThreadFunction {
00064 
00065 
00066     //[-------------------------------------------------------]
00067     //[ Friends                                               ]
00068     //[-------------------------------------------------------]
00069     friend class System;
00070     friend class ThreadLinux;
00071 
00072 
00073     //[-------------------------------------------------------]
00074     //[ Public definitions                                    ]
00075     //[-------------------------------------------------------]
00076     public:
00077         /**
00078         *  @brief
00079         *    Thread priority class
00080         */
00081         enum EPriorityClass {
00082             IdlePriorityClass,          /**< Idle priority class */
00083             BelowNormalPriorityClass,   /**< Below normal priority class */
00084             NormalPriorityClass,        /**< Normal priority class (default) */
00085             AboveNormalPriorityClass,   /**< Above normal priority class */
00086             HighPriorityClass,          /**< High priority class */
00087             RealtimePriorityClass       /**< Realtime priority class (ONLY use this if you REALLY need it!) */
00088         };
00089 
00090         /**
00091         *  @brief
00092         *    Thread priority within the priority class it is in
00093         */
00094         enum EPriority {
00095             IdlePriority,           /**< Idle priority */
00096             LowestPriority,         /**< Lowest priority */
00097             BelowNormalPriority,    /**< Below normal priority */
00098             NormalPriority,         /**< Normal priority (default) */
00099             AboveNormalPriority,    /**< Above normal priority */
00100             HighestPriority,        /**< Highest priority */
00101             TimeCriticalPriority    /**< Time critical priority */
00102         };
00103 
00104 
00105     //[-------------------------------------------------------]
00106     //[ Public functions                                      ]
00107     //[-------------------------------------------------------]
00108     public:
00109         /**
00110         *  @brief
00111         *    Constructor
00112         */
00113         PLCORE_API Thread();
00114 
00115         /**
00116         *  @brief
00117         *    Constructor
00118         *
00119         *  @param[in] pThreadFunction
00120         *    Pointer to the thread function, can be a null pointer
00121         */
00122         PLCORE_API Thread(ThreadFunction *pThreadFunction);
00123 
00124         /**
00125         *  @brief
00126         *    Constructor
00127         *
00128         *  @param[in] pThreadFunction
00129         *    Pointer to a static thread function, can be a null pointer
00130         *  @param[in] pData
00131         *    Data to be passed to the thread function, can be a null pointer
00132         */
00133         PLCORE_API Thread(THREADFUNCTION pThreadFunction, void *pData);
00134 
00135         /**
00136         *  @brief
00137         *    Constructor
00138         *
00139         *  @param[in] nThreadID
00140         *    A system specific thread ID, NULL_HANDLE for current thread
00141         */
00142         PLCORE_API Thread(handle nThreadID);
00143 
00144         /**
00145         *  @brief
00146         *    Destructor
00147         */
00148         PLCORE_API virtual ~Thread();
00149 
00150         /**
00151         *  @brief
00152         *    Returns the name of the thread
00153         *
00154         *  @return
00155         *    Thread name
00156         *
00157         *  @remarks
00158         *    The thread name is not used internally, it's for user usage only and especially
00159         *    for debugging purposes quite useful. It's recommended to give threads unique
00160         *    names. By default, no thread name is set.
00161         */
00162         inline String GetName() const;
00163 
00164         /**
00165         *  @brief
00166         *    Sets the name of the thread
00167         *
00168         *  @param[in] sName
00169         *    Thread name
00170         *
00171         *  @see
00172         *    - GetName()
00173         */
00174         inline void SetName(const String &sName);
00175 
00176         /**
00177         *  @brief
00178         *    Returns the unique system ID of the thread
00179         *
00180         *  @return
00181         *    Thread ID
00182         */
00183         inline handle GetID() const;
00184 
00185         /**
00186         *  @brief
00187         *    Returns whether the thread is active
00188         *
00189         *  @return
00190         *    'true' if the thread is active, else 'false'
00191         */
00192         inline bool IsActive() const;
00193 
00194         /**
00195         *  @brief
00196         *    Starts the execution of the thread
00197         *
00198         *  @return
00199         *    'true' if the thread could be started
00200         */
00201         inline bool Start();
00202 
00203         /**
00204         *  @brief
00205         *    Stops the execution of the thread
00206         *
00207         *  @return
00208         *    'true' if the thread could be stopped
00209         *
00210         *  @remarks
00211         *    Terminates the thread ungracefully (does not allow proper thread clean up!). Instead of
00212         *    using this function you should signal the thread and wait until it has quit by itself.
00213         *    The internal platform implementation may or may not accept this violent act. For example,
00214         *    Androids Bionic doesn't support it and a call of this method will have no effect at all.
00215         */
00216         inline bool Terminate();
00217 
00218         /**
00219         *  @brief
00220         *    Waits until the thread has been stopped
00221         *
00222         *  @return
00223         *    'true' if the thread has been stopped
00224         *
00225         *  @note
00226         *    - It's recommended to use the join version with a timeout instead of this function to
00227         *      ensure that the thread is stopped within a defined time (no deadlock's)
00228         */
00229         inline bool Join();
00230 
00231         /**
00232         *  @brief
00233         *    Waits until the thread has been stopped
00234         *
00235         *  @param[in] nTimeout
00236         *    Time in milliseconds to wait
00237         *
00238         *  @return
00239         *    'true' if the thread has been stopped
00240         */
00241         inline bool Join(uint64 nTimeout);
00242 
00243         /**
00244         *  @brief
00245         *    Returns the priority class the thread is in
00246         *
00247         *  @return
00248         *    The priority class the thread is in
00249         *
00250         *  @remarks
00251         *    The thread priority settings are only 'hints' for the OS how to deal with the thread. They
00252         *    are no 'facts' and may differ from OS to OS.
00253         */
00254         inline EPriorityClass GetPriorityClass() const;
00255 
00256         /**
00257         *  @brief
00258         *    Sets the priority class the thread is in
00259         *
00260         *  @param[in] nPriorityClass
00261         *    New priority class the thread is in
00262         *
00263         *  @return
00264         *    'true' if all went fine, else 'false'
00265         *
00266         *  @see
00267         *    - GetPriorityClass()
00268         */
00269         inline bool SetPriorityClass(EPriorityClass nPriorityClass = NormalPriorityClass);
00270 
00271         /**
00272         *  @brief
00273         *    Returns the thread priority within the priority class it is in
00274         *
00275         *  @return
00276         *    The thread priority within the priority class it is in
00277         *
00278         *  @see
00279         *    - GetPriorityClass()
00280         */
00281         inline EPriority GetPriority() const;
00282 
00283         /**
00284         *  @brief
00285         *    Sets the thread priority within the priority class it is in
00286         *
00287         *  @param[in] nPriority
00288         *    New thread priority within the priority class it is in
00289         *
00290         *  @return
00291         *    'true' if all went fine, else 'false'
00292         *
00293         *  @see
00294         *    - GetPriorityClass()
00295         */
00296         inline bool SetPriority(EPriority nPriority = NormalPriority);
00297 
00298 
00299     //[-------------------------------------------------------]
00300     //[ Public virtual ThreadFunction functions               ]
00301     //[-------------------------------------------------------]
00302     public:
00303         PLCORE_API virtual int Run() override;
00304 
00305 
00306     //[-------------------------------------------------------]
00307     //[ Private functions                                     ]
00308     //[-------------------------------------------------------]
00309     private:
00310         /**
00311         *  @brief
00312         *    Copy constructor
00313         *
00314         *  @param[in] cSource
00315         *    Source to copy from
00316         */
00317         Thread(const Thread &cSource);
00318 
00319         /**
00320         *  @brief
00321         *    Copy operator
00322         *
00323         *  @param[in] cSource
00324         *    Source to copy from
00325         *
00326         *  @return
00327         *    Reference to this instance
00328         */
00329         Thread &operator =(const Thread &cSource);
00330 
00331 
00332     //[-------------------------------------------------------]
00333     //[ Private data                                          ]
00334     //[-------------------------------------------------------]
00335     private:
00336         ThreadImpl     *m_pThreadImpl;      /**< Pointer to the system specific implementation (assumed to be never a null pointer!) */
00337         ThreadFunction *m_pThreadFunction;  /**< Pointer to a thread function to execute, can be a null pointer */
00338         THREADFUNCTION  m_pStaticFunction;  /**< Pointer to a static function to execute, can be a null pointer */
00339         void           *m_pStaticData;      /**< Additional data to pass to the static function, can be a null pointer */
00340         String          m_sName;            /**< Thread name */
00341 
00342 
00343 };
00344 
00345 
00346 //[-------------------------------------------------------]
00347 //[ Namespace                                             ]
00348 //[-------------------------------------------------------]
00349 } // PLCore
00350 
00351 
00352 //[-------------------------------------------------------]
00353 //[ Implementation                                        ]
00354 //[-------------------------------------------------------]
00355 #include "PLCore/System/Thread.inl"
00356 
00357 
00358 #endif // __PLCORE_THREAD_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:09:01
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported