diff --git a/Apewer/Internals/Locker.cs b/Apewer/Internals/Locker.cs index d44f34b..3cd6296 100644 --- a/Apewer/Internals/Locker.cs +++ b/Apewer/Internals/Locker.cs @@ -14,6 +14,15 @@ namespace Apewer.Internals } + /// 整数锁。 + internal sealed class Int64Locker : Locker + { + + /// 获取整数的哈希值。 + protected override int GetHash(long target) => target.GetHashCode(); + + } + /// 文本锁。 internal sealed class TextLocker : Locker { diff --git a/Apewer/NumberUtility.cs b/Apewer/NumberUtility.cs index f74cf6a..52b6fa2 100644 --- a/Apewer/NumberUtility.cs +++ b/Apewer/NumberUtility.cs @@ -143,6 +143,53 @@ namespace Apewer #endregion + #region 线程锁 + + static Int32Locker _int32_locker = new Int32Locker(); + static Int64Locker _int64_locker = new Int64Locker(); + + /// 锁定数值,在锁中执行函数。 + /// 要锁定的文本。 + /// 要在锁中执行的函数。 + /// + public static T Lock(this int value, Func inLock) + { + if (inLock == null) throw new ArgumentNullException(nameof(inLock)); + return _int32_locker.InLock(value, inLock); + } + + /// 锁定文本,在锁中执行函数。 + /// 要锁定的文本。 + /// 要在锁中执行的函数。 + /// + public static void Lock(this int value, Action inLock) + { + if (inLock == null) throw new ArgumentNullException(nameof(inLock)); + _int32_locker.InLock(value, inLock); + } + + /// 锁定数值,在锁中执行函数。 + /// 要锁定的文本。 + /// 要在锁中执行的函数。 + /// + public static T Lock(this long value, Func inLock) + { + if (inLock == null) throw new ArgumentNullException(nameof(inLock)); + return _int64_locker.InLock(value, inLock); + } + + /// 锁定文本,在锁中执行函数。 + /// 要锁定的文本。 + /// 要在锁中执行的函数。 + /// + 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 约束值范围。 /// 约束值范围,若源值不在范围中,则修改为接近的值。