KeyLockEngine.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace fis
  8. {
  9. internal interface IKeyLockEngine
  10. {
  11. Task InvokeAsync(string key, Func<Task> action);
  12. }
  13. internal class SpinLockEngine : IKeyLockEngine
  14. {
  15. private const int LockeCount = 0x12fd; //素数,减少hash冲突,值越大冲突概率越小,但占用内存越大
  16. private static readonly int[] Locks = new int[LockeCount];
  17. public async Task InvokeAsync(string key, Func<Task> action)
  18. {
  19. int index = (key.GetHashCode() & 0x7fffffff) % LockeCount;
  20. // 尝试0变1,进入对应index的临界状态;
  21. while (Interlocked.CompareExchange(ref Locks[index], 1, 0) == 1)
  22. {
  23. Thread.Sleep(1);
  24. ////可也以计数方式.每X次尝试失败则睡眠1,否则睡眠0
  25. //Thread.Sleep(((++count) | 0x000f) == 0 ? 1 : 0);
  26. }
  27. try
  28. {
  29. await action();
  30. }
  31. finally
  32. {
  33. Thread.VolatileWrite(ref Locks[index], 0);
  34. }
  35. }
  36. }
  37. }