]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu/job.h
job: Add Job.aio_context
[mirror_qemu.git] / include / qemu / job.h
CommitLineData
33e9e9bd
KW
1/*
2 * Declarations for background jobs
3 *
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
25
26#ifndef JOB_H
27#define JOB_H
28
252291ea 29#include "qapi/qapi-types-block-core.h"
e7c1d78b 30#include "qemu/queue.h"
252291ea 31
33e9e9bd
KW
32typedef struct JobDriver JobDriver;
33
34/**
35 * Long-running operation.
36 */
37typedef struct Job {
38 /** The ID of the job. May be NULL for internal jobs. */
39 char *id;
40
41 /** The type of this job. */
42 const JobDriver *driver;
e7c1d78b 43
80fa2c75
KW
44 /** Reference count of the block job */
45 int refcnt;
46
a50c2ab8
KW
47 /** Current state; See @JobStatus for details. */
48 JobStatus status;
49
08be6fe2
KW
50 /** AioContext to run the job coroutine in */
51 AioContext *aio_context;
52
daa7f2f9
KW
53 /**
54 * Set to true if the job should cancel itself. The flag must
55 * always be tested just before toggling the busy flag from false
56 * to true. After a job has been cancelled, it should only yield
57 * if #aio_poll will ("sooner or later") reenter the coroutine.
58 */
59 bool cancelled;
60
e7c1d78b
KW
61 /** Element of the list of jobs */
62 QLIST_ENTRY(Job) job_list;
33e9e9bd
KW
63} Job;
64
65/**
66 * Callbacks and other information about a Job driver.
67 */
68struct JobDriver {
69 /** Derived Job struct size */
70 size_t instance_size;
252291ea
KW
71
72 /** Enum describing the operation */
73 JobType job_type;
80fa2c75
KW
74
75 /** Called when the job is freed */
76 void (*free)(Job *job);
33e9e9bd
KW
77};
78
79
80/**
81 * Create a new long-running job and return it.
82 *
83 * @job_id: The id of the newly-created job, or %NULL for internal jobs
84 * @driver: The class object for the newly-created job.
08be6fe2 85 * @ctx: The AioContext to run the job coroutine in.
33e9e9bd
KW
86 * @errp: Error object.
87 */
08be6fe2
KW
88void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
89 Error **errp);
33e9e9bd 90
80fa2c75
KW
91/**
92 * Add a reference to Job refcnt, it will be decreased with job_unref, and then
93 * be freed if it comes to be the last reference.
94 */
95void job_ref(Job *job);
96
97/**
98 * Release a reference that was previously acquired with job_ref() or
99 * job_create(). If it's the last reference to the object, it will be freed.
100 */
101void job_unref(Job *job);
fd61a701 102
252291ea
KW
103/** Returns the JobType of a given Job. */
104JobType job_type(const Job *job);
105
106/** Returns the enum string for the JobType of a given Job. */
107const char *job_type_str(const Job *job);
108
daa7f2f9
KW
109/** Returns whether the job is scheduled for cancellation. */
110bool job_is_cancelled(Job *job);
111
e7c1d78b
KW
112/**
113 * Get the next element from the list of block jobs after @job, or the
114 * first one if @job is %NULL.
115 *
116 * Returns the requested job, or %NULL if there are no more jobs left.
117 */
118Job *job_next(Job *job);
119
120/**
121 * Get the job identified by @id (which must not be %NULL).
122 *
123 * Returns the requested job, or %NULL if it doesn't exist.
124 */
125Job *job_get(const char *id);
126
a50c2ab8
KW
127/**
128 * Check whether the verb @verb can be applied to @job in its current state.
129 * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
130 * returned.
131 */
132int job_apply_verb(Job *job, JobVerb verb, Error **errp);
133
134/* TODO To be removed from the public interface */
135void job_state_transition(Job *job, JobStatus s1);
136
33e9e9bd 137#endif