Exemplo n.º 1
0
static void adjust_squeue_for_time_change(squeue_t **q, int delta)
{
	timed_event *event;
	squeue_t *sq_new;

	/*
	 * this is pretty inefficient in terms of free() + malloc(),
	 * but it should be pretty rare that we have to adjust times
	 * so we go with the well-tested codepath.
	 */
	sq_new = squeue_create(squeue_size(*q));
	while ((event = squeue_pop(*q))) {
		if (event->compensate_for_time_change == TRUE) {
			if (event->timing_func) {
				time_t (*timingfunc)(void);
				timingfunc = event->timing_func;
				event->run_time = timingfunc();
			} else {
				event->run_time += delta;
			}
		}
		if (event->priority) {
			event->sq_event = squeue_add_usec(sq_new, event->run_time, event->priority - 1, event);
		} else {
			event->sq_event = squeue_add(sq_new, event->run_time, event);
		}
	}
	squeue_destroy(*q, 0);
	*q = sq_new;
}
Exemplo n.º 2
0
static void exit_worker(int code, const char *msg)
{
	child_process *cp;
	int discard;

	if (msg) {
		perror(msg);
	}

	/*
	 * We must kill our children, so let's embark on that
	 * large scale filicide. Each process should be in a
	 * process group of its own, so we can signal not only
	 * the plugin but also all of its children.
	 */
	signal(SIGTERM, SIG_IGN);
	kill(0, SIGTERM);
	while (waitpid(-1, &discard, WNOHANG) > 0)
		; /* do nothing */
	sleep(1);
	while ((cp = (child_process *)squeue_pop(sq))) {
		/* kill all processes in the child's process group */
		(void)kill(-cp->ei->pid, SIGKILL);
	}
	sleep(1);
	while (waitpid(-1, &discard, WNOHANG) > 0)
		; /* do nothing */

	exit(code);
}
Exemplo n.º 3
0
static int sq_test_random(squeue_t *sq)
{
	unsigned long size, i;
	unsigned long long numbers[EVT_ARY], *d, max = 0;
	struct timeval now;

	size = squeue_size(sq);
	now.tv_sec = time(NULL);
	srand((int)now.tv_sec);
	for (i = 0; i < EVT_ARY; i++) {
		now.tv_usec = (time_t)rand();
		squeue_add_tv(sq, &now, &numbers[i]);
		numbers[i] = evt_compute_pri(&now);
		t(squeue_size(sq) == i + 1 + size);
	}

	t(pqueue_is_valid(sq));

	/*
	 * make sure we pop events in increasing "priority",
	 * since we calculate priority based on time and later
	 * is lower prio
	 */
	for (i = 0; i < EVT_ARY; i++) {
		d = (unsigned long long *)squeue_pop(sq);
		t(max <= *d, "popping randoms. i: %lu; delta: %lld; max: %llu; *d: %llu\n",
		  i, max - *d, max, *d);
		max = *d;
		t(squeue_size(sq) == size + (EVT_ARY - i - 1));
	}
	t(pqueue_is_valid(sq));

	return 0;
}
Exemplo n.º 4
0
int main() {
  squeue *q;
  int rc = 0;
  int i;
  int *p;
  size_t n;
  int d;

  q = squeue_new(1024, 4);
  if (!q) {
    printf("squeue_new failed\n");
    exit(EXIT_FAILURE);
  }

  uint32_t data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  rc = squeue_enter(q, 1);
  if (rc) {
    for (p = squeue_get_next_push_slot(q), i=0; 
          p; 
          p = squeue_get_next_push_slot(q), i=(i+1)%10) {
      *p = data[i];
    }
    squeue_exit(q);
  }

  rc = squeue_enter(q, 1);
  if (rc) {
    for (p = squeue_get_next_pop_slot(q); p; p = squeue_get_next_pop_slot(q)) {
      printf("%u ", *p);
    }
    squeue_exit(q);
  }
  printf("\n********************\n");

  do {
    n = squeue_push_many(q, data, 1, 10); 
  } while (n == 10); 

  for (n = squeue_pop(q, &d, 1); n > 0; n = squeue_pop(q, &d, 0)) {
    printf("%u ", d);
  }
  printf("\n********************\n");

  exit(EXIT_SUCCESS);
}
Exemplo n.º 5
0
/**
 * Returns the "smallest" element from the double ring.
 *
 * @param d Handle of the double ring.
 * @return The "smallest" element of the double ring.
 */
