1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <http://www.gnu.org/licenses/>. */
29 PTHREAD_RWLOCK_PREFER_READER_NP
,
30 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
31 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
38 pthread_rwlock_t
*r
= arg
;
40 /* Timeout: 0.3 secs. */
42 (void) gettimeofday (&tv
, NULL
);
45 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
46 ts
.tv_nsec
+= 300000000;
47 if (ts
.tv_nsec
>= 1000000000)
49 ts
.tv_nsec
-= 1000000000;
53 puts ("child calling timedrdlock");
55 int err
= pthread_rwlock_timedrdlock (r
, &ts
);
58 puts ("rwlock_timedrdlock returned");
59 pthread_exit ((void *) 1l);
64 printf ("err = %s (%d), expected %s (%d)\n",
65 strerror (err
), err
, strerror (ETIMEDOUT
), ETIMEDOUT
);
66 pthread_exit ((void *) 1l);
69 puts ("1st child timedrdlock done");
72 (void) gettimeofday (&tv2
, NULL
);
74 timersub (&tv2
, &tv
, &tv
);
76 if (tv
.tv_usec
< 200000)
78 puts ("timeout too short");
79 pthread_exit ((void *) 1l);
82 (void) gettimeofday (&tv
, NULL
);
83 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
85 /* Note that the following operation makes ts invalid. */
86 ts
.tv_nsec
+= 1000000000;
88 err
= pthread_rwlock_timedrdlock (r
, &ts
);
91 puts ("2nd timedrdlock succeeded");
92 pthread_exit ((void *) 1l);
96 puts ("2nd timedrdlock did not return EINVAL");
97 pthread_exit ((void *) 1l);
100 puts ("2nd child timedrdlock done");
110 for (cnt
= 0; cnt
< sizeof (kind
) / sizeof (kind
[0]); ++cnt
)
113 pthread_rwlockattr_t a
;
115 if (pthread_rwlockattr_init (&a
) != 0)
117 printf ("round %Zu: rwlockattr_t failed\n", cnt
);
121 if (pthread_rwlockattr_setkind_np (&a
, kind
[cnt
]) != 0)
123 printf ("round %Zu: rwlockattr_setkind failed\n", cnt
);
127 if (pthread_rwlock_init (&r
, &a
) != 0)
129 printf ("round %Zu: rwlock_init failed\n", cnt
);
133 if (pthread_rwlockattr_destroy (&a
) != 0)
135 printf ("round %Zu: rwlockattr_destroy failed\n", cnt
);
140 (void) gettimeofday (&tv
, NULL
);
143 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
147 /* Get a write lock. */
148 int e
= pthread_rwlock_timedwrlock (&r
, &ts
);
151 printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt
, e
);
155 puts ("1st timedwrlock done");
157 (void) gettimeofday (&tv
, NULL
);
158 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
160 e
= pthread_rwlock_timedrdlock (&r
, &ts
);
163 puts ("timedrdlock succeeded");
168 puts ("timedrdlock did not return EDEADLK");
172 puts ("1st timedrdlock done");
174 (void) gettimeofday (&tv
, NULL
);
175 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
177 e
= pthread_rwlock_timedwrlock (&r
, &ts
);
180 puts ("2nd timedwrlock succeeded");
185 puts ("2nd timedwrlock did not return EDEADLK");
189 puts ("2nd timedwrlock done");
192 if (pthread_create (&th
, NULL
, tf
, &r
) != 0)
194 printf ("round %Zu: create failed\n", cnt
);
198 puts ("started thread");
201 if (pthread_join (th
, &status
) != 0)
203 printf ("round %Zu: join failed\n", cnt
);
208 printf ("failure in round %Zu\n", cnt
);
212 puts ("joined thread");
214 if (pthread_rwlock_destroy (&r
) != 0)
216 printf ("round %Zu: rwlock_destroy failed\n", cnt
);
224 #define TEST_FUNCTION do_test ()
225 #include "../test-skeleton.c"