From: <qr...@us...> - 2006-11-29 20:25:24
|
Revision: 223 http://svn.sourceforge.net/opengate/?rev=223&view=rev Author: qrstuvw Date: 2006-11-29 12:25:20 -0800 (Wed, 29 Nov 2006) Log Message: ----------- - Debugged some stuff Modified Paths: -------------- src/common/Posix/Thread.h Modified: src/common/Posix/Thread.h =================================================================== --- src/common/Posix/Thread.h 2006-11-29 14:20:45 UTC (rev 222) +++ src/common/Posix/Thread.h 2006-11-29 20:25:20 UTC (rev 223) @@ -1,5 +1,6 @@ ///////////////////////////////////////////////////////////////////// -// Written by Phillip Sitbon +// Written by Phillip Sitbon - modified by Tim Beelen to compile on +// GCC 4.1.1 :) // Copyright 2003 // // Posix/Thread.h @@ -17,296 +18,302 @@ #define InvalidHandle 0 -template -< - typename Thread_T -> +typedef void *( * pthread_fn )( void * ); +template <typename Thread_T> class Thread { - private: - typedef struct Instance; +private: + typedef struct Instance; - public: - typedef Thread_T & Thread_R; - typedef const Thread_T & Thread_C_R; +public: + typedef Thread_T & Thread_R; + typedef const Thread_T & Thread_C_R; - typedef pthread_t Handle; - typedef void ( *Handler)( Thread_R ); + typedef pthread_t Handle; + typedef void ( *Handler)( Thread_R ); - protected: - Thread() {} +protected: + Thread() {} - virtual void ThreadMain( Thread_R ) = 0; + virtual void ThreadMain( Thread_R ) = 0; - static void Exit() - { pthread_exit(0); } + static void Exit() + { pthread_exit(0); } - static void TestCancel() - { pthread_testcancel(); } + static void TestCancel() + { pthread_testcancel(); } - static Handle Self() - { return (Handle)pthread_self(); } + static Handle Self() + { return (Handle)pthread_self(); } - public: +public: - static int Create( - const Handler & Function, - Thread_C_R Param, - Handle * const & H = 0, - const bool & CreateDetached = false, - const unsigned int & StackSize = 0, - const bool & CancelEnable = false, - const bool & CancelAsync = false - ) - { - M_Create().Lock(); - pthread_attr_t attr; - pthread_attr_init(&attr); + static int Create( + const Handler & Function, + Thread_C_R Param, + Handle * const & H = 0, + const bool & CreateDetached = false, + const unsigned int & StackSize = 0, + const bool & CancelEnable = false, + const bool & CancelAsync = false + ) + { + M_Create().Lock(); + pthread_attr_t attr; + pthread_attr_init(&attr); - if ( CreateDetached ) - pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); + if ( CreateDetached ) + pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); - if ( StackSize ) - pthread_attr_setstacksize(&attr,StackSize); + if ( StackSize ) + pthread_attr_setstacksize(&attr,StackSize); - Instance I(Param,0,Function,CancelEnable,CancelAsync); + Instance I(Param,0,Function,CancelEnable,CancelAsync); - int R = pthread_create((pthread_t *)H,&attr,ThreadMainHandler,(void *)&I); - pthread_attr_destroy(&attr); + Handle h=InvalidHandle; + int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I); - if ( !R ) S_Create().Wait(); - else if ( H ) *H = InvalidHandle; + pthread_attr_destroy(&attr); - M_Create().Unlock(); - return errno; - } + if(H) *H = h; + if ( !R ) S_Create().Wait(); - int Create( - Thread_C_R Param, - Handle * const & H = 0, - const bool & CreateDetached = false, - const unsigned int & StackSize = 0, - const bool & CancelEnable = false, - const bool & CancelAsync = false - ) const - { - M_Create().Lock(); - pthread_attr_t attr; - pthread_attr_init(&attr); + M_Create().Unlock(); + return errno; + } - if ( CreateDetached ) - pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); + int Create( + Thread_C_R Param, + Handle * const & H = 0, + const bool & CreateDetached = false, + const unsigned int & StackSize = 0, + const bool & CancelEnable = false, + const bool & CancelAsync = false + ) const + { + M_Create().Lock(); + pthread_attr_t attr; + pthread_attr_init(&attr); - if ( StackSize ) - pthread_attr_setstacksize(&attr,StackSize); + if ( CreateDetached ) + pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); - Instance I(Param,const_cast<Thread *>(this),0,CancelEnable,CancelAsync); + if ( StackSize ) + pthread_attr_setstacksize(&attr,StackSize); - int R = pthread_create((pthread_t *)H,&attr,ThreadMainHandler,(void *)&I); - pthread_attr_destroy(&attr); + Instance I(Param,const_cast<Thread *>(this),0,CancelEnable,CancelAsync); - if ( !R ) S_Create().Wait(); - else if ( H ) *H = InvalidHandle; + Handle h=InvalidHandle; + int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I); - M_Create().Unlock(); - return errno; - } + pthread_attr_destroy(&attr); - static int Join( Handle H ) - { return pthread_join(H,0); } + if(H) *H = h; + if ( !R ) S_Create().Wait(); - static int Kill( Handle H ) - { return pthread_cancel(H); } + M_Create().Unlock(); + return errno; + } - static int Detach( Handle H ) - { return pthread_detach(H); } + static int Join( Handle H ) +{ return pthread_join(H,0); } - private: + static int Kill( Handle H ) + { return pthread_cancel(H); } - static const Mutex &M_Create() { static Mutex M; return M; } - static const Semaphore &S_Create() { static Semaphore S; return S; } + static int Detach( Handle H ) + { return pthread_detach(H); } - static void *ThreadMainHandler( Instance *Param ) - { - Instance I(*Param); - Thread_T Data(I.Data); - S_Create().Post(); +private: - if ( I.Flags & 1 /*CancelEnable*/ ) - { - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + static const Mutex &M_Create() { static Mutex M; return M; } + static const Semaphore &S_Create() { static Semaphore S; return S; } - if ( I.Flags & 2 /*CancelAsync*/ ) - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); - else - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); - } - else - { - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); - } + static void *ThreadMainHandler( Instance *Param ) + { + Instance I(*Param); + Thread_T Data(I.Data); + S_Create().Post(); - if ( I.Owner ) - I.Owner->ThreadMain(Data); - else - I.pFN(Data); + if ( I.Flags & 1 /*CancelEnable*/ ) + { + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); - return 0; - } + if ( I.Flags & 2 /*CancelAsync*/ ) + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); + else + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); + } + else + { + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); + } - struct Instance - { - Instance( Thread_C_R P, Thread<Thread_T> *const &O, const Thread<Thread_T>::Handler &pH = 0, const bool &CE=false, const bool &CA=false ) - : pFN(pH), Data(P), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; } + if ( I.Owner ) + I.Owner->ThreadMain(Data); + else + I.pFN(Data); - Thread<Thread_T>::Thread_C_R Data; - Thread<Thread_T> * Owner; - Thread<Thread_T>::Handler pFN; - unsigned char Flags; - }; + return 0; + } + + struct Instance + { + Instance( Thread_C_R P, Thread<Thread_T> *const &O, const Thread<Thread_T>::Handler &pH = 0, const bool &CE=false, const bool &CA=false ) + : pFN(pH), Data(P), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; } + + Handler pFN; + Thread_C_R Data; + Thread<Thread_T> * Owner; + unsigned char Flags; + }; }; ///////////////////////////////////////////////////////////////////// // Explicit specialization, no thread parameters // -class Thread<void> +template <> class Thread<void> { - private: - typedef struct Instance; +private: + typedef struct Instance; - public: - typedef pthread_t Handle; - typedef void ( *Handler)(); +public: + typedef pthread_t Handle; + typedef void ( *Handler)(); - protected: - Thread<void>() {} +protected: + Thread<void>() {} - virtual void ThreadMain() = 0; + virtual void ThreadMain() = 0; - static void Exit() - { pthread_exit(0); } + static void Exit() + { pthread_exit(0); } - static void TestCancel() - { pthread_testcancel(); } + static void TestCancel() + { pthread_testcancel(); } - static Handle Self() - { return (Handle)pthread_self(); } + static Handle Self() + { return (Handle)pthread_self(); } - public: +public: - static int Create( - const Handler & Function, - Handle * const & H = 0, - const bool & CreateDetached = false, - const unsigned int & StackSize = 0, - const bool & CancelEnable = false, - const bool & CancelAsync = false - ) - { - M_Create().Lock(); - pthread_attr_t attr; - pthread_attr_init(&attr); + static int Create( + const Handler & Function, + Handle * const & H = 0, + const bool & CreateDetached = false, + const unsigned int & StackSize = 0, + const bool & CancelEnable = false, + const bool & CancelAsync = false + ) + { + M_Create().Lock(); + pthread_attr_t attr; + pthread_attr_init(&attr); - if ( CreateDetached ) - pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); + if ( CreateDetached ) + pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); - if ( StackSize ) - pthread_attr_setstacksize(&attr,StackSize); + if ( StackSize ) + pthread_attr_setstacksize(&attr,StackSize); - Instance I(0,Function,CancelEnable,CancelAsync); + Instance I(0,Function,CancelEnable,CancelAsync); - int R = pthread_create((pthread_t *)H,&attr,ThreadMainHandler,(void *)&I); - pthread_attr_destroy(&attr); + Handle h=InvalidHandle; + int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I); - if ( !R ) S_Create().Wait(); - else if ( H ) *H = InvalidHandle; + pthread_attr_destroy(&attr); - M_Create().Unlock(); - return errno; - } + if(H) *H = h; + if ( !R ) S_Create().Wait(); - int Create( - Handle * const & H = 0, - const bool & CreateDetached = false, - const unsigned int & StackSize = 0, - const bool & CancelEnable = false, - const bool & CancelAsync = false - ) const - { - M_Create().Lock(); - pthread_attr_t attr; - pthread_attr_init(&attr); + M_Create().Unlock(); + return errno; + } - if ( CreateDetached ) - pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); + int Create( + Handle * const & H = 0, + const bool & CreateDetached = false, + const unsigned int & StackSize = 0, + const bool & CancelEnable = false, + const bool & CancelAsync = false + ) const + { + M_Create().Lock(); + pthread_attr_t attr; + pthread_attr_init(&attr); - if ( StackSize ) - pthread_attr_setstacksize(&attr,StackSize); + if ( CreateDetached ) + pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); - Instance I(const_cast<Thread *>(this),0,CancelEnable,CancelAsync); + if ( StackSize ) + pthread_attr_setstacksize(&attr,StackSize); - int R = pthread_create((pthread_t *)H,&attr,ThreadMainHandler,(void *)&I); - pthread_attr_destroy(&attr); + Instance I(const_cast<Thread *>(this),0,CancelEnable,CancelAsync); - if ( !R ) S_Create().Wait(); - else if ( H ) *H = InvalidHandle; + Handle h=InvalidHandle; + int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I); - M_Create().Unlock(); - return errno; - } + pthread_attr_destroy(&attr); - static int Join( Handle H ) - { return pthread_join(H,0); } + if(H) *H = h; + if ( !R ) S_Create().Wait(); - static int Kill( Handle H ) - { return pthread_cancel(H); } + M_Create().Unlock(); + return errno; + } - static int Detach( Handle H ) - { return pthread_detach(H); } + static int Join( Handle H ) +{ return pthread_join(H,0); } - private: + static int Kill( Handle H ) + { return pthread_cancel(H); } - static const Mutex &M_Create() { static Mutex M; return M; } - static const Semaphore &S_Create() { static Semaphore S; return S; } + static int Detach( Handle H ) + { return pthread_detach(H); } - static void *ThreadMainHandler( Instance *Param ) - { - Instance I(*Param); - S_Create().Post(); +private: - if ( I.Flags & 1 /*CancelEnable*/ ) - { - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + static const Mutex &M_Create() { static Mutex M; return M; } + static const Semaphore &S_Create() { static Semaphore S; return S; } - if ( I.Flags & 2 /*CancelAsync*/ ) - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); - else - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); - } - else - { - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); - } + static void *ThreadMainHandler( Instance *Param ) + { + Instance I(*Param); + S_Create().Post(); - if ( I.Owner ) - I.Owner->ThreadMain(); - else - I.pFN(); + if ( I.Flags & 1 /*CancelEnable*/ ) + { + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); - return 0; - } + if ( I.Flags & 2 /*CancelAsync*/ ) + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); + else + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); + } + else + { + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); + } - struct Instance - { - Instance( Thread<void> *const &O, const Thread<void>::Handler &pH = 0, const bool &CE=false, const bool &CA=false ) - : pFN(pH), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; } + if ( I.Owner ) + I.Owner->ThreadMain(); + else + I.pFN(); - Thread<void> * Owner; - Thread<void>::Handler pFN; - unsigned char Flags; - }; + return 0; + } + + struct Instance + { + Instance( Thread<void> *const &O, const Thread<void>::Handler &pH = 0, const bool &CE=false, const bool &CA=false ) + : pFN(pH), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; } + + Thread<void>::Handler pFN; + Thread<void> * Owner; + unsigned char Flags; + }; }; #endif // !_Thread_Posix_ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |