Repository navigation
Expand file tree
/
Copy pathThreadPoolClass.cs
More file actions
121 lines (107 loc) · 4.22 KB
/
Copy pathThreadPoolClass.cs
File metadata and controls
121 lines (107 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Diagnostics.CodeAnalysis;
namespace BadThreadPool
{
public class ThreadPoolClass
{
// An array of threads
PooledThreadClass[] _pooledThreadClasses;
/// <summary>
/// Number of threads currently in use
/// </summary>
public byte Threads;
/// <summary>
/// Sets the threadpool up to be used
/// </summary>
/// <param name="threads"> Threads to use in threadpool leave at 0 if unsure </param>
public void InitializePool(byte threads = 0)
{
if (threads == 0)
{
Threads = (byte)Environment.ProcessorCount;
}
else
{
Threads = threads;
}
SetMaxThreadCount(Threads);
}
/// <summary>
/// Sets the max thread count after intialization
/// </summary>
/// <param name="threadCount">Threads to allocate</param>
public void SetMaxThreadCount(byte threadCount)
{
_pooledThreadClasses = new PooledThreadClass[threadCount];
for (int i = 0; i < Threads; i++)
{
_pooledThreadClasses[i] = new PooledThreadClass();
}
}
/// <summary>
/// Add request to task Queue, that way it can be run (with delegate to run after!).
/// </summary>
/// <param name="method"> Method you want to call, format in lambda AKA like: () => {method in question} </param>
/// <param name="callback">What to call after the call has been completed</param>
/// <returns>Returns A task that will contain the value returned, a task can also in theory be re-added in the future</returns>
public ThreadTaskRequest AddRequest(Func<object> method, Action<CallbackArgs<object>> callback = null)
{
if (method == null) throw new ArgumentNullException(nameof(method));
ThreadTaskRequest taskClass = new ThreadTaskRequest(method, callback);
PooledThreadClass lowestTaskThread = null;
int lowestTaskNumber = int.MaxValue;
foreach (PooledThreadClass threadClass in _pooledThreadClasses)
{
if (lowestTaskNumber > threadClass.TasksAssigned.Count)
{
lowestTaskNumber = threadClass.TasksAssigned.Count;
lowestTaskThread = threadClass;
}
}
if (lowestTaskThread?.TaskAccessLock != null)
{
lock (lowestTaskThread?.TaskAccessLock)
{
lowestTaskThread?.TasksAssigned.Add(taskClass);
}
}
return taskClass;
}
/// <summary>
/// Starts the threadpool, the name scheme is set up like this because the threadpool acts like a fire of sorts, you stoke it with fuel (Tasks) then you let it burn until all the "fuel" is gone, then it stops, will make an autoignite feature later
/// </summary>
public void IgniteThreadPool()
{
foreach (PooledThreadClass thread in _pooledThreadClasses)
{
thread.PrepareThread();
}
}
/// <summary>
/// Currently Non-functional
/// </summary>
public void ShutDownHandler()
{
foreach (PooledThreadClass pooledThreadClass in _pooledThreadClasses)
{
pooledThreadClass.DestroyThread();
}
}
/// <summary>
/// Unsure of this works, this might crash the program, that being said is supposed to signal true if the threadpool is completely idle
/// </summary>
/// <returns> true if the threadpool is idle, false if it is not</returns>
[SuppressMessage("ReSharper", "LoopCanBeConvertedToQuery")]
public bool AllThreadsIdle()
{
foreach (PooledThreadClass thread in _pooledThreadClasses)
{
if (thread.BIsIdle == false && thread.TasksAssigned.Count != 0 && !thread.PendingShutdown)
{
return false;
}
}
return true;
}
}
}