From cc7551d1e8c25a544b6dc4fd36fce1d9e6d11f92 Mon Sep 17 00:00:00 2001 From: Elivo Date: Mon, 27 Oct 2025 15:33:50 +0800 Subject: [PATCH] =?UTF-8?q?NumberUtility=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=95=B4=E6=95=B0=E9=94=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Internals/Locker.cs | 9 ++++++++ Apewer/NumberUtility.cs | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) 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 约束值范围。 /// 约束值范围,若源值不在范围中,则修改为接近的值。