Skip to content

Commit eec44f8

Browse files
committed
Added docs.
1 parent c9ebdda commit eec44f8

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

JobScheduler/JobHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal JobHandle RentJobHandle(IJob iJob)
6161
throw new("Job cannot be null");
6262
}
6363

64-
_freeIds.GetHandle(out var index);
64+
_freeIds.RentHandle(out var index);
6565

6666
if (index == null)
6767
{

JobScheduler/Utils/IParallelJobProducer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,21 @@ public void Execute()
122122
}
123123
}
124124

125+
/// <summary>
126+
/// Calculates the amount of children this job will split into.
127+
/// </summary>
128+
/// <returns>The amount.</returns>
125129
private int CalculateChildrenToSplitInto()
126130
{
127131
const int ChildrenToSplitInto = 128; //This should be equal to the number of threads(or that times 2/3?) but for now it's just a constant
128132
var range = _to - _from;
129133
return range < ChildrenToSplitInto ? range : ChildrenToSplitInto;
130134
}
131135

136+
/// <summary>
137+
/// Checks and splits this instance into more instances.
138+
/// </summary>
139+
/// <returns>If a split occured</returns>
132140
private bool CheckAndSplit()
133141
{
134142
if (!_shouldSplitWhenAvailable || CalculateChildrenToSplitInto() <= 1)
@@ -141,6 +149,10 @@ private bool CheckAndSplit()
141149
return true;
142150
}
143151

152+
/// <summary>
153+
/// Splits this instance into sub instances.
154+
/// </summary>
155+
/// <exception cref="Exception">An exception occuring when an invalid range was passed.</exception>
144156
private void Split()
145157
{
146158
var childrenToSplitInto = CalculateChildrenToSplitInto();
Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
namespace Schedulers.Utils;
22

3+
/// <summary>
4+
/// This <see cref="JobHandlePool"/> class
5+
/// acts as a pool for <see cref="JobHandle"/> ids.
6+
/// </summary>
37
internal class JobHandlePool
48
{
5-
public readonly ushort[] _handles;
9+
private readonly ushort[] _handles;
610
private readonly bool[] _isFree;
711
private readonly Queue<ushort> _freeHandles;
812
private int _returnedHandles;
913

14+
/// <summary>
15+
/// Creates a new instance.
16+
/// </summary>
17+
/// <param name="size">Its initial size.</param>
1018
public JobHandlePool(int size)
1119
{
1220
_freeHandles = new(size);
@@ -21,36 +29,19 @@ public JobHandlePool(int size)
2129
}
2230
}
2331

24-
private void CleanupHandles()
25-
{
26-
// Prevent cleanup if no handles were returned
27-
// This is a guard to prevent cleanup every time someone tries to get a handle, in a tight loop
28-
if (_returnedHandles == 0)
29-
{
30-
return;
31-
}
32-
33-
_freeHandles.Clear();
34-
for (ushort i = 0; i < _isFree.Length; i++)
35-
{
36-
if (_isFree[i])
37-
{
38-
_freeHandles.Enqueue(i);
39-
}
40-
}
41-
42-
_returnedHandles = 0;
43-
}
44-
45-
// Not intrinsically thread safe so we lock
46-
// Assumption is that the user won't generate handles on other threads
47-
internal bool GetHandle(out ushort? handle)
32+
/// <summary>
33+
/// Rents a new handle.
34+
/// </summary>
35+
/// <param name="handle">The rented handle.</param>
36+
/// <remarks>Not intrinsically thread safe so we lock. Assumption is that the user won't generate handles on other threads</remarks>
37+
/// <returns>True or false.</returns>
38+
internal bool RentHandle(out ushort? handle)
4839
{
4940
lock (this)
5041
{
5142
if (_freeHandles.Count == 0)
5243
{
53-
CleanupHandles();
44+
Clear();
5445
}
5546

5647
if (_freeHandles.Count == 0)
@@ -66,10 +57,39 @@ internal bool GetHandle(out ushort? handle)
6657
}
6758
}
6859

69-
//Thread safe
60+
/// <summary>
61+
/// Returns a rented handle.
62+
/// </summary>
63+
/// <param name="handle">The initial rented handle.</param>
64+
/// <remarks>Thread safe.</remarks>
7065
internal void ReturnHandle(JobHandle handle)
7166
{
7267
_isFree[handle.Index] = true;
7368
_returnedHandles++; // We don't care about thread safety here
7469
}
70+
71+
72+
/// <summary>
73+
/// Clears this instance.
74+
/// </summary>
75+
private void Clear()
76+
{
77+
// Prevent cleanup if no handles were returned
78+
// This is a guard to prevent cleanup every time someone tries to get a handle, in a tight loop
79+
if (_returnedHandles == 0)
80+
{
81+
return;
82+
}
83+
84+
_freeHandles.Clear();
85+
for (ushort i = 0; i < _isFree.Length; i++)
86+
{
87+
if (_isFree[i])
88+
{
89+
_freeHandles.Enqueue(i);
90+
}
91+
}
92+
93+
_returnedHandles = 0;
94+
}
7595
}

0 commit comments

Comments
 (0)