Skip to content

Commit dcb7b29

Browse files
Updated for release.
1 parent f5ad96c commit dcb7b29

File tree

5 files changed

+40
-44
lines changed

5 files changed

+40
-44
lines changed

Benchmarking/ArrayBenchmark.cs

+6-17
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,39 @@ public class ArrayBenchmark
1111
readonly ReaderWriterLockSlim RWLock = new();
1212
readonly ReaderWriterLockSlim[] RWLocks = Enumerable.Range(0, Length).Select(_ => new ReaderWriterLockSlim()).ToArray();
1313

14-
1514
[Benchmark(Baseline = true)]
1615
public void RandomParallelReads()
17-
{
18-
Parallel.For(0, Count, i => _ = TestData[i % Length]);
19-
}
16+
=> Parallel.For(0, Count, i => _ = TestData[i % Length]);
2017

2118
[Benchmark]
2219
public void RandomParallelSingleLockReads()
23-
{
24-
Parallel.For(0, Count, i =>
20+
=> Parallel.For(0, Count, i =>
2521
{
26-
lock(TestData) _ = TestData[i % Length];
22+
lock (TestData) _ = TestData[i % Length];
2723
});
28-
}
2924

3025
[Benchmark]
3126
public void RandomParallelSingleRWLockReads()
32-
{
33-
Parallel.For(0, Count, i =>
27+
=> Parallel.For(0, Count, i =>
3428
{
3529
using var rwlock = RWLock.ReadLock();
3630
_ = TestData[i % Length];
3731
});
38-
}
3932

4033
[Benchmark]
4134
public void RandomParallelLockedReads()
42-
{
43-
Parallel.For(0, Count, i =>
35+
=> Parallel.For(0, Count, i =>
4436
{
4537
var n = i % Length;
4638
lock (Locks[n]) _ = TestData[n];
4739
});
48-
}
4940

5041
[Benchmark]
5142
public void RandomParallelRWLockedReads()
52-
{
53-
Parallel.For(0, Count, i =>
43+
=> Parallel.For(0, Count, i =>
5444
{
5545
var n = i % Length;
5646
using var rwlock = RWLocks[n].ReadLock();
5747
_ = TestData[n];
5848
});
59-
}
6049
}

Open.Threading.sln

+1-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ VisualStudioVersion = 17.1.32210.238
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Threading", "source\Open.Threading.csproj", "{7E8DFAB3-24B4-41F8-AA3E-AA9A0AA7D7A6}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Threading.Tests", "..\tests\Open.Threading.Tests.csproj", "{A3BC9107-6091-470F-9049-8ADD3F43CFB4}"
9-
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open.Threading.Benchmarking", "Benchmarking\Open.Threading.Benchmarking.csproj", "{A28E7459-E82B-4F3B-847A-2249CBD4E412}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Open.Threading.Benchmarking", "Benchmarking\Open.Threading.Benchmarking.csproj", "{A28E7459-E82B-4F3B-847A-2249CBD4E412}"
119
EndProject
1210
Global
1311
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,10 +17,6 @@ Global
1917
{7E8DFAB3-24B4-41F8-AA3E-AA9A0AA7D7A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
2018
{7E8DFAB3-24B4-41F8-AA3E-AA9A0AA7D7A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
2119
{7E8DFAB3-24B4-41F8-AA3E-AA9A0AA7D7A6}.Release|Any CPU.Build.0 = Release|Any CPU
22-
{A3BC9107-6091-470F-9049-8ADD3F43CFB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23-
{A3BC9107-6091-470F-9049-8ADD3F43CFB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
24-
{A3BC9107-6091-470F-9049-8ADD3F43CFB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
25-
{A3BC9107-6091-470F-9049-8ADD3F43CFB4}.Release|Any CPU.Build.0 = Release|Any CPU
2620
{A28E7459-E82B-4F3B-847A-2249CBD4E412}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2721
{A28E7459-E82B-4F3B-847A-2249CBD4E412}.Debug|Any CPU.Build.0 = Debug|Any CPU
2822
{A28E7459-E82B-4F3B-847A-2249CBD4E412}.Release|Any CPU.ActiveCfg = Release|Any CPU

source/ModificationSynchronizer.cs

+18-10
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ public sealed class ReadOnlyModificationSynchronizer : IModificationSynchronizer
2828

2929
public T Reading<T>(Func<T> action) => action();
3030

31-
public bool Modifying(Action action, bool assumeChange = false) => throw new NotSupportedException("Synchronizer is read-only.");
31+
private const string ReadOnlyMessage = "Synchronizer is read-only.";
3232

33-
public bool Modifying(Func<bool> action) => throw new NotSupportedException("Synchronizer is read-only.");
33+
public bool Modifying(Action action, bool assumeChange = false)
34+
=> throw new NotSupportedException(ReadOnlyMessage);
35+
36+
public bool Modifying(Func<bool> action)
37+
=> throw new NotSupportedException(ReadOnlyMessage);
3438

35-
public bool Modifying(Func<bool> condition, Func<bool> action) => throw new NotSupportedException("Synchronizer is read-only.");
39+
public bool Modifying(Func<bool> condition, Func<bool> action)
40+
=> throw new NotSupportedException(ReadOnlyMessage);
3641

37-
public bool Modifying<T>(ref T target, T newValue) => throw new NotSupportedException("Synchronizer is read-only.");
42+
public bool Modifying<T>(ref T target, T newValue)
43+
=> throw new NotSupportedException(ReadOnlyMessage);
3844

3945
public void Poke()
4046
{
@@ -75,9 +81,11 @@ public virtual T Reading<T>(Func<T> action)
7581
return action();
7682
}
7783

78-
protected void SignalModified() => Modified?.Invoke(this, EventArgs.Empty);
84+
protected void SignalModified()
85+
=> Modified?.Invoke(this, EventArgs.Empty);
7986

80-
public bool Modifying(Func<bool> action) => Modifying(null, action);
87+
public bool Modifying(Func<bool> action)
88+
=> Modifying(null, action);
8189

8290
public bool Modifying(Action action, bool assumeChange = false)
8391
=> Modifying(() =>
@@ -188,21 +196,21 @@ protected override void OnDispose()
188196
public override void Reading(Action action)
189197
{
190198
AssertIsAlive();
191-
ReaderWriterLockSlim? sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
199+
ReaderWriterLockSlim sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
192200
sync.Read(action);
193201
}
194202

195203
public override T Reading<T>(Func<T> action)
196204
{
197205
AssertIsAlive();
198-
ReaderWriterLockSlim? sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
206+
ReaderWriterLockSlim sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
199207
return sync.Read(action);
200208
}
201209

202210
public override bool Modifying(Func<bool>? condition, Func<bool> action)
203211
{
204212
AssertIsAlive();
205-
ReaderWriterLockSlim? sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
213+
ReaderWriterLockSlim sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
206214

207215
return (condition is null || sync.Read(condition)) // Try and early invalidate.
208216
&& sync.WriteConditional(
@@ -215,7 +223,7 @@ public override bool Modifying<T>(ref T target, T newValue)
215223
AssertIsAlive();
216224
if (target is null ? newValue is null : target.Equals(newValue)) return false;
217225

218-
ReaderWriterLockSlim? sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
226+
ReaderWriterLockSlim sync = _sync ?? throw new ObjectDisposedException(GetType().ToString());
219227
// Note, there's no need for _modifyingDepth recursion tracking here.
220228
using UpgradableReadLock readLock = sync.UpgradableReadLock();
221229
AssertIsAlive();

source/Open.Threading.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Part of the "Open" set of libraries.
1515
</Description>
1616
<PackageTags>collections;extensions;threadsafe;thread-safe;readwrite;read-write;readerwriterlock;readerwriterlockslim</PackageTags>
17-
<Version>2.0.0-preview1</Version>
17+
<Version>2.0.0</Version>
1818
<Copyright>© electricessence (Oren F.) All rights reserved.</Copyright>
1919
<PackageProjectUrl>https://github.com/Open-NET-Libraries/Open.Threading/</PackageProjectUrl>
2020
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Threading/</RepositoryUrl>

source/ThreadSafety.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ internal static void ValidatePath(string path)
584584
}
585585

586586
static ReadWriteHelper<string>? _instance;
587-
private static ReadWriteHelper<string> Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new ReadWriteHelper<string>())!;
587+
private static ReadWriteHelper<string> Instance
588+
=> LazyInitializer.EnsureInitialized(ref _instance, () => new ReadWriteHelper<string>())!;
588589

589590
/// <summary>
590591
/// Manages registering a ReaderWriterLockSlim an synchronizing the provided query write access.
@@ -630,7 +631,8 @@ public static void WriteTo(string path, Action<FileStream> closure,
630631
int retries = DEFAULT_RETRIES,
631632
int millisecondsRetryDelay = DEFAULT_RETRYDELAY,
632633
LockTimeout timeout = default,
633-
bool throwsOnTimeout = false) => WriteToInternal(path, closure, retries, millisecondsRetryDelay, timeout, throwsOnTimeout);
634+
bool throwsOnTimeout = false)
635+
=> WriteToInternal(path, closure, retries, millisecondsRetryDelay, timeout, throwsOnTimeout);
634636

635637
/// <summary>
636638
/// Manages file stream read access and retries.
@@ -639,7 +641,8 @@ public static void AppendTo(string path, Action<FileStream> closure,
639641
int retries = DEFAULT_RETRIES,
640642
int millisecondsRetryDelay = DEFAULT_RETRYDELAY,
641643
LockTimeout timeout = default,
642-
bool throwsOnTimeout = false) => WriteToInternal(path, closure, retries, millisecondsRetryDelay, timeout, throwsOnTimeout, FileMode.Append);
644+
bool throwsOnTimeout = false)
645+
=> WriteToInternal(path, closure, retries, millisecondsRetryDelay, timeout, throwsOnTimeout, FileMode.Append);
643646

644647
/// <summary>
645648
/// Manages file stream read access and retries.
@@ -796,11 +799,12 @@ public static T ReadFrom<T>(string path, Func<FileStream, T> closure,
796799

797800
public static string ReadToString(string path, int retries = DEFAULT_RETRIES,
798801
int millisecondsRetryDelay = DEFAULT_RETRYDELAY,
799-
LockTimeout timeout = default) => ReadFrom(path, (fs) =>
800-
{
801-
using var reader = new StreamReader(fs);
802-
return reader.ReadToEnd();
803-
}, retries, millisecondsRetryDelay, timeout);
802+
LockTimeout timeout = default)
803+
=> ReadFrom(path, (fs) =>
804+
{
805+
using var reader = new StreamReader(fs);
806+
return reader.ReadToEnd();
807+
}, retries, millisecondsRetryDelay, timeout);
804808

805809
public static class Unsafe
806810
{
@@ -887,7 +891,8 @@ public static Task<FileStream> GetFileStreamForReadAsync(
887891
public static FileStream GetFileStreamForRead(string path,
888892
int retries = DEFAULT_RETRIES,
889893
int millisecondsRetryDelay = DEFAULT_RETRYDELAY,
890-
LockTimeout timeout = default) => ReadFrom(path, () => Unsafe.GetFileStreamForRead(path, retries, millisecondsRetryDelay), timeout);
894+
LockTimeout timeout = default)
895+
=> ReadFrom(path, () => Unsafe.GetFileStreamForRead(path, retries, millisecondsRetryDelay), timeout);
891896

892897
/// <summary>
893898
/// Uses registered read access conditions to determine if a file exists.

0 commit comments

Comments
 (0)