Update install.texi, and regenerate INSTALL.
[glibc.git] / nptl / tst-rwlock7.c
blobe32cf323a77258eeef9451f8b7f29c11b77a74ec
1 /* Copyright (C) 2002-2021 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/timespec.h>
27 #include <support/xthread.h>
28 #include <support/xtime.h>
31 /* A bogus clock value that tells run_test to use pthread_rwlock_timedrdlock
32 and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and
33 pthread_rwlock_clockwrlock. */
34 #define CLOCK_USE_TIMEDLOCK (-1)
36 static int kind[] =
38 PTHREAD_RWLOCK_PREFER_READER_NP,
39 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
40 PTHREAD_RWLOCK_PREFER_WRITER_NP,
43 struct thread_args
45 pthread_rwlock_t *rwlock;
46 clockid_t clockid;
47 const char *fnname;
50 static void *
51 tf (void *arg)
53 struct thread_args *args = arg;
54 pthread_rwlock_t *r = args->rwlock;
55 const clockid_t clockid = args->clockid;
56 const clockid_t clockid_for_get =
57 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
58 const char *fnname = args->fnname;
60 /* Timeout: 0.3 secs. */
61 struct timespec ts_start;
62 xclock_gettime (clockid_for_get, &ts_start);
63 const struct timespec ts_timeout = timespec_add (ts_start,
64 make_timespec (0, 300000000));
66 if (clockid == CLOCK_USE_TIMEDLOCK)
67 TEST_COMPARE (pthread_rwlock_timedwrlock (r, &ts_timeout), ETIMEDOUT);
68 else
69 TEST_COMPARE (pthread_rwlock_clockwrlock (r, clockid, &ts_timeout),
70 ETIMEDOUT);
71 printf ("child: %swrlock failed with ETIMEDOUT", fnname);
73 TEST_TIMESPEC_NOW_OR_AFTER (clockid_for_get, ts_timeout);
75 struct timespec ts_invalid;
76 xclock_gettime (clockid_for_get, &ts_invalid);
77 ts_invalid.tv_sec += 10;
78 /* Note that the following operation makes ts invalid. */
79 ts_invalid.tv_nsec += 1000000000;
81 if (clockid == CLOCK_USE_TIMEDLOCK)
82 TEST_COMPARE (pthread_rwlock_timedwrlock (r, &ts_invalid), EINVAL);
83 else
84 TEST_COMPARE (pthread_rwlock_clockwrlock (r, clockid, &ts_invalid), EINVAL);
86 printf ("child: %swrlock failed with EINVAL", fnname);
88 return NULL;
92 static int
93 do_test_clock (clockid_t clockid, const char *fnname)
95 const clockid_t clockid_for_get =
96 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
97 size_t cnt;
98 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
100 pthread_rwlock_t r;
101 pthread_rwlockattr_t a;
103 if (pthread_rwlockattr_init (&a) != 0)
104 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
106 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
107 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
109 if (pthread_rwlock_init (&r, &a) != 0)
110 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
112 if (pthread_rwlockattr_destroy (&a) != 0)
113 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
115 struct timespec ts;
116 xclock_gettime (clockid_for_get, &ts);
118 ++ts.tv_sec;
120 /* Get a read lock. */
121 if (clockid == CLOCK_USE_TIMEDLOCK) {
122 if (pthread_rwlock_timedrdlock (&r, &ts) != 0)
123 FAIL_EXIT1 ("round %Zu: rwlock_timedrdlock failed\n", cnt);
124 } else {
125 if (pthread_rwlock_clockrdlock (&r, clockid, &ts) != 0)
126 FAIL_EXIT1 ("round %Zu: rwlock_%srdlock failed\n", cnt, fnname);
129 printf ("%zu: got %srdlock\n", cnt, fnname);
131 struct thread_args args;
132 args.rwlock = &r;
133 args.clockid = clockid;
134 args.fnname = fnname;
135 pthread_t th = xpthread_create (NULL, tf, &args);
136 void *status = xpthread_join (th);
137 if (status != NULL)
138 FAIL_EXIT1 ("failure in round %Zu\n", cnt);
140 if (pthread_rwlock_destroy (&r) != 0)
141 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
144 return 0;
147 static int
148 do_test (void)
150 do_test_clock (CLOCK_USE_TIMEDLOCK, "timed");
151 do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)");
152 do_test_clock (CLOCK_REALTIME, "clock(realtime)");
154 return 0;
157 #include <support/test-driver.c>