Browse Source

NumberUtility:增加整数锁。

master
王厅 1 week ago
parent
commit
cc7551d1e8
  1. 9
      Apewer/Internals/Locker.cs
  2. 47
      Apewer/NumberUtility.cs

9
Apewer/Internals/Locker.cs

@ -14,6 +14,15 @@ namespace Apewer.Internals
}
/// <summary>整数锁。</summary>
internal sealed class Int64Locker : Locker<long>
{
/// <summary>获取整数的哈希值。</summary>
protected override int GetHash(long target) => target.GetHashCode();
}
/// <summary>文本锁。</summary>
internal sealed class TextLocker : Locker<string>
{

47
Apewer/NumberUtility.cs

@ -143,6 +143,53 @@ namespace Apewer
#endregion
#region 线程锁
static Int32Locker _int32_locker = new Int32Locker();
static Int64Locker _int64_locker = new Int64Locker();
/// <summary>锁定数值,在锁中执行函数。</summary>
/// <param name="value">要锁定的文本。</param>
/// <param name="inLock">要在锁中执行的函数。</param>
/// <exception cref="ArgumentNullException"></exception>
public static T Lock<T>(this int value, Func<T> inLock)
{
if (inLock == null) throw new ArgumentNullException(nameof(inLock));
return _int32_locker.InLock(value, inLock);
}
/// <summary>锁定文本,在锁中执行函数。</summary>
/// <param name="value">要锁定的文本。</param>
/// <param name="inLock">要在锁中执行的函数。</param>
/// <exception cref="ArgumentNullException"></exception>
public static void Lock(this int value, Action inLock)
{
if (inLock == null) throw new ArgumentNullException(nameof(inLock));
_int32_locker.InLock(value, inLock);
}
/// <summary>锁定数值,在锁中执行函数。</summary>
/// <param name="value">要锁定的文本。</param>
/// <param name="inLock">要在锁中执行的函数。</param>
/// <exception cref="ArgumentNullException"></exception>
public static T Lock<T>(this long value, Func<T> inLock)
{
if (inLock == null) throw new ArgumentNullException(nameof(inLock));
return _int64_locker.InLock(value, inLock);
}
/// <summary>锁定文本,在锁中执行函数。</summary>
/// <param name="value">要锁定的文本。</param>
/// <param name="inLock">要在锁中执行的函数。</param>
/// <exception cref="ArgumentNullException"></exception>
public static void Lock(this long value, Action inLock)
{
if (inLock == null) throw new ArgumentNullException(nameof(inLock));
_int64_locker.InLock(value, inLock);
}
#endregion
#region Restrict 约束值范围。
/// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>

Loading…
Cancel
Save