mirror of
https://github.com/islehorse/HISP.git
synced 2025-04-23 13:15:53 +12:00
Add Thread-Safe Lists, and make all list access thread-safe
This commit is contained in:
parent
900fe0d48a
commit
40c34ac030
24 changed files with 111 additions and 44 deletions
67
HorseIsleServer/LibHISP/Server/ThreadSafeList.cs
Normal file
67
HorseIsleServer/LibHISP/Server/ThreadSafeList.cs
Normal file
|
@ -0,0 +1,67 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace HISP.Server
|
||||
{
|
||||
public class ThreadSafeList<T> : List<T>
|
||||
{
|
||||
private Mutex listLock = new Mutex();
|
||||
new public void AddRange(IEnumerable<T> collection)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.AddRange(collection);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
new public void Add(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Add(value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
new public void Clear()
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Clear();
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public bool Contains(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
bool test = base.Contains(value);
|
||||
listLock.ReleaseMutex();
|
||||
|
||||
return test;
|
||||
}
|
||||
new public IEnumerator GetEnumerator()
|
||||
{
|
||||
listLock.WaitOne();
|
||||
IEnumerator res = base.ToArray().GetEnumerator();
|
||||
listLock.ReleaseMutex();
|
||||
return res;
|
||||
}
|
||||
|
||||
new public void Insert(int index, T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Insert(index, value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public void Remove(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Remove(value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public void RemoveAt(int index)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.RemoveAt(index);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue