Skip to content

Review of public Ref(external count, Thread.Yield, error return) #8

@dzmitry-lahoda

Description

@dzmitry-lahoda

Did some view onto, hope you find some useful.

throw new InvalidOperationException(_errorRetryCountExceeded);

Also it is public, it may deliver some caveats to possible users:

  1. Spin probably should yield for better CPU https://referencesource.microsoft.com/#mscorlib/system/threading/thread.cs,dd960cd58d3d20c1,references
  2. Default counter should in public API, not private hidden surprise in production.
  3. Consider return error(value tuple, option, or by ref return) result from Swap instead of throwing exception. Retry exceed could be normal condition on low level.
  4. Reproduce in test retry count reached via one fast and one slow getNewValue. Until getNewValue is hanged and if Thread.Yield used I guess retry could be made infinite by default.
  5. Consider make it more private-internal if possible.
  6. T is class. Consider adding know primiteves swap like Interlocked for long int etc.
    public static T Swap<T>(ref T value, Func<T, T> getNewValue) where T : class
  7. /// <summary>Compares current Referred value with <paramref name="currentValue"/> and if equal replaces current with <paramref name="newValue"/></summary>
    consider document reference equals instead of equals (to be crystal clear).
  8. CompareAndSwap or other like name may be better name
    public bool TrySwapIfStillCurrent(T currentValue, T newValue) =>
  9. Interlocked.CompareExchange(ref _value, newValue, currentValue) == currentValue;
    does compares by reference, but returns by bool depending on possible == https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator. Same seems here
    if (Interlocked.CompareExchange(ref value, newValue, oldValue) == oldValue)
  10. Consider document usage scenarios, i.e. it seems not for long running operations, not ordered operations (while does not orders getNewValue delegates).
  11. Inspire https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-579.pdf :) and think of laptop Intel with 12 cores or Ryzen with 32 cores https://en.wikipedia.org/wiki/Ryzen (these are commodities now)

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions