Merge pull request #27413 from yuwata/core-job-cleanups

core/job: cleanups for job ID
This commit is contained in:
Yu Watanabe
2023-04-27 12:29:29 +09:00
committed by GitHub
2 changed files with 32 additions and 5 deletions
+30 -3
View File
@@ -48,6 +48,28 @@ Job* job_new_raw(Unit *unit) {
return j;
}
static uint32_t manager_get_new_job_id(Manager *m) {
bool overflow = false;
assert(m);
for (;;) {
uint32_t id = m->current_job_id;
if (_unlikely_(id == UINT32_MAX)) {
assert_se(!overflow);
m->current_job_id = 1;
overflow = true;
} else
m->current_job_id++;
if (hashmap_contains(m->jobs, UINT32_TO_PTR(id)))
continue;
return id;
}
}
Job* job_new(Unit *unit, JobType type) {
Job *j;
@@ -57,7 +79,7 @@ Job* job_new(Unit *unit, JobType type) {
if (!j)
return NULL;
j->id = j->manager->current_job_id++;
j->id = manager_get_new_job_id(j->manager);
j->type = type;
/* We don't link it here, that's what job_dependency() is for */
@@ -237,6 +259,7 @@ Job* job_install(Job *j) {
}
/* Install the job */
assert(!*pj);
*pj = j;
j->installed = true;
@@ -261,14 +284,18 @@ int job_install_deserialized(Job *j) {
if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION)
return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EINVAL),
"Invalid job type %s in deserialization.",
strna(job_type_to_string(j->type)));
"Invalid job type %s in deserialization.",
strna(job_type_to_string(j->type)));
pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job;
if (*pj)
return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EEXIST),
"Unit already has a job installed. Not installing deserialized job.");
/* When the job does not have ID, or we failed to deserialize the job ID, then use a new ID. */
if (j->id <= 0)
j->id = manager_get_new_job_id(j->manager);
r = hashmap_ensure_put(&j->manager->jobs, NULL, UINT32_TO_PTR(j->id), j);
if (r == -EEXIST)
return log_unit_debug_errno(j->unit, r, "Job ID %" PRIu32 " already used, cannot deserialize job.", j->id);
+2 -2
View File
@@ -662,7 +662,7 @@ static int transaction_apply(
/* j has been merged into a previously installed job */
if (tr->anchor_job == j)
tr->anchor_job = installed_job;
hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
hashmap_remove_value(m->jobs, UINT32_TO_PTR(j->id), j);
job_free(j);
j = installed_job;
}
@@ -683,7 +683,7 @@ static int transaction_apply(
rollback:
HASHMAP_FOREACH(j, tr->jobs)
hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
hashmap_remove_value(m->jobs, UINT32_TO_PTR(j->id), j);
return r;
}