processor: Simplified the main loop
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2011 Martin Willi
|
||||
* Copyright (C) 2011 revosec AG
|
||||
* Copyright (C) 2008-2012 Tobias Brunner
|
||||
* Copyright (C) 2008-2013 Tobias Brunner
|
||||
* Copyright (C) 2005 Jan Hutter
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
@@ -180,55 +180,53 @@ static u_int get_idle_threads_nolock(private_processor_t *this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Process queued jobs, called by the worker threads
|
||||
* Get a job from any job queue, starting with the highest priority.
|
||||
*
|
||||
* this->mutex is expected to be locked.
|
||||
*/
|
||||
static void process_jobs(worker_thread_t *worker)
|
||||
static bool get_job(private_processor_t *this, worker_thread_t *worker)
|
||||
{
|
||||
private_processor_t *this = worker->processor;
|
||||
int i, reserved = 0, idle;
|
||||
|
||||
/* worker threads are not cancelable by default */
|
||||
thread_cancelability(FALSE);
|
||||
|
||||
DBG2(DBG_JOB, "started worker thread %.2u", thread_current_id());
|
||||
|
||||
this->mutex->lock(this->mutex);
|
||||
while (TRUE)
|
||||
{
|
||||
int i, reserved, idle;
|
||||
|
||||
recheck_queues:
|
||||
if (this->desired_threads < this->total_threads)
|
||||
{
|
||||
break;
|
||||
}
|
||||
idle = get_idle_threads_nolock(this);
|
||||
reserved = 0;
|
||||
|
||||
for (i = 0; i < JOB_PRIO_MAX; i++)
|
||||
{
|
||||
job_t *to_destroy = NULL;
|
||||
job_requeue_t requeue;
|
||||
|
||||
if (reserved && reserved >= idle)
|
||||
{
|
||||
DBG2(DBG_JOB, "delaying %N priority jobs: %d threads idle, "
|
||||
"but %d reserved for higher priorities",
|
||||
job_priority_names, i, idle, reserved);
|
||||
/* go and wait until a job of higher priority gets queued */
|
||||
break;
|
||||
/* wait until a job of higher priority gets queued */
|
||||
return FALSE;
|
||||
}
|
||||
if (this->working_threads[i] < this->prio_threads[i])
|
||||
{
|
||||
reserved += this->prio_threads[i] - this->working_threads[i];
|
||||
}
|
||||
if (this->jobs[i]->remove_first(this->jobs[i],
|
||||
(void**)&worker->job) != SUCCESS)
|
||||
{ /* check next priority queue for a job */
|
||||
continue;
|
||||
}
|
||||
this->working_threads[i]++;
|
||||
worker->job->status = JOB_STATUS_EXECUTING;
|
||||
(void**)&worker->job) == SUCCESS)
|
||||
{
|
||||
worker->priority = i;
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a single job (provided in worker->job, worker->priority is also
|
||||
* expected to be set)
|
||||
*
|
||||
* this->mutex is expected to be locked.
|
||||
*/
|
||||
static void process_job(private_processor_t *this, worker_thread_t *worker)
|
||||
{
|
||||
job_t *to_destroy = NULL;
|
||||
job_requeue_t requeue;
|
||||
|
||||
this->working_threads[worker->priority]++;
|
||||
worker->job->status = JOB_STATUS_EXECUTING;
|
||||
this->mutex->unlock(this->mutex);
|
||||
/* canceled threads are restarted to get a constant pool */
|
||||
thread_cleanup_push((thread_cleanup_t)restart, worker);
|
||||
@@ -247,7 +245,7 @@ recheck_queues:
|
||||
}
|
||||
thread_cleanup_pop(FALSE);
|
||||
this->mutex->lock(this->mutex);
|
||||
this->working_threads[i]--;
|
||||
this->working_threads[worker->priority]--;
|
||||
if (worker->job->status == JOB_STATUS_CANCELED)
|
||||
{ /* job was canceled via a custom cancel() method or did not
|
||||
* use JOB_REQUEUE_TYPE_DIRECT */
|
||||
@@ -263,8 +261,8 @@ recheck_queues:
|
||||
break;
|
||||
case JOB_REQUEUE_TYPE_FAIR:
|
||||
worker->job->status = JOB_STATUS_QUEUED;
|
||||
this->jobs[i]->insert_last(this->jobs[i],
|
||||
worker->job);
|
||||
this->jobs[worker->priority]->insert_last(
|
||||
this->jobs[worker->priority], worker->job);
|
||||
this->job_added->signal(this->job_added);
|
||||
break;
|
||||
case JOB_REQUEUE_TYPE_SCHEDULE:
|
||||
@@ -301,12 +299,32 @@ recheck_queues:
|
||||
to_destroy->destroy(to_destroy);
|
||||
this->mutex->lock(this->mutex);
|
||||
}
|
||||
/* check the priority queues for another job from the beginning */
|
||||
goto recheck_queues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process queued jobs, called by the worker threads
|
||||
*/
|
||||
static void process_jobs(worker_thread_t *worker)
|
||||
{
|
||||
private_processor_t *this = worker->processor;
|
||||
|
||||
/* worker threads are not cancelable by default */
|
||||
thread_cancelability(FALSE);
|
||||
|
||||
DBG2(DBG_JOB, "started worker thread %.2u", thread_current_id());
|
||||
|
||||
this->mutex->lock(this->mutex);
|
||||
while (this->desired_threads >= this->total_threads)
|
||||
{
|
||||
if (get_job(this, worker))
|
||||
{
|
||||
process_job(this, worker);
|
||||
}
|
||||
/* wait until a job gets queued */
|
||||
else
|
||||
{
|
||||
this->job_added->wait(this->job_added, this->mutex);
|
||||
}
|
||||
}
|
||||
this->total_threads--;
|
||||
this->thread_terminated->signal(this->thread_terminated);
|
||||
this->mutex->unlock(this->mutex);
|
||||
|
||||
Reference in New Issue
Block a user