Skip to content

Commit

Permalink
jobs: change start callback to run callback
Browse files Browse the repository at this point in the history
Presently we codify the entry point for a job as the "start" callback,
but a more apt name would be "run" to clarify the idea that when this
function returns we consider the job to have "finished," except for
any cleanup which occurs in separate callbacks later.

As part of this clarification, change the signature to include an error
object and a return code. The error ptr is not yet used, and the return
code while captured, will be overwritten by actions in the job_completed
function.

Signed-off-by: John Snow <[email protected]>
Reviewed-by: Max Reitz <[email protected]>
Message-id: [email protected]
Reviewed-by: Jeff Cody <[email protected]>
Signed-off-by: Max Reitz <[email protected]>
  • Loading branch information
jnsnow authored and XanClic committed Aug 31, 2018
1 parent 7b43db3 commit f67432a
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 34 deletions.
7 changes: 4 additions & 3 deletions block/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
bdrv_dirty_iter_free(dbi);
}

static void coroutine_fn backup_run(void *opaque)
static int coroutine_fn backup_run(Job *opaque_job, Error **errp)
{
BackupBlockJob *job = opaque;
BackupBlockJob *job = container_of(opaque_job, BackupBlockJob, common.job);
BackupCompleteData *data;
BlockDriverState *bs = blk_bs(job->common.blk);
int64_t offset, nb_clusters;
Expand Down Expand Up @@ -587,6 +587,7 @@ static void coroutine_fn backup_run(void *opaque)
data = g_malloc(sizeof(*data));
data->ret = ret;
job_defer_to_main_loop(&job->common.job, backup_complete, data);
return ret;
}

static const BlockJobDriver backup_job_driver = {
Expand All @@ -596,7 +597,7 @@ static const BlockJobDriver backup_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = backup_run,
.run = backup_run,
.commit = backup_commit,
.abort = backup_abort,
.clean = backup_clean,
Expand Down
7 changes: 4 additions & 3 deletions block/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ static void commit_complete(Job *job, void *opaque)
bdrv_unref(top);
}

static void coroutine_fn commit_run(void *opaque)
static int coroutine_fn commit_run(Job *job, Error **errp)
{
CommitBlockJob *s = opaque;
CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
CommitCompleteData *data;
int64_t offset;
uint64_t delay_ns = 0;
Expand Down Expand Up @@ -213,6 +213,7 @@ static void coroutine_fn commit_run(void *opaque)
data = g_malloc(sizeof(*data));
data->ret = ret;
job_defer_to_main_loop(&s->common.job, commit_complete, data);
return ret;
}

static const BlockJobDriver commit_job_driver = {
Expand All @@ -222,7 +223,7 @@ static const BlockJobDriver commit_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = commit_run,
.run = commit_run,
},
};

Expand Down
8 changes: 5 additions & 3 deletions block/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ static void blockdev_create_complete(Job *job, void *opaque)
job_completed(job, s->ret, s->err);
}

static void coroutine_fn blockdev_create_run(void *opaque)
static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
{
BlockdevCreateJob *s = opaque;
BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);

job_progress_set_remaining(&s->common, 1);
s->ret = s->drv->bdrv_co_create(s->opts, &s->err);
job_progress_update(&s->common, 1);

qapi_free_BlockdevCreateOptions(s->opts);
job_defer_to_main_loop(&s->common, blockdev_create_complete, NULL);

return s->ret;
}

static const JobDriver blockdev_create_job_driver = {
.instance_size = sizeof(BlockdevCreateJob),
.job_type = JOB_TYPE_CREATE,
.start = blockdev_create_run,
.run = blockdev_create_run,
};

void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
Expand Down
10 changes: 6 additions & 4 deletions block/mirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,9 @@ static int mirror_flush(MirrorBlockJob *s)
return ret;
}

static void coroutine_fn mirror_run(void *opaque)
static int coroutine_fn mirror_run(Job *job, Error **errp)
{
MirrorBlockJob *s = opaque;
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
MirrorExitData *data;
BlockDriverState *bs = s->mirror_top_bs->backing->bs;
BlockDriverState *target_bs = blk_bs(s->target);
Expand Down Expand Up @@ -1041,7 +1041,9 @@ static void coroutine_fn mirror_run(void *opaque)
if (need_drain) {
bdrv_drained_begin(bs);
}

job_defer_to_main_loop(&s->common.job, mirror_exit, data);
return ret;
}

static void mirror_complete(Job *job, Error **errp)
Expand Down Expand Up @@ -1138,7 +1140,7 @@ static const BlockJobDriver mirror_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = mirror_run,
.run = mirror_run,
.pause = mirror_pause,
.complete = mirror_complete,
},
Expand All @@ -1154,7 +1156,7 @@ static const BlockJobDriver commit_active_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = mirror_run,
.run = mirror_run,
.pause = mirror_pause,
.complete = mirror_complete,
},
Expand Down
7 changes: 4 additions & 3 deletions block/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static void stream_complete(Job *job, void *opaque)
g_free(data);
}

