Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[*] rename enums #43

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PowerThreadPool/Constants/CanCreateNewWorker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PowerThreadPool.Constants
{
internal enum CanCreateNewWorker
{
Allowed = 0,
NotAllowed = 1,
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace PowerThreadPool.Constants
{
internal enum WorkerGettedFlags
internal enum CanGetWork
{
Free = 0,
Getted = 1,
Allowed = 0,
NotAllowed = 1,
ToBeDisabled = 2,
Disabled = -1,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace PowerThreadPool.Constants
{
internal enum PoolRunningFlags
internal enum PoolStates
{
NotRunning = 0,
IdleChecked = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace PowerThreadPool.Constants
{
internal enum WorkHeldFlags
internal enum WorkHeldStates
{
NotHeld = 0,
Held = 1
Expand Down
8 changes: 8 additions & 0 deletions PowerThreadPool/Constants/WorkStealability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PowerThreadPool.Constants
{
internal enum WorkStealability
{
Allowed = 0,
NotAllowed = 1,
}
}
8 changes: 0 additions & 8 deletions PowerThreadPool/Constants/WorkerCreationFlags.cs

This file was deleted.

8 changes: 0 additions & 8 deletions PowerThreadPool/Constants/WorkerStealingFlags.cs

This file was deleted.

4 changes: 2 additions & 2 deletions PowerThreadPool/Core/PowerThreadPool.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private bool CheckIfRequestedStopAndGetWork(ref WorkBase work)
/// </summary>
public void Wait()
{
if (_poolRunning == PoolRunningFlags.NotRunning)
if (_poolState == PoolStates.NotRunning)
{
return;
}
Expand Down Expand Up @@ -444,7 +444,7 @@ public Task<List<ExecuteResult<object>>> FetchAsync(IEnumerable<string> idList,
/// <returns>Return false if no thread running</returns>
public bool Stop(bool forceStop = false)
{
if (_poolRunning == PoolRunningFlags.NotRunning)
if (_poolState == PoolStates.NotRunning)
{
return false;
}
Expand Down
24 changes: 12 additions & 12 deletions PowerThreadPool/Core/PowerThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public partial class PowerPool : IDisposable

private bool _suspended;

private InterlockedFlag<WorkerCreationFlags> _createWorkerFlag = WorkerCreationFlags.Allow;
private InterlockedFlag<CanCreateNewWorker> _canCreateNewWorker = CanCreateNewWorker.Allowed;

private PowerPoolOption _powerPoolOption;
public PowerPoolOption PowerPoolOption
Expand All @@ -61,9 +61,9 @@ public PowerPoolOption PowerPoolOption

private System.Timers.Timer _poolTimer;

private InterlockedFlag<PoolRunningFlags> _poolRunning = PoolRunningFlags.NotRunning;
private InterlockedFlag<PoolStates> _poolState = PoolStates.NotRunning;

public bool PoolRunning => _poolRunning == PoolRunningFlags.Running;
public bool PoolRunning => _poolState == PoolStates.Running;

private bool _poolStopping = false;
public bool PoolStopping { get => _poolStopping; }
Expand Down Expand Up @@ -287,7 +287,7 @@ private Worker GetWorker(bool longRunning)
{
if (_idleWorkerDic.TryRemove(firstWorkerID, out worker))
{
SpinWait.SpinUntil(() => worker.GettedFlag.TrySet(WorkerGettedFlags.Getted, WorkerGettedFlags.Free));
SpinWait.SpinUntil(() => worker.CanGetWork.TrySet(CanGetWork.NotAllowed, CanGetWork.Allowed));
Interlocked.Decrement(ref _idleWorkerCount);
if (longRunning)
{
Expand All @@ -299,13 +299,13 @@ private Worker GetWorker(bool longRunning)

if (AliveWorkerCount < PowerPoolOption.MaxThreads + LongRunningWorkerCount)
{
if (_createWorkerFlag.TrySet(WorkerCreationFlags.Reject, WorkerCreationFlags.Allow))
if (_canCreateNewWorker.TrySet(CanCreateNewWorker.NotAllowed, CanCreateNewWorker.Allowed))
{
if (AliveWorkerCount < PowerPoolOption.MaxThreads + LongRunningWorkerCount)
{
worker = new Worker(this);

worker.GettedFlag.InterlockedValue = WorkerGettedFlags.Getted;
worker.CanGetWork.InterlockedValue = CanGetWork.NotAllowed;

if (_aliveWorkerDic.TryAdd(worker.ID, worker))
{
Expand All @@ -318,7 +318,7 @@ private Worker GetWorker(bool longRunning)
}
}

_createWorkerFlag.InterlockedValue = WorkerCreationFlags.Allow;
_canCreateNewWorker.InterlockedValue = CanCreateNewWorker.Allowed;
}
}

Expand All @@ -336,11 +336,11 @@ private Worker GetWorker(bool longRunning)
int waitingWorkCountTemp = aliveWorker.WaitingWorkCount;
if (waitingWorkCountTemp < min)
{
if (aliveWorker.GettedFlag.TrySet(WorkerGettedFlags.Getted, WorkerGettedFlags.Free))
if (aliveWorker.CanGetWork.TrySet(CanGetWork.NotAllowed, CanGetWork.Allowed))
{
if (worker != null)
{
worker.GettedFlag.TrySet(WorkerGettedFlags.Free, WorkerGettedFlags.Getted);
worker.CanGetWork.TrySet(CanGetWork.Allowed, CanGetWork.NotAllowed);
}

worker = aliveWorker;
Expand All @@ -363,7 +363,7 @@ private Worker GetWorker(bool longRunning)
/// </summary>
private void CheckPoolStart()
{
if (RunningWorkerCount == 0 && _poolRunning.TrySet(PoolRunningFlags.Running, PoolRunningFlags.NotRunning))
if (RunningWorkerCount == 0 && _poolState.TrySet(PoolStates.Running, PoolStates.NotRunning))
{
if (PoolStarted != null)
{
Expand Down Expand Up @@ -422,7 +422,7 @@ internal void CheckPoolIdle()

if (RunningWorkerCount == 0 &&
WaitingWorkCount == 0 &&
_poolRunning.TrySet(PoolRunningFlags.IdleChecked, PoolRunningFlags.Running)
_poolState.TrySet(PoolStates.IdleChecked, PoolStates.Running)
)
{
if (PoolIdled != null)
Expand Down Expand Up @@ -459,7 +459,7 @@ private void IdleSetting()
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();

_poolRunning.InterlockedValue = PoolRunningFlags.NotRunning;
_poolState.InterlockedValue = PoolStates.NotRunning;
if (_poolStopping)
{
_poolStopping = false;
Expand Down
40 changes: 20 additions & 20 deletions PowerThreadPool/Core/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ internal class Worker : IDisposable
internal int ID { get; set; }

internal InterlockedFlag<WorkerStates> WorkerState { get; set; } = WorkerStates.Idle;
internal InterlockedFlag<WorkerGettedFlags> GettedFlag { get; set; } = WorkerGettedFlags.Free;
internal InterlockedFlag<WorkHeldFlags> WorkHeld { get; set; } = WorkHeldFlags.NotHeld;
internal InterlockedFlag<WorkerStealingFlags> StealingFlag { get; set; } = WorkerStealingFlags.Allow;
internal InterlockedFlag<CanGetWork> CanGetWork { get; set; } = Constants.CanGetWork.Allowed;
internal InterlockedFlag<WorkHeldStates> WorkHeldState { get; set; } = WorkHeldStates.NotHeld;
internal InterlockedFlag<WorkStealability> WorkStealability { get; set; } = Constants.WorkStealability.Allowed;

private IStealablePriorityCollection<string> _waitingWorkIDPriorityCollection;
private ConcurrentDictionary<string, WorkBase> _waitingWorkDic = new ConcurrentDictionary<string, WorkBase>();
Expand Down Expand Up @@ -143,7 +143,7 @@ private void ExecuteWork(PowerPool powerPool)

private void ThreadInterrupted(PowerPool powerPool, ThreadInterruptedException ex)
{
GettedFlag.InterlockedValue = WorkerGettedFlags.Disabled;
CanGetWork.InterlockedValue = Constants.CanGetWork.Disabled;

WorkerStates origWorkState = WorkerState.InterlockedValue;
WorkerState.InterlockedValue = WorkerStates.ToBeDisposed;
Expand Down Expand Up @@ -240,7 +240,7 @@ private ExecuteResultBase ExecuteMain()
executeResult = Work.SetExecuteResult(_powerPool, null, ex, Status.Failed);
_powerPool.OnWorkErrorOccurred(ex, ErrorFrom.WorkLogic, executeResult);
}
SpinWait.SpinUntil(() => WorkHeld == WorkHeldFlags.NotHeld);
SpinWait.SpinUntil(() => WorkHeldState == WorkHeldStates.NotHeld);
Work.Worker = null;
executeResult.ID = Work.ID;

Expand Down Expand Up @@ -298,7 +298,7 @@ internal void SetWork(WorkBase work, bool resetted)

if (!resetted)
{
GettedFlag.InterlockedValue = WorkerGettedFlags.Free;
CanGetWork.InterlockedValue = Constants.CanGetWork.Allowed;
}

if (originalWorkerState == WorkerStates.Idle)
Expand Down Expand Up @@ -398,13 +398,13 @@ private void StealWorksFromOtherWorker(ref string waitingWorkID, ref WorkBase wo
int waitingWorkCountTemp = runningWorker.WaitingWorkCount;
if (waitingWorkCountTemp >= 2 && waitingWorkCountTemp > max)
{
if (!runningWorker.StealingFlag.TrySet(WorkerStealingFlags.Reject, WorkerStealingFlags.Allow))
if (!runningWorker.WorkStealability.TrySet(Constants.WorkStealability.NotAllowed, Constants.WorkStealability.Allowed))
{
continue;
}
if (worker != null)
{
worker.StealingFlag.InterlockedValue = WorkerStealingFlags.Allow;
worker.WorkStealability.InterlockedValue = Constants.WorkStealability.Allowed;
}
max = waitingWorkCountTemp;
worker = runningWorker;
Expand All @@ -418,7 +418,7 @@ private void StealWorksFromOtherWorker(ref string waitingWorkID, ref WorkBase wo
{
stolenWorkList = worker.Steal(count);
}
worker.StealingFlag.InterlockedValue = WorkerStealingFlags.Allow;
worker.WorkStealability.InterlockedValue = Constants.WorkStealability.Allowed;
if (stolenWorkList != null)
{
foreach (WorkBase stolenWork in stolenWorkList)
Expand All @@ -440,7 +440,7 @@ private void StealWorksFromOtherWorker(ref string waitingWorkID, ref WorkBase wo

private bool TurnToIdle(ref string waitingWorkID, ref WorkBase work)
{
SpinWait.SpinUntil(() => GettedFlag.TrySet(WorkerGettedFlags.ToBeDisabled, WorkerGettedFlags.Free));
SpinWait.SpinUntil(() => CanGetWork.TrySet(Constants.CanGetWork.ToBeDisabled, Constants.CanGetWork.Allowed));

waitingWorkID = _waitingWorkIDPriorityCollection.Get();
if (waitingWorkID != null)
Expand All @@ -450,7 +450,7 @@ private bool TurnToIdle(ref string waitingWorkID, ref WorkBase work)
Interlocked.Decrement(ref _waitingWorkCount);
}

GettedFlag.TrySet(WorkerGettedFlags.Free, WorkerGettedFlags.ToBeDisabled);
CanGetWork.TrySet(Constants.CanGetWork.Allowed, Constants.CanGetWork.ToBeDisabled);

return false;
}
Expand All @@ -469,7 +469,7 @@ private bool TurnToIdle(ref string waitingWorkID, ref WorkBase work)

WorkerState.InterlockedValue = WorkerStates.Idle;

GettedFlag.TrySet(WorkerGettedFlags.Free, WorkerGettedFlags.ToBeDisabled);
CanGetWork.TrySet(Constants.CanGetWork.Allowed, Constants.CanGetWork.ToBeDisabled);

if (_powerPool._idleWorkerDic.TryAdd(ID, this))
{
Expand Down Expand Up @@ -518,15 +518,15 @@ private void OnKillTimerElapsed(object s, ElapsedEventArgs e)
if (_powerPool.IdleWorkerCount > _powerPool.PowerPoolOption.DestroyThreadOption.MinThreads)
{
// ① There is a possibility that a worker may still obtain and execute work between the
// time the _killTimer triggers OnKillTimerElapsed and when GettedFlag is set to Disabled.
// time the _killTimer triggers OnKillTimerElapsed and when CanGetWork is set to Disabled.
SpinWait.SpinUntil(() =>
{
GettedFlag.TrySet(WorkerGettedFlags.Disabled, WorkerGettedFlags.Free, out WorkerGettedFlags origValue);
CanGetWork.TrySet(Constants.CanGetWork.Disabled, Constants.CanGetWork.Allowed, out CanGetWork origValue);
// If situation ① occurs and _killTimer.Stop() has not yet been executed, the current state
// of GettedFlag will be Disabled, although this is an extremely rare case.
// Therefore, SpinUntil will exit either when GettedFlag is successfully set from Free to Disabled,
// or if the current state of GettedFlag is already Disabled.
return origValue == WorkerGettedFlags.Free || origValue == WorkerGettedFlags.Disabled;
// of CanGetWork will be Disabled, although this is an extremely rare case.
// Therefore, SpinUntil will exit either when CanGetWork is successfully set from Allowed to Disabled,
// or if the current state of CanGetWork is already Disabled.
return origValue == Constants.CanGetWork.Allowed || origValue == Constants.CanGetWork.Disabled;
});

if (WorkerState.TrySet(WorkerStates.ToBeDisposed, WorkerStates.Idle))
Expand All @@ -543,10 +543,10 @@ private void OnKillTimerElapsed(object s, ElapsedEventArgs e)

// Reaching this point means that WorkerState was not set from Idle to ToBeDisposed,
// indicating that situation ① has occurred and that work is currently running.
// Therefore, reset the GettedFlag. This is also an extremely rare case,
// Therefore, reset the CanGetWork. This is also an extremely rare case,
// and it's almost impossible to reproduce this situation with code coverage testing,
// so code coverage testing for this line is ignored.
GettedFlag.TrySet(WorkerGettedFlags.Free, WorkerGettedFlags.Disabled);
CanGetWork.TrySet(Constants.CanGetWork.Allowed, Constants.CanGetWork.Disabled);
}
}

Expand Down
8 changes: 4 additions & 4 deletions PowerThreadPool/Helpers/WorkGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ private void Freeze()
SpinWait.SpinUntil(() => (_worker = _work.Worker) != null);

// Prevent the target work from being stolen by other workers using the work-stealing algorithm when it is stopped or canceled
SpinWait.SpinUntil(() => _worker.StealingFlag.TrySet(WorkerStealingFlags.Reject, WorkerStealingFlags.Allow));
SpinWait.SpinUntil(() => _worker.WorkStealability.TrySet(WorkStealability.NotAllowed, WorkStealability.Allowed));

// Temporarily prevent the executing work from allowing the worker to switch to the next work when the current work is completed
if (_isHoldWork)
{
SpinWait.SpinUntil(() => _worker.WorkHeld.TrySet(WorkHeldFlags.Held, WorkHeldFlags.NotHeld));
SpinWait.SpinUntil(() => _worker.WorkHeldState.TrySet(WorkHeldStates.Held, WorkHeldStates.NotHeld));
}
}
while (_work.Worker?.ID != _worker.ID);
Expand All @@ -49,11 +49,11 @@ private void UnFreeze()
{
if (_worker != null)
{
_worker.StealingFlag.InterlockedValue = WorkerStealingFlags.Allow;
_worker.WorkStealability.InterlockedValue = WorkStealability.Allowed;

if (_isHoldWork)
{
_worker.WorkHeld.InterlockedValue = WorkHeldFlags.NotHeld;
_worker.WorkHeldState.InterlockedValue = WorkHeldStates.NotHeld;
}
}
}
Expand Down
Loading
Loading