/* Fixed preparation pool. A timed-out worker can never dispatch a signal. */ #include "guard.h" #include #include #include #include #include #define VALIDATION_SLOTS 16 enum job_state { FREE, RESERVED, QUEUED, RUNNING, READY, CLAIMED, RELEASE, ABANDONED }; typedef struct { volatile int state; sem_t wake; const config_t *config; /* immutable after startup */ process_t expected; int real, handle, result, error; uint64_t ceiling; validation_t timing; } validation_job_t; static validation_job_t jobs[VALIDATION_SLOTS]; static int pool_started; static int state(validation_job_t *j) { return __sync_fetch_and_add(&j->state, 0); } static void *prepare_worker(void *arg) { validation_job_t *j = arg; for (;;) { int wait_rc; do { wait_rc = sem_wait(&j->wake); } while (wait_rc && errno == EINTR); if (wait_rc) return NULL; if (__sync_bool_compare_and_swap(&j->state, QUEUED, RUNNING)) { double begin = monotime(); j->handle = -1; memset(&j->timing, 0, sizeof (j->timing)); errno = 0; j->result = platform_prepare((unsigned)(j - jobs), j->config, &j->expected, j->real, j->ceiling, &j->handle, &j->timing); j->error = j->result ? errno : 0; j->timing.total_ms = 1000 * (monotime() - begin); if (__sync_bool_compare_and_swap(&j->state, RUNNING, READY)) continue; /* Timeout won: discard even a successful preparation. */ validation_late_result(j->expected.pid, &j->timing, j->error); platform_dispose(j->handle); __sync_lock_test_and_set(&j->state, FREE); } else if (__sync_bool_compare_and_swap(&j->state, RELEASE, RESERVED) || __sync_bool_compare_and_swap(&j->state, ABANDONED, RESERVED)) { platform_dispose(j->handle); __sync_lock_test_and_set(&j->state, FREE); } } return NULL; } int validation_start(void) { unsigned i; if (pool_started) return 0; if (platform_validation_init(VALIDATION_SLOTS)) return -1; for (i = 0; i < VALIDATION_SLOTS; i++) { pthread_t thread; jobs[i].handle = -1; if (sem_init(&jobs[i].wake, 0, 0)) return -1; if (pthread_create(&thread, NULL, prepare_worker, &jobs[i])) return -1; (void)pthread_detach(thread); } pool_started = 1; return 0; } int validation_acquire(const config_t *c, const process_t *p, int real, uint64_t ceiling, unsigned budget_ms, int *ticket, validation_t *timing) { unsigned i; double begin = monotime(), deadline = begin + budget_ms / 1000.0; const struct timespec pause = {0, 250000}; validation_job_t *j = NULL; memset(timing, 0, sizeof (*timing)); *ticket = -1; if (!pool_started) { errno = ENXIO; return -1; } /* Single caller owns request fields. Do not fill the pool with repeated * attempts on the same stuck PID incarnation after cache refreshes. */ for (i = 0; i < VALIDATION_SLOTS; i++) { const process_t *old = &jobs[i].expected; if (state(&jobs[i]) != FREE && old->pid == p->pid && old->start_sec == p->start_sec && old->start_nsec == p->start_nsec && old->zone_generation == p->zone_generation && !strcmp(old->uuid, p->uuid)) { errno = EBUSY; return -1; } } for (i = 0; i < VALIDATION_SLOTS; i++) if (__sync_bool_compare_and_swap(&jobs[i].state, FREE, RESERVED)) { j = &jobs[i]; break; } if (!j) { errno = EBUSY; return -1; } j->config = c; j->expected = *p; j->real = real; j->ceiling = ceiling; j->handle = -1; __sync_lock_test_and_set(&j->state, QUEUED); (void)sem_post(&j->wake); for (;;) { if (monotime() >= deadline) { int s; do { s = state(j); } while (!__sync_bool_compare_and_swap(&j->state, s, s == READY ? RELEASE : ABANDONED)); if (s == READY) (void)sem_post(&j->wake); timing->total_ms = 1000 * (monotime() - begin); errno = ETIMEDOUT; return -1; } if (__sync_bool_compare_and_swap(&j->state, READY, CLAIMED)) { int error = j->error; *timing = j->timing; timing->total_ms = 1000 * (monotime() - begin); if (j->result) { __sync_lock_test_and_set(&j->state, RELEASE); (void)sem_post(&j->wake); errno = error; return -1; } /* A delayed scheduler must not turn a late READY into authorization. */ if (monotime() >= deadline) { __sync_lock_test_and_set(&j->state, RELEASE); (void)sem_post(&j->wake); errno = ETIMEDOUT; return -1; } *ticket = (int)i; return 0; } (void)nanosleep(&pause, NULL); } } int validation_handle(int ticket) { return jobs[ticket].handle; } void validation_release(int ticket) { if (ticket >= 0 && ticket < VALIDATION_SLOTS) if (__sync_bool_compare_and_swap(&jobs[ticket].state, CLAIMED, RELEASE)) (void)sem_post(&jobs[ticket].wake); }