Skip to content

Commit 05fd287

Browse files
Checkpoint.
1 parent 7992184 commit 05fd287

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

ReadWriteHelper/ReadWriteHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private bool TryExecute<T>(
312312
internal ContextHandler(ReadWriteHelper<TContext> helper, TContext context)
313313
{
314314
Helper = helper;
315-
Context = context;
315+
Context = context ?? throw new ArgumentNullException(nameof(context));
316316
}
317317

318318
/// <inheritdoc />

source/Documentation.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/Locks/Lock.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ public Lock(
4848
LockHeld = true;
4949
}
5050

51+
/// <summary>
52+
/// Returns true if <paramref name="syncObject"/> is valid for locking.
53+
/// </summary>
54+
public static bool IsValidSyncObject(object? syncObject)
55+
=> syncObject switch // Avoid the lock object being immutable...
56+
{
57+
null or string or Type or ValueType => false,
58+
_ => true,
59+
};
60+
61+
/// <summary>
62+
/// Throws an exception if <paramref name="syncObject"/> is not valid for locking.
63+
/// </summary>
64+
/// <exception cref="ArgumentNullException">If <paramref name="syncObject"/> is null.</exception>
65+
/// <exception cref="ArgumentException">If <paramref name="syncObject"/> is not valid for locking.</exception>
66+
public static void AssertSyncObject(object? syncObject)
67+
{
68+
if (syncObject is null)
69+
throw new ArgumentNullException(nameof(syncObject));
70+
if (syncObject is string or Type or ValueType)
71+
throw new ArgumentException($"Is not valid sync object. Invalid type: ({syncObject.GetType()})", nameof(syncObject));
72+
}
73+
5174
/// <inheritdoc cref="ILock.LockHeld"/>
5275
[ExcludeFromCodeCoverage]
5376
public static implicit operator bool(Lock monitor) => monitor.LockHeld;

source/Open.Threading.ReadWrite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageProjectUrl>https://github.com/Open-NET-Libraries/Open.Threading.ReadWrite/</PackageProjectUrl>
2020
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Threading.ReadWrite/</RepositoryUrl>
2121
<RepositoryType>git</RepositoryType>
22-
<Version>2.0.0-preview2</Version>
22+
<Version>2.0.0-preview3</Version>
2323
<PackageReleaseNotes></PackageReleaseNotes>
2424
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2525
<PublishRepositoryUrl>true</PublishRepositoryUrl>

tests/MonitorLockTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,27 @@ public void BasicTests()
3131
sync.LockHeld.Should().BeTrue();
3232
}
3333
}
34+
35+
struct Test { }
36+
37+
[Theory]
38+
[InlineData(null)]
39+
[InlineData(true)]
40+
[InlineData(false)]
41+
[InlineData("hello")]
42+
[InlineData(0)]
43+
[InlineData(1)]
44+
public void InvalidSyncObjectTests(object? syncObject)
45+
{
46+
Lock.IsValidSyncObject(syncObject).Should().BeFalse();
47+
if(syncObject is null)
48+
Assert.Throws<ArgumentNullException>(() => Lock.AssertSyncObject(syncObject));
49+
else
50+
Assert.Throws<ArgumentException>(() => Lock.AssertSyncObject(syncObject));
51+
}
52+
53+
[Fact]
54+
public void StructSyncObjectTests()
55+
=> InvalidSyncObjectTests(new Test());
56+
3457
}

0 commit comments

Comments
 (0)