Skip to content

Commit

Permalink
Simplify ThreadPool.cancelJob().
Browse files Browse the repository at this point in the history
Offering four options makes the underlying code more complicated for minimal benefit.
  • Loading branch information
player-03 authored Aug 24, 2024
1 parent 0b93684 commit 96c5c1c
Showing 1 changed file with 13 additions and 89 deletions.
102 changes: 13 additions & 89 deletions src/lime/system/ThreadPool.hx
Original file line number Diff line number Diff line change
Expand Up @@ -301,30 +301,21 @@ class ThreadPool extends WorkOutput

/**
Cancels one active or queued job. Does not dispatch an error event.
@param job A `JobData` object, or a job's unique `id`, `state`, or
`doWork` function.
@return Whether a job was canceled.
**/
public function cancelJob(job:JobIdentifier):Bool
public function cancelJob(jobID:Int):Bool
{
var data:JobData = __activeJobs.get(job);

if (data != null)
#if lime_threads
var thread:Thread = __activeThreads[jobID];
if (thread != null)
{
#if lime_threads
var thread:Thread = __activeThreads[data.id];
if (thread != null)
{
thread.sendMessage({event: CANCEL});
__activeThreads.remove(data.id);
__idleThreads.push(thread);
}
#end

return __activeJobs.remove(data);
thread.sendMessage({event: CANCEL});
__activeThreads.remove(jobID);
__idleThreads.push(thread);
}
#end

return __jobQueue.remove(__jobQueue.get(job));
return __activeJobs.remove(__activeJobs.get(jobID)) || __jobQueue.remove(__jobQueue.get(jobID));
}

/**
Expand Down Expand Up @@ -551,7 +542,7 @@ class ThreadPool extends WorkOutput
{
if (threadEvent.jobID != null)
{
activeJob = __activeJobs.getByID(threadEvent.jobID);
activeJob = __activeJobs.get(threadEvent.jobID);
}
else
{
Expand Down Expand Up @@ -750,7 +741,7 @@ class JobList

public inline function exists(job:JobData):Bool
{
return getByID(job.id) != null;
return get(job.id) != null;
}

public inline function hasNext():Bool
Expand Down Expand Up @@ -798,7 +789,7 @@ class JobList

public inline function removeByID(id:Int):Bool
{
if (__jobs.remove(getByID(id)))
if (__jobs.remove(get(id)))
{
__addingWorkPriority = length > 0;
return true;
Expand All @@ -809,7 +800,7 @@ class JobList
}
}

public function getByID(id:Int):JobData
public function get(id:Int):JobData
{
for (job in __jobs)
{
Expand All @@ -820,33 +811,6 @@ class JobList
}
return null;
}

public function get(jobIdentifier:JobIdentifier):JobData
{
switch (jobIdentifier)
{
case ID(id):
return getByID(id);
case FUNCTION(doWork):
for (job in __jobs)
{
if (job.doWork == doWork)
{
return job;
}
}
case STATE(state):
for (job in __jobs)
{
if (job.state == state)
{
return job;
}
}
}
return null;
}

public inline function push(job:JobData):Void
{
__jobs.push(job);
Expand Down Expand Up @@ -880,43 +844,3 @@ class JobList
return __jobs.length;
}
}

/**
A piece of data that uniquely represents a job. This can be the integer ID
(and integers will be assumed to be such), the `doWork` function, or the
`JobData` object itself. Failing any of those, a value will be assumed to be
the job's `state`.
Caution: if the provided data isn't unique, such as a `doWork` function
that's in use by multiple jobs, the wrong job may be selected or canceled.
**/
@:forward
abstract JobIdentifier(JobIdentifierImpl) from JobIdentifierImpl
{
@:from private static inline function fromJob(job:JobData):JobIdentifier
{
return ID(job.id);
}

@:from private static inline function fromID(id:Int):JobIdentifier
{
return ID(id);
}

@:from private static inline function fromFunction(doWork:WorkFunction<State->WorkOutput->Void>):JobIdentifier
{
return FUNCTION(doWork);
}

@:from private static inline function fromState(state:State):JobIdentifier
{
return STATE(state);
}
}

private enum JobIdentifierImpl
{
ID(id:Int);
FUNCTION(doWork:WorkFunction<State->WorkOutput->Void>);
STATE(state:State);
}

0 comments on commit 96c5c1c

Please sign in to comment.