Update of /cvsroot/cppunit/cppunit2/include/cpptl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11122
Added Files:
thread.h
Log Message:
* moved from cpput/thread.h
* implementation clean-up
--- NEW FILE: thread.h ---
#ifndef CPPTL_THREAD_H_INCLUDED
# define CPPTL_THREAD_H_INCLUDED
# include <cpptl/forwards.h>
# if CPPTL_HAS_THREAD
# include <cpptl/functor.h>
# endif // # if CPPTL_HAS_THREAD
namespace CppTL {
void CPPTL_API addThreadExitHandler( const Functor0 &handler,
const void *tag = 0 );
void CPPTL_API removeThreadExitHandler( const void *tag );
void CPPTL_API processThreadExitHandlers();
class CPPTL_API Mutex : public NonCopyable
{
public:
class ScopedLockGuard
{
public:
ScopedLockGuard( Mutex &mutex )
: mutex_( mutex )
{
mutex_.lock();
}
~ScopedLockGuard()
{
mutex_.unlock();
}
private:
Mutex &mutex_;
};
Mutex();
~Mutex();
void lock();
void unlock();
# if CPPTL_HAS_THREAD
private:
void *data_;
# endif
};
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// Implementation of thread API for NON THREAD-SAFE mode
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
# if !(CPPTL_HAS_THREAD)
template<class ValueType>
class ThreadLocalStorage
{
public:
typedef ThreadLocalStorage<ValueType> SelfType;
ThreadLocalStorage( const ValueType &defaultValue = ValueType() )
: value_( defaultValue )
{
}
bool isInitialized() const
{
return true;
}
ValueType &get()
{
return value_;
}
private:
ValueType value_;
};
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// Implementation of thread API for THREAD-SAFE mode
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
# else // # if !(CPPTL_HAS_THREAD)
namespace Impl {
class RawThreadStorage;
RawThreadStorage *CPPTL_API createRawThreadStorage( const Functor1<void *> &deallocator );
void CPPTL_API freeRawThreadStorage( RawThreadStorage *storage );
void *CPPTL_API getRawThreadStorage( RawThreadStorage *storage );
void setRawThreadStorage( RawThreadStorage *storage, void *value );
} // namespace Impl
template<class ValueType>
class ThreadLocalStorage : public NonCopyable
{
public:
typedef void(*FreeFn)(void*);
typedef ThreadLocalStorage<ValueType> SelfType;
ThreadLocalStorage( const ValueType &defaultValue = ValueType() )
: storage_( Impl::createRawThreadStorage( cfn1( FreeFn(freeValue) ) ) )
, defaultValue_( defaultValue )
{
}
virtual ~ThreadLocalStorage()
{
Impl::freeRawThreadStorage( storage_ );
}
bool isInitialized() const
{
return Impl::getRawThreadStorage( storage_ ) != 0;
}
ValueType &get()
{
ValueType * value = static_cast<ValueType *>(
Impl::getRawThreadStorage( storage_ ) );
if ( !value )
{
value = new ValueType( defaultValue_ );
Impl::setRawThreadStorage( storage_, value );
}
return *value;
}
SelfType &operator =( const ValueType &other )
{
get() = other;
return *this;
}
private:
static void freeValue( void *p )
{
delete static_cast<ValueType *>( p );
}
ValueType defaultValue_;
Impl::RawThreadStorage *storage_;
};
class CPPTL_API Thread
{
public:
typedef unsigned long ThreadIdType;
// Thread( Functor0 threadFunction );
// void join();
static ThreadIdType currentId();
// static void sleep( unsigned long durationInMillisecond );
private:
void *data_;
};
# endif // # ifndef CPPTL_THREAD_SAFE
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
// Implementation of thread API common to all thread-safety mode
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
/*
class Thread;
typedef CppTL::SharedPtr<Thread> ThreadPtr;
inline ThreadPtr createThread( Functor0 threadFunction )
{
return ThreadPtr( new Thread( threadFunction ) );
}
*/
} // namespace CppTL
#endif // CPPTL_THREAD_H_INCLUDED
|