1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 Job* job_new(Manager *m, JobType type, Name *name) {
13 assert(type < _JOB_TYPE_MAX);
16 if (!(j = new0(Job, 1)))
20 j->id = m->current_job_id++;
24 /* We don't link it here, that's what job_dependency() is for */
29 void job_free(Job *j) {
32 /* Detach from next 'bigger' objects */
35 if (j->name->meta.job == j)
36 j->name->meta.job = NULL;
38 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
41 manager_transaction_unlink_job(j->manager, j);
46 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
51 /* Adds a new job link, which encodes that the 'subject' job
52 * needs the 'object' job in some way. If 'subject' is NULL
53 * this means the 'anchor' job (i.e. the one the user
54 * explcitily asked for) is the requester. */
56 if (!(l = new0(JobDependency, 1)))
64 l->subject_next = subject->subject_list;
65 subject->subject_list = l;
67 l->subject_next = object->manager->transaction_anchor;
68 object->manager->transaction_anchor = l;
72 l->subject_next->subject_prev = l;
73 l->subject_prev = NULL;
75 if ((l->object_next = object->object_list))
76 l->object_next->object_prev = l;
77 l->object_prev = NULL;
78 object->object_list = l;
83 void job_dependency_free(JobDependency *l) {
87 l->subject_prev->subject_next = l->subject_next;
89 l->subject->subject_list = l->subject_next;
91 l->object->manager->transaction_anchor = l->subject_next;
94 l->subject_next->subject_prev = l->subject_prev;
97 l->object_prev->object_next = l->object_next;
99 l->object->object_list = l->object_next;
102 l->object_next->object_prev = l->object_prev;
107 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
112 for (l = object->object_list; l; l = l->object_next) {
113 assert(l->object == object);
115 if (l->subject == subject)
126 *matters = l->matters;
128 job_dependency_free(l);
131 const char* job_type_to_string(JobType t) {
133 static const char* const job_type_table[_JOB_TYPE_MAX] = {
134 [JOB_START] = "start",
136 [JOB_VERIFY_STARTED] = "verify-started",
137 [JOB_RELOAD] = "reload",
138 [JOB_RELOAD_OR_START] = "reload-or-start",
139 [JOB_RESTART] = "restart",
140 [JOB_TRY_RESTART] = "try-restart",
144 if (t < 0 || t >= _JOB_TYPE_MAX)
147 return job_type_table[t];
150 void job_dump(Job *j, FILE*f, const char *prefix) {
152 static const char* const job_state_table[_JOB_STATE_MAX] = {
153 [JOB_WAITING] = "waiting",
154 [JOB_RUNNING] = "running",
163 "%s\tAction: %s → %s\n"
166 prefix, name_id(j->name), job_type_to_string(j->type),
167 prefix, job_state_table[j->state]);
170 bool job_is_anchor(Job *j) {
175 for (l = j->object_list; l; l = l->object_next)
182 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
184 (a == c && b == d) ||
188 int job_type_merge(JobType *a, JobType b) {
192 /* Merging is associative! a merged with b merged with c is
193 * the same as a merged with c merged with b. */
195 /* Mergeability is transitive! if a can be merged with b and b
196 * with c then a also with c */
198 /* Also, if a merged with b cannot be merged with c, then
199 * either a or b cannot be merged with c either */
201 if (types_match(*a, b, JOB_START, JOB_VERIFY_STARTED))
203 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
204 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
205 types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD_OR_START) ||
206 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
207 *a = JOB_RELOAD_OR_START;
208 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
209 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
210 types_match(*a, b, JOB_VERIFY_STARTED, JOB_RESTART) ||
211 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
212 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
213 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
214 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
216 else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD))
218 else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_TRY_RESTART) ||
219 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
220 *a = JOB_TRY_RESTART;
227 bool job_type_mergeable(JobType a, JobType b) {
228 return job_type_merge(&a, b) >= 0;
231 bool job_type_is_superset(JobType a, JobType b) {
233 /* Checks whether operation a is a "superset" of b */
240 return b == JOB_VERIFY_STARTED;
243 return b == JOB_VERIFY_STARTED;
245 case JOB_RELOAD_OR_START:
253 b == JOB_VERIFY_STARTED ||
255 b == JOB_RELOAD_OR_START ||
256 b == JOB_TRY_RESTART;
258 case JOB_TRY_RESTART:
260 b == JOB_VERIFY_STARTED ||