int dring_pop(DRING d)
{
	_DRING *dring = (_DRING *)d;

	if(squeue_nitems(dring->s) > 0) {
		return squeue_pop(dring->s);
	}
	else if (squeue_nitems(dring->l)) {
		/* dring has become empty -> switch rings */
		SQUEUE *tmp = dring->l;
		dring->l = dring->s;
		dring->s = tmp;
		dring->maxFirstRing = squeue_peek_tail(dring->s);

		return squeue_pop(dring->s);
	} else {
		/* dring empty */
		assert(squeue_nitems(dring->l) == 0);
		return -1;
	}
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
	squeue_t *sq;
	struct timeval tv;
	sq_test_event a, b, c, d, *x;

	t_set_colors(0);
	t_start("squeue tests");

	a.id = 1;
	b.id = 2;
	c.id = 3;
	d.id = 4;

	gettimeofday(&tv, NULL);
	/* Order in is a, b, c, d, but we should get b, c, d, a out. */
	srand(tv.tv_usec ^ tv.tv_sec);
	t((sq = squeue_create(1024)) != NULL);
	t(squeue_size(sq) == 0);

	/* we fill and empty the squeue completely once before testing */
	sq_test_random(sq);
	t(squeue_size(sq) == 0, "Size should be 0 after first sq_test_random");

	t((a.evt = squeue_add(sq, time(NULL) + 9, &a)) != NULL);
	t(squeue_size(sq) == 1);
	t((b.evt = squeue_add(sq, time(NULL) + 3, &b)) != NULL);
	t(squeue_size(sq) == 2);
	t((c.evt = squeue_add_msec(sq, time(NULL) + 5, 0, &c)) != NULL);
	t(squeue_size(sq) == 3);
	t((d.evt = squeue_add_usec(sq, time(NULL) + 5, 1, &d)) != NULL);
	t(squeue_size(sq) == 4);

	/* add and remove lots. remainder should be what we have above */
	sq_test_random(sq);

	/* testing squeue_peek() */
	t((x = (sq_test_event *)squeue_peek(sq)) != NULL);
	t(x == &b, "x: %p; a: %p; b: %p; c: %p; d: %p\n", x, &a, &b, &c, &d);
	t(x->id == b.id);
	t(squeue_size(sq) == 4);

	/* testing squeue_remove() and re-add */
	t(squeue_remove(sq, b.evt) == 0);
	t(squeue_size(sq) == 3);
	t((x = squeue_peek(sq)) != NULL);
	t(x == &c);
	t((b.evt = squeue_add(sq, time(NULL) + 3, &b)) != NULL);
	t(squeue_size(sq) == 4);

	/* peek should now give us the &b event (again) */
	t((x = squeue_peek(sq)) != NULL);
	if (x != &b) {
		printf("about to fail pretty f*****g hard...\n");
		printf("ea: %p; &b: %p; &c: %p; ed: %p; x: %p\n",
		       &a, &b, &c, &d, x);
	}
	t(x == &b);
	t(x->id == b.id);
	t(squeue_size(sq) == 4);

	/* testing squeue_pop(), lifo manner */
	t((x = squeue_pop(sq)) != NULL);
	t(squeue_size(sq) == 3,
	  "squeue_size(sq) = %d\n", squeue_size(sq));
	t(x == &b, "x: %p; &b: %p\n", x, &b);
	t(x->id == b.id, "x->id: %lu; d.id: %lu\n", x->id, d.id);

	/* Test squeue_pop() */
	t((x = squeue_pop(sq)) != NULL);
	t(squeue_size(sq) == 2);
	t(x == &c, "x->id: %lu; c.id: %lu\n", x->id, c.id);
	t(x->id == c.id, "x->id: %lu; c.id: %lu\n", x->id, c.id);

	/* this should fail gracefully (-1 return from squeue_remove()) */
	t(squeue_remove(NULL, NULL) == -1);
	t(squeue_remove(NULL, a.evt) == -1);

	squeue_foreach(sq, sq_walker, NULL);

	/* clean up to prevent false valgrind positives */
	squeue_destroy(sq, 0);

	return t_end();
}