summaryrefslogtreecommitdiff
path: root/src/core/job.h
diff options
context:
space:
mode:
authorMichal Schmidt <mschmidt@redhat.com>2012-04-25 11:58:27 +0200
committerMichal Schmidt <mschmidt@redhat.com>2012-04-25 18:38:27 +0200
commite0209d83e7b30153f43b1a633c955f66eb2c2e4a (patch)
tree95841c9e763cecd6c2ed10d54de67e74e107122b /src/core/job.h
parente2875c46936a16efc0f58f9e6e2570cdda8d6d98 (diff)
core: add NOP jobs, job type collapsing
Two of our current job types are special: JOB_TRY_RESTART, JOB_RELOAD_OR_START. They differ from other job types by being sensitive to the unit active state. They perform some action when the unit is active and some other action otherwise. This raises a question: when exactly should the unit state be checked to make the decision? Currently the unit state is checked when the job becomes runnable. It's more sensible to check the state immediately when the job is added by the user. When the user types "systemctl try-restart foo.service", he really intends to restart the service if it's running right now. If it isn't running right now, the restart is pointless. Consider the example (from Bugzilla[1]): sleep.service takes some time to start. hello.service has After=sleep.service. Both services get started. Two jobs will appear: hello.service/start waiting sleep.service/start running Then someone runs "systemctl try-restart hello.service". Currently the try-restart operation will block and wait for sleep.service/start to complete. The correct result is to complete the try-restart operation immediately with success, because hello.service is not running. The two original jobs must not be disturbed by this. To fix this we introduce two new concepts: - a new job type: JOB_NOP A JOB_NOP job does not do anything to the unit. It does not pull in any dependencies. It is always immediately runnable. When installed to a unit, it sits in a special slot (u->nop_job) where it never conflicts with the installed job (u->job) of a different type. It never merges with jobs of other types, but it can merge into an already installed JOB_NOP job. - "collapsing" of job types When a job of one of the two special types is added, the state of the unit is checked immediately and the job type changes: JOB_TRY_RESTART -> JOB_RESTART or JOB_NOP JOB_RELOAD_OR_START -> JOB_RELOAD or JOB_START Should a job type JOB_RELOAD_OR_START appear later during job merging, it collapses immediately afterwards. Collapsing actually makes some things simpler, because there are now fewer job types that are allowed in the transaction. [1] Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=753586
Diffstat (limited to 'src/core/job.h')
-rw-r--r--src/core/job.h44
1 files changed, 31 insertions, 13 deletions
diff --git a/src/core/job.h b/src/core/job.h
index 2b679735fc..2e10143d2b 100644
--- a/src/core/job.h
+++ b/src/core/job.h
@@ -46,14 +46,34 @@ enum JobType {
JOB_STOP,
- JOB_RELOAD, /* if running reload */
- JOB_RELOAD_OR_START, /* if running reload, if not running start */
+ JOB_RELOAD, /* if running, reload */
/* Note that restarts are first treated like JOB_STOP, but
* then instead of finishing are patched to become
* JOB_START. */
- JOB_RESTART, /* if running stop, then start unconditionally */
- JOB_TRY_RESTART, /* if running stop and then start */
+ JOB_RESTART, /* If running, stop. Then start unconditionally. */
+
+ _JOB_TYPE_MAX_MERGING,
+
+ /* JOB_NOP can enter into a transaction, but as it won't pull in
+ * any dependencies, it won't have to merge with anything.
+ * job_install() avoids the problem of merging JOB_NOP too (it's
+ * special-cased, only merges with other JOB_NOPs). */
+ JOB_NOP = _JOB_TYPE_MAX_MERGING, /* do nothing */
+
+ _JOB_TYPE_MAX_IN_TRANSACTION,
+
+ /* JOB_TRY_RESTART can never appear in a transaction, because
+ * it always collapses into JOB_RESTART or JOB_NOP before entering.
+ * Thus we never need to merge it with anything. */
+ JOB_TRY_RESTART = _JOB_TYPE_MAX_IN_TRANSACTION, /* if running, stop and then start */
+
+ /* JOB_RELOAD_OR_START won't enter into a transaction and cannot result
+ * from transaction merging (there's no way for JOB_RELOAD and
+ * JOB_START to meet in one transaction). It can result from a merge
+ * during job installation, but then it will immediately collapse into
+ * one of the two simpler types. */
+ JOB_RELOAD_OR_START, /* if running, reload, otherwise start */
_JOB_TYPE_MAX,
_JOB_TYPE_INVALID = -1
@@ -150,7 +170,7 @@ Job* job_new(Unit *unit, JobType type);
Job* job_new_raw(Unit *unit);
void job_free(Job *job);
Job* job_install(Job *j);
-void job_install_deserialized(Job *j);
+int job_install_deserialized(Job *j);
void job_uninstall(Job *j);
void job_dump(Job *j, FILE*f, const char *prefix);
int job_serialize(Job *j, FILE *f, FDSet *fds);
@@ -164,14 +184,6 @@ int job_merge(Job *j, Job *other);
JobType job_type_lookup_merge(JobType a, JobType b);
-static inline int job_type_merge(JobType *a, JobType b) {
- JobType t = job_type_lookup_merge(*a, b);
- if (t < 0)
- return -EEXIST;
- *a = t;
- return 0;
-}
-
static inline bool job_type_is_mergeable(JobType a, JobType b) {
return job_type_lookup_merge(a, b) >= 0;
}
@@ -187,6 +199,12 @@ static inline bool job_type_is_superset(JobType a, JobType b) {
bool job_type_is_redundant(JobType a, UnitActiveState b);
+/* Collapses a state-dependent job type into a simpler type by observing
+ * the state of the unit which it is going to be applied to. */
+void job_type_collapse(JobType *t, Unit *u);
+
+int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u);
+
bool job_is_runnable(Job *j);
void job_add_to_run_queue(Job *j);