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/>. */
34 #define TIMEOUT 1000000
38 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
41 static pthread_rwlock_t lock
;
45 writer_thread (void *nr
)
48 struct timespec delay
;
52 delay
.tv_nsec
= DELAY
;
54 for (n
= 0; n
< WRITETRIES
; ++n
)
60 (void) gettimeofday (&tv
, NULL
);
61 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
63 ts
.tv_nsec
+= 2 * TIMEOUT
;
64 if (ts
.tv_nsec
>= 1000000000)
66 ts
.tv_nsec
-= 1000000000;
70 printf ("writer thread %ld tries again\n", (long int) nr
);
72 e
= pthread_rwlock_timedwrlock (&lock
, &ts
);
73 if (e
!= 0 && e
!= ETIMEDOUT
)
75 puts ("timedwrlock failed");
79 while (e
== ETIMEDOUT
);
81 printf ("writer thread %ld succeeded\n", (long int) nr
);
83 nanosleep (&delay
, NULL
);
85 if (pthread_rwlock_unlock (&lock
) != 0)
87 puts ("unlock for writer failed");
91 printf ("writer thread %ld released\n", (long int) nr
);
99 reader_thread (void *nr
)
102 struct timespec delay
;
106 delay
.tv_nsec
= DELAY
;
108 for (n
= 0; n
< READTRIES
; ++n
)
114 (void) gettimeofday (&tv
, NULL
);
115 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
117 ts
.tv_nsec
+= TIMEOUT
;
118 if (ts
.tv_nsec
>= 1000000000)
120 ts
.tv_nsec
-= 1000000000;
124 printf ("reader thread %ld tries again\n", (long int) nr
);
126 e
= pthread_rwlock_timedrdlock (&lock
, &ts
);
127 if (e
!= 0 && e
!= ETIMEDOUT
)
129 puts ("timedrdlock failed");
133 while (e
== ETIMEDOUT
);
135 printf ("reader thread %ld succeeded\n", (long int) nr
);
137 nanosleep (&delay
, NULL
);
139 if (pthread_rwlock_unlock (&lock
) != 0)
141 puts ("unlock for reader failed");
145 printf ("reader thread %ld released\n", (long int) nr
);
155 pthread_t thwr
[NWRITERS
];
156 pthread_t thrd
[NREADERS
];
159 pthread_rwlockattr_t a
;
161 if (pthread_rwlockattr_init (&a
) != 0)
163 puts ("rwlockattr_t failed");
167 if (pthread_rwlockattr_setkind_np (&a
, KIND
) != 0)
169 puts ("rwlockattr_setkind failed");
173 if (pthread_rwlock_init (&lock
, &a
) != 0)
175 puts ("rwlock_init failed");
179 /* Make standard error the same as standard output. */
182 /* Make sure we see all message, even those on stdout. */
183 setvbuf (stdout
, NULL
, _IONBF
, 0);
185 for (n
= 0; n
< NWRITERS
; ++n
)
186 if (pthread_create (&thwr
[n
], NULL
, writer_thread
,
187 (void *) (long int) n
) != 0)
189 puts ("writer create failed");
193 for (n
= 0; n
< NREADERS
; ++n
)
194 if (pthread_create (&thrd
[n
], NULL
, reader_thread
,
195 (void *) (long int) n
) != 0)
197 puts ("reader create failed");
201 /* Wait for all the threads. */
202 for (n
= 0; n
< NWRITERS
; ++n
)
203 if (pthread_join (thwr
[n
], &res
) != 0)
205 puts ("writer join failed");
208 for (n
= 0; n
< NREADERS
; ++n
)
209 if (pthread_join (thrd
[n
], &res
) != 0)
211 puts ("reader join failed");
220 #define TEST_FUNCTION do_test ()
221 #include "../test-skeleton.c"