Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced custom lock 'ReaderWriterNotifyLock' with 'System.Threading.ReaderWriterLockSlim' #6

Merged
merged 3 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
[![CI](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CI.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CI.yml)
[![CD](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CD.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CD.yml)
[![Nuget](https://img.shields.io/nuget/v/ThunderDesign.Net-PCL.Threading)](https://www.nuget.org/packages/ThunderDesign.Net-PCL.Threading)
[![License](https://img.shields.io/github/license/ThunderDesign/ThunderDesign.Net-PCL.Threading)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/blob/main/LICENSE)

Thread-Safe Objects
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using ThunderDesign.Net.Threading.Interfaces;
using ThunderDesign.Net.Threading.Threading;

namespace ThunderDesign.Net.Threading.Collections
{
Expand All @@ -23,14 +22,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
{
get
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.Comparer;
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
}
Expand All @@ -39,14 +38,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
{
get
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.Count;
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
}
Expand All @@ -55,14 +54,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
{
get
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.Keys;
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
}
Expand All @@ -71,14 +70,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
{
get
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.Values;
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
}
Expand All @@ -87,26 +86,26 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
{
get
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base[key];
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
set
{
_ReaderWriterNotifyLock.EnterWriteLock();
_ReaderWriterLockSlim.EnterWriteLock();
try
{
base[key] = value;
}
finally
{
_ReaderWriterNotifyLock.ExitWriteLock();
_ReaderWriterLockSlim.ExitWriteLock();
}
}
}
Expand All @@ -115,127 +114,127 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
#region methods
public new virtual void Add(TKey key, TValue value)
{
_ReaderWriterNotifyLock.EnterWriteLock();
_ReaderWriterLockSlim.EnterWriteLock();
try
{
base.Add(key, value);
}
finally
{
_ReaderWriterNotifyLock.ExitWriteLock();
_ReaderWriterLockSlim.ExitWriteLock();
}
}

public new virtual void Clear()
{
_ReaderWriterNotifyLock.EnterWriteLock();
_ReaderWriterLockSlim.EnterWriteLock();
try
{
base.Clear();
}
finally
{
_ReaderWriterNotifyLock.ExitWriteLock();
_ReaderWriterLockSlim.ExitWriteLock();
}
}

public new bool ContainsKey(TKey key)
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.ContainsKey(key);
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}

public new bool ContainsValue(TValue value)
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.ContainsValue(value);
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}

public new Enumerator GetEnumerator()
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.GetEnumerator();
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}

[System.Security.SecurityCritical] // auto-generated_required
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
base.GetObjectData(info, context);
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}

public override void OnDeserialization(Object sender)
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
base.OnDeserialization(sender);
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}

public new virtual bool Remove(TKey key)
{
bool result = false;
_ReaderWriterNotifyLock.EnterWriteLock();
_ReaderWriterLockSlim.EnterWriteLock();
try
{
result = base.Remove(key);
}
finally
{
_ReaderWriterNotifyLock.ExitWriteLock();
_ReaderWriterLockSlim.ExitWriteLock();
}
return result;
}

public new bool TryGetValue(TKey key, out TValue value)
{
_ReaderWriterNotifyLock.EnterReadLock();
_ReaderWriterLockSlim.EnterReadLock();
try
{
return base.TryGetValue(key, out value);
}
finally
{
_ReaderWriterNotifyLock.ExitReadLock();
_ReaderWriterLockSlim.ExitReadLock();
}
}
#endregion

#region variables
protected static readonly ReaderWriterNotifyLock _ReaderWriterNotifyLock = new ReaderWriterNotifyLock();
protected static readonly ReaderWriterLockSlim _ReaderWriterLockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

#endregion
}
Expand Down
Loading