Dreamine.Threading 1.0.1
이 패키지는 .NET ThreadPool이나 Task Parallel Library를 대체하려는 목적이 아닙니다.
로딩중...
검색중...
일치하는것 없음
AutoCoreAllocator.cs
이 파일의 문서화 페이지로 가기
3
5
15{
24 private readonly object _syncRoot = new();
33 private readonly int _logicalCoreCount;
42 private readonly int[] _assignedCounts;
43
53 : this(Environment.ProcessorCount)
54 {
55 }
56
73 public AutoCoreAllocator(int logicalCoreCount)
74 {
75 _logicalCoreCount = logicalCoreCount <= 0 ? 1 : logicalCoreCount;
77 }
78
120 {
121 ArgumentNullException.ThrowIfNull(options);
122
123 var normalized = options.Normalize();
124
125 lock (_syncRoot)
126 {
127 return normalized.CoreMode switch
128 {
133 };
134 }
135 }
136
161 public void Release(DreamineThreadCoreAssignment assignment)
162 {
163 ArgumentNullException.ThrowIfNull(assignment);
164
165 if (assignment.CoreIndex is null || assignment.IsOverflowPolling)
166 {
167 return;
168 }
169
170 lock (_syncRoot)
171 {
172 var coreIndex = assignment.CoreIndex.Value;
173
174 if (coreIndex < 0 || coreIndex >= _assignedCounts.Length)
175 {
176 return;
177 }
178
179 if (_assignedCounts[coreIndex] > 0)
180 {
181 _assignedCounts[coreIndex]--;
182 }
183 }
184 }
185
219 {
220 if (options.CoreIndex is null)
221 {
223 }
224
225 var coreIndex = options.CoreIndex.Value;
226
227 if (coreIndex < 0 || coreIndex >= _logicalCoreCount)
228 {
229 throw new ArgumentOutOfRangeException(
230 nameof(options),
231 $"CPU core index {coreIndex} is out of range. Logical core count is {_logicalCoreCount}.");
232 }
233
234 _assignedCounts[coreIndex]++;
235 return DreamineThreadCoreAssignment.Dedicated(coreIndex, true);
236 }
237
263 {
264 var maxPerCore = options.AutoThreadsPerCore <= 0 ? 2 : options.AutoThreadsPerCore;
265
266 var selectedCore = 0;
267 var selectedCount = _assignedCounts[0];
268
269 for (var i = 1; i < _assignedCounts.Length; i++)
270 {
271 if (_assignedCounts[i] < selectedCount)
272 {
273 selectedCore = i;
274 selectedCount = _assignedCounts[i];
275 }
276 }
277
278 if (selectedCount >= maxPerCore)
279 {
281 }
282
283 _assignedCounts[selectedCore]++;
284 return DreamineThreadCoreAssignment.Dedicated(selectedCore, true);
285 }
286}
void Release(DreamineThreadCoreAssignment assignment)
DreamineThreadCoreAssignment AllocateAuto(DreamineThreadOptions options)
DreamineThreadCoreAssignment AllocateManual(DreamineThreadOptions options)
DreamineThreadCoreAssignment Allocate(DreamineThreadOptions options)
static DreamineThreadCoreAssignment Dedicated(int coreIndex, bool useAffinity)