L4 is now very unsafe

This commit is contained in:
Nitload
2026-04-12 23:55:18 +08:00
parent 9aa096dc63
commit 556008ad8c
5 changed files with 177 additions and 165 deletions
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelThread"
// file="MatrixParallelThread.cs"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. 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.
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -50,14 +53,14 @@ namespace StgSharp.Mathematics.Numeric
{
private IntrinsicContext* _context;
private MatrixParallelTask* _currentTask;
private volatile bool _disposed, _isRetireRequested;
private readonly MatrixParallelTask* _currentTask;
private volatile bool _disposed;
private readonly ManualResetEventSlim _resetEvent;
private volatile MatrixThreadState _target;
private Thread _managedThread;
internal MatrixParallelThread(
int poolId
)
internal MatrixParallelThread(int poolId)
{
_resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad);
@@ -69,8 +72,6 @@ namespace StgSharp.Mathematics.Numeric
public ThreadPriority AfterWork { get; set; }
internal bool IsSingle { get; set; }
internal MatrixThreadState State { get; private set; }
internal SIMDID Simd { get; private set; }
@@ -88,31 +89,26 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Retire()
internal void Pause()
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
if (State == MatrixThreadState.Retired) {
return;
}
_isRetireRequested = true;
_resetEvent.Set();
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
_managedThread.Join();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetSingleThreadTask(
ref MatrixParallelTask task
)
internal void SetSingleThreadTask(ref MatrixParallelTask task)
{
if (IsSingle) {
*_currentTask = task;
}
}
protected virtual void Dispose(
bool disposing
)
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
@@ -152,37 +148,30 @@ namespace StgSharp.Mathematics.Numeric
{
break;
}
if (_isRetireRequested)
if (_target == MatrixThreadState.Idle)
{
State = MatrixThreadState.Retired;
State = MatrixThreadState.Idle;
continue;
}
State = MatrixThreadState.Idle;
State = MatrixThreadState.Running;
_managedThread.Priority = ThreadPriority.Highest;
if (IsSingle)
{
MatrixParallelTask* outer = _currentTask;
// compute here
IsSingle = false;
} else
{
// MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
/*
if (0 > (int)p ->ComputeHandle) {
SpecialBufferCompute(p);
}
switch (p->ComputeMode)
{
case MatrixIndexStyle.BUFFER_INDEX:
BufferCompute(p);
break;
default:
break;
}
/**/
// MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
/*
if (0 > (int)p ->ComputeHandle) {
SpecialBufferCompute(p);
}
switch (p->ComputeMode)
{
case MatrixIndexStyle.BUFFER_INDEX:
BufferCompute(p);
break;
default:
break;
}
/**/
_managedThread.Priority = AfterWork;
State = MatrixThreadState.Idle;
}
@@ -191,24 +180,19 @@ namespace StgSharp.Mathematics.Numeric
#pragma warning disable CA5393
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial ulong GetSimdId();
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial void SetThreadAffinity(
int coreId
);
internal static partial void SetThreadAffinity(int coreId);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static unsafe partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
#pragma warning restore CA5393
}
}