1 /* Test rdlock overflow.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
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 License as
6 published by the Free Software Foundation; either version 2.1 of the
7 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; see the file COPYING.LIB. If
16 not, see <https://www.gnu.org/licenses/>. */
29 #define READTRIES 5000
33 static pthread_rwlock_t lock
= PTHREAD_RWLOCK_INITIALIZER
;
34 static int eagain_returned
= 0;
35 static int success_returned
= 0;
38 reader_thread (void *nr
)
40 struct timespec delay
;
44 delay
.tv_nsec
= DELAY
;
46 for (n
= 0; n
< READTRIES
; ++n
)
48 int err
= pthread_rwlock_rdlock (&lock
);
51 atomic_store_relaxed (&eagain_returned
, 1);
55 atomic_store_relaxed (&success_returned
, 1);
58 puts ("rdlock failed");
62 nanosleep (&delay
, NULL
);
64 if (pthread_rwlock_unlock (&lock
) != 0)
66 puts ("unlock for reader failed");
78 pthread_t thrd
[NREADERS
];
82 /* Set the rwlock so that it's close to a reader overflow.
83 PTHREAD_RWLOCK_WRPHASE and PTHREAD_RWLOCK_WRLOCK are zero initially. */
84 unsigned int readers
= PTHREAD_RWLOCK_READER_OVERFLOW
85 - ((NREADERS
/ 3) << PTHREAD_RWLOCK_READER_SHIFT
);
86 lock
.__data
.__readers
= readers
;
88 for (n
= 0; n
< NREADERS
; ++n
)
89 if (pthread_create (&thrd
[n
], NULL
, reader_thread
,
90 (void *) (long int) n
) != 0)
92 puts ("reader create failed");
96 /* Wait for all the threads. */
97 for (n
= 0; n
< NREADERS
; ++n
)
98 if (pthread_join (thrd
[n
], &res
) != 0)
100 puts ("reader join failed");
104 if (atomic_load_relaxed (&eagain_returned
) == 0)
106 puts ("EAGAIN has never been returned");
110 if (atomic_load_relaxed (&success_returned
) == 0)
112 puts ("rdlock was never successfully acquired");
116 if (lock
.__data
.__readers
!= readers
)
118 puts ("__readers in rwlock differs from initial value");
126 #define TEST_FUNCTION do_test ()
127 #include "../test-skeleton.c"