1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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 License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
27 #include <support/check.h>
28 #include <support/timespec.h>
29 #include <support/xthread.h>
37 static const struct timespec timeout
= { 0,1000000 };
38 static const struct timespec delay
= { 0, 1000000 };
41 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
44 /* A bogus clock value that tells the tests to use pthread_rwlock_timedrdlock
45 and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and
46 pthread_rwlock_clockwrlock. */
47 #define CLOCK_USE_TIMEDLOCK (-1)
49 static pthread_rwlock_t lock
;
59 writer_thread (void *arg
)
61 struct thread_args
*args
= arg
;
62 const int nr
= args
->nr
;
63 const clockid_t clockid
= args
->clockid
;
64 const clockid_t clockid_for_get
=
65 (clockid
== CLOCK_USE_TIMEDLOCK
) ? CLOCK_REALTIME
: clockid
;
66 const char *fnname
= args
->fnname
;
71 for (n
= 0; n
< WRITETRIES
; ++n
)
76 xclock_gettime (clockid_for_get
, &ts
);
78 ts
= timespec_add (ts
, timeout
);
79 ts
= timespec_add (ts
, timeout
);
81 printf ("writer thread %d tries again\n", nr
);
83 e
= (clockid
== CLOCK_USE_TIMEDLOCK
)
84 ? pthread_rwlock_timedwrlock (&lock
, &ts
)
85 : pthread_rwlock_clockwrlock (&lock
, clockid
, &ts
);
86 if (e
!= 0 && e
!= ETIMEDOUT
)
87 FAIL_EXIT1 ("%swrlock failed", fnname
);
89 while (e
== ETIMEDOUT
);
91 printf ("writer thread %d succeeded\n", nr
);
93 nanosleep (&delay
, NULL
);
95 if (pthread_rwlock_unlock (&lock
) != 0)
96 FAIL_EXIT1 ("unlock for writer failed");
98 printf ("writer thread %d released\n", nr
);
106 reader_thread (void *arg
)
108 struct thread_args
*args
= arg
;
109 const int nr
= args
->nr
;
110 const clockid_t clockid
= args
->clockid
;
111 const clockid_t clockid_for_get
=
112 (clockid
== CLOCK_USE_TIMEDLOCK
) ? CLOCK_REALTIME
: clockid
;
113 const char *fnname
= args
->fnname
;
118 for (n
= 0; n
< READTRIES
; ++n
)
123 xclock_gettime (clockid_for_get
, &ts
);
125 ts
= timespec_add (ts
, timeout
);
127 printf ("reader thread %d tries again\n", nr
);
129 if (clockid
== CLOCK_USE_TIMEDLOCK
)
130 e
= pthread_rwlock_timedrdlock (&lock
, &ts
);
132 e
= pthread_rwlock_clockrdlock (&lock
, clockid
, &ts
);
133 if (e
!= 0 && e
!= ETIMEDOUT
)
134 FAIL_EXIT1 ("%srdlock failed", fnname
);
136 while (e
== ETIMEDOUT
);
138 printf ("reader thread %d succeeded\n", nr
);
140 nanosleep (&delay
, NULL
);
142 if (pthread_rwlock_unlock (&lock
) != 0)
143 FAIL_EXIT1 ("unlock for reader failed");
145 printf ("reader thread %d released\n", nr
);
153 do_test_clock (clockid_t clockid
, const char *fnname
)
155 pthread_t thwr
[NWRITERS
];
156 pthread_t thrd
[NREADERS
];
158 pthread_rwlockattr_t a
;
160 if (pthread_rwlockattr_init (&a
) != 0)
161 FAIL_EXIT1 ("rwlockattr_t failed");
163 if (pthread_rwlockattr_setkind_np (&a
, KIND
) != 0)
164 FAIL_EXIT1 ("rwlockattr_setkind failed");
166 if (pthread_rwlock_init (&lock
, &a
) != 0)
167 FAIL_EXIT1 ("rwlock_init failed");
169 /* Make standard error the same as standard output. */
172 /* Make sure we see all message, even those on stdout. */
173 setvbuf (stdout
, NULL
, _IONBF
, 0);
175 struct thread_args wargs
[NWRITERS
];
176 for (n
= 0; n
< NWRITERS
; ++n
) {
178 wargs
[n
].clockid
= clockid
;
179 wargs
[n
].fnname
= fnname
;
180 thwr
[n
] = xpthread_create (NULL
, writer_thread
, &wargs
[n
]);
183 struct thread_args rargs
[NREADERS
];
184 for (n
= 0; n
< NREADERS
; ++n
) {
186 rargs
[n
].clockid
= clockid
;
187 rargs
[n
].fnname
= fnname
;
188 thrd
[n
] = xpthread_create (NULL
, reader_thread
, &rargs
[n
]);
191 /* Wait for all the threads. */
192 for (n
= 0; n
< NWRITERS
; ++n
)
193 xpthread_join (thwr
[n
]);
194 for (n
= 0; n
< NREADERS
; ++n
)
195 xpthread_join (thrd
[n
]);
203 do_test_clock (CLOCK_USE_TIMEDLOCK
, "timed");
204 do_test_clock (CLOCK_REALTIME
, "clock(realtime)");
205 do_test_clock (CLOCK_MONOTONIC
, "clock(monotonic)");
211 #include <support/test-driver.c>