Blame htl/tests/test-14.c

Packit 6c4009
/* Test pthread_mutex_timedlock.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library;  if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#define _GNU_SOURCE
Packit 6c4009
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  struct timespec ts;
Packit 6c4009
  pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
  struct timeval before, after;
Packit 6c4009
  int diff;
Packit 6c4009
Packit 6c4009
  gettimeofday (&before, 0);
Packit 6c4009
  ts.tv_sec = before.tv_sec + 1;
Packit 6c4009
  ts.tv_nsec = before.tv_usec * 1000;
Packit 6c4009
Packit 6c4009
  printf ("Starting wait @ %d\n", (int) before.tv_sec);
Packit 6c4009
Packit 6c4009
  pthread_mutex_lock (&m);
Packit 6c4009
  /* A default mutex shall dead lock if locked twice.  As such we do
Packit 6c4009
     not need spawn a second thread.  */
Packit 6c4009
  err = pthread_mutex_timedlock (&m, &ts);
Packit 6c4009
  assert (err == ETIMEDOUT);
Packit 6c4009
Packit 6c4009
  gettimeofday (&after, 0);
Packit 6c4009
Packit 6c4009
  printf ("End wait @ %d\n", (int) after.tv_sec);
Packit 6c4009
Packit 6c4009
  diff = after.tv_sec * 1000000 + after.tv_usec
Packit 6c4009
      - before.tv_sec * 1000000 - before.tv_usec;
Packit 6c4009
Packit 6c4009
  if (diff < 900000 || diff > 1100000)
Packit 6c4009
    error (1, EGRATUITOUS, "pthread_mutex_timedlock waited %d us", diff);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}