1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000-2021 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/>. */
35 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
38 static pthread_rwlock_t lock
;
42 writer_thread (void *nr
)
44 struct timespec delay
;
48 delay
.tv_nsec
= DELAY
;
50 for (n
= 0; n
< WRITETRIES
; ++n
)
52 printf ("writer thread %ld tries again\n", (long int) nr
);
54 if (pthread_rwlock_wrlock (&lock
) != 0)
56 puts ("wrlock failed");
60 printf ("writer thread %ld succeeded\n", (long int) nr
);
62 nanosleep (&delay
, NULL
);
64 if (pthread_rwlock_unlock (&lock
) != 0)
66 puts ("unlock for writer failed");
70 printf ("writer thread %ld released\n", (long int) nr
);
78 reader_thread (void *nr
)
80 struct timespec delay
;
84 delay
.tv_nsec
= DELAY
;
86 for (n
= 0; n
< READTRIES
; ++n
)
88 printf ("reader thread %ld tries again\n", (long int) nr
);
90 if (pthread_rwlock_rdlock (&lock
) != 0)
92 puts ("rdlock failed");
96 printf ("reader thread %ld succeeded\n", (long int) nr
);
98 nanosleep (&delay
, NULL
);
100 if (pthread_rwlock_unlock (&lock
) != 0)
102 puts ("unlock for reader failed");
106 printf ("reader thread %ld released\n", (long int) nr
);
116 pthread_t thwr
[NWRITERS
];
117 pthread_t thrd
[NREADERS
];
120 pthread_rwlockattr_t a
;
122 if (pthread_rwlockattr_init (&a
) != 0)
124 puts ("rwlockattr_t failed");
128 if (pthread_rwlockattr_setkind_np (&a
, KIND
) != 0)
130 puts ("rwlockattr_setkind failed");
134 if (pthread_rwlock_init (&lock
, &a
) != 0)
136 puts ("rwlock_init failed");
140 /* Make standard error the same as standard output. */
143 /* Make sure we see all message, even those on stdout. */
144 setvbuf (stdout
, NULL
, _IONBF
, 0);
146 for (n
= 0; n
< NWRITERS
; ++n
)
147 if (pthread_create (&thwr
[n
], NULL
, writer_thread
,
148 (void *) (long int) n
) != 0)
150 puts ("writer create failed");
154 for (n
= 0; n
< NREADERS
; ++n
)
155 if (pthread_create (&thrd
[n
], NULL
, reader_thread
,
156 (void *) (long int) n
) != 0)
158 puts ("reader create failed");
162 /* Wait for all the threads. */
163 for (n
= 0; n
< NWRITERS
; ++n
)
164 if (pthread_join (thwr
[n
], &res
) != 0)
166 puts ("writer join failed");
169 for (n
= 0; n
< NREADERS
; ++n
)
170 if (pthread_join (thrd
[n
], &res
) != 0)
172 puts ("reader join failed");
180 #define TEST_FUNCTION do_test ()
181 #include "../test-skeleton.c"