]> git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/job.h
job: Add Job.aio_context
[mirror_qemu.git] / include / qemu / job.h
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
29 #include "qapi/qapi-types-block-core.h"
30 #include "qemu/queue.h"
31
32 typedef struct JobDriver JobDriver;
33
34 /**
35 * Long-running operation.
36 */
37 typedef 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;
43
44 /** Reference count of the block job */
45 int refcnt;
46
47 /** Current state; See @JobStatus for details. */
48 JobStatus status;
49
50 /** AioContext to run the job coroutine in */
51 AioContext *aio_context;
52
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
61 /** Element of the list of jobs */
62 QLIST_ENTRY(Job) job_list;
63 } Job;
64
65 /**
66 * Callbacks and other information about a Job driver.
67 */
68 struct JobDriver {
69 /** Derived Job struct size */
70 size_t instance_size;
71
72 /** Enum describing the operation */
73 JobType job_type;
74
75 /** Called when the job is freed */
76 void (*free)(Job *job);
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.
85 * @ctx: The AioContext to run the job coroutine in.
86 * @errp: Error object.
87 */
88 void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
89 Error **errp);
90
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 */
95 void 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 */
101 void job_unref(Job *job);
102
103 /** Returns the JobType of a given Job. */
104 JobType job_type(const Job *job);
105
106 /** Returns the enum string for the JobType of a given Job. */
107 const char *job_type_str(const Job *job);
108
109 /** Returns whether the job is scheduled for cancellation. */
110 bool job_is_cancelled(Job *job);
111
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 */
118 Job *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 */
125 Job *job_get(const char *id);
126
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 */
132 int job_apply_verb(Job *job, JobVerb verb, Error **errp);
133
134 /* TODO To be removed from the public interface */
135 void job_state_transition(Job *job, JobStatus s1);
136
137 #endif