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

wefts_thread.h

Go to the documentation of this file.
00001 /*
00002    wefts_thread.h
00003    Main thread class
00004 
00005    $Id: wefts_thread.h,v 1.7 2003/08/15 18:03:11 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : 2003-08-02 23:45
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011    Implementing OS independent base threading
00012 */
00013 
00014 /**************************************************************************
00015 *   This program is free software; you can redistribute it and/or modify  *
00016 *   it under the terms of the GNU Library General Public License as       *
00017 *   published by the Free Software Foundation; either version 2.1 of the  *
00018 *   License, or (at your option) any later version.                       *
00019 ***************************************************************************/
00020 
00021 #ifndef WT_THREAD_H
00022 #define WT_THREAD_H
00023 
00024 #include <wefts_os.h>
00025 #include <wefts_counter.h>
00026 #include <wefts_cleanup.h>
00027 #include <stack>
00028 
00029 namespace Wefts {
00030 
00037 extern Counter runningThreads;
00038 
00045 extern Counter startedThreads;
00046 
00047 
00048 
00121 class Thread
00122 {
00123 private:
00135    inline virtual void executionEnd() { m_running = false; }
00136 
00138    void setCancel() { setCancel( m_canCancel ); }
00139 
00141    CleanupList m_cleanupHandlers;
00142 
00143    friend void s_ThreadCleanupper( void *);
00144    friend void s_ThreadRunner( void *);
00145    friend void *s_ThreadRunFunc( void *);
00146 
00147 protected:
00149    void *m_runReturnValue;
00151    OSThread    m_thread;
00153    bool        m_canCancel;
00155    void        *m_thread_data;
00157    bool        m_detached;
00159    volatile bool m_stopped;
00160 
00164    volatile bool m_running;
00165 
00169    int m_thSequence;
00170 
00178    void testCancel() { m_thread.testCancel(); if ( m_stopped ) m_thread.exit(0); }
00179 
00187    void setCancel( bool cando );
00188 
00200    void doDetach() { m_thread.detach(); }
00201 
00205    void setReturn( void *ret ) { m_runReturnValue = ret; }
00206 
00207 public:
00209    Thread();
00210 
00214    virtual ~Thread();
00215 
00224    bool start( bool detachable = false, bool cancelable = true );
00225 
00233    bool stop();
00234 
00246    void detach() { m_detached = true; }
00248    bool detached() const { return m_detached; }
00249 
00268    virtual void *join() throw( NotJoinableError );
00269 
00281    bool running() const { return m_running; }
00282 
00284    OSThread *getThread() { return &m_thread; }
00285 
00287    inline void setData( void *data ) { m_thread_data = data; }
00288 
00296    inline int  sequence() const { return m_thSequence; }
00297 
00329    virtual void cleanup() {
00330       m_stopped = true; // force this to avoid races.
00331       while( m_cleanupHandlers.size() > 0 ) {
00332             m_cleanupHandlers.top().first->
00333                   handleCleanup( m_cleanupHandlers.top().second );
00334          m_cleanupHandlers.pop();
00335       }
00336    };
00337 
00352    bool pushCleanupHandler( CleanupHandler *handler, int value=0 )
00353    {
00354       //avoid pushing a cleanup handler while the thread is diyng
00355       if ( m_stopped ) return false;
00356       m_cleanupHandlers.push( CleanupItem( handler, value ) );
00357       return true;
00358    }
00359 
00373    bool popCleanupHandler( bool execute=false )
00374    {
00375       if ( m_stopped )
00376          return false;
00377       if ( m_cleanupHandlers.size() > 0 ) {
00378          if ( execute )
00379             m_cleanupHandlers.top().first->
00380                   handleCleanup( m_cleanupHandlers.top().second );
00381          m_cleanupHandlers.pop();
00382          return true;
00383       }
00384       return false;
00385    }
00386 
00420    virtual void *run()=0;
00421 
00422 };
00423 
00424 }
00425 
00426 #endif
00427 /* end of wefts_thread.h */

Generated on Mon Aug 18 05:53:37 2003 for Wefts by doxygen1.2.18