diff options
author | Kevin Wolf <kwolf@redhat.com> | 2018-04-13 18:50:05 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2018-05-23 14:30:49 +0200 |
commit | 80fa2c756b3241f24015a7503a01f7999d4a942d (patch) | |
tree | db1b410af62162feb7d8b69651a9d671cf4cc030 /job.c | |
parent | a50c2ab858fe613fb805e53b4f6b970ab936706d (diff) |
job: Add reference counting
This moves reference counting from BlockJob to Job.
In order to keep calling the BlockJob cleanup code when the job is
deleted via job_unref(), introduce a new JobDriver.free callback. Every
block job must use block_job_free() for this callback, this is asserted
in block_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.c | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -134,6 +134,7 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp) job = g_malloc0(driver->instance_size); job->driver = driver; job->id = g_strdup(job_id); + job->refcnt = 1; job_state_transition(job, JOB_STATUS_CREATED); @@ -142,10 +143,23 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp) return job; } -void job_delete(Job *job) +void job_ref(Job *job) { - QLIST_REMOVE(job, job_list); + ++job->refcnt; +} + +void job_unref(Job *job) +{ + if (--job->refcnt == 0) { + assert(job->status == JOB_STATUS_NULL); - g_free(job->id); - g_free(job); + if (job->driver->free) { + job->driver->free(job); + } + + QLIST_REMOVE(job, job_list); + + g_free(job->id); + g_free(job); + } } |