Make rescheduling a job more predictable

This avoids race conditions between calls to cancel() and jobs that like
to be rescheduled.  If jobs were able to reschedule themselves it would
theoretically be possible that two worker threads have the same job
assigned (the one currently executing the job and the one executing the
same but rescheduled job if it already is time to execute it), this means
that cancel() could be called twice for that job.

Creating a new job based on the current one and reschedule that is also
OK, but rescheduling itself is more efficient for jobs that need to be
executed often.
This commit is contained in:
Tobias Brunner
2012-06-25 17:49:12 +02:00
parent 26d77eb3e6
commit e0efd7c121
4 changed files with 90 additions and 49 deletions
@@ -55,7 +55,7 @@ METHOD(job_t, execute, job_requeue_t,
private_inactivity_job_t *this)
{
ike_sa_t *ike_sa;
bool rescheduled = FALSE;
u_int32_t reschedule = 0;
ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
this->reqid, TRUE);
@@ -87,9 +87,7 @@ METHOD(job_t, execute, job_requeue_t,
}
else
{
lib->scheduler->schedule_job(lib->scheduler,
&this->public.job_interface, this->timeout - diff);
rescheduled = TRUE;
reschedule = this->timeout - diff;
}
}
children++;
@@ -121,9 +119,9 @@ METHOD(job_t, execute, job_requeue_t,
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}
}
if (rescheduled)
if (reschedule)
{
return JOB_REQUEUE_SCHEDULED;
return JOB_RESCHEDULE(reschedule);
}
return JOB_REQUEUE_NONE;
}