using System;
using System.Threading;
namespace Crestron.SimplSharp
{
///
/// Mock implementation of Crestron CCriticalSection for testing purposes
/// Provides the same public API surface as the real CCriticalSection
///
public class CCriticalSection : IDisposable
{
#region Private Fields
private readonly object _lockObject = new object();
private readonly ReaderWriterLockSlim _readerWriterLock = new ReaderWriterLockSlim();
private bool _disposed = false;
#endregion
#region Constructor
/// Initializes a new instance of the CCriticalSection class
public CCriticalSection()
{
// Mock implementation - no actual initialization required
}
#endregion
#region Public Methods
/// Enters the critical section
public void Enter()
{
if (_disposed) throw new ObjectDisposedException(nameof(CCriticalSection));
Monitor.Enter(_lockObject);
}
/// Tries to enter the critical section
/// True if the critical section was entered successfully
public bool TryEnter()
{
if (_disposed) return false;
return Monitor.TryEnter(_lockObject);
}
/// Tries to enter the critical section with a timeout
/// Timeout in milliseconds
/// True if the critical section was entered successfully
public bool TryEnter(int timeout)
{
if (_disposed) return false;
return Monitor.TryEnter(_lockObject, timeout);
}
/// Tries to enter the critical section with a TimeSpan timeout
/// Timeout as TimeSpan
/// True if the critical section was entered successfully
public bool TryEnter(TimeSpan timeout)
{
if (_disposed) return false;
return Monitor.TryEnter(_lockObject, timeout);
}
/// Leaves the critical section
public void Leave()
{
if (_disposed) return;
try
{
Monitor.Exit(_lockObject);
}
catch (SynchronizationLockException)
{
// Ignore if not held by current thread
}
}
/// Enters a read lock
public void EnterReadLock()
{
if (_disposed) throw new ObjectDisposedException(nameof(CCriticalSection));
_readerWriterLock.EnterReadLock();
}
/// Tries to enter a read lock
/// True if the read lock was acquired successfully
public bool TryEnterReadLock()
{
if (_disposed) return false;
return _readerWriterLock.TryEnterReadLock(0);
}
/// Tries to enter a read lock with a timeout
/// Timeout in milliseconds
/// True if the read lock was acquired successfully
public bool TryEnterReadLock(int timeout)
{
if (_disposed) return false;
return _readerWriterLock.TryEnterReadLock(timeout);
}
/// Exits the read lock
public void ExitReadLock()
{
if (_disposed) return;
try
{
_readerWriterLock.ExitReadLock();
}
catch (SynchronizationLockException)
{
// Ignore if not held by current thread
}
}
/// Enters a write lock
public void EnterWriteLock()
{
if (_disposed) throw new ObjectDisposedException(nameof(CCriticalSection));
_readerWriterLock.EnterWriteLock();
}
/// Tries to enter a write lock
/// True if the write lock was acquired successfully
public bool TryEnterWriteLock()
{
if (_disposed) return false;
return _readerWriterLock.TryEnterWriteLock(0);
}
/// Tries to enter a write lock with a timeout
/// Timeout in milliseconds
/// True if the write lock was acquired successfully
public bool TryEnterWriteLock(int timeout)
{
if (_disposed) return false;
return _readerWriterLock.TryEnterWriteLock(timeout);
}
/// Exits the write lock
public void ExitWriteLock()
{
if (_disposed) return;
try
{
_readerWriterLock.ExitWriteLock();
}
catch (SynchronizationLockException)
{
// Ignore if not held by current thread
}
}
#endregion
#region IDisposable Implementation
/// Disposes the critical section and releases resources
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// Protected dispose method
/// True if disposing managed resources
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_readerWriterLock?.Dispose();
}
_disposed = true;
}
}
#endregion
}
}