| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #pragma once
|
|
|
| #if !defined (_INC_MSCLR_LOCK)
|
|
|
| #ifndef __cplusplus_cli
|
| #error ERROR: msclr libraries are not compatible with /clr:oldSyntax
|
| #endif
|
|
|
| #include <msclr/safebool.h>
|
| #if !defined (_M_CEE_SAFE)
|
| #include <vcclr.h>
|
| #endif
|
|
|
| namespace msclr
|
| {
|
|
|
|
|
|
|
| enum lock_when { lock_later };
|
|
|
| ref class lock
|
| {
|
| private:
|
| System::Object ^ m_object;
|
| bool m_locked;
|
|
|
| template<class T,class U> value struct is_not { typedef int __dont_use_this_type__; };
|
| template<class T> value struct is_not<T,T> { };
|
|
|
| public:
|
|
|
|
|
| template<class T> lock( T ^ _object)
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire(System::Threading::Timeout::Infinite);
|
| }
|
|
|
| template<class T> lock( T ^ _object, int _timeout )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire( _timeout );
|
| }
|
|
|
|
|
|
|
| template<class T> lock( T ^ _object, System::TimeSpan _timeout )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire( _timeout );
|
| }
|
|
|
|
|
|
|
| template<class T> lock( T ^ _object, lock_when )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
| }
|
|
|
| #if !defined (_M_CEE_SAFE)
|
| template<class T> lock( gcroot<T ^> _object)
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire(System::Threading::Timeout::Infinite);
|
| }
|
|
|
|
|
| template<class T> lock( gcroot<T ^> _object, int _timeout )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire( _timeout );
|
| }
|
|
|
|
|
|
|
| template<class T> lock( gcroot<T ^> _object, System::TimeSpan _timeout )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
|
|
| acquire( _timeout );
|
| }
|
|
|
|
|
|
|
| template<class T> lock( gcroot<T ^> _object, lock_when )
|
| : m_object( _object ),
|
| m_locked( false )
|
| {
|
|
|
| typedef is_not<T, System::Threading::ReaderWriterLock>::__dont_use_this_type__ _Diagnostic;
|
| }
|
| #endif
|
|
|
|
|
| ~lock()
|
| {
|
| release();
|
| }
|
|
|
|
|
| bool is_locked()
|
| {
|
| return m_locked;
|
| }
|
|
|
|
|
| operator _detail_class::_safe_bool()
|
| {
|
| return is_locked() ? _detail_class::_safe_true : _detail_class::_safe_false;
|
| }
|
|
|
|
|
| template<class T> bool operator==( T t )
|
| {
|
|
|
| typedef is_not<T, _detail_class::_safe_bool>::__dont_use_this_type__ _Diagnostic;
|
|
|
| return m_object == t;
|
| }
|
|
|
| template<class T> bool operator!=( T t )
|
| {
|
|
|
| typedef is_not<T, _detail_class::_safe_bool>::__dont_use_this_type__ _Diagnostic;
|
|
|
| return m_object != t;
|
| }
|
|
|
|
|
|
|
| void acquire( int _timeout )
|
| {
|
| if( ! m_locked )
|
| {
|
| System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
|
| if( ! m_locked )
|
| {
|
| static const long _hresult_wait_timeout = 0x80070102;
|
| throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
|
| }
|
| }
|
| }
|
|
|
| void acquire()
|
| {
|
| if( ! m_locked )
|
| {
|
| System::Threading::Monitor::TryEnter( m_object,
|
| System::Threading::Timeout::Infinite, m_locked );
|
| if( ! m_locked )
|
| {
|
| static const long _hresult_wait_timeout = 0x80070102;
|
| throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
| void acquire( System::TimeSpan _timeout )
|
| {
|
| if( ! m_locked )
|
| {
|
| System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
|
| if( ! m_locked )
|
| {
|
| static const long _hresult_wait_timeout = 0x80070102;
|
| throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
| bool try_acquire( int _timeout )
|
| {
|
| if( ! m_locked )
|
| {
|
| System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
|
| if( ! m_locked )
|
| {
|
| return false;
|
| }
|
| }
|
| return true;
|
| }
|
|
|
|
|
|
|
| bool try_acquire( System::TimeSpan _timeout )
|
| {
|
| if( ! m_locked )
|
| {
|
| System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
|
| if( ! m_locked )
|
| {
|
| return false;
|
| }
|
| }
|
| return true;
|
| }
|
|
|
|
|
| void release()
|
| {
|
| if( m_locked )
|
| {
|
| System::Threading::Monitor::Exit( m_object );
|
| m_locked = false;
|
| }
|
| }
|
| };
|
| }
|
|
|
| #define _INC_MSCLR_LOCK
|
|
|
| #endif
|
|
|