]>
Commit | Line | Data |
---|---|---|
581c785b | 1 | /* Copyright (C) 2002-2022 Free Software Foundation, Inc. |
76a50749 | 2 | This file is part of the GNU C Library. |
76a50749 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
17 | |
18 | #include <errno.h> | |
19 | #include <pthread.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <sys/time.h> | |
495514ee MC |
24 | #include <support/check.h> |
25 | #include <support/timespec.h> | |
26 | #include <support/xthread.h> | |
27 | #include <support/xtime.h> | |
76a50749 UD |
28 | |
29 | ||
e996fa72 MC |
30 | /* A bogus clock value that tells run_test to use pthread_rwlock_timedrdlock |
31 | and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and | |
32 | pthread_rwlock_clockwrlock. */ | |
33 | #define CLOCK_USE_TIMEDLOCK (-1) | |
34 | ||
76a50749 UD |
35 | static int kind[] = |
36 | { | |
37 | PTHREAD_RWLOCK_PREFER_READER_NP, | |
38 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, | |
39 | PTHREAD_RWLOCK_PREFER_WRITER_NP, | |
40 | }; | |
41 | ||
e996fa72 MC |
42 | struct thread_args |
43 | { | |
44 | pthread_rwlock_t *rwlock; | |
45 | clockid_t clockid; | |
46 | const char *fnname; | |
47 | }; | |
76a50749 UD |
48 | |
49 | static void * | |
50 | tf (void *arg) | |
51 | { | |
e996fa72 MC |
52 | struct thread_args *args = arg; |
53 | pthread_rwlock_t *r = args->rwlock; | |
54 | const clockid_t clockid = args->clockid; | |
55 | const clockid_t clockid_for_get = | |
56 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
57 | const char *fnname = args->fnname; | |
76a50749 UD |
58 | |
59 | /* Timeout: 0.3 secs. */ | |
495514ee | 60 | struct timespec ts_start; |
e996fa72 | 61 | xclock_gettime (clockid_for_get, &ts_start); |
495514ee MC |
62 | const struct timespec ts_timeout = timespec_add (ts_start, |
63 | make_timespec (0, 300000000)); | |
76a50749 | 64 | |
e996fa72 MC |
65 | if (clockid == CLOCK_USE_TIMEDLOCK) |
66 | TEST_COMPARE (pthread_rwlock_timedwrlock (r, &ts_timeout), ETIMEDOUT); | |
67 | else | |
68 | TEST_COMPARE (pthread_rwlock_clockwrlock (r, clockid, &ts_timeout), | |
69 | ETIMEDOUT); | |
70 | printf ("child: %swrlock failed with ETIMEDOUT", fnname); | |
76a50749 | 71 | |
e996fa72 | 72 | TEST_TIMESPEC_NOW_OR_AFTER (clockid_for_get, ts_timeout); |
76a50749 | 73 | |
495514ee | 74 | struct timespec ts_invalid; |
e996fa72 | 75 | xclock_gettime (clockid_for_get, &ts_invalid); |
495514ee | 76 | ts_invalid.tv_sec += 10; |
0a37669a | 77 | /* Note that the following operation makes ts invalid. */ |
495514ee MC |
78 | ts_invalid.tv_nsec += 1000000000; |
79 | ||
e996fa72 MC |
80 | if (clockid == CLOCK_USE_TIMEDLOCK) |
81 | TEST_COMPARE (pthread_rwlock_timedwrlock (r, &ts_invalid), EINVAL); | |
82 | else | |
83 | TEST_COMPARE (pthread_rwlock_clockwrlock (r, clockid, &ts_invalid), EINVAL); | |
0a37669a | 84 | |
e996fa72 | 85 | printf ("child: %swrlock failed with EINVAL", fnname); |
0a37669a | 86 | |
76a50749 UD |
87 | return NULL; |
88 | } | |
89 | ||
90 | ||
91 | static int | |
e996fa72 | 92 | do_test_clock (clockid_t clockid, const char *fnname) |
76a50749 | 93 | { |
e996fa72 MC |
94 | const clockid_t clockid_for_get = |
95 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
f9657e88 | 96 | size_t cnt; |
76a50749 UD |
97 | for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) |
98 | { | |
99 | pthread_rwlock_t r; | |
100 | pthread_rwlockattr_t a; | |
101 | ||
102 | if (pthread_rwlockattr_init (&a) != 0) | |
495514ee | 103 | FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n", cnt); |
76a50749 UD |
104 | |
105 | if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0) | |
495514ee | 106 | FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n", cnt); |
76a50749 UD |
107 | |
108 | if (pthread_rwlock_init (&r, &a) != 0) | |
495514ee | 109 | FAIL_EXIT1 ("round %Zu: rwlock_init failed\n", cnt); |
76a50749 UD |
110 | |
111 | if (pthread_rwlockattr_destroy (&a) != 0) | |
495514ee | 112 | FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n", cnt); |
76a50749 UD |
113 | |
114 | struct timespec ts; | |
e996fa72 | 115 | xclock_gettime (clockid_for_get, &ts); |
76a50749 UD |
116 | |
117 | ++ts.tv_sec; | |
118 | ||
119 | /* Get a read lock. */ | |
e996fa72 MC |
120 | if (clockid == CLOCK_USE_TIMEDLOCK) { |
121 | if (pthread_rwlock_timedrdlock (&r, &ts) != 0) | |
122 | FAIL_EXIT1 ("round %Zu: rwlock_timedrdlock failed\n", cnt); | |
123 | } else { | |
124 | if (pthread_rwlock_clockrdlock (&r, clockid, &ts) != 0) | |
125 | FAIL_EXIT1 ("round %Zu: rwlock_%srdlock failed\n", cnt, fnname); | |
126 | } | |
127 | ||
128 | printf ("%zu: got %srdlock\n", cnt, fnname); | |
129 | ||
130 | struct thread_args args; | |
131 | args.rwlock = &r; | |
132 | args.clockid = clockid; | |
133 | args.fnname = fnname; | |
134 | pthread_t th = xpthread_create (NULL, tf, &args); | |
135 | void *status = xpthread_join (th); | |
76a50749 | 136 | if (status != NULL) |
495514ee | 137 | FAIL_EXIT1 ("failure in round %Zu\n", cnt); |
76a50749 UD |
138 | |
139 | if (pthread_rwlock_destroy (&r) != 0) | |
495514ee | 140 | FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n", cnt); |
76a50749 UD |
141 | } |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
e996fa72 MC |
146 | static int |
147 | do_test (void) | |
148 | { | |
149 | do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); | |
150 | do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); | |
151 | do_test_clock (CLOCK_REALTIME, "clock(realtime)"); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
495514ee | 156 | #include <support/test-driver.c> |