job: Add job_yield()

This moves block_job_yield() to the Job layer.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Kevin Wolf 2018-04-24 16:55:04 +02:00
parent b3b5299d58
commit 198c49cc8d
7 changed files with 28 additions and 31 deletions

View File

@ -444,7 +444,7 @@ static void coroutine_fn backup_run(void *opaque)
while (!job_is_cancelled(&job->common.job)) { while (!job_is_cancelled(&job->common.job)) {
/* Yield until the job is cancelled. We just let our before_write /* Yield until the job is cancelled. We just let our before_write
* notify callback service CoW requests. */ * notify callback service CoW requests. */
block_job_yield(&job->common); job_yield(&job->common.job);
} }
} else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) { } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
ret = backup_run_incremental(job); ret = backup_run_incremental(job);

View File

@ -731,7 +731,7 @@ static void coroutine_fn mirror_run(void *opaque)
block_job_event_ready(&s->common); block_job_event_ready(&s->common);
s->synced = true; s->synced = true;
while (!job_is_cancelled(&s->common.job) && !s->should_complete) { while (!job_is_cancelled(&s->common.job) && !s->should_complete) {
block_job_yield(&s->common); job_yield(&s->common.job);
} }
s->common.job.cancelled = false; s->common.job.cancelled = false;
goto immediate_exit; goto immediate_exit;

View File

@ -431,22 +431,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
return job; return job;
} }
void block_job_yield(BlockJob *job)
{
assert(job->job.busy);
/* Check cancellation *before* setting busy = false, too! */
if (job_is_cancelled(&job->job)) {
return;
}
if (!job_should_pause(&job->job)) {
job_do_yield(&job->job, -1);
}
job_pause_point(&job->job);
}
void block_job_iostatus_reset(BlockJob *job) void block_job_iostatus_reset(BlockJob *job)
{ {
if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {

View File

@ -107,14 +107,6 @@ void block_job_user_resume(Job *job);
*/ */
void block_job_drain(Job *job); void block_job_drain(Job *job);
/**
* block_job_yield:
* @job: The job that calls the function.
*
* Yield the block job coroutine.
*/
void block_job_yield(BlockJob *job);
/** /**
* block_job_ratelimit_get_delay: * block_job_ratelimit_get_delay:
* *

View File

@ -337,6 +337,13 @@ void job_enter(Job *job);
*/ */
void coroutine_fn job_pause_point(Job *job); void coroutine_fn job_pause_point(Job *job);
/**
* @job: The job that calls the function.
*
* Yield the job coroutine.
*/
void job_yield(Job *job);
/** /**
* @job: The job that calls the function. * @job: The job that calls the function.
* @ns: How many nanoseconds to stop for. * @ns: How many nanoseconds to stop for.
@ -508,8 +515,6 @@ int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp)
/* TODO To be removed from the public interface */ /* TODO To be removed from the public interface */
void job_state_transition(Job *job, JobStatus s1); void job_state_transition(Job *job, JobStatus s1);
void coroutine_fn job_do_yield(Job *job, uint64_t ns);
bool job_should_pause(Job *job);
void job_do_dismiss(Job *job); void job_do_dismiss(Job *job);
#endif #endif

20
job.c
View File

@ -226,7 +226,7 @@ static bool job_started(Job *job)
return job->co; return job->co;
} }
bool job_should_pause(Job *job) static bool job_should_pause(Job *job)
{ {
return job->pause_count > 0; return job->pause_count > 0;
} }
@ -396,7 +396,7 @@ void job_enter(Job *job)
* *
* If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be
* called explicitly. */ * called explicitly. */
void coroutine_fn job_do_yield(Job *job, uint64_t ns) static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
{ {
job_lock(); job_lock();
if (ns != -1) { if (ns != -1) {
@ -441,6 +441,22 @@ void coroutine_fn job_pause_point(Job *job)
} }
} }
void job_yield(Job *job)
{
assert(job->busy);
/* Check cancellation *before* setting busy = false, too! */
if (job_is_cancelled(job)) {
return;
}
if (!job_should_pause(job)) {
job_do_yield(job, -1);
}
job_pause_point(job);
}
void coroutine_fn job_sleep_ns(Job *job, int64_t ns) void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
{ {
assert(job->busy); assert(job->busy);

View File

@ -47,7 +47,7 @@ static void coroutine_fn test_block_job_run(void *opaque)
if (s->use_timer) { if (s->use_timer) {
job_sleep_ns(&job->job, 0); job_sleep_ns(&job->job, 0);
} else { } else {
block_job_yield(job); job_yield(&job->job);
} }
if (job_is_cancelled(&job->job)) { if (job_is_cancelled(&job->job)) {