Ejemplo n.º 1
0
gboolean
timespec_equal (const Timespec *ta, const Timespec *tb)
{
    Timespec pta, ptb;

    if (ta == tb) return TRUE;
/* Copy and normalize the copies */
    pta = *ta;
    ptb = *tb;
    timespec_normalize (&pta);
    timespec_normalize (&ptb);

    if (pta.tv_sec != ptb.tv_sec) return FALSE;
    if (pta.tv_nsec != ptb.tv_nsec) return FALSE;
    return TRUE;
}
Ejemplo n.º 2
0
int
main(void)
{
  int err, conderr;
  long *input_set;
  int i, size = NUM_ITERATION;

  input_set = polynomial_dist(size, 4, 0, 10000000);

  pthread_condattr_t condattr;
  pthread_condattr_init(&condattr);
#ifdef COND_SETCLOCK
  pthread_condattr_setclock(&condattr, CLOCK_ID);
#endif
  pthread_cond_init(&cond, &condattr);

  for (i = 0; i < size; i++) {
    struct timespec ts_begin, ts_deadline, ts_end, ts_diff;

    err = pthread_mutex_lock(&mutex);
    if (err != 0)
      error(1, err, "pthread_mutex_lock() failed");

    err = clock_gettime(CLOCK_ID, &ts_begin);
    if (err != 0)
      error(1, errno, "clock_gettime() failed");

    ts_deadline = ts_begin;
    ts_deadline.tv_nsec += input_set[i];
    timespec_normalize(&ts_deadline);

    conderr = pthread_cond_timedwait(&cond, &mutex, &ts_deadline);

    err = clock_gettime(CLOCK_ID, &ts_end);
    if (err != 0)
      error(1, errno, "clock_gettime() failed");

    timespec_subtract(&ts_diff, &ts_deadline, &ts_end);

    if (conderr != 0) {
      if (conderr == ETIMEDOUT) {
        printf("TIMEOUT,%.9Lf,%ld.%09ld\n",
               (long double)input_set[i] / 1000000000, ts_diff.tv_sec, ts_diff.tv_nsec);
      }
      else
        error(1, conderr, "pthread_cond_timedwait() failed");

      err = pthread_mutex_unlock(&mutex);
      if (err != 0)
        error(1, err, "pthread_mutex_unlock() failed");
    }
    else {                      /* unlikely, except suprious wakeup */
      printf("SUCCESS %Lf %ld.%09ld\n",
             (long double)input_set[i] / 1000000000, ts_diff.tv_sec, ts_diff.tv_nsec);
    }
  }

  return 0;
}
Ejemplo n.º 3
0
Timespec
timespec_diff(const Timespec *ta, const Timespec *tb)
{
    Timespec retval;
    retval.tv_sec = ta->tv_sec - tb->tv_sec;
    retval.tv_nsec = ta->tv_nsec - tb->tv_nsec;
    timespec_normalize(&retval);
    return retval;
}
Ejemplo n.º 4
0
gint
timespec_cmp(const Timespec *ta, const Timespec *tb)
{
    Timespec pta, ptb;

    if (ta == tb) return 0;
/* Copy and normalize the copies */
    pta = *ta;
    ptb = *tb;
    timespec_normalize (&pta);
    timespec_normalize (&ptb);

    if (pta.tv_sec < ptb.tv_sec) return -1;
    if (pta.tv_sec > ptb.tv_sec) return 1;
    if (pta.tv_nsec < ptb.tv_nsec) return -1;
    if (pta.tv_nsec > ptb.tv_nsec) return 1;
    return 0;
}
Ejemplo n.º 5
0
void
timespec_diff(struct timespec *sum,
              const struct timespec *begin, const struct timespec *end)
{
  struct timespec ts;
  ts.tv_nsec = end->tv_nsec - begin->tv_nsec;
  ts.tv_sec = end->tv_sec - begin->tv_sec;

  sum->tv_sec += ts.tv_sec;
  sum->tv_nsec += ts.tv_nsec;
  timespec_normalize(sum);
}
Ejemplo n.º 6
0
Timespec
timespec_abs(const Timespec *t)
{
    Timespec retval = *t;

    timespec_normalize(&retval);
    if (retval.tv_sec < 0)
    {
        retval.tv_sec = - retval.tv_sec;
        retval.tv_nsec = - retval.tv_nsec;
    }

    return retval;
}