//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="MatrixParallel.Await" // 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.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StgSharp.Mathematics.Numeric { public static partial class MatrixParallel { private static ConcurrentQueue _awaitingLeaderHandle; private static volatile int _createGeneration = 0; private static int _sortState; public static MatrixParallelThread[] LeadParallel(int recommendCount) { int min = int.Min(recommendCount, _threads.Capacity); min /= 2; MatrixParallelThread[] ret; if (_threads.Count < min) { WaitHandle handle = new WaitHandle { CreateGeneration = _createGeneration, LeastCount = min, BestCount = recommendCount }; _awaitingLeaderHandle.Enqueue(handle); int cycleGeneration = _createGeneration; while (true) { // TODO 排队等待,对前八个按照优先级排序,优先级最高的独占进行等待,其他继续排队 if (_createGeneration != cycleGeneration) { if (Interlocked.Exchange(ref _sortState, 1) == 0) { // TODO 排序 _ = Interlocked.Exchange(ref _sortState, 0); } else { cycleGeneration = _createGeneration; continue; } } if (_awaitingLeaderHandle.TryPeek(out WaitHandle? p) && p == handle) { // get the leader ret = _threads.PopRange(recommendCount); _awaitingLeaderHandle.TryDequeue(out _); break; } Thread.Sleep(0); } } else { // directly get the leader ret = _threads.PopRange(recommendCount); } _ = Interlocked.Increment(ref _createGeneration); return ret; } public static void ReturnParallelResources(ref MatrixParallelThread[] pool) { _threads.PushRange(pool); pool = null!; } private record class WaitHandle { public int CreateGeneration { get; set; } public int LeastCount { get; set; } public int BestCount { get; set; } public int CurrentPriority(int currentGeneration) { return (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount); } } } }