c5040c591a238c0a718a7f1d30922d979d932b15
[external/glibc.git] / nptl / tst-abstime.c
1 /* Copyright (C) 2010-2019 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Andreas Schwab <schwab@redhat.com>, 2010.
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    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <semaphore.h>
22 #include <stdio.h>
23 #include <support/check.h>
24 #include <support/xthread.h>
25
26 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
27 static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_rwlock_t rw1 = PTHREAD_RWLOCK_INITIALIZER;
30 static pthread_rwlock_t rw2 = PTHREAD_RWLOCK_INITIALIZER;
31 static sem_t sem;
32
33 static void *
34 th (void *arg)
35 {
36   struct timespec t = { -2, 0 };
37
38   TEST_COMPARE (pthread_mutex_timedlock (&m1, &t), ETIMEDOUT);
39   TEST_COMPARE (pthread_rwlock_timedrdlock (&rw1, &t), ETIMEDOUT);
40   TEST_COMPARE (pthread_rwlock_timedwrlock (&rw2, &t), ETIMEDOUT);
41   TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t),
42                 ETIMEDOUT);
43   TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_REALTIME, &t),
44                 ETIMEDOUT);
45   TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t),
46                 ETIMEDOUT);
47   TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_MONOTONIC, &t),
48                 ETIMEDOUT);
49   return NULL;
50 }
51
52 static int
53 do_test (void)
54 {
55   struct timespec t = { -2, 0 };
56
57   sem_init (&sem, 0, 0);
58   TEST_COMPARE (sem_timedwait (&sem, &t), -1);
59   TEST_COMPARE (errno, ETIMEDOUT);
60
61   xpthread_mutex_lock (&m1);
62   xpthread_rwlock_wrlock (&rw1);
63   xpthread_rwlock_rdlock (&rw2);
64   xpthread_mutex_lock (&m2);
65   pthread_t pth = xpthread_create (0, th, 0);
66   TEST_COMPARE (pthread_cond_timedwait (&c, &m2, &t), ETIMEDOUT);
67   xpthread_join (pth);
68   return 0;
69 }
70
71 #include <support/test-driver.c>