job: Move pause/resume functions to Job
[qemu/ar7.git] / job.c
blobfd10b1d26712a3663972dc2889443f1837c223b1
1 /*
2 * Background jobs (long-running operations)
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/job.h"
30 #include "qemu/id.h"
31 #include "qemu/main-loop.h"
32 #include "trace-root.h"
34 static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
36 /* Job State Transition Table */
37 bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
38 /* U, C, R, P, Y, S, W, D, X, E, N */
39 /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
40 /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
41 /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
42 /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
43 /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
44 /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
45 /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
46 /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
47 /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
48 /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
49 /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
52 bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
53 /* U, C, R, P, Y, S, W, D, X, E, N */
54 [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
55 [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
56 [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
57 [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
58 [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
59 [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
60 [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
63 /* Right now, this mutex is only needed to synchronize accesses to job->busy
64 * and job->sleep_timer, such as concurrent calls to job_do_yield and
65 * job_enter. */
66 static QemuMutex job_mutex;
68 static void job_lock(void)
70 qemu_mutex_lock(&job_mutex);
73 static void job_unlock(void)
75 qemu_mutex_unlock(&job_mutex);
78 static void __attribute__((__constructor__)) job_init(void)
80 qemu_mutex_init(&job_mutex);
83 /* TODO Make static once the whole state machine is in job.c */
84 void job_state_transition(Job *job, JobStatus s1)
86 JobStatus s0 = job->status;
87 assert(s1 >= 0 && s1 <= JOB_STATUS__MAX);
88 trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0,
89 JobSTT[s0][s1] ? "allowed" : "disallowed",
90 JobStatus_str(s0), JobStatus_str(s1));
91 assert(JobSTT[s0][s1]);
92 job->status = s1;
95 int job_apply_verb(Job *job, JobVerb verb, Error **errp)
97 JobStatus s0 = job->status;
98 assert(verb >= 0 && verb <= JOB_VERB__MAX);
99 trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
100 JobVerbTable[verb][s0] ? "allowed" : "prohibited");
101 if (JobVerbTable[verb][s0]) {
102 return 0;
104 error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
105 job->id, JobStatus_str(s0), JobVerb_str(verb));
106 return -EPERM;
109 JobType job_type(const Job *job)
111 return job->driver->job_type;
114 const char *job_type_str(const Job *job)
116 return JobType_str(job_type(job));
119 bool job_is_cancelled(Job *job)
121 return job->cancelled;
124 bool job_started(Job *job)
126 return job->co;
129 bool job_should_pause(Job *job)
131 return job->pause_count > 0;
134 Job *job_next(Job *job)
136 if (!job) {
137 return QLIST_FIRST(&jobs);
139 return QLIST_NEXT(job, job_list);
142 Job *job_get(const char *id)
144 Job *job;
146 QLIST_FOREACH(job, &jobs, job_list) {
147 if (job->id && !strcmp(id, job->id)) {
148 return job;
152 return NULL;
155 static void job_sleep_timer_cb(void *opaque)
157 Job *job = opaque;
159 job_enter(job);
162 void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
163 Error **errp)
165 Job *job;
167 if (job_id) {
168 if (!id_wellformed(job_id)) {
169 error_setg(errp, "Invalid job ID '%s'", job_id);
170 return NULL;
172 if (job_get(job_id)) {
173 error_setg(errp, "Job ID '%s' already in use", job_id);
174 return NULL;
178 job = g_malloc0(driver->instance_size);
179 job->driver = driver;
180 job->id = g_strdup(job_id);
181 job->refcnt = 1;
182 job->aio_context = ctx;
183 job->busy = false;
184 job->paused = true;
185 job->pause_count = 1;
187 job_state_transition(job, JOB_STATUS_CREATED);
188 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
189 QEMU_CLOCK_REALTIME, SCALE_NS,
190 job_sleep_timer_cb, job);
192 QLIST_INSERT_HEAD(&jobs, job, job_list);
194 return job;
197 void job_ref(Job *job)
199 ++job->refcnt;
202 void job_unref(Job *job)
204 if (--job->refcnt == 0) {
205 assert(job->status == JOB_STATUS_NULL);
206 assert(!timer_pending(&job->sleep_timer));
208 if (job->driver->free) {
209 job->driver->free(job);
212 QLIST_REMOVE(job, job_list);
214 g_free(job->id);
215 g_free(job);
219 void job_enter_cond(Job *job, bool(*fn)(Job *job))
221 if (!job_started(job)) {
222 return;
224 if (job->deferred_to_main_loop) {
225 return;
228 job_lock();
229 if (job->busy) {
230 job_unlock();
231 return;
234 if (fn && !fn(job)) {
235 job_unlock();
236 return;
239 assert(!job->deferred_to_main_loop);
240 timer_del(&job->sleep_timer);
241 job->busy = true;
242 job_unlock();
243 aio_co_wake(job->co);
246 void job_enter(Job *job)
248 job_enter_cond(job, NULL);
251 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
252 * Reentering the job coroutine with block_job_enter() before the timer has
253 * expired is allowed and cancels the timer.
255 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
256 * called explicitly. */
257 void coroutine_fn job_do_yield(Job *job, uint64_t ns)
259 job_lock();
260 if (ns != -1) {
261 timer_mod(&job->sleep_timer, ns);
263 job->busy = false;
264 job_unlock();
265 qemu_coroutine_yield();
267 /* Set by job_enter_cond() before re-entering the coroutine. */
268 assert(job->busy);
271 void coroutine_fn job_pause_point(Job *job)
273 assert(job && job_started(job));
275 if (!job_should_pause(job)) {
276 return;
278 if (job_is_cancelled(job)) {
279 return;
282 if (job->driver->pause) {
283 job->driver->pause(job);
286 if (job_should_pause(job) && !job_is_cancelled(job)) {
287 JobStatus status = job->status;
288 job_state_transition(job, status == JOB_STATUS_READY
289 ? JOB_STATUS_STANDBY
290 : JOB_STATUS_PAUSED);
291 job->paused = true;
292 job_do_yield(job, -1);
293 job->paused = false;
294 job_state_transition(job, status);
297 if (job->driver->resume) {
298 job->driver->resume(job);
302 void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
304 assert(job->busy);
306 /* Check cancellation *before* setting busy = false, too! */
307 if (job_is_cancelled(job)) {
308 return;
311 if (!job_should_pause(job)) {
312 job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
315 job_pause_point(job);
319 * All jobs must allow a pause point before entering their job proper. This
320 * ensures that jobs can be paused prior to being started, then resumed later.
322 static void coroutine_fn job_co_entry(void *opaque)
324 Job *job = opaque;
326 assert(job && job->driver && job->driver->start);
327 job_pause_point(job);
328 job->driver->start(job);
332 void job_start(Job *job)
334 assert(job && !job_started(job) && job->paused &&
335 job->driver && job->driver->start);
336 job->co = qemu_coroutine_create(job_co_entry, job);
337 job->pause_count--;
338 job->busy = true;
339 job->paused = false;
340 job_state_transition(job, JOB_STATUS_RUNNING);
341 aio_co_enter(job->aio_context, job->co);
344 /* Assumes the block_job_mutex is held */
345 static bool job_timer_not_pending(Job *job)
347 return !timer_pending(&job->sleep_timer);
350 void job_pause(Job *job)
352 job->pause_count++;
355 void job_resume(Job *job)
357 assert(job->pause_count > 0);
358 job->pause_count--;
359 if (job->pause_count) {
360 return;
363 /* kick only if no timer is pending */
364 job_enter_cond(job, job_timer_not_pending);
367 void job_user_pause(Job *job, Error **errp)
369 if (job_apply_verb(job, JOB_VERB_PAUSE, errp)) {
370 return;
372 if (job->user_paused) {
373 error_setg(errp, "Job is already paused");
374 return;
376 job->user_paused = true;
377 job_pause(job);
380 bool job_user_paused(Job *job)
382 return job->user_paused;
385 void job_user_resume(Job *job, Error **errp)
387 assert(job);
388 if (!job->user_paused || job->pause_count <= 0) {
389 error_setg(errp, "Can't resume a job that was not paused");
390 return;
392 if (job_apply_verb(job, JOB_VERB_RESUME, errp)) {
393 return;
395 if (job->driver->user_resume) {
396 job->driver->user_resume(job);
398 job->user_paused = false;
399 job_resume(job);
403 typedef struct {
404 Job *job;
405 JobDeferToMainLoopFn *fn;
406 void *opaque;
407 } JobDeferToMainLoopData;
409 static void job_defer_to_main_loop_bh(void *opaque)
411 JobDeferToMainLoopData *data = opaque;
412 Job *job = data->job;
413 AioContext *aio_context = job->aio_context;
415 aio_context_acquire(aio_context);
416 data->fn(data->job, data->opaque);
417 aio_context_release(aio_context);
419 g_free(data);
422 void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque)
424 JobDeferToMainLoopData *data = g_malloc(sizeof(*data));
425 data->job = job;
426 data->fn = fn;
427 data->opaque = opaque;
428 job->deferred_to_main_loop = true;
430 aio_bh_schedule_oneshot(qemu_get_aio_context(),
431 job_defer_to_main_loop_bh, data);