]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-rwlock6.c
Rename sys/ucontext.h to bits/ucontext.h.
[thirdparty/glibc.git] / nptl / tst-rwlock6.c
CommitLineData
d614a753 1/* Copyright (C) 2002-2020 Free Software Foundation, Inc.
76a50749
UD
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
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749
UD
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>
495514ee 25#include <support/check.h>
e996fa72 26#include <support/test-driver.h>
495514ee
MC
27#include <support/timespec.h>
28#include <support/xthread.h>
29#include <support/xtime.h>
76a50749
UD
30
31
e996fa72
MC
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
76a50749
UD
37static int kind[] =
38 {
39 PTHREAD_RWLOCK_PREFER_READER_NP,
40 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
41 PTHREAD_RWLOCK_PREFER_WRITER_NP,
42 };
43
e996fa72
MC
44struct thread_args
45{
46 pthread_rwlock_t *rwlock;
47 clockid_t clockid;
48 const char *fnname;
49};
76a50749
UD
50
51static void *
52tf (void *arg)
53{
e996fa72
MC
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;
76a50749
UD
60
61 /* Timeout: 0.3 secs. */
495514ee 62 struct timespec ts_start;
e996fa72 63 xclock_gettime (clockid_for_get, &ts_start);
76a50749 64
495514ee
MC
65 struct timespec ts_timeout = timespec_add (ts_start,
66 make_timespec (0, 300000000));
76a50749 67
e996fa72 68 verbose_printf ("child calling %srdlock\n", fnname);
51d0678c 69
e996fa72
MC
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);
76a50749 75
e996fa72 76 verbose_printf ("1st child %srdlock done\n", fnname);
51d0678c 77
495514ee 78 TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout);
76a50749 79
e996fa72 80 xclock_gettime (clockid_for_get, &ts_timeout);
495514ee 81 ts_timeout.tv_sec += 10;
0a37669a 82 /* Note that the following operation makes ts invalid. */
495514ee 83 ts_timeout.tv_nsec += 1000000000;
0a37669a 84
e996fa72
MC
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);
0a37669a 89
e996fa72 90 verbose_printf ("2nd child %srdlock done\n", fnname);
51d0678c 91
76a50749
UD
92 return NULL;
93}
94
95
96static int
e996fa72 97do_test_clock (clockid_t clockid, const char *fnname)
76a50749 98{
e996fa72
MC
99 const clockid_t clockid_for_get =
100 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
f9657e88 101 size_t cnt;
76a50749
UD
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)
495514ee 108 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
76a50749
UD
109
110 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
495514ee 111 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
76a50749
UD
112
113 if (pthread_rwlock_init (&r, &a) != 0)
495514ee 114 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
76a50749
UD
115
116 if (pthread_rwlockattr_destroy (&a) != 0)
495514ee 117 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
76a50749
UD
118
119 struct timespec ts;
e996fa72 120 xclock_gettime (clockid_for_get, &ts);
76a50749
UD
121 ++ts.tv_sec;
122
123 /* Get a write lock. */
e996fa72
MC
124 int e = (clockid == CLOCK_USE_TIMEDLOCK)
125 ? pthread_rwlock_timedwrlock (&r, &ts)
126 : pthread_rwlock_clockwrlock (&r, clockid, &ts);
51d0678c 127 if (e != 0)
e996fa72
MC
128 FAIL_EXIT1 ("round %Zu: %swrlock failed (%d)\n",
129 cnt, fnname, e);
76a50749 130
e996fa72 131 verbose_printf ("1st %swrlock done\n", fnname);
51d0678c 132
e996fa72 133 xclock_gettime (clockid_for_get, &ts);
0a37669a 134 ++ts.tv_sec;
e996fa72
MC
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);
0a37669a 139
e996fa72 140 verbose_printf ("1st %srdlock done\n", fnname);
51d0678c 141
e996fa72 142 xclock_gettime (clockid_for_get, &ts);
0a37669a 143 ++ts.tv_sec;
e996fa72
MC
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);
0a37669a 148
e996fa72 149 verbose_printf ("2nd %swrlock done\n", fnname);
51d0678c 150
e996fa72
MC
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);
76a50749 156
51d0678c
UD
157 puts ("started thread");
158
e996fa72 159 (void) xpthread_join (th);
76a50749 160
51d0678c
UD
161 puts ("joined thread");
162
76a50749 163 if (pthread_rwlock_destroy (&r) != 0)
495514ee 164 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
76a50749
UD
165 }
166
167 return 0;
168}
169
e996fa72
MC
170static 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
495514ee 178#include <support/test-driver.c>