-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hand written custom stack size pool task scheduler
- Loading branch information
1 parent
9e8fc99
commit 520bd52
Showing
3 changed files
with
78 additions
and
330 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Source/ExecutionEngine/CustomStackSizePoolTaskScheduler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Boogie; | ||
|
||
public class CustomStackSizePoolTaskScheduler : TaskScheduler, IDisposable | ||
{ | ||
private readonly int threadCount; | ||
private readonly ConcurrentQueue<Task> queue = new(); | ||
private readonly SemaphoreSlim isWorkAvailable = new(0); | ||
private readonly Thread[] threads; | ||
|
||
public static CustomStackSizePoolTaskScheduler Create(int stackSize, int threadCount) | ||
{ | ||
return new CustomStackSizePoolTaskScheduler(stackSize, threadCount); | ||
} | ||
|
||
private CustomStackSizePoolTaskScheduler(int stackSize, int threadCount) | ||
{ | ||
this.threadCount = threadCount; | ||
|
||
threads = new Thread[this.threadCount]; | ||
for (int i = 0; i < threads.Length; i++) | ||
{ | ||
threads[i] = new Thread(WorkLoop, stackSize) { IsBackground = true }; | ||
threads[i].Start(); | ||
} | ||
} | ||
|
||
protected override void QueueTask(Task task) | ||
{ | ||
queue.Enqueue(task); | ||
isWorkAvailable.Release(1); | ||
} | ||
|
||
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) | ||
{ | ||
return TryExecuteTask(task); | ||
} | ||
|
||
public override int MaximumConcurrencyLevel => threadCount; | ||
|
||
protected override IEnumerable<Task> GetScheduledTasks() | ||
{ | ||
return queue; | ||
} | ||
|
||
private void WorkLoop() | ||
{ | ||
while (true) | ||
{ | ||
var task = BlockUntilTaskIsAvailable(); | ||
TryExecuteTask(task); | ||
} | ||
} | ||
|
||
private Task BlockUntilTaskIsAvailable() | ||
{ | ||
isWorkAvailable.Wait(); | ||
queue.TryDequeue(out var task); | ||
return task; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
for (int i = 0; i < threads.Length; i++) { | ||
threads[i].Join(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.