Alien-uv

 view release on metacpan or  search on metacpan

libuv/test/test-condvar.c  view on Meta::CPAN


  r = 0;

  uv_mutex_lock(&c->mutex);

  /* Tell signal'er that I am waiting. */
  uv_sem_post(&c->sem_waiting);

  /* Wait until I get a non-spurious signal. */
  do {
    r = uv_cond_timedwait(&c->cond, &c->mutex, (uint64_t)(1 * 1e9)); /* 1 s */
    ASSERT(r == 0); /* Should not time out. */
  } while (*flag == 0);
  ASSERT(*flag == 1);

  uv_mutex_unlock(&c->mutex);

  /* Wait for my signal'er to finish. */
  uv_sem_wait(&c->sem_signaled);
  return r;
}

/* uv_cond_timedwait: One thread signals, the other timedwaits. */
TEST_IMPL(condvar_3) {
  worker_config wc;
  uv_thread_t thread;

  /* Helper to signal-then-wait. */
  worker_config_init(&wc, 0, condvar_signal, condvar_timedwait);
  ASSERT(0 == uv_thread_create(&thread, worker, &wc));

  /* We wait-then-signal. */
  wc.wait_cond(&wc, &wc.posted_1);
  wc.signal_cond(&wc, &wc.posted_2);

  ASSERT(0 == uv_thread_join(&thread));
  worker_config_destroy(&wc);

  return 0;
}

/* uv_cond_timedwait: One thread broadcasts, the other waits. */
TEST_IMPL(condvar_4) {
  worker_config wc;
  uv_thread_t thread;

  /* Helper to signal-then-wait. */
  worker_config_init(&wc, 1, condvar_signal, condvar_timedwait);
  ASSERT(0 == uv_thread_create(&thread, worker, &wc));

  /* We wait-then-signal. */
  wc.wait_cond(&wc, &wc.posted_1);
  wc.signal_cond(&wc, &wc.posted_2);

  ASSERT(0 == uv_thread_join(&thread));
  worker_config_destroy(&wc);

  return 0;
}

/* uv_cond_timedwait: One thread waits, no signal. Timeout should be delivered. */
TEST_IMPL(condvar_5) {
  worker_config wc;
  int r;
  /* ns */
  uint64_t before;
  uint64_t after;
  uint64_t elapsed;
  uint64_t timeout;

  timeout = 100 * 1000 * 1000; /* 100 ms in ns */

  /* Mostly irrelevant. We need cond and mutex initialized. */
  worker_config_init(&wc, 0, NULL, NULL);

  uv_mutex_lock(&wc.mutex);

  /* We wait.
   * No signaler, so this will only return if timeout is delivered. */
  before = uv_hrtime();
  r = uv_cond_timedwait(&wc.cond, &wc.mutex, timeout);
  after = uv_hrtime();

  uv_mutex_unlock(&wc.mutex);

  /* It timed out. */
  ASSERT(r == UV_ETIMEDOUT);

  /* It must have taken at least timeout, modulo system timer ticks.
   * But it should not take too much longer.
   * cf. MSDN docs:
   * https://msdn.microsoft.com/en-us/library/ms687069(VS.85).aspx */
  elapsed = after - before;
  ASSERT(0.75 * timeout <= elapsed); /* 1.0 too large for Windows. */
  ASSERT(elapsed <= 5.0 * timeout); /* MacOS has reported failures up to 1.75. */

  worker_config_destroy(&wc);

  return 0;
}

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.559 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )