Dreamine.Threading
1.0.1
이 패키지는 .NET ThreadPool이나 Task Parallel Library를 대체하려는 목적이 아닙니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
AutoCoreAllocator.cs
이 파일의 문서화 페이지로 가기
1
using
Dreamine.Threading.Interfaces
;
2
using
Dreamine.Threading.Models
;
3
4
namespace
Dreamine.Threading.Allocators
;
5
14
public
sealed
class
AutoCoreAllocator
:
IThreadCoreAllocator
15
{
24
private
readonly
object
_syncRoot
=
new
();
33
private
readonly
int
_logicalCoreCount
;
42
private
readonly
int
[]
_assignedCounts
;
43
52
public
AutoCoreAllocator
()
53
: this(Environment.ProcessorCount)
54
{
55
}
56
73
public
AutoCoreAllocator
(
int
logicalCoreCount)
74
{
75
_logicalCoreCount
= logicalCoreCount <= 0 ? 1 : logicalCoreCount;
76
_assignedCounts
=
new
int
[
_logicalCoreCount
];
77
}
78
119
public
DreamineThreadCoreAssignment
Allocate
(
DreamineThreadOptions
options)
120
{
121
ArgumentNullException.ThrowIfNull(options);
122
123
var normalized = options.
Normalize
();
124
125
lock (
_syncRoot
)
126
{
127
return
normalized.CoreMode
switch
128
{
129
DreamineThreadCoreMode.None
=>
DreamineThreadCoreAssignment
.
None
(),
130
DreamineThreadCoreMode.Manual
=>
AllocateManual
(normalized),
131
DreamineThreadCoreMode.Auto
=>
AllocateAuto
(normalized),
132
_ =>
DreamineThreadCoreAssignment
.
None
()
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
218
private
DreamineThreadCoreAssignment
AllocateManual
(
DreamineThreadOptions
options)
219
{
220
if
(options.
CoreIndex
is
null
)
221
{
222
return
DreamineThreadCoreAssignment
.
None
();
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
262
private
DreamineThreadCoreAssignment
AllocateAuto
(
DreamineThreadOptions
options)
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
{
280
return
DreamineThreadCoreAssignment
.
Overflow
();
281
}
282
283
_assignedCounts
[selectedCore]++;
284
return
DreamineThreadCoreAssignment
.
Dedicated
(selectedCore,
true
);
285
}
286
}
Dreamine.Threading.Allocators
Definition
AutoCoreAllocator.cs:4
Dreamine.Threading.Interfaces
Definition
ICpuInfoProvider.cs:1
Dreamine.Threading.Models
Definition
DreamineThreadCoreAssignment.cs:1
Dreamine.Threading.Models.DreamineThreadCoreMode.Auto
@ Auto
Definition
DreamineThreadCoreMode.cs:31
Dreamine.Threading.Models.DreamineThreadCoreMode.None
@ None
Definition
DreamineThreadCoreMode.cs:21
Dreamine.Threading.Models.DreamineThreadCoreMode.Manual
@ Manual
Definition
DreamineThreadCoreMode.cs:41
Dreamine.Threading.Allocators.AutoCoreAllocator.Release
void Release(DreamineThreadCoreAssignment assignment)
Definition
AutoCoreAllocator.cs:161
Dreamine.Threading.Allocators.AutoCoreAllocator._syncRoot
readonly object _syncRoot
Definition
AutoCoreAllocator.cs:24
Dreamine.Threading.Allocators.AutoCoreAllocator.AutoCoreAllocator
AutoCoreAllocator(int logicalCoreCount)
Definition
AutoCoreAllocator.cs:73
Dreamine.Threading.Allocators.AutoCoreAllocator._assignedCounts
readonly int[] _assignedCounts
Definition
AutoCoreAllocator.cs:42
Dreamine.Threading.Allocators.AutoCoreAllocator.AutoCoreAllocator
AutoCoreAllocator()
Definition
AutoCoreAllocator.cs:52
Dreamine.Threading.Allocators.AutoCoreAllocator._logicalCoreCount
readonly int _logicalCoreCount
Definition
AutoCoreAllocator.cs:33
Dreamine.Threading.Allocators.AutoCoreAllocator.AllocateAuto
DreamineThreadCoreAssignment AllocateAuto(DreamineThreadOptions options)
Definition
AutoCoreAllocator.cs:262
Dreamine.Threading.Allocators.AutoCoreAllocator.AllocateManual
DreamineThreadCoreAssignment AllocateManual(DreamineThreadOptions options)
Definition
AutoCoreAllocator.cs:218
Dreamine.Threading.Allocators.AutoCoreAllocator.Allocate
DreamineThreadCoreAssignment Allocate(DreamineThreadOptions options)
Definition
AutoCoreAllocator.cs:119
Dreamine.Threading.Interfaces.IThreadCoreAllocator
Definition
IThreadCoreAllocator.cs:14
Dreamine.Threading.Models.DreamineThreadCoreAssignment
Definition
DreamineThreadCoreAssignment.cs:12
Dreamine.Threading.Models.DreamineThreadCoreAssignment.None
static DreamineThreadCoreAssignment None()
Definition
DreamineThreadCoreAssignment.cs:108
Dreamine.Threading.Models.DreamineThreadCoreAssignment.Dedicated
static DreamineThreadCoreAssignment Dedicated(int coreIndex, bool useAffinity)
Definition
DreamineThreadCoreAssignment.cs:145
Dreamine.Threading.Models.DreamineThreadCoreAssignment.Overflow
static DreamineThreadCoreAssignment Overflow()
Definition
DreamineThreadCoreAssignment.cs:166
Dreamine.Threading.Models.DreamineThreadCoreAssignment.CoreIndex
int? CoreIndex
Definition
DreamineThreadCoreAssignment.cs:21
Dreamine.Threading.Models.DreamineThreadCoreAssignment.IsOverflowPolling
bool IsOverflowPolling
Definition
DreamineThreadCoreAssignment.cs:41
Dreamine.Threading.Models.DreamineThreadOptions
Definition
DreamineThreadOptions.cs:12
Dreamine.Threading.Models.DreamineThreadOptions.AutoThreadsPerCore
int AutoThreadsPerCore
Definition
DreamineThreadOptions.cs:71
Dreamine.Threading.Models.DreamineThreadOptions.CoreIndex
int? CoreIndex
Definition
DreamineThreadOptions.cs:61
Dreamine.Threading.Models.DreamineThreadOptions.Normalize
DreamineThreadOptions Normalize()
Definition
DreamineThreadOptions.cs:149
Allocators
AutoCoreAllocator.cs
다음에 의해 생성됨 :
1.17.0