[complement-svn] SF.net SVN: complement: [1586] branches/complement-xmt/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2007-06-14 06:47:35
|
Revision: 1586 http://svn.sourceforge.net/complement/?rev=1586&view=rev Author: complement Date: 2007-06-13 23:47:19 -0700 (Wed, 13 Jun 2007) Log Message: ----------- step to interface like boost or http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2007/n2178.html; Locker* changed to basic_lock or basic_read_lock, internal lock flag added, lock/unlock methods added to basic_*_lock and exception lock_error added too. Modified Paths: -------------- branches/complement-xmt/explore/include/mt/shm.h branches/complement-xmt/explore/include/mt/xmt.h branches/complement-xmt/explore/lib/mt/ChangeLog branches/complement-xmt/explore/lib/mt/xmt.cc Modified: branches/complement-xmt/explore/include/mt/shm.h =================================================================== --- branches/complement-xmt/explore/include/mt/shm.h 2007-06-08 19:40:15 UTC (rev 1585) +++ branches/complement-xmt/explore/include/mt/shm.h 2007-06-14 06:47:19 UTC (rev 1586) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/02/07 10:11:34 ptr> +// -*- C++ -*- Time-stamp: <07/06/14 10:16:59 ptr> /* * Copyright (c) 2006, 2007 @@ -303,7 +303,7 @@ template <class T> void named( const T& obj, int name ) { - xmt::__Locker<__Mutex<false,true> > lk( _lock ); + xmt::basic_lock<__Mutex<false,true> > lk( _lock ); if ( _last == 255 ) { throw std::range_error( "too many named objects" ); } @@ -328,7 +328,7 @@ template <class T> T& named( int name ) { - xmt::__Locker<__Mutex<false,true> > lk( _lock ); + xmt::basic_lock<__Mutex<false,true> > lk( _lock ); for ( int i = 0; _nm_table[i].name != -1; ++i ) { if ( _nm_table[i].name == name ) { ++_nm_table[i].count; @@ -341,7 +341,7 @@ template <class T> const T& named( int name ) const { - xmt::__Locker<__Mutex<false,true> > lk( _lock ); + xmt::basic_lock<__Mutex<false,true> > lk( _lock ); for ( int i = 0; _nm_table[i].name != -1; ++i ) { if ( _nm_table[i].name == name ) { ++_nm_table[i].count; @@ -354,7 +354,7 @@ template <class T> void release( int name ) { - xmt::__Locker<__Mutex<false,true> > lk( _lock ); + xmt::basic_lock<__Mutex<false,true> > lk( _lock ); for ( int i = 0; _nm_table[i].name != -1; ++i ) { if ( _nm_table[i].name == name ) { if ( --_nm_table[i].count == 0 ) { @@ -371,7 +371,7 @@ int count( int name ) const throw() { - xmt::__Locker<__Mutex<false,true> > lk( _lock ); + xmt::basic_lock<__Mutex<false,true> > lk( _lock ); for ( int i = 0; _nm_table[i].name != -1; ++i ) { if ( _nm_table[i].name == name ) { return _nm_table[i].count; @@ -480,7 +480,7 @@ if ( p != reinterpret_cast<pointer>(-1) ) { _master *m = reinterpret_cast<_master *>( p ); if ( m->_nm == 0 ) { - xmt::__Locker<xmt::__Mutex<false,true> > lk( m->_lock ); + xmt::basic_lock<xmt::__Mutex<false,true> > lk( m->_lock ); void *nm = _traverse( &m->_first, sizeof(shm_name_mgr<_Inst>) ); m->_nm = reinterpret_cast<char *>(nm) - reinterpret_cast<char *>(p); return *new ( nm ) shm_name_mgr<_Inst>(); @@ -496,7 +496,7 @@ { _master *m = reinterpret_cast<_master *>( _seg.address() ); if ( m != reinterpret_cast<_master *>(-1) ) { - xmt::__Locker<xmt::__Mutex<false,true> > lk( m->_lock ); + xmt::basic_lock<xmt::__Mutex<false,true> > lk( m->_lock ); return _traverse( &m->_first, n ); } @@ -513,7 +513,7 @@ { m._magic = MAGIC; new ( &m._lock ) xmt::__Mutex<false,true>(); - xmt::__Locker<xmt::__Mutex<false,true> > lk( m._lock ); + xmt::basic_lock<xmt::__Mutex<false,true> > lk( m._lock ); m._first = sizeof( _master ); m._nm = 0; _fheader& h = *new ( reinterpret_cast<char *>(&m) + sizeof(_master) ) _fheader(); @@ -535,7 +535,7 @@ n = max( n + (__align - n % __align) % __align, sizeof(_fheader) ); _master *m = reinterpret_cast<_master *>( _seg.address() ); if ( m != reinterpret_cast<_master *>(-1) && (reinterpret_cast<char *>(p) - reinterpret_cast<char *>(_seg.address())) < (_seg.max_size() + sizeof(_master) + sizeof(_aheader) ) ) { - xmt::__Locker<xmt::__Mutex<false,true> > lk( m->_lock ); + xmt::basic_lock<xmt::__Mutex<false,true> > lk( m->_lock ); _aheader *a = reinterpret_cast<_aheader *>( reinterpret_cast<char *>(p) - sizeof(_aheader) ); size_type off = reinterpret_cast<char *>(p) - reinterpret_cast<char *>(_seg.address()); if ( m->_first == 0 ) { Modified: branches/complement-xmt/explore/include/mt/xmt.h =================================================================== --- branches/complement-xmt/explore/include/mt/xmt.h 2007-06-08 19:40:15 UTC (rev 1585) +++ branches/complement-xmt/explore/include/mt/xmt.h 2007-06-14 06:47:19 UTC (rev 1586) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/03/12 20:09:11 ptr> +// -*- C++ -*- Time-stamp: <07/06/14 10:15:07 ptr> /* * Copyright (c) 1997-1999, 2002-2007 @@ -121,6 +121,32 @@ namespace xmt { + +// Exceptions + +// class thread_exit; +// class thread_cancel: public thread_exit; +// class thread_error: public exception; + +class lock_error : + public std::exception +{ + private: + int r_; + + public: + + explicit lock_error( int r ) : + r_( r ) + { } + + virtual char const *what() throw() + { return "std::lock_error"; } + + int error() const + { return r_; } +}; + namespace detail { #ifdef __FIT_PSHARED_MUTEX @@ -321,7 +347,7 @@ } #if !defined( WIN32 ) || (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400) - int trylock() + int try_lock() { #ifdef _PTHREADS return pthread_mutex_trylock( &this->_M_lock ); @@ -384,7 +410,7 @@ # endif } - int trylock() + int try_lock() { # ifdef _PTHREADS return pthread_spin_trylock( &this->_M_lock ); @@ -434,7 +460,7 @@ # endif // !_NOTHREADS } - int trylock() + int try_lock() { # ifdef _NOTHREADS return 0; @@ -549,11 +575,11 @@ // Equivalent to lock(), except that if the mutex object referenced // by mutex is currently locked the call return immediately. // If mutex is currently owned by the calling thread, the mutex lock count - // incremented by one and the trylock() function immediately return success + // incremented by one and the try_lock() function immediately return success // (value 0). Otherwise, if mutex is currently owned by another thread, // return error (non-zero). - int trylock() + int try_lock() { # ifdef _NOTHREADS return 0; @@ -718,7 +744,7 @@ #endif } - void wrlock() + void lock() { #ifdef _PTHREADS pthread_rwlock_wrlock( &this->_M_lock ); @@ -734,7 +760,7 @@ } #if !defined( WIN32 ) || (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400) - int tryrdlock() + int try_rdlock() { #ifdef _PTHREADS return pthread_rwlock_tryrdlock( &this->_M_lock ); @@ -753,7 +779,7 @@ #endif } - int trywrlock() + int try_lock() { #ifdef _PTHREADS return pthread_rwlock_trywrlock( &this->_M_lock ); @@ -797,52 +823,82 @@ #endif // __FIT_RWLOCK template <class M> -class __Locker +class basic_lock { public: - __Locker( const M& point ) : - m( point ) - { const_cast<M&>(m).lock(); } - ~__Locker() - { const_cast<M&>(m).unlock(); } + basic_lock( const M& point, bool initially_locked = true ) : + m( point ), + lk( false ) + { if ( initially_locked ) lock(); } + ~basic_lock() + { if ( lk ) const_cast<M&>(m).unlock(); } + void lock() + { + if ( lk ) { + throw lock_error( 0 ); + } + const_cast<M&>(m).lock(); + lk = true; + } + + void unlock() + { + if ( !lk ) { + throw lock_error( 0 ); + } + lk = false; + const_cast<M&>(m).unlock(); + } + private: - __Locker( const __Locker& ) + basic_lock( const basic_lock& ) { } + basic_lock& operator =( const basic_lock& ) + { return *this; } + const M& m; + bool lk; }; #ifdef __FIT_RWLOCK -template <bool SCOPE> -class __LockerRd +template <class M> +class basic_read_lock { public: - __LockerRd( const __MutexRW<SCOPE>& point ) : - m( point ) - { const_cast<__MutexRW<SCOPE>&>(m).rdlock(); } - ~__LockerRd() - { const_cast<__MutexRW<SCOPE>&>(m).unlock(); } + basic_read_lock( const M& point, bool initially_locked = true ) : + m( point ), + lk( false ) + { if ( initially_locked ) lock(); } + ~basic_read_lock() + { if ( lk ) const_cast<M&>(m).unlock(); } - private: - __LockerRd( const __LockerRd& ) - { } - const __MutexRW<SCOPE>& m; -}; + void lock() + { + if ( lk ) { + throw lock_error( 0 ); + } + const_cast<M&>(m).rdlock(); + lk = true; + } -template <bool SCOPE> -class __LockerWr -{ - public: - __LockerWr( const __MutexRW<SCOPE>& point ) : - m( point ) - { const_cast<__MutexRW<SCOPE>&>(m).wrlock(); } - ~__LockerWr() - { const_cast<__MutexRW<SCOPE>&>(m).unlock(); } + void unlock() + { + if ( !lk ) { + throw lock_error( 0 ); + } + lk = false; + const_cast<M&>(m).unlock(); + } private: - __LockerWr( const __LockerWr& ) + basic_read_lock( const basic_read_lock& ) { } - const __MutexRW<SCOPE>& m; + basic_read_lock& operator =( const basic_read_lock& ) + { return *this; } + + const M& m; + bool lk; }; #endif // __FIT_RWLOCK @@ -857,16 +913,17 @@ typedef __Spinlock<true,false> SpinlockRS; #endif // __FIT_RWLOCK -typedef __Locker<Mutex> Locker; -typedef __Locker<MutexRS> LockerRS; -typedef __Locker<MutexRS> LockerSDS; // obsolete, use instead LockerRS +typedef basic_lock<Mutex> Locker; +typedef basic_lock<Mutex> scoped_lock; +typedef basic_lock<MutexRS> LockerRS; +typedef basic_lock<MutexRS> LockerSDS; // obsolete, use instead LockerRS #ifdef __FIT_RWLOCK -typedef __LockerRd<false> LockerRd; -typedef __LockerWr<false> LockerWr; +typedef basic_read_lock<__MutexRW<false> > LockerRd; +typedef basic_lock<__MutexRW<false> > LockerWr; #endif // __FIT_RWLOCK #ifdef __FIT_PTHREAD_SPINLOCK -typedef __Locker<Spinlock> LockerSpin; -typedef __Locker<SpinlockRS> LockerSpinRS; +typedef basic_lock<Spinlock> LockerSpin; +typedef basic_lock<SpinlockRS> LockerSpinRS; #endif // __FIT_RWLOCK class LockerExt @@ -963,7 +1020,7 @@ bool set( bool __v, bool _broadcast = false ) { - __Locker<__Mutex<false,SCOPE> > _x1( _lock ); + basic_lock<__Mutex<false,SCOPE> > _x1( _lock ); bool tmp = _val; _val = __v; @@ -1004,7 +1061,7 @@ _lock.lock(); #endif #if defined(__FIT_UITHREADS) || defined(_PTHREADS) - __Locker<__Mutex<false,SCOPE> > _x1( _lock ); + basic_lock<__Mutex<false,SCOPE> > _x1( _lock ); #endif if ( _val == false ) { #ifdef __FIT_WIN32THREADS @@ -1047,7 +1104,7 @@ return 0; #endif #if defined(_PTHREADS) || defined(__FIT_UITHREADS) - __Locker<__Mutex<false,SCOPE> > lk( _lock ); + basic_lock<__Mutex<false,SCOPE> > lk( _lock ); _val = false; int ret; while ( !_val ) { @@ -1085,7 +1142,7 @@ int signal( bool _broadcast = false ) { - __Locker<__Mutex<false,SCOPE> > _x1( _lock ); + basic_lock<__Mutex<false,SCOPE> > _x1( _lock ); _val = true; #ifdef __FIT_WIN32THREADS Modified: branches/complement-xmt/explore/lib/mt/ChangeLog =================================================================== --- branches/complement-xmt/explore/lib/mt/ChangeLog 2007-06-08 19:40:15 UTC (rev 1585) +++ branches/complement-xmt/explore/lib/mt/ChangeLog 2007-06-14 06:47:19 UTC (rev 1586) @@ -1,3 +1,11 @@ +2007-06-14 Petr Ovtchenkov <pt...@is...> + + * xmt.h, xmt.cc, shm.h: step to interface like boost or + http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2007/n2178.html; + Locker* changed to basic_lock or basic_read_lock, internal lock flag + added, lock/unlock methods added to basic_*_lock and exception + lock_error added too. + 2007-03-12 Petr Ovtchenkov <pt...@is...> * xmt.h, xmt.cc: code for Novell NetWare removed. Modified: branches/complement-xmt/explore/lib/mt/xmt.cc =================================================================== --- branches/complement-xmt/explore/lib/mt/xmt.cc 2007-06-08 19:40:15 UTC (rev 1585) +++ branches/complement-xmt/explore/lib/mt/xmt.cc 2007-06-14 06:47:19 UTC (rev 1586) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/03/12 20:14:35 ptr> +// -*- C++ -*- Time-stamp: <07/06/14 10:10:54 ptr> /* * Copyright (c) 1997-1999, 2002-2007 @@ -926,7 +926,7 @@ int Thread::xalloc() { - Locker _l( _idx_lock ); + scoped_lock _l( _idx_lock ); return _idx++; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |