Open dzmitry-lahoda opened 6 years ago
Found similar code in .NET source
public static X TryEnterWriteLock(ref X value, TimeSpan timeout)
{
// https://github.com/dotnet/corefx/blob/3b24c535852d19274362ad3dbc75e932b7d41766/src/Common/src/CoreLib/System/Threading/ReaderWriterLockSlim.cs#L233
var tracker = new TimeoutTracker(timeout);
if (tracker.IsExpired)
{
throw new TimeoutException("Some other lock is hold for too long time");
}
I guess throw via Thrower may save some performance of spin. Could check that. Example is https://github.com/jtmueller/Collections.Pooled/issues/8
These people are not afraid of timeout https://github.com/scalaz/scalaz-zio/blob/master/core/shared/src/main/scala/scalaz/zio/Ref.scala#L42. And use function. May be this Ref should to.
Another Ref
https://github.com/atifaziz/Interlocker/issues
Did some view onto, hope you find some useful.
https://github.com/dadhi/ImTools/blob/56b7f53a784c3843e4299658592d7b9340153fbc/src/ImTools/ImTools.cs#L534
Also it is public, it may deliver some caveats to possible users:
reference equals
instead ofequals
(to be crystal clear).==
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator. Same seems here https://github.com/dadhi/ImTools/blob/56b7f53a784c3843e4299658592d7b9340153fbc/src/ImTools/ImTools.cs#L531