Skip to content

Commit e25f1b1

Browse files
Udpated reference.
1 parent 750679f commit e25f1b1

8 files changed

Lines changed: 74 additions & 62 deletions

source/Extensions.Generic.Synchronized.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public static T GetOrAddSynchronized<TKey, T>(
225225
Contract.EndContractBlock();
226226

227227
T result = default!;
228-
bool condition(LockType _) => !target.TryGetValue(key, out result);
228+
bool condition(bool _) => !target.TryGetValue(key, out result);
229229

230230
void render()
231231
{
@@ -257,7 +257,7 @@ public static T GetOrAddSynchronized<TKey, T>(
257257

258258
T result = default!;
259259
// Note, the following sync read is on the TARGET and not the key. See below.
260-
bool condition(LockType _) => !ThreadSafety.SynchronizeRead(target, () => target.TryGetValue(key, out result));
260+
bool condition(bool _) => !ThreadSafety.SynchronizeRead(target, () => target.TryGetValue(key, out result));
261261

262262
// Once a per value write lock is established, execute the scheduler, and syncronize adding...
263263
void render() => target.GetOrAddSynchronized(key, result = valueFactory(key), millisecondsTimeout);

source/LazyList.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected override bool EnsureIndex(int maxIndex)
5959
// This is where the fun begins...
6060
// Mutliple threads can be out of sync (probably through a memory barrier)
6161
// And a sync read operation must be done to ensure safety.
62-
int count = Sync.ReadValue(() => _cached.Count);
62+
int count = Sync.Read(() => _cached.Count);
6363
if (maxIndex < count)
6464
{
6565
// We're still within the existing results, but safe count is not up to date.
@@ -73,38 +73,36 @@ protected override bool EnsureIndex(int maxIndex)
7373
if (_enumerator is null)
7474
return false;
7575

76-
// This very well could be a simple lock{} statement but the ReaderWriterLockSlim recursion protection is actually quite useful.
77-
using (UpgradableReadLock? uLock = Sync.UpgradableReadLock())
78-
{
76+
// This very well could be a simple lock{} statement but the ReaderWriterLockSlim recursion protection is actually quite useful.
77+
using var uLock = Sync.UpgradableReadLock();
7978
// Note: Within an upgradable read, other reads pile up.
8079

81-
int c = _cached.Count;
82-
if (_safeCount != c) // Always do comparisons outside of interlocking first.
83-
Interlocked.CompareExchange(ref _safeCount, c, _safeCount);
84-
85-
if (maxIndex < _safeCount)
86-
return true;
80+
int c = _cached.Count;
81+
if (_safeCount != c) // Always do comparisons outside of interlocking first.
82+
Interlocked.CompareExchange(ref _safeCount, c, _safeCount);
8783

88-
if (_enumerator is null)
89-
return false;
84+
if (maxIndex < _safeCount)
85+
return true;
9086

91-
uLock.UpgradeToWriteLock();
87+
if (_enumerator is null)
88+
return false;
9289

93-
while (_enumerator.MoveNext())
94-
{
95-
if (_cached.Count == int.MaxValue)
96-
throw new Exception("Reached maximium contents for a single list. Cannot memoize further.");
90+
using var wLock = Sync.WriteLock();
9791

98-
_cached.Add(_enumerator.Current);
92+
while (_enumerator.MoveNext())
93+
{
94+
if (_cached.Count == int.MaxValue)
95+
throw new Exception("Reached maximium contents for a single list. Cannot memoize further.");
9996

100-
if (maxIndex < _cached.Count)
101-
return true;
102-
}
97+
_cached.Add(_enumerator.Current);
10398

104-
IsEndless = false;
105-
DisposeOf(ref _enumerator);
99+
if (maxIndex < _cached.Count)
100+
return true;
106101
}
107102

103+
IsEndless = false;
104+
DisposeOf(ref _enumerator);
105+
108106
return false;
109107
}
110108

source/NonGeneric/Extensions.Synchronized.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static T GetOrAddSynchronized<T>(
164164

165165
T result = default!;
166166
// Uses threadsafe means to acquire value.
167-
bool condition(LockType _) => !target.TryGetValue(key, out result);
167+
bool condition(bool _) => !target.TryGetValue(key, out result);
168168

169169
void render()
170170
{
@@ -194,7 +194,7 @@ public static T GetOrAddSynchronized<T>(
194194
ValidateMillisecondsTimeout(millisecondsTimeout);
195195

196196
T result = default!;
197-
bool condition(LockType _) => !ThreadSafety.SynchronizeRead(target, () => target.TryGetValue(key, out result));
197+
bool condition(bool _) => !ThreadSafety.SynchronizeRead(target, () => target.TryGetValue(key, out result));
198198

199199
// Once a per value write lock is established, execute the scheduler, and syncronize adding...
200200
void render() => target.GetOrAddSynchronized(key, result = valueFactory(key), millisecondsTimeout);

source/Open.Collections.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
</ItemGroup>
4545

4646
<ItemGroup>
47-
<PackageReference Include="Open.Text" Version="6.0.2" />
48-
<PackageReference Include="Open.Threading" Version="1.6.8" />
47+
<PackageReference Include="Open.Text" Version="6.1.0" />
48+
<PackageReference Include="Open.Threading" Version="2.0.0-preview1" />
4949
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
5050
</ItemGroup>
5151

source/Synchronized/ReadWriteSynchronizedCollectionWrapper.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ public override void Clear()
4545

4646
/// <inheritdoc />
4747
public override bool Contains(T item)
48-
=> Sync.ReadValue(() => InternalSource.Contains(item));
48+
=> Sync.Read(() => InternalSource.Contains(item));
4949

5050
/// <inheritdoc />
5151
public override bool Remove(T item)
5252
=> IfContains(item, c => c.Remove(item));
5353

5454
/// <inheritdoc />
5555
public override int Count
56-
=> Sync.ReadValue(() => InternalSource.Count);
56+
=> Sync.Read(() => InternalSource.Count);
5757

5858
/// <inheritdoc />
5959
public T[] Snapshot()
60-
=> Sync.ReadValue(() => InternalSource.ToArray());
60+
=> Sync.Read(() => InternalSource.ToArray());
6161

6262
/// <inheritdoc />
6363
public override void Export(ICollection<T> to)
@@ -110,40 +110,40 @@ public void ForEach(Action<T> action, bool useSnapshot = true)
110110

111111
/// <inheritdoc />
112112
public void Modify(Func<bool> condition, Action<TCollection> action)
113-
=> Sync.ReadWriteConditionalOptimized(_ => condition(), () => action(InternalSource));
113+
=> Sync.ReadWriteConditional(_ => condition(), () => action(InternalSource));
114114

115115
/// <summary>
116116
/// Allows for multiple modifications at once.
117117
/// </summary>
118118
/// <param name="condition">Only executes the action if the condition is true. The condition may be invoked more than once.</param>
119119
/// <param name="action">The action to execute safely on the underlying collection safely.</param>
120-
public void Modify(Func<LockType, bool> condition, Action<TCollection> action)
121-
=> Sync.ReadWriteConditionalOptimized(condition, () => action(InternalSource));
120+
public void Modify(Func<bool, bool> condition, Action<TCollection> action)
121+
=> Sync.ReadWriteConditional(condition, () => action(InternalSource));
122122

123123
/// <inheritdoc />
124124
public void Modify(Action<TCollection> action) => Sync.Write(()
125125
=> action(InternalSource));
126126

127127
/// <inheritdoc />
128128
public TResult Modify<TResult>(Func<TCollection, TResult> action)
129-
=> Sync.WriteValue(() => action(InternalSource));
129+
=> Sync.Write(() => action(InternalSource));
130130

131131
/// <inheritdoc />
132132
public virtual bool IfContains(T item, Action<TCollection> action)
133133
{
134-
using var syncLock = Sync.UpgradableReadLock();
134+
using var uLock = Sync.UpgradableReadLock();
135135
if (!InternalSource.Contains(item)) return false;
136-
syncLock.UpgradeToWriteLock();
136+
using var wLock = Sync.WriteLock();
137137
action(InternalSource);
138138
return true;
139139
}
140140

141141
/// <inheritdoc />
142142
public virtual bool IfNotContains(T item, Action<TCollection> action)
143143
{
144-
using var syncLock = Sync.UpgradableReadLock();
144+
using var uLock = Sync.UpgradableReadLock();
145145
if (!InternalSource.Contains(item)) return false;
146-
syncLock.UpgradeToWriteLock();
146+
using var wLock = Sync.WriteLock();
147147
action(InternalSource);
148148
return true;
149149
}

source/Synchronized/ReadWriteSynchronizedHashSet.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ public void IntersectWith(IEnumerable<T> other)
3131

3232
/// <inheritdoc />
3333
public bool IsProperSubsetOf(IEnumerable<T> other)
34-
=> Sync.ReadValue(() => InternalSource.IsProperSubsetOf(other));
34+
=> Sync.Read(() => InternalSource.IsProperSubsetOf(other));
3535

3636
/// <inheritdoc />
3737
public bool IsProperSupersetOf(IEnumerable<T> other)
38-
=> Sync.ReadValue(() => InternalSource.IsProperSupersetOf(other));
38+
=> Sync.Read(() => InternalSource.IsProperSupersetOf(other));
3939

4040
/// <inheritdoc />
4141
public bool IsSubsetOf(IEnumerable<T> other)
42-
=> Sync.ReadValue(() => InternalSource.IsSubsetOf(other));
42+
=> Sync.Read(() => InternalSource.IsSubsetOf(other));
4343

4444
/// <inheritdoc />
4545
public bool IsSupersetOf(IEnumerable<T> other)
46-
=> Sync.ReadValue(() => InternalSource.IsSupersetOf(other));
46+
=> Sync.Read(() => InternalSource.IsSupersetOf(other));
4747

4848
/// <inheritdoc />
4949
public bool Overlaps(IEnumerable<T> other)
50-
=> Sync.ReadValue(() => InternalSource.Overlaps(other));
50+
=> Sync.Read(() => InternalSource.Overlaps(other));
5151

5252
/// <inheritdoc />
5353
public bool SetEquals(IEnumerable<T> other)
54-
=> Sync.ReadValue(() => InternalSource.SetEquals(other));
54+
=> Sync.Read(() => InternalSource.SetEquals(other));
5555

5656
/// <inheritdoc />
5757
public void SymmetricExceptWith(IEnumerable<T> other)

source/Synchronized/ReadWriteSynchronizedLinkedList.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,56 @@ public LinkedListNode<T> Last
1717
=> InternalSource.Last;
1818

1919
/// <inheritdoc />
20-
public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T item) => Sync.WriteValue(() => InternalSource.AddAfter(node, item));
20+
public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T item)
21+
=> Sync.Write(() => InternalSource.AddAfter(node, item));
2122

2223
/// <inheritdoc />
23-
public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode) => Sync.Write(() => InternalSource.AddAfter(node, newNode));
24+
public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode)
25+
=> Sync.Write(() => InternalSource.AddAfter(node, newNode));
2426

2527
/// <inheritdoc />
26-
public LinkedListNode<T> AddBefore(LinkedListNode<T> node, T item) => Sync.WriteValue(() => InternalSource.AddBefore(node, item));
28+
public LinkedListNode<T> AddBefore(LinkedListNode<T> node, T item)
29+
=> Sync.Write(() => InternalSource.AddBefore(node, item));
2730

2831
/// <inheritdoc />
29-
public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode) => Sync.Write(() => InternalSource.AddBefore(node, newNode));
32+
public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
33+
=> Sync.Write(() => InternalSource.AddBefore(node, newNode));
3034

3135
/// <inheritdoc />
32-
public LinkedListNode<T> AddFirst(T item) => Sync.WriteValue(() => InternalSource.AddFirst(item));
36+
public LinkedListNode<T> AddFirst(T item)
37+
=> Sync.Write(() => InternalSource.AddFirst(item));
3338

3439
/// <inheritdoc />
35-
public void AddFirst(LinkedListNode<T> newNode) => Sync.Write(() => InternalSource.AddFirst(newNode));
40+
public void AddFirst(LinkedListNode<T> newNode)
41+
=> Sync.Write(() => InternalSource.AddFirst(newNode));
3642

3743
/// <inheritdoc />
38-
public LinkedListNode<T> AddLast(T item) => Sync.WriteValue(() => InternalSource.AddLast(item));
44+
public LinkedListNode<T> AddLast(T item)
45+
=> Sync.Write(() => InternalSource.AddLast(item));
3946

4047
/// <inheritdoc />
41-
public void AddLast(LinkedListNode<T> newNode) => Sync.Write(() => InternalSource.AddLast(newNode));
48+
public void AddLast(LinkedListNode<T> newNode)
49+
=> Sync.Write(() => InternalSource.AddLast(newNode));
4250

4351
/// <inheritdoc />
44-
public void Remove(LinkedListNode<T> node) => Sync.Write(() => InternalSource.Remove(node));
52+
public void Remove(LinkedListNode<T> node)
53+
=> Sync.Write(() => InternalSource.Remove(node));
4554

4655
/// <inheritdoc />
47-
public void RemoveFirst() => Sync.Write(() => InternalSource.RemoveFirst());
56+
public void RemoveFirst() => Sync.Write(()
57+
=> InternalSource.RemoveFirst());
4858

4959
/// <inheritdoc />
50-
public void RemoveLast() => Sync.Write(() => InternalSource.RemoveLast());
60+
public void RemoveLast() => Sync.Write(()
61+
=> InternalSource.RemoveLast());
5162

5263
/// <inheritdoc />
5364
public bool TryTakeFirst(out T item)
5465
{
5566
bool success = false;
5667
LinkedListNode<T>? node = null;
5768
T result = default!;
58-
Sync.ReadWriteConditionalOptimized(
69+
Sync.ReadWriteConditional(
5970
_ => (node = InternalSource.First) is not null,
6071
() =>
6172
{
@@ -73,7 +84,7 @@ public bool TryTakeLast(out T item)
7384
bool success = false;
7485
LinkedListNode<T>? node = null;
7586
T result = default!;
76-
Sync.ReadWriteConditionalOptimized(
87+
Sync.ReadWriteConditional(
7788
_ => (node = InternalSource.Last) is not null,
7889
() =>
7990
{

source/Synchronized/ReadWriteSynchronizedListWrapper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ public T this[int index]
1919
}
2020

2121
/// <inheritdoc />
22-
public int IndexOf(T item) => Sync.ReadValue(() => InternalSource.IndexOf(item));
22+
public int IndexOf(T item)
23+
=> Sync.Read(() => InternalSource.IndexOf(item));
2324

2425
/// <inheritdoc />
25-
public void Insert(int index, T item) => Sync.Write(() => InternalSource.Insert(index, item));
26+
public void Insert(int index, T item)
27+
=> Sync.Write(() => InternalSource.Insert(index, item));
2628

2729
/// <inheritdoc />
28-
public void RemoveAt(int index) => Sync.Write(() => InternalSource.RemoveAt(index));
30+
public void RemoveAt(int index)
31+
=> Sync.Write(() => InternalSource.RemoveAt(index));
2932
}

0 commit comments

Comments
 (0)