[Cppunit-cvs] cppunit2/src/cpptl thread.cpp,1.3,1.4
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-03-07 08:46:48
|
Update of /cvsroot/cppunit/cppunit2/src/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27927/src/cpptl Modified Files: thread.cpp Log Message: * removed thread creation code. It is not required for core CppUnit. * exposed only processThreadExitHandlers(), other at exit handlers are implementation details. * modified RawThreadStorage::onThreadExit so that it may be called multiple time in the same thread without advert effects. Index: thread.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpptl/thread.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** thread.cpp 6 Mar 2005 21:22:46 -0000 1.3 --- thread.cpp 7 Mar 2005 08:46:37 -0000 1.4 *************** *** 56,59 **** --- 56,199 ---- #else + + + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + // Common Thread API implementation + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + // ////////////////////////////////////////////////////////////////// + + // class ThreadExitHandler + // ////////////////////////////////////////////////////////////////////// + + namespace Impl { + + class ThreadExitHandler : public NonCopyable + { + public: + static ThreadExitHandler &instance() + { + // This static is instantiated at static construction time + // so it's thread-safe + static ThreadExitHandler handler; + return handler; + } + + ThreadExitHandler() + : magic1_( 0x1a9cd67f ) // magic number are used to protect against + , magic2_( 0x3e4a3c9d ) // possible race condition on shutdown. + { + } + + ~ThreadExitHandler() + { + Mutex::ScopedLockGuard guard( lock_ ); + while ( !handlers_.empty() ) + { + handlers_.begin()->first(); + handlers_.erase( handlers_.begin() ); + } + magic1_ = 0xdeadbeef; + magic2_ = 0xdeadbeef; + } + + void add( const Functor0 &handler, + const void *tag ) + { + if ( !isValid() ) + return; + Mutex::ScopedLockGuard guard( lock_ ); + Handlers::iterator it = find( tag ); + if ( it == handlers_.end() ) + handlers_.push_back( HandlerInfo( handler, tag ) ); + else + *it = HandlerInfo( handler, tag ); + } + + void remove( const void *tag ) + { + if ( !isValid() ) + return; + Mutex::ScopedLockGuard guard( lock_ ); + Handlers::iterator it = find( tag ); + if ( it != handlers_.end() ) + { + it->first(); + handlers_.erase( it ); + } + } + + void process() + { + if ( !isValid() ) + return; + Mutex::ScopedLockGuard guard( lock_ ); + Handlers::iterator it = handlers_.begin(); + for ( ; it != handlers_.end(); ++it ) + it->first(); + } + + private: + typedef std::pair<Functor0,const void *> HandlerInfo; + typedef std::vector<HandlerInfo> Handlers; + + Handlers::iterator find( const void *tag ) + { + Handlers::iterator it = handlers_.begin(); + for ( ; it != handlers_.end(); ++it ) + if ( it->second == tag ) + return it; + return handlers_.end(); + } + + bool isValid() const + { + return magic1_ == 0x1a9cd67f && magic2_ == 0x3e4a3c9d; + } + + Handlers handlers_; + Mutex lock_; + unsigned int magic1_; + unsigned int magic2_; + }; + + } // namespace Impl + + + class ThreadExitHandlerInitializer + { + public: + ThreadExitHandlerInitializer() + { + Impl::ThreadExitHandler::instance(); + } + }; + + // This force a call to ThreadExitHandler::instance(), + // and ensure it is properly initialized. + static ThreadExitHandlerInitializer threadExitInitializer; + + void addThreadExitHandler( const Functor0 &handler, + const void *tag ) + { + Impl::ThreadExitHandler::instance().add( handler, tag ); + } + + void removeThreadExitHandler( const void *tag ) + { + Impl::ThreadExitHandler::instance().remove( tag ); + } + + /// This fonction must be called at the end of each thread + /// to ensure ThreadLocalStorage are properly cleaned-up. + void processThreadExitHandlers() + { + Impl::ThreadExitHandler::instance().process(); + } + + + # if CPPTL_USE_WIN32_THREAD *************** *** 128,131 **** --- 268,272 ---- { deallocator_( getRawThreadStorage( this ) ); + setRawThreadStorage( this, 0 ); } *************** *** 161,196 **** } // namespace Impl - // class Thread (win32) - // ////////////////////////////////////////////////////////////////////// - - /* - Thread::Thread( Functor0 threadFunction ) - { - // threadFunction(); - } - - - void - Thread::join() - { - } - */ - - Thread::ThreadIdType - Thread::currentId() - { - return ThreadIdType( ::GetCurrentThreadId() ); - } - - /* - void - Thread::sleep( unsigned long durationInMillisecond ) - { - ::Sleep( durationInMillisecond ); - } - */ - - - # elif defined(CPPTL_USE_PTHREAD_THREAD) --- 302,305 ---- *************** *** 274,277 **** --- 383,387 ---- { deallocator_( getRawThreadStorage( this ) ); + setRawThreadStorage( this, 0 ); } *************** *** 309,448 **** # endif // # elif defined(CPPTL_USE_PTHREAD_THREAD) - - - // ////////////////////////////////////////////////////////////////// - // ////////////////////////////////////////////////////////////////// - // ////////////////////////////////////////////////////////////////// - // Common Thread API implementation - // ////////////////////////////////////////////////////////////////// - // ////////////////////////////////////////////////////////////////// - // ////////////////////////////////////////////////////////////////// - - // class ThreadExitHandler (win32) - // ////////////////////////////////////////////////////////////////////// - - namespace Impl { - - class ThreadExitHandler : public NonCopyable - { - public: - static ThreadExitHandler &instance() - { - // This static is instantiated at static construction time - // so it's thread-safe - static ThreadExitHandler handler; - return handler; - } - - ThreadExitHandler() - : magic1_( 0x1a9cd67f ) // magic number are used to protect against - , magic2_( 0x3e4a3c9d ) // possible race condition on shutdown. - { - } - - ~ThreadExitHandler() - { - Mutex::ScopedLockGuard guard( lock_ ); - while ( !handlers_.empty() ) - { - handlers_.begin()->first(); - handlers_.erase( handlers_.begin() ); - } - magic1_ = 0xdeadbeef; - magic2_ = 0xdeadbeef; - } - - void add( const Functor0 &handler, - const void *tag ) - { - if ( !isValid() ) - return; - Mutex::ScopedLockGuard guard( lock_ ); - Handlers::iterator it = find( tag ); - if ( it == handlers_.end() ) - handlers_.push_back( HandlerInfo( handler, tag ) ); - else - *it = HandlerInfo( handler, tag ); - } - - void remove( const void *tag ) - { - if ( !isValid() ) - return; - Mutex::ScopedLockGuard guard( lock_ ); - Handlers::iterator it = find( tag ); - if ( it != handlers_.end() ) - { - it->first(); - handlers_.erase( it ); - } - } - - void process() - { - if ( !isValid() ) - return; - Mutex::ScopedLockGuard guard( lock_ ); - Handlers::iterator it = handlers_.begin(); - for ( ; it != handlers_.end(); ++it ) - it->first(); - } - - private: - typedef std::pair<Functor0,const void *> HandlerInfo; - typedef std::vector<HandlerInfo> Handlers; - - Handlers::iterator find( const void *tag ) - { - Handlers::iterator it = handlers_.begin(); - for ( ; it != handlers_.end(); ++it ) - if ( it->second == tag ) - return it; - return handlers_.end(); - } - - bool isValid() const - { - return magic1_ == 0x1a9cd67f && magic2_ == 0x3e4a3c9d; - } - - Handlers handlers_; - Mutex lock_; - unsigned int magic1_; - unsigned int magic2_; - }; - - } // namespace Impl - - - class ThreadExitHandlerInitializer - { - public: - ThreadExitHandlerInitializer() - { - Impl::ThreadExitHandler::instance(); - } - }; - - // This force a call to ThreadExitHandler::instance(), - // and ensure it is properly initialized. - static ThreadExitHandlerInitializer threadExitInitializer; - - void addThreadExitHandler( const Functor0 &handler, - const void *tag ) - { - Impl::ThreadExitHandler::instance().add( handler, tag ); - } - - void removeThreadExitHandler( const void *tag ) - { - Impl::ThreadExitHandler::instance().remove( tag ); - } - - void processThreadExitHandlers() - { - Impl::ThreadExitHandler::instance().process(); - } - #endif // #ifdef CPPTL_HAS_THREAD --- 419,422 ---- |