1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
24 #include <support/check.h>
25 #include <support/test-driver.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)
38 PTHREAD_RWLOCK_PREFER_READER_NP
,
39 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
40 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
45 pthread_rwlock_t
*rwlock
;
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
);
64 struct timespec ts_timeout
= timespec_add (ts_start
,
65 make_timespec (0, 300000000));
67 verbose_printf ("child calling %srdlock\n", fnname
);
69 if (clockid
== CLOCK_USE_TIMEDLOCK
)
70 TEST_COMPARE (pthread_rwlock_timedrdlock (r
, &ts_timeout
), ETIMEDOUT
);
72 TEST_COMPARE (pthread_rwlock_clockrdlock (r
, clockid
, &ts_timeout
),
75 verbose_printf ("1st child %srdlock done\n", fnname
);
77 TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME
, ts_timeout
);
79 xclock_gettime (clockid_for_get
, &ts_timeout
);
80 ts_timeout
.tv_sec
+= 10;
81 /* Note that the following operation makes ts invalid. */
82 ts_timeout
.tv_nsec
+= 1000000000;
84 if (clockid
== CLOCK_USE_TIMEDLOCK
)
85 TEST_COMPARE (pthread_rwlock_timedrdlock (r
, &ts_timeout
), EINVAL
);
87 TEST_COMPARE (pthread_rwlock_clockrdlock (r
, clockid
, &ts_timeout
), EINVAL
);
89 verbose_printf ("2nd child %srdlock done\n", fnname
);
96 do_test_clock (clockid_t clockid
, const char *fnname
)
98 const clockid_t clockid_for_get
=
99 (clockid
== CLOCK_USE_TIMEDLOCK
) ? CLOCK_REALTIME
: clockid
;
101 for (cnt
= 0; cnt
< sizeof (kind
) / sizeof (kind
[0]); ++cnt
)
104 pthread_rwlockattr_t a
;
106 if (pthread_rwlockattr_init (&a
) != 0)
107 FAIL_EXIT1 ("round %zu: rwlockattr_t failed\n", cnt
);
109 if (pthread_rwlockattr_setkind_np (&a
, kind
[cnt
]) != 0)
110 FAIL_EXIT1 ("round %zu: rwlockattr_setkind failed\n", cnt
);
112 if (pthread_rwlock_init (&r
, &a
) != 0)
113 FAIL_EXIT1 ("round %zu: rwlock_init failed\n", cnt
);
115 if (pthread_rwlockattr_destroy (&a
) != 0)
116 FAIL_EXIT1 ("round %zu: rwlockattr_destroy failed\n", cnt
);
119 xclock_gettime (clockid_for_get
, &ts
);
122 /* Get a write lock. */
123 int e
= (clockid
== CLOCK_USE_TIMEDLOCK
)
124 ? pthread_rwlock_timedwrlock (&r
, &ts
)
125 : pthread_rwlock_clockwrlock (&r
, clockid
, &ts
);
127 FAIL_EXIT1 ("round %zu: %swrlock failed (%d)\n",
130 verbose_printf ("1st %swrlock done\n", fnname
);
132 xclock_gettime (clockid_for_get
, &ts
);
134 if (clockid
== CLOCK_USE_TIMEDLOCK
)
135 TEST_COMPARE (pthread_rwlock_timedrdlock (&r
, &ts
), EDEADLK
);
137 TEST_COMPARE (pthread_rwlock_clockrdlock (&r
, clockid
, &ts
), EDEADLK
);
139 verbose_printf ("1st %srdlock done\n", fnname
);
141 xclock_gettime (clockid_for_get
, &ts
);
143 if (clockid
== CLOCK_USE_TIMEDLOCK
)
144 TEST_COMPARE (pthread_rwlock_timedwrlock (&r
, &ts
), EDEADLK
);
146 TEST_COMPARE (pthread_rwlock_clockwrlock (&r
, clockid
, &ts
), EDEADLK
);
148 verbose_printf ("2nd %swrlock done\n", fnname
);
150 struct thread_args args
;
152 args
.clockid
= clockid
;
153 args
.fnname
= fnname
;
154 pthread_t th
= xpthread_create (NULL
, tf
, &args
);
156 puts ("started thread");
158 (void) xpthread_join (th
);
160 puts ("joined thread");
162 if (pthread_rwlock_destroy (&r
) != 0)
163 FAIL_EXIT1 ("round %zu: rwlock_destroy failed\n", cnt
);
169 static int do_test (void)
171 do_test_clock (CLOCK_USE_TIMEDLOCK
, "timed");
172 do_test_clock (CLOCK_REALTIME
, "clock(realtime)");
173 do_test_clock (CLOCK_MONOTONIC
, "clock(monotonic)");
177 #include <support/test-driver.c>