1 /* Copyright (C) 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 PTHREAD_RWLOCK_PREFER_READER_NP
,
31 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
,
32 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
39 pthread_rwlock_t
*r
= arg
;
41 /* Timeout: 0.3 secs. */
43 (void) gettimeofday (&tv
, NULL
);
46 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
47 ts
.tv_nsec
+= 300000000;
48 if (ts
.tv_nsec
>= 1000000000)
50 ts
.tv_nsec
-= 1000000000;
54 puts ("child calling timedrdlock");
56 int err
= pthread_rwlock_timedrdlock (r
, &ts
);
59 puts ("rwlock_timedrdlock returned");
60 pthread_exit ((void *) 1l);
65 printf ("err = %s (%d), expected %s (%d)\n",
66 strerror (err
), err
, strerror (ETIMEDOUT
), ETIMEDOUT
);
67 pthread_exit ((void *) 1l);
70 puts ("1st child timedrdlock done");
73 (void) gettimeofday (&tv2
, NULL
);
75 timersub (&tv2
, &tv
, &tv
);
77 if (tv
.tv_usec
< 200000)
79 puts ("timeout too short");
80 pthread_exit ((void *) 1l);
83 (void) gettimeofday (&tv
, NULL
);
84 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
86 /* Note that the following operation makes ts invalid. */
87 ts
.tv_nsec
+= 1000000000;
89 err
= pthread_rwlock_timedrdlock (r
, &ts
);
92 puts ("2nd timedrdlock succeeded");
93 pthread_exit ((void *) 1l);
97 puts ("2nd timedrdlock did not return EINVAL");
98 pthread_exit ((void *) 1l);
101 puts ("2nd child timedrdlock done");
111 for (cnt
= 0; cnt
< sizeof (kind
) / sizeof (kind
[0]); ++cnt
)
114 pthread_rwlockattr_t a
;
116 if (pthread_rwlockattr_init (&a
) != 0)
118 printf ("round %Zu: rwlockattr_t failed\n", cnt
);
122 if (pthread_rwlockattr_setkind_np (&a
, kind
[cnt
]) != 0)
124 printf ("round %Zu: rwlockattr_setkind failed\n", cnt
);
128 if (pthread_rwlock_init (&r
, &a
) != 0)
130 printf ("round %Zu: rwlock_init failed\n", cnt
);
134 if (pthread_rwlockattr_destroy (&a
) != 0)
136 printf ("round %Zu: rwlockattr_destroy failed\n", cnt
);
141 (void) gettimeofday (&tv
, NULL
);
144 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
148 /* Get a write lock. */
149 int e
= pthread_rwlock_timedwrlock (&r
, &ts
);
152 printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt
, e
);
156 puts ("1st timedwrlock done");
158 (void) gettimeofday (&tv
, NULL
);
159 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
161 e
= pthread_rwlock_timedrdlock (&r
, &ts
);
164 puts ("timedrdlock succeeded");
169 puts ("timedrdlock did not return EDEADLK");
173 puts ("1st timedrdlock done");
175 (void) gettimeofday (&tv
, NULL
);
176 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
178 e
= pthread_rwlock_timedwrlock (&r
, &ts
);
181 puts ("2nd timedwrlock succeeded");
186 puts ("2nd timedwrlock did not return EDEADLK");
190 puts ("2nd timedwrlock done");
193 if (pthread_create (&th
, NULL
, tf
, &r
) != 0)
195 printf ("round %Zu: create failed\n", cnt
);
199 puts ("started thread");
202 if (pthread_join (th
, &status
) != 0)
204 printf ("round %Zu: join failed\n", cnt
);
209 printf ("failure in round %d\n", cnt
);
213 puts ("joined thread");
215 if (pthread_rwlock_destroy (&r
) != 0)
217 printf ("round %Zu: rwlock_destroy failed\n", cnt
);
225 #define TEST_FUNCTION do_test ()
226 #include "../test-skeleton.c"