]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-rwlock6.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / tst-rwlock6.c
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.
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 #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>
30
31
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)
36
37 static int kind[] =
38 {
39 PTHREAD_RWLOCK_PREFER_READER_NP,
40 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
41 PTHREAD_RWLOCK_PREFER_WRITER_NP,
42 };
43
44 struct thread_args
45 {
46 pthread_rwlock_t *rwlock;
47 clockid_t clockid;
48 const char *fnname;
49 };
50
51 static void *
52 tf (void *arg)
53 {
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;
60
61 /* Timeout: 0.3 secs. */
62 struct timespec ts_start;
63 xclock_gettime (clockid_for_get, &ts_start);
64
65 struct timespec ts_timeout = timespec_add (ts_start,
66 make_timespec (0, 300000000));
67
68 verbose_printf ("child calling %srdlock\n", fnname);
69
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);
75
76 verbose_printf ("1st child %srdlock done\n", fnname);
77
78 TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout);
79
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;
84
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);
89
90 verbose_printf ("2nd child %srdlock done\n", fnname);
91
92 return NULL;
93 }
94
95
96 static int
97 do_test_clock (clockid_t clockid, const char *fnname)
98 {
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)
103 {
104 pthread_rwlock_t r;
105 pthread_rwlockattr_t a;
106
107 if (pthread_rwlockattr_init (&a) != 0)
108 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
109
110 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
111 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
112
113 if (pthread_rwlock_init (&r, &a) != 0)
114 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
115
116 if (pthread_rwlockattr_destroy (&a) != 0)
117 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
118
119 struct timespec ts;
120 xclock_gettime (clockid_for_get, &ts);
121 ++ts.tv_sec;
122
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);
130
131 verbose_printf ("1st %swrlock done\n", fnname);
132
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);
139
140 verbose_printf ("1st %srdlock done\n", fnname);
141
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);
148
149 verbose_printf ("2nd %swrlock done\n", fnname);
150
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);
156
157 puts ("started thread");
158
159 (void) xpthread_join (th);
160
161 puts ("joined thread");
162
163 if (pthread_rwlock_destroy (&r) != 0)
164 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
165 }
166
167 return 0;
168 }
169
170 static int do_test (void)
171 {
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;
176 }
177
178 #include <support/test-driver.c>