You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncGuard.cs 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Concurrent;
  2. namespace LLama.Web.Async
  3. {
  4. /// <summary>
  5. /// Creates a async/thread-safe guard helper
  6. /// </summary>
  7. /// <seealso cref="AsyncGuard&lt;byte&gt;" />
  8. public class AsyncGuard : AsyncGuard<byte>
  9. {
  10. private readonly byte _key;
  11. private readonly ConcurrentDictionary<byte, bool> _lockData;
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="AsyncGuard"/> class.
  14. /// </summary>
  15. public AsyncGuard()
  16. {
  17. _key = 0;
  18. _lockData = new ConcurrentDictionary<byte, bool>();
  19. }
  20. /// <summary>
  21. /// Guards this instance.
  22. /// </summary>
  23. /// <returns>true if able to enter an guard, false if already guarded</returns>
  24. public bool Guard()
  25. {
  26. return _lockData.TryAdd(_key, true);
  27. }
  28. /// <summary>
  29. /// Releases the guard.
  30. /// </summary>
  31. /// <returns></returns>
  32. public bool Release()
  33. {
  34. return _lockData.TryRemove(_key, out _);
  35. }
  36. /// <summary>
  37. /// Determines whether this instance is guarded.
  38. /// </summary>
  39. /// <returns>
  40. /// <c>true</c> if this instance is guarded; otherwise, <c>false</c>.
  41. /// </returns>
  42. public bool IsGuarded()
  43. {
  44. return _lockData.ContainsKey(_key);
  45. }
  46. }
  47. public class AsyncGuard<T>
  48. {
  49. private readonly ConcurrentDictionary<T, bool> _lockData;
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="AsyncGuard{T}"/> class.
  52. /// </summary>
  53. public AsyncGuard()
  54. {
  55. _lockData = new ConcurrentDictionary<T, bool>();
  56. }
  57. /// <summary>
  58. /// Guards the specified value.
  59. /// </summary>
  60. /// <param name="value">The value.</param>
  61. /// <returns>true if able to enter a guard for this value, false if this value is already guarded</returns>
  62. public bool Guard(T value)
  63. {
  64. return _lockData.TryAdd(value, true);
  65. }
  66. /// <summary>
  67. /// Releases the guard on the specified value.
  68. /// </summary>
  69. /// <param name="value">The value.</param>
  70. /// <returns></returns>
  71. public bool Release(T value)
  72. {
  73. return _lockData.TryRemove(value, out _);
  74. }
  75. /// <summary>
  76. /// Determines whether the specified value is guarded.
  77. /// </summary>
  78. /// <param name="value">The value.</param>
  79. /// <returns>
  80. /// <c>true</c> if the specified value is guarded; otherwise, <c>false</c>.
  81. /// </returns>
  82. public bool IsGuarded(T value)
  83. {
  84. return _lockData.ContainsKey(value);
  85. }
  86. }
  87. }