[25a9fec] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Michal Koutny
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <adt/list.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 |
|
---|
| 34 | #include "job_queue.h"
|
---|
| 35 | #include "log.h"
|
---|
| 36 | #include "sysman.h"
|
---|
| 37 |
|
---|
| 38 | static list_t job_queue;
|
---|
| 39 |
|
---|
| 40 | /*
|
---|
| 41 | * Static functions
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | static bool job_is_runnable(job_t *job)
|
---|
| 45 | {
|
---|
| 46 | assert(job->state == JOB_PENDING);
|
---|
| 47 | return job->blocking_jobs == 0;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /** Pop next runnable job
|
---|
| 51 | *
|
---|
| 52 | * @return runnable job or NULL when there's none
|
---|
| 53 | */
|
---|
| 54 | static job_t *job_queue_pop_runnable(void)
|
---|
| 55 | {
|
---|
| 56 | job_t *result = NULL;
|
---|
| 57 |
|
---|
| 58 | /* Select first runnable job */
|
---|
| 59 | list_foreach(job_queue, job_queue, job_t, candidate) {
|
---|
| 60 | if (job_is_runnable(candidate)) {
|
---|
| 61 | result = candidate;
|
---|
| 62 | break;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | if (result) {
|
---|
| 66 | /* Remove job from queue and pass reference to caller */
|
---|
| 67 | list_remove(&result->job_queue);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | return result;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Add multiple references to job
|
---|
| 74 | *
|
---|
| 75 | * Non-atomicity doesn't mind as long as individual increments are atomic.
|
---|
| 76 | *
|
---|
| 77 | * @note Function is not exported as other modules shouldn't need it.
|
---|
| 78 | */
|
---|
| 79 | static inline void job_add_refs(job_t *job, size_t refs)
|
---|
| 80 | {
|
---|
| 81 | for (size_t i = 0; i < refs; ++i) {
|
---|
| 82 | job_add_ref(job);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /** Delete multiple references to job
|
---|
| 87 | *
|
---|
| 88 | * Behavior of concurrent runs with job_add_refs aren't specified.
|
---|
| 89 | */
|
---|
| 90 | static inline void job_del_refs(job_t **job_ptr, size_t refs)
|
---|
| 91 | {
|
---|
| 92 | for (size_t i = 0; i < refs; ++i) {
|
---|
| 93 | job_del_ref(job_ptr);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Merge two jobs together
|
---|
| 98 | *
|
---|
| 99 | * @param[in/out] trunk job that
|
---|
| 100 | * @param[in] other job that will be cleared out
|
---|
| 101 | *
|
---|
| 102 | * @return EOK on success
|
---|
| 103 | * @return error code on fail
|
---|
| 104 | */
|
---|
| 105 | static int job_pre_merge(job_t *trunk, job_t *other)
|
---|
| 106 | {
|
---|
| 107 | assert(trunk->unit == other->unit);
|
---|
| 108 | assert(trunk->target_state == other->target_state);
|
---|
| 109 | assert(trunk->blocked_jobs.size == trunk->blocked_jobs_count);
|
---|
| 110 | assert(other->merged_into == NULL);
|
---|
| 111 |
|
---|
| 112 | int rc = dyn_array_concat(&trunk->blocked_jobs, &other->blocked_jobs);
|
---|
| 113 | if (rc != EOK) {
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 | dyn_array_clear(&other->blocked_jobs);
|
---|
| 117 |
|
---|
| 118 | // TODO allocate observed object
|
---|
| 119 |
|
---|
| 120 | other->merged_into = trunk;
|
---|
| 121 |
|
---|
| 122 | return EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static void job_finish_merge(job_t *trunk, job_t *other)
|
---|
| 126 | {
|
---|
| 127 | assert(trunk->blocked_jobs.size >= trunk->blocked_jobs_count);
|
---|
| 128 | //TODO aggregate merged blocked_jobs
|
---|
| 129 | trunk->blocked_jobs_count = other->blocked_jobs.size;
|
---|
| 130 |
|
---|
| 131 | /*
|
---|
| 132 | * Note, the sysman_move_observers cannot fail here sice all necessary
|
---|
| 133 | * allocation is done in job_pre_merge.
|
---|
| 134 | */
|
---|
| 135 | size_t observers_refs = sysman_observers_count(other);
|
---|
| 136 | int rc = sysman_move_observers(other, trunk);
|
---|
| 137 | assert(rc == EOK);
|
---|
| 138 |
|
---|
| 139 | /* When we move observers, don't forget to pass their references too. */
|
---|
| 140 | job_add_refs(trunk, observers_refs);
|
---|
| 141 | job_del_refs(&other, observers_refs);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static void job_undo_merge(job_t *trunk)
|
---|
| 145 | {
|
---|
| 146 | assert(trunk->blocked_jobs.size >= trunk->blocked_jobs_count);
|
---|
| 147 | dyn_array_clear_range(&trunk->blocked_jobs,
|
---|
| 148 | trunk->blocked_jobs_count, trunk->blocked_jobs.size);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Non-static functions
|
---|
| 153 | */
|
---|
| 154 |
|
---|
| 155 | void job_queue_init()
|
---|
| 156 | {
|
---|
| 157 | list_initialize(&job_queue);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /** Consistenly add jobs to the queue
|
---|
| 161 | *
|
---|
| 162 | * @param[in/out] closure jobs closure, on success it's emptied, otherwise
|
---|
| 163 | * you should take care of remaining jobs
|
---|
| 164 | *
|
---|
| 165 | * @return EOK on success
|
---|
| 166 | * @return EBUSY when any job in closure is conflicting
|
---|
| 167 | */
|
---|
| 168 | int job_queue_add_closure(job_closure_t *closure)
|
---|
| 169 | {
|
---|
| 170 | bool has_error = false;
|
---|
| 171 | int rc = EOK;
|
---|
| 172 |
|
---|
| 173 | /* Check consistency with existing jobs. */
|
---|
| 174 | dyn_array_foreach(*closure, job_t *, job_it) {
|
---|
| 175 | job_t *job = *job_it;
|
---|
| 176 | job_t *other_job = job->unit->job;
|
---|
| 177 |
|
---|
| 178 | if (other_job == NULL) {
|
---|
| 179 | continue;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (other_job->target_state != job->target_state) {
|
---|
| 183 | switch (other_job->state) {
|
---|
| 184 | case JOB_RUNNING:
|
---|
| 185 | sysman_log(LVL_ERROR,
|
---|
| 186 | "Unit '%s' has already different job running.",
|
---|
| 187 | unit_name(job->unit));
|
---|
| 188 | has_error = true;
|
---|
| 189 | continue;
|
---|
| 190 | case JOB_PENDING:
|
---|
| 191 | /*
|
---|
| 192 | * Currently we have strict strategy not
|
---|
| 193 | * permitting multiple jobs for one unit in the
|
---|
| 194 | * queue at a time.
|
---|
| 195 | */
|
---|
| 196 | sysman_log(LVL_ERROR,
|
---|
| 197 | "Cannot queue multiple jobs for unit '%s'.",
|
---|
| 198 | unit_name(job->unit));
|
---|
| 199 | has_error = true;
|
---|
| 200 | continue;
|
---|
| 201 | default:
|
---|
| 202 | assert(false);
|
---|
| 203 | }
|
---|
| 204 | } else {
|
---|
| 205 | // TODO think about other options to merging
|
---|
| 206 | // (replacing, cancelling)
|
---|
| 207 | rc = job_pre_merge(other_job, job);
|
---|
| 208 | if (rc != EOK) {
|
---|
| 209 | break;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* Aggregate merged jobs, or rollback any changes in existing jobs */
|
---|
| 215 | bool finish_merge = (rc == EOK) && !has_error;
|
---|
| 216 | dyn_array_foreach(*closure, job_t *, job_it) {
|
---|
| 217 | if ((*job_it)->merged_into == NULL) {
|
---|
| 218 | continue;
|
---|
| 219 | }
|
---|
| 220 | if (finish_merge) {
|
---|
| 221 | job_finish_merge((*job_it)->merged_into, *job_it);
|
---|
| 222 | } else {
|
---|
| 223 | job_undo_merge((*job_it)->merged_into);
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | if (has_error) {
|
---|
| 227 | return EBUSY;
|
---|
| 228 | } else if (rc != EOK) {
|
---|
| 229 | return rc;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /* Unmerged jobs are enqueued, merged are disposed */
|
---|
| 233 | dyn_array_foreach(*closure, job_t *, job_it) {
|
---|
| 234 | job_t *job = (*job_it);
|
---|
| 235 | if (job->merged_into != NULL) {
|
---|
| 236 | job_del_ref(&job);
|
---|
| 237 | continue;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | unit_t *u = job->unit;
|
---|
| 242 | assert(u->job == NULL);
|
---|
| 243 | /* Pass reference from the closure to the unit */
|
---|
| 244 | u->job = job;
|
---|
| 245 |
|
---|
| 246 | /* Enqueue job (new reference) */
|
---|
| 247 | job->state = JOB_PENDING;
|
---|
| 248 | job_add_ref(job);
|
---|
| 249 | list_append(&job->job_queue, &job_queue);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /* We've stolen references from the closure, so erase it */
|
---|
| 253 | dyn_array_clear(closure);
|
---|
| 254 |
|
---|
| 255 | return EOK;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /** Process all jobs that aren't transitively blocked
|
---|
| 259 | *
|
---|
| 260 | * Job can be blocked either by another job or by an incoming event, that will
|
---|
| 261 | * be queued after this job_queue_process call.
|
---|
| 262 | *
|
---|
| 263 | * TODO Write down rules from where this function can be called, to avoid stack
|
---|
| 264 | * overflow.
|
---|
| 265 | */
|
---|
| 266 | void job_queue_process(void)
|
---|
| 267 | {
|
---|
| 268 | job_t *job;
|
---|
| 269 | while ((job = job_queue_pop_runnable())) {
|
---|
| 270 | job_run(job);
|
---|
| 271 | job_del_ref(&job);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|