aboutsummaryrefslogtreecommitdiff
path: root/job.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2018-04-12 17:54:37 +0200
committerKevin Wolf <kwolf@redhat.com>2018-05-23 14:30:49 +0200
commite7c1d78bbd5867804debeb7159b137fd9a6c44d3 (patch)
tree1048aef4f48fe88812d896b0fe4f4497d79e5da0 /job.c
parentfd61a701f1de8e4c1d89b3716ba9ca749cf5c724 (diff)
job: Maintain a list of all jobs
This moves the job list from BlockJob to Job. Now we can check for duplicate IDs in job_create(). Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Reviewed-by: John Snow <jsnow@redhat.com>
Diffstat (limited to 'job.c')
-rw-r--r--job.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/job.c b/job.c
index cfdd008c52..e57303c6bb 100644
--- a/job.c
+++ b/job.c
@@ -29,6 +29,8 @@
#include "qemu/job.h"
#include "qemu/id.h"
+static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
+
JobType job_type(const Job *job)
{
return job->driver->job_type;
@@ -39,6 +41,27 @@ const char *job_type_str(const Job *job)
return JobType_str(job_type(job));
}
+Job *job_next(Job *job)
+{
+ if (!job) {
+ return QLIST_FIRST(&jobs);
+ }
+ return QLIST_NEXT(job, job_list);
+}
+
+Job *job_get(const char *id)
+{
+ Job *job;
+
+ QLIST_FOREACH(job, &jobs, job_list) {
+ if (job->id && !strcmp(id, job->id)) {
+ return job;
+ }
+ }
+
+ return NULL;
+}
+
void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
{
Job *job;
@@ -48,17 +71,25 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
error_setg(errp, "Invalid job ID '%s'", job_id);
return NULL;
}
+ if (job_get(job_id)) {
+ error_setg(errp, "Job ID '%s' already in use", job_id);
+ return NULL;
+ }
}
job = g_malloc0(driver->instance_size);
job->driver = driver;
job->id = g_strdup(job_id);
+ QLIST_INSERT_HEAD(&jobs, job, job_list);
+
return job;
}
void job_delete(Job *job)
{
+ QLIST_REMOVE(job, job_list);
+
g_free(job->id);
g_free(job);
}