1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000-2017 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 <http://www.gnu.org/licenses/>. */
36 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
39 static pthread_rwlock_t lock
;
43 writer_thread (void *nr
)
45 struct timespec delay
;
49 delay
.tv_nsec
= DELAY
;
51 for (n
= 0; n
< WRITETRIES
; ++n
)
53 printf ("writer thread %ld tries again\n", (long int) nr
);
55 if (pthread_rwlock_wrlock (&lock
) != 0)
57 puts ("wrlock failed");
61 printf ("writer thread %ld succeeded\n", (long int) nr
);
63 nanosleep (&delay
, NULL
);
65 if (pthread_rwlock_unlock (&lock
) != 0)
67 puts ("unlock for writer failed");
71 printf ("writer thread %ld released\n", (long int) nr
);
79 reader_thread (void *nr
)
81 struct timespec delay
;
85 delay
.tv_nsec
= DELAY
;
87 for (n
= 0; n
< READTRIES
; ++n
)
89 printf ("reader thread %ld tries again\n", (long int) nr
);
91 if (pthread_rwlock_rdlock (&lock
) != 0)
93 puts ("rdlock failed");
97 printf ("reader thread %ld succeeded\n", (long int) nr
);
99 nanosleep (&delay
, NULL
);
101 if (pthread_rwlock_unlock (&lock
) != 0)
103 puts ("unlock for reader failed");
107 printf ("reader thread %ld released\n", (long int) nr
);
117 pthread_t thwr
[NWRITERS
];
118 pthread_t thrd
[NREADERS
];
121 pthread_rwlockattr_t a
;
123 if (pthread_rwlockattr_init (&a
) != 0)
125 puts ("rwlockattr_t failed");
129 if (pthread_rwlockattr_setkind_np (&a
, KIND
) != 0)
131 puts ("rwlockattr_setkind failed");
135 if (pthread_rwlock_init (&lock
, &a
) != 0)
137 puts ("rwlock_init failed");
141 /* Make standard error the same as standard output. */
144 /* Make sure we see all message, even those on stdout. */
145 setvbuf (stdout
, NULL
, _IONBF
, 0);
147 for (n
= 0; n
< NWRITERS
; ++n
)
148 if (pthread_create (&thwr
[n
], NULL
, writer_thread
,
149 (void *) (long int) n
) != 0)
151 puts ("writer create failed");
155 for (n
= 0; n
< NREADERS
; ++n
)
156 if (pthread_create (&thrd
[n
], NULL
, reader_thread
,
157 (void *) (long int) n
) != 0)
159 puts ("reader create failed");
163 /* Wait for all the threads. */
164 for (n
= 0; n
< NWRITERS
; ++n
)
165 if (pthread_join (thwr
[n
], &res
) != 0)
167 puts ("writer join failed");
170 for (n
= 0; n
< NREADERS
; ++n
)
171 if (pthread_join (thrd
[n
], &res
) != 0)
173 puts ("reader join failed");
181 #define TEST_FUNCTION do_test ()
182 #include "../test-skeleton.c"