1 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <support/check.h>
24 #include <support/xthread.h>
25 #include <support/xtime.h>
26 
27 
28 static pthread_barrier_t b;
29 static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER;
30 
31 
32 static void *
tf(void * arg)33 tf (void *arg)
34 {
35   /* Lock the read-write lock.  */
36   TEST_COMPARE (pthread_rwlock_wrlock (&r), 0);
37 
38   pthread_t mt = *(pthread_t *) arg;
39 
40   xpthread_barrier_wait (&b);
41 
42   /* This call will never return.  */
43   xpthread_join (mt);
44 
45   return NULL;
46 }
47 
48 
49 static int
do_test(void)50 do_test (void)
51 {
52   struct timespec ts;
53 
54   xclock_gettime (CLOCK_REALTIME, &ts);
55   xpthread_barrier_init (&b, NULL, 2);
56 
57   pthread_t me = pthread_self ();
58   xpthread_create (NULL, tf, &me);
59 
60   /* Wait until the rwlock is locked.  */
61   xpthread_barrier_wait (&b);
62 
63   ts.tv_nsec = -1;
64 
65   TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL);
66   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL);
67   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
68   TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL);
69   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL);
70   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
71 
72   ts.tv_nsec = 1000000000;
73 
74   TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL);
75   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL);
76   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
77   TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL);
78   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL);
79   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
80 
81   ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL;
82   if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL)
83     ts.tv_nsec = 2000000000;
84 
85   TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL);
86   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL);
87   TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
88   TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL);
89   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL);
90   TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL);
91 
92   return 0;
93 }
94 
95 #include <support/test-driver.c>
96