find a way to create Thread binded to LogicalCore properly
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="EmptyThread"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Threading
|
||||
{
|
||||
public static partial class ThreadHelper
|
||||
{
|
||||
|
||||
private static readonly Thread _emptyThreadCache = new Thread(() => { });
|
||||
|
||||
static ThreadHelper()
|
||||
{
|
||||
_emptyThreadCache.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An individual thread doing nothing, and its <see cref="Thread.IsAlive" /> <see
|
||||
/// langword="property" /> always returns false.
|
||||
/// </summary>
|
||||
public static Thread EmptyThread
|
||||
{
|
||||
get => _emptyThreadCache;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumaBuffer"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Common.Threading
|
||||
{
|
||||
public unsafe struct NumaBuffer<T> where T : unmanaged
|
||||
{
|
||||
|
||||
public readonly T* Buffers;
|
||||
public readonly int BufferColumnCapacity;
|
||||
public readonly int BufferRowCapacity;
|
||||
|
||||
public int CurrentColumnCount;
|
||||
public int CurrentRowCount;
|
||||
|
||||
}
|
||||
}
|
||||
+35
-2
@@ -1,6 +1,6 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MainThreadAssert"
|
||||
// file="ThreadHelper"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
@@ -25,8 +25,10 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace StgSharp.Threading
|
||||
@@ -35,9 +37,29 @@ namespace StgSharp.Threading
|
||||
{
|
||||
|
||||
private static readonly int mainThreadId = Environment.CurrentManagedThreadId;
|
||||
|
||||
private static readonly ThreadLocal<bool> isNotMainThread = new(
|
||||
() => Environment.CurrentManagedThreadId != mainThreadId);
|
||||
|
||||
static ThreadHelper()
|
||||
{
|
||||
EmptyThread.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An individual thread doing nothing, and its <see cref="Thread.IsAlive" /> <see
|
||||
/// langword="property" /> always returns false.
|
||||
/// </summary>
|
||||
public static Thread EmptyThread { get; } = new(() => { });
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void BindToLogicalCore(
|
||||
LogicalCore core
|
||||
)
|
||||
{
|
||||
SetThreadAffinity(core.GlobalCpuId, core.NumaNodeId);
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
internal static void MainThreadAssert()
|
||||
{
|
||||
@@ -47,5 +69,16 @@ namespace StgSharp.Threading
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable CA5393
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
internal static partial void SetThreadAffinity(
|
||||
uint globalCoreId,
|
||||
uint numaNodeId
|
||||
);
|
||||
|
||||
#pragma warning restore CA5393
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,33 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="VariableCapacitySemaphore"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="VariableCapacitySemaphore"
|
||||
// Project: StgSharp.Common
|
||||
// AuthorGroup: Nitload
|
||||
// License: MIT
|
||||
@@ -40,7 +67,11 @@ namespace StgSharp.Threading
|
||||
/// <param name="stripes">
|
||||
/// Optional stripe count for MRES (<= 0 to auto choose by CPU).
|
||||
/// </param>
|
||||
public VariableCapacitySemaphore(int initialCount = 0, int maxPermits = 1, int stripes = 0)
|
||||
public VariableCapacitySemaphore(
|
||||
int initialCount = 0,
|
||||
int maxPermits = 1,
|
||||
int stripes = 0
|
||||
)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(maxPermits, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(initialCount);
|
||||
@@ -48,14 +79,16 @@ namespace StgSharp.Threading
|
||||
_max = maxPermits;
|
||||
_current = Math.Min(initialCount, maxPermits);
|
||||
|
||||
int stripeCount = stripes > 0 ? stripes : ClampToPow2(Math.Min(Environment.ProcessorCount, 8));
|
||||
int stripeCount = (stripes > 0) ?
|
||||
stripes :
|
||||
ClampToPow2(Math.Min(Environment.ProcessorCount, 8));
|
||||
_events = new ManualResetEventSlim[stripeCount];
|
||||
bool set = _current > 0;
|
||||
for (int i = 0; i < stripeCount; i++) {
|
||||
_events[i] = new ManualResetEventSlim(set, 0);
|
||||
}
|
||||
|
||||
_stripeMask = IsPow2(stripeCount) ? (stripeCount - 1) : -1;
|
||||
_stripeMask = IsPow2(stripeCount) ? (stripeCount - 1) : (-1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -139,7 +172,9 @@ namespace StgSharp.Threading
|
||||
/// count.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int Release(int permits)
|
||||
public int Release(
|
||||
int permits
|
||||
)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(permits);
|
||||
while (true)
|
||||
@@ -150,7 +185,7 @@ namespace StgSharp.Threading
|
||||
return cur;
|
||||
}
|
||||
|
||||
long target = (long)cur + permits;
|
||||
long target = ((long)cur) + permits;
|
||||
if (target > max) {
|
||||
target = max;
|
||||
}
|
||||
@@ -201,7 +236,7 @@ namespace StgSharp.Threading
|
||||
while (true)
|
||||
{
|
||||
int cur = Volatile.Read(ref _current);
|
||||
if (cur > 0 && Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur)
|
||||
if ((cur > 0) && (Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur))
|
||||
{
|
||||
if (cur - 1 == 0) {
|
||||
ResetAll();
|
||||
@@ -222,7 +257,10 @@ namespace StgSharp.Threading
|
||||
/// <summary>
|
||||
/// Block until a permit is available or canceled, then consume one.
|
||||
/// </summary>
|
||||
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken = default)
|
||||
public bool Wait(
|
||||
int millisecondsTimeout,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
ManualResetEventSlim gate = GetStripeEvent();
|
||||
SpinWait spinner = new SpinWait();
|
||||
@@ -232,7 +270,7 @@ namespace StgSharp.Threading
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
int cur = Volatile.Read(ref _current);
|
||||
if (cur > 0 && Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur)
|
||||
if ((cur > 0) && (Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur))
|
||||
{
|
||||
if (cur - 1 == 0) {
|
||||
ResetAll();
|
||||
@@ -263,7 +301,9 @@ namespace StgSharp.Threading
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int ClampToPow2(int x)
|
||||
private static int ClampToPow2(
|
||||
int x
|
||||
)
|
||||
{
|
||||
if (x < 1) {
|
||||
x = 1;
|
||||
@@ -274,7 +314,7 @@ namespace StgSharp.Threading
|
||||
}
|
||||
|
||||
int p = 1;
|
||||
while ((p << 1) <= x) {
|
||||
while (p << 1 <= x) {
|
||||
p <<= 1;
|
||||
}
|
||||
|
||||
@@ -294,7 +334,12 @@ namespace StgSharp.Threading
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsPow2(int x) => (x & (x - 1)) == 0;
|
||||
private static bool IsPow2(
|
||||
int x
|
||||
)
|
||||
{
|
||||
return (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void ResetAll()
|
||||
|
||||
Reference in New Issue
Block a user