static void coroutine_fn stream_run(void *opaque)
static int coroutine_fn stream_run(Job *job, Error **errp)
{
StreamBlockJob *s = opaque;
StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
StreamCompleteData *data;
BlockBackend *blk = s->common.blk;
BlockDriverState *bs = blk_bs(blk);
Expand Down Expand Up @@ -206,14 +206,15 @@ static void coroutine_fn stream_run(void *opaque)
data = g_malloc(sizeof(*data));
data->ret = ret;
job_defer_to_main_loop(&s->common.job, stream_complete, data);
return ret;
}

static const BlockJobDriver stream_job_driver = {
.job_driver = {
.instance_size = sizeof(StreamBlockJob),
.job_type = JOB_TYPE_STREAM,
.free = block_job_free,
.start = stream_run,
.run = stream_run,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
},
Expand Down
2 changes: 1 addition & 1 deletion include/qemu/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct JobDriver {
JobType job_type;

/** Mandatory: Entrypoint for the Coroutine. */
CoroutineEntry *start;
int coroutine_fn (*run)(Job *job, Error **errp);

/**
* If the callback is not NULL, it will be invoked when the job transitions
Expand Down
6 changes: 3 additions & 3 deletions job.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,16 @@ static void coroutine_fn job_co_entry(void *opaque)
{
Job *job = opaque;

assert(job && job->driver && job->driver->start);
assert(job && job->driver && job->driver->run);
job_pause_point(job);
job->driver->start(job);
job->ret = job->driver->run(job, NULL);
}


void job_start(Job *job)
{
assert(job && !job_started(job) && job->paused &&
job->driver && job->driver->start);
job->driver && job->driver->run);
job->co = qemu_coroutine_create(job_co_entry, job);
job->pause_count--;
job->busy = true;
Expand Down
7 changes: 4 additions & 3 deletions tests/test-bdrv-drain.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,9 @@ static void test_job_completed(Job *job, void *opaque)
job_completed(job, 0, NULL);
}

static void coroutine_fn test_job_start(void *opaque)
static int coroutine_fn test_job_run(Job *job, Error **errp)
{
TestBlockJob *s = opaque;
TestBlockJob *s = container_of(job, TestBlockJob, common.job);

job_transition_to_ready(&s->common.job);
while (!s->should_complete) {
Expand All @@ -771,6 +771,7 @@ static void coroutine_fn test_job_start(void *opaque)
}

job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
return 0;
}

static void test_job_complete(Job *job, Error **errp)
Expand All @@ -785,7 +786,7 @@ BlockJobDriver test_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = test_job_start,
.run = test_job_run,
.complete = test_job_complete,
},
};
Expand Down
16 changes: 8 additions & 8 deletions tests/test-blockjob-txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ static void test_block_job_complete(Job *job, void *opaque)
bdrv_unref(bs);
}

static void coroutine_fn test_block_job_run(void *opaque)
static int coroutine_fn test_block_job_run(Job *job, Error **errp)
{
TestBlockJob *s = opaque;
BlockJob *job = &s->common;
TestBlockJob *s = container_of(job, TestBlockJob, common.job);

while (s->iterations--) {
if (s->use_timer) {
job_sleep_ns(&job->job, 0);
job_sleep_ns(job, 0);
} else {
job_yield(&job->job);
job_yield(job);
}

if (job_is_cancelled(&job->job)) {
if (job_is_cancelled(job)) {
break;
}
}

job_defer_to_main_loop(&job->job, test_block_job_complete,
job_defer_to_main_loop(job, test_block_job_complete,
(void *)(intptr_t)s->rc);
return s->rc;
}

typedef struct {
Expand All @@ -80,7 +80,7 @@ static const BlockJobDriver test_block_job_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = test_block_job_run,
.run = test_block_job_run,
},
};

Expand Down
7 changes: 4 additions & 3 deletions tests/test-blockjob.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ static void cancel_job_complete(Job *job, Error **errp)
s->should_complete = true;
}

static void coroutine_fn cancel_job_start(void *opaque)
static int coroutine_fn cancel_job_run(Job *job, Error **errp)
{
CancelJob *s = opaque;
CancelJob *s = container_of(job, CancelJob, common.job);

while (!s->should_complete) {
if (job_is_cancelled(&s->common.job)) {
Expand All @@ -194,6 +194,7 @@ static void coroutine_fn cancel_job_start(void *opaque)

defer:
job_defer_to_main_loop(&s->common.job, cancel_job_completed, s);
return 0;
}

static const BlockJobDriver test_cancel_driver = {
Expand All @@ -202,7 +203,7 @@ static const BlockJobDriver test_cancel_driver = {
.free = block_job_free,
.user_resume = block_job_user_resume,
.drain = block_job_drain,
.start = cancel_job_start,
.run = cancel_job_run,
.complete = cancel_job_complete,
},
};
Expand Down

0 comments on commit f67432a

Please sign in to comment.