RTP Audio System  2.0.0
thread.h
Go to the documentation of this file.
00001 /*
00002  *  $Id: thread.h 1259 2011-12-17 13:26:09Z dreibh $
00003  *
00004  * SocketAPI implementation for the sctplib.
00005  * Copyright (C) 1999-2012 by Thomas Dreibholz
00006  *
00007  * Realized in co-operation between
00008  * - Siemens AG
00009  * - University of Essen, Institute of Computer Networking Technology
00010  * - University of Applied Sciences, Muenster
00011  *
00012  * Acknowledgement
00013  * This work was partially funded by the Bundesministerium fuer Bildung und
00014  * Forschung (BMBF) of the Federal Republic of Germany (Foerderkennzeichen 01AK045).
00015  * The authors alone are responsible for the contents.
00016  *
00017  * This program is free software: you can redistribute it and/or modify
00018  * it under the terms of the GNU General Public License as published by
00019  * the Free Software Foundation, either version 3 of the License, or
00020  * (at your option) any later version.
00021 
00022  * This program 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
00025  * GNU General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029  *
00030  * Contact: discussion@sctp.de
00031  *          dreibh@iem.uni-due.de
00032  *          tuexen@fh-muenster.de
00033  *
00034  * Purpose: Thread Implementation
00035  *
00036  */
00037 
00038 
00039 #ifndef THREAD_H
00040 #define THREAD_H
00041 
00042 
00043 #include "tdsystem.h"
00044 #include "synchronizable.h"
00045 #include "condition.h"
00046 
00047 
00048 #include <pthread.h>
00049 #include <set>
00050 
00051 
00052 
00066 class Thread : public Synchronizable
00067 {
00068    // ====== Constants ======================================================
00069    public:
00070    static const cardinal TF_CancelAsynchronous = 0;
00071    static const cardinal TF_CancelDeferred     = (1 << 0);
00072    static const cardinal TCS_CancelEnabled  = PTHREAD_CANCEL_ENABLE;
00073    static const cardinal TCS_CancelDisabled = PTHREAD_CANCEL_DISABLE;
00074    static const cardinal TCS_CancelDeferred = PTHREAD_CANCEL_DEFERRED;
00075 
00076 
00077    // ====== Constructor/Destructor =========================================
00078    public:
00088    Thread(const char*    name  = "Thread",
00089           const cardinal flags = TF_CancelDeferred);
00090 
00094    virtual ~Thread();
00095 
00096 
00097    // ====== Status =========================================================
00103    inline bool running() const;
00104 
00108    inline pid_t getPID() const;
00109 
00110 
00111    // ====== Delay ==========================================================
00120    static card64 delay(const card64 delayTimeout, const bool interruptable = false);
00121 
00122 
00123    // ====== Thread control =================================================
00130    virtual bool start(const char* name = NULL);
00131 
00142    virtual void* stop();
00143 
00149    void* join();
00150 
00154    virtual void cancel();
00155 
00166    inline static cardinal setCancelState(const cardinal state);
00167 
00168 
00169    // ====== Tests for cancellation =========================================
00170    protected:
00175    virtual void testCancel();
00176 
00182    inline static void exit(void* result = NULL);
00183 
00189    inline static void yield();
00190 
00191 
00192    // ====== run() method to be implemented by subclass =====================
00193    protected:
00198    virtual void run() = 0;
00199 
00200 
00201    // ====== Protected data =================================================
00202    protected:
00203    pthread_t PThread;
00204    pid_t     PID;
00205 
00206 
00207    // ====== Private data ===================================================
00208    private:
00209    static void* go(void* argument);
00210 
00211 
00212    cardinal        Flags;
00213    pthread_mutex_t StartupMutex;
00214    pthread_cond_t  StartupCondition;
00215 };
00216 
00217 
00218 #include "thread.icc"
00219 
00220 
00221 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines