Makeconfig: sandwich gnulib-tests between libc/ld linking of tests
[glibc.git] / nptl / tst-rwlock6.c
blob36afa22da4b2dfba41b8fd09d8c7dafe1394ddb7
1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <support/check.h>
26 #include <support/test-driver.h>
27 #include <support/timespec.h>
28 #include <support/xthread.h>
29 #include <support/xtime.h>
32 /* A bogus clock value that tells run_test to use pthread_rwlock_timedrdlock
33 and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and
34 pthread_rwlock_clockwrlock. */
35 #define CLOCK_USE_TIMEDLOCK (-1)
37 static int kind[] =
39 PTHREAD_RWLOCK_PREFER_READER_NP,
40 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
41 PTHREAD_RWLOCK_PREFER_WRITER_NP,
44 struct thread_args
46 pthread_rwlock_t *rwlock;
47 clockid_t clockid;
48 const char *fnname;
51 static void *
52 tf (void *arg)
54 struct thread_args *args = arg;
55 pthread_rwlock_t *r = args->rwlock;
56 const clockid_t clockid = args->clockid;
57 const clockid_t clockid_for_get =
58 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
59 const char *fnname = args->fnname;
61 /* Timeout: 0.3 secs. */
62 struct timespec ts_start;
63 xclock_gettime (clockid_for_get, &ts_start);
65 struct timespec ts_timeout = timespec_add (ts_start,
66 make_timespec (0, 300000000));
68 verbose_printf ("child calling %srdlock\n", fnname);
70 if (clockid == CLOCK_USE_TIMEDLOCK)
71 TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), ETIMEDOUT);
72 else
73 TEST_COMPARE (pthread_rwlock_clockrdlock (r, clockid, &ts_timeout),
74 ETIMEDOUT);
76 verbose_printf ("1st child %srdlock done\n", fnname);
78 TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout);
80 xclock_gettime (clockid_for_get, &ts_timeout);
81 ts_timeout.tv_sec += 10;
82 /* Note that the following operation makes ts invalid. */
83 ts_timeout.tv_nsec += 1000000000;
85 if (clockid == CLOCK_USE_TIMEDLOCK)
86 TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), EINVAL);
87 else
88 TEST_COMPARE (pthread_rwlock_clockrdlock (r, clockid, &ts_timeout), EINVAL);
90 verbose_printf ("2nd child %srdlock done\n", fnname);
92 return NULL;
96 static int
97 do_test_clock (clockid_t clockid, const char *fnname)
99 const clockid_t clockid_for_get =
100 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
101 size_t cnt;
102 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
104 pthread_rwlock_t r;
105 pthread_rwlockattr_t a;
107 if (pthread_rwlockattr_init (&a) != 0)
108 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
110 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
111 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
113 if (pthread_rwlock_init (&r, &a) != 0)
114 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
116 if (pthread_rwlockattr_destroy (&a) != 0)
117 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
119 struct timespec ts;
120 xclock_gettime (clockid_for_get, &ts);
121 ++ts.tv_sec;
123 /* Get a write lock. */
124 int e = (clockid == CLOCK_USE_TIMEDLOCK)
125 ? pthread_rwlock_timedwrlock (&r, &ts)
126 : pthread_rwlock_clockwrlock (&r, clockid, &ts);
127 if (e != 0)
128 FAIL_EXIT1 ("round %Zu: %swrlock failed (%d)\n",
129 cnt, fnname, e);
131 verbose_printf ("1st %swrlock done\n", fnname);
133 xclock_gettime (clockid_for_get, &ts);
134 ++ts.tv_sec;
135 if (clockid == CLOCK_USE_TIMEDLOCK)
136 TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EDEADLK);
137 else
138 TEST_COMPARE (pthread_rwlock_clockrdlock (&r, clockid, &ts), EDEADLK);
140 verbose_printf ("1st %srdlock done\n", fnname);
142 xclock_gettime (clockid_for_get, &ts);
143 ++ts.tv_sec;
144 if (clockid == CLOCK_USE_TIMEDLOCK)
145 TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EDEADLK);
146 else
147 TEST_COMPARE (pthread_rwlock_clockwrlock (&r, clockid, &ts), EDEADLK);
149 verbose_printf ("2nd %swrlock done\n", fnname);
151 struct thread_args args;
152 args.rwlock = &r;
153 args.clockid = clockid;
154 args.fnname = fnname;
155 pthread_t th = xpthread_create (NULL, tf, &args);
157 puts ("started thread");
159 (void) xpthread_join (th);
161 puts ("joined thread");
163 if (pthread_rwlock_destroy (&r) != 0)
164 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
167 return 0;
170 static int do_test (void)
172 do_test_clock (CLOCK_USE_TIMEDLOCK, "timed");
173 do_test_clock (CLOCK_REALTIME, "clock(realtime)");
174 do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)");
175 return 0;
178 #include <support/test-driver.c>