]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-rwlock7.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-rwlock7.c
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 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
MC
25#include <support/check.h>
26#include <support/timespec.h>
27#include <support/xthread.h>
28#include <support/xtime.h>
76a50749
UD
29
30
e996fa72
MC
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)
35
76a50749
UD
36static int kind[] =
37 {
38 PTHREAD_RWLOCK_PREFER_READER_NP,
39 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
40 PTHREAD_RWLOCK_PREFER_WRITER_NP,
41 };
42
e996fa72
MC
43struct thread_args
44{
45 pthread_rwlock_t *rwlock;
46 clockid_t clockid;
47 const char *fnname;
48};
76a50749
UD
49
50static void *
51tf (void *arg)
52{
e996fa72
MC
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;
76a50749
UD
59
60 /* Timeout: 0.3 secs. */
495514ee 61 struct timespec ts_start;
e996fa72 62 xclock_gettime (clockid_for_get, &ts_start);
495514ee
MC
63 const struct timespec ts_timeout = timespec_add (ts_start,
64 make_timespec (0, 300000000));
76a50749 65
e996fa72
MC
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);
76a50749 72
e996fa72 73 TEST_TIMESPEC_NOW_OR_AFTER (clockid_for_get, ts_timeout);
76a50749 74
495514ee 75 struct timespec ts_invalid;
e996fa72 76 xclock_gettime (clockid_for_get, &ts_invalid);
495514ee 77 ts_invalid.tv_sec += 10;
0a37669a 78 /* Note that the following operation makes ts invalid. */
495514ee
MC
79 ts_invalid.tv_nsec += 1000000000;
80
e996fa72
MC
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);
0a37669a 85
e996fa72 86 printf ("child: %swrlock failed with EINVAL", fnname);
0a37669a 87
76a50749
UD
88 return NULL;
89}
90
91
92static int
e996fa72 93do_test_clock (clockid_t clockid, const char *fnname)
76a50749 94{
e996fa72
MC
95 const clockid_t clockid_for_get =
96 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
f9657e88 97 size_t cnt;
76a50749
UD
98 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
99 {
100 pthread_rwlock_t r;
101 pthread_rwlockattr_t a;
102
103 if (pthread_rwlockattr_init (&a) != 0)
495514ee 104 FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt);
76a50749
UD
105
106 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
495514ee 107 FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt);
76a50749
UD
108
109 if (pthread_rwlock_init (&r, &a) != 0)
495514ee 110 FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt);
76a50749
UD
111
112 if (pthread_rwlockattr_destroy (&a) != 0)
495514ee 113 FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt);
76a50749
UD
114
115 struct timespec ts;
e996fa72 116 xclock_gettime (clockid_for_get, &ts);
76a50749
UD
117
118 ++ts.tv_sec;
119
120 /* Get a read lock. */
e996fa72
MC
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);
127 }
128
129 printf ("%zu: got %srdlock\n", cnt, fnname);
130
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);
76a50749 137 if (status != NULL)
495514ee 138 FAIL_EXIT1 ("failure in round %Zu\n", cnt);
76a50749
UD
139
140 if (pthread_rwlock_destroy (&r) != 0)
495514ee 141 FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt);
76a50749
UD
142 }
143
144 return 0;
145}
146
e996fa72
MC
147static int
148do_test (void)
149{
150 do_test_clock (CLOCK_USE_TIMEDLOCK, "timed");
151 do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)");
152 do_test_clock (CLOCK_REALTIME, "clock(realtime)");
153
154 return 0;
155}
156
495514ee 157#include <support/test-driver.c>