mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-09 20:12:21 +02:00
Added Parallel class and ForEach method
This commit is contained in:
parent
cbb4e8b738
commit
c4283a3d79
3 changed files with 41 additions and 1 deletions
BIN
BeefDep1.zip
Normal file
BIN
BeefDep1.zip
Normal file
Binary file not shown.
40
BeefLibs/corlib/src/Threading/Tasks/Parallel.bf
Normal file
40
BeefLibs/corlib/src/Threading/Tasks/Parallel.bf
Normal file
|
@ -0,0 +1,40 @@
|
|||
namespace System.Threading.Tasks;
|
||||
|
||||
class Parallel
|
||||
{
|
||||
public static void ForEach<T>(Span<T> source, delegate void(T item) body, int maxDegreeOfParallelism = -1) {
|
||||
int count = source.Length;
|
||||
if (count == 0)
|
||||
return;
|
||||
var processorCount = OperatingSystem.[Friend]GetSystemInfo(.. scope .()).dwNumberOfProcessors;
|
||||
int degree = maxDegreeOfParallelism == -1 ? processorCount : maxDegreeOfParallelism;
|
||||
if (degree <= 0)
|
||||
degree = 1;
|
||||
degree = Math.Min(degree, count);
|
||||
if (degree == 1)
|
||||
{
|
||||
for (var item in source)
|
||||
body(item);
|
||||
return;
|
||||
}
|
||||
|
||||
Task[] tasks = new Task[degree];
|
||||
defer delete tasks;
|
||||
|
||||
int workItemIndex = -1;
|
||||
for (var i < degree)
|
||||
{
|
||||
tasks[i] = new Task(new [&]() => {
|
||||
int index;
|
||||
while ((index = Interlocked.Increment(ref workItemIndex)) < count)
|
||||
body(source[index]);
|
||||
});
|
||||
tasks[i].Start();
|
||||
}
|
||||
|
||||
for (var i < degree) {
|
||||
tasks[i].Wait();
|
||||
delete tasks[i];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -106,7 +106,7 @@ namespace IDE.Compiler
|
|||
|
||||
public void* mNativeBfSystem;
|
||||
public bool mIsTiming;
|
||||
public Monitor mMonitor = new Monitor() ~ delete _;
|
||||
public Monitor mMonitor = new Monitor() ~ delete _;
|
||||
|
||||
public Dictionary<ProjectSource, BfParser> mParserMap = new Dictionary<ProjectSource, BfParser>() ~ delete _;
|
||||
public Dictionary<Project, BfProject> mProjectMap = new Dictionary<Project, BfProject>() ~ delete _;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue