(root)/
glibc-2.38/
htl/
tests/
test-14.c
       1  /* Test pthread_mutex_timedlock.
       2     Copyright (C) 2000-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library;  if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #define _GNU_SOURCE
      20  
      21  #include <pthread.h>
      22  #include <stdio.h>
      23  #include <assert.h>
      24  #include <error.h>
      25  #include <errno.h>
      26  #include <sys/time.h>
      27  
      28  int
      29  main (int argc, char **argv)
      30  {
      31    error_t err;
      32    struct timespec ts;
      33    pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
      34    struct timeval before, after;
      35    int diff;
      36  
      37    gettimeofday (&before, 0);
      38    ts.tv_sec = before.tv_sec + 1;
      39    ts.tv_nsec = before.tv_usec * 1000;
      40  
      41    printf ("Starting wait @ %d\n", (int) before.tv_sec);
      42  
      43    pthread_mutex_lock (&m);
      44    /* A default mutex shall dead lock if locked twice.  As such we do
      45       not need spawn a second thread.  */
      46    err = pthread_mutex_timedlock (&m, &ts);
      47    assert (err == ETIMEDOUT);
      48  
      49    gettimeofday (&after, 0);
      50  
      51    printf ("End wait @ %d\n", (int) after.tv_sec);
      52  
      53    diff = after.tv_sec * 1000000 + after.tv_usec
      54        - before.tv_sec * 1000000 - before.tv_usec;
      55  
      56    if (diff < 900000 || diff > 1100000)
      57      error (1, EGRATUITOUS, "pthread_mutex_timedlock waited %d us", diff);
      58  
      59    return 0;
      60  }