Skip to content

Commit 153b2fe

Browse files
committed
Allow clearing CPU selection without selection payload
1 parent 2560885 commit 153b2fe

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

Platforms/Windows/IProcessCpuSetHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public interface IProcessCpuSetHandler : IDisposable
4848
/// <summary>
4949
/// Applies a topology-aware CPU selection to the process using CPU Sets.
5050
/// </summary>
51-
/// <param name="selection">The CPU selection to apply.</param>
51+
/// <param name="selection">The CPU selection to apply. Ignored and allowed to be null when <paramref name="clearSelection"/> is true.</param>
5252
/// <param name="clearSelection">If true, clears the CPU Set selection and ignores <paramref name="selection"/>.</param>
5353
/// <returns>True if the operation succeeded, false otherwise.</returns>
54-
bool ApplyCpuSelection(CpuSelection selection, bool clearSelection = false);
54+
bool ApplyCpuSelection(CpuSelection? selection, bool clearSelection = false);
5555

5656
/// <summary>
5757
/// Gets the average CPU usage for this process.

Platforms/Windows/ProcessCpuSetHandler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,8 @@ public bool ApplyCpuSetMask(long affinityMask, bool clearMask = false)
231231
return false;
232232
}
233233

234-
public bool ApplyCpuSelection(CpuSelection selection, bool clearSelection = false)
234+
public bool ApplyCpuSelection(CpuSelection? selection, bool clearSelection = false)
235235
{
236-
ArgumentNullException.ThrowIfNull(selection);
237-
238236
if (this.disposed)
239237
{
240238
throw new ObjectDisposedException(nameof(ProcessCpuSetHandler));
@@ -250,6 +248,8 @@ public bool ApplyCpuSelection(CpuSelection selection, bool clearSelection = fals
250248
return this.ApplyCpuSetIds(null, 0, "clear CPU Set selection");
251249
}
252250

251+
ArgumentNullException.ThrowIfNull(selection);
252+
253253
var cpuSetIds = this.cpuSetMapping.ResolveCpuSetIds(selection);
254254
if (cpuSetIds.Count == 0)
255255
{
@@ -393,4 +393,3 @@ private class CpuTimeTimestamp
393393
}
394394
}
395395
}
396-

Tests/ThreadPilot.Core.Tests/ProcessCpuSetHandlerTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ public void ProcessCpuSetHandler_ApplyCpuSelection_WithClearSelection_ClearsCpuS
116116
Assert.Equal(0U, nativeApi.LastAppliedCpuSetCount);
117117
}
118118

119+
[Fact]
120+
public void ProcessCpuSetHandler_ApplyCpuSelection_WithClearSelectionAndNullSelection_ClearsCpuSets()
121+
{
122+
var nativeApi = new FakeProcessCpuSetNativeApi();
123+
using var handler = CreateHandler(nativeApi, CpuSetMapping.Empty);
124+
125+
var result = handler.ApplyCpuSelection(null!, clearSelection: true);
126+
127+
Assert.True(result);
128+
Assert.Null(nativeApi.LastAppliedCpuSetIds);
129+
Assert.Equal(0U, nativeApi.LastAppliedCpuSetCount);
130+
}
131+
132+
[Fact]
133+
public void ProcessCpuSetHandler_ApplyCpuSelection_WithNullSelectionAndClearFalse_ThrowsArgumentNullException()
134+
{
135+
var nativeApi = new FakeProcessCpuSetNativeApi();
136+
using var handler = CreateHandler(nativeApi, CpuSetMapping.Empty);
137+
138+
Assert.Throws<ArgumentNullException>(() =>
139+
handler.ApplyCpuSelection(null!, clearSelection: false));
140+
}
141+
119142
[Fact]
120143
public void ProcessCpuSetHandler_ApplyCpuSelection_WithExplicitCpuSetIds_AppliesThoseIds()
121144
{

0 commit comments

Comments
 (0)