Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created January 21, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianEdwards/1654095 to your computer and use it in GitHub Desktop.
Save DamianEdwards/1654095 to your computer and use it in GitHub Desktop.
Moar Signalr
using System;
using System.Collections.Generic;
using System.Threading;
namespace SignalR.Infrastructure
{
internal class LockedList<T> : List<T>
{
private readonly ReaderWriterLockSlim _listLock = new ReaderWriterLockSlim();
public void AddWithLock(T item)
{
try
{
_listLock.EnterWriteLock();
Add(item);
}
finally
{
_listLock.ExitWriteLock();
}
}
public void RemoveWithLock(T item)
{
try
{
_listLock.EnterWriteLock();
Remove(item);
}
finally
{
_listLock.ExitWriteLock();
}
}
public List<T> CopyWithLock()
{
try
{
_listLock.EnterReadLock();
return GetRange(0, Count);
}
finally
{
_listLock.ExitReadLock();
}
}
public void CopyToWithLock(T[] array)
{
try
{
_listLock.EnterReadLock();
CopyTo(array);
}
finally
{
_listLock.ExitReadLock();
}
}
public int FindLastIndexWithLock(Predicate<T> match)
{
try
{
_listLock.EnterReadLock();
return FindLastIndex(match);
}
finally
{
_listLock.ExitReadLock();
}
}
public List<T> GetRangeWithLock(int index, int count)
{
try
{
_listLock.EnterReadLock();
return GetRange(index, count);
}
finally
{
_listLock.ExitReadLock();
}
}
public int CountWithLock
{
get
{
try
{
_listLock.EnterReadLock();
return Count;
}
finally
{
_listLock.ExitReadLock();
}
}
}
public T GetWithLock(int index)
{
try
{
_listLock.EnterReadLock();
return this[index];
}
finally
{
_listLock.ExitReadLock();
}
}
public void SetWithLock(int index, T value)
{
try
{
_listLock.EnterWriteLock();
this[index] = value;
}
finally
{
_listLock.EnterWriteLock();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment