Fix warning from latest GCC in tst-printf.c
[glibc.git] / nptl / tst-rwlock7.c
blobcecd2927b0cad23b0c936c7c85949e2689a756bc
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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/time.h>
27 static int kind[] =
29 PTHREAD_RWLOCK_PREFER_READER_NP,
30 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
31 PTHREAD_RWLOCK_PREFER_WRITER_NP,
35 static void *
36 tf (void *arg)
38 pthread_rwlock_t *r = arg;
40 /* Timeout: 0.3 secs. */
41 struct timeval tv;
42 (void) gettimeofday (&tv, NULL);
44 struct timespec ts;
45 TIMEVAL_TO_TIMESPEC (&tv, &ts);
46 ts.tv_nsec += 300000000;
47 if (ts.tv_nsec >= 1000000000)
49 ts.tv_nsec -= 1000000000;
50 ++ts.tv_sec;
53 int err = pthread_rwlock_timedwrlock (r, &ts);
54 if (err == 0)
56 puts ("rwlock_timedwrlock returned");
57 pthread_exit ((void *) 1l);
60 if (err != ETIMEDOUT)
62 printf ("err = %s (%d), expected %s (%d)\n",
63 strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT);
64 pthread_exit ((void *) 1l);
66 puts ("child: timedwrlock failed with ETIMEDOUT");
68 struct timeval tv2;
69 (void) gettimeofday (&tv2, NULL);
71 timersub (&tv2, &tv, &tv);
73 if (tv.tv_usec < 200000)
75 puts ("timeout too short");
76 pthread_exit ((void *) 1l);
79 (void) gettimeofday (&tv, NULL);
80 TIMEVAL_TO_TIMESPEC (&tv, &ts);
81 ts.tv_sec += 10;
82 /* Note that the following operation makes ts invalid. */
83 ts.tv_nsec += 1000000000;
85 err = pthread_rwlock_timedwrlock (r, &ts);
86 if (err == 0)
88 puts ("2nd timedwrlock succeeded");
89 pthread_exit ((void *) 1l);
91 if (err != EINVAL)
93 puts ("2nd timedwrlock did not return EINVAL");
94 pthread_exit ((void *) 1l);
96 puts ("child: timedwrlock failed with EINVAL");
98 return NULL;
102 static int
103 do_test (void)
105 size_t cnt;
106 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
108 pthread_rwlock_t r;
109 pthread_rwlockattr_t a;
111 if (pthread_rwlockattr_init (&a) != 0)
113 printf ("round %Zu: rwlockattr_t failed\n", cnt);
114 exit (1);
117 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
119 printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
120 exit (1);
123 if (pthread_rwlock_init (&r, &a) != 0)
125 printf ("round %Zu: rwlock_init failed\n", cnt);
126 exit (1);
129 if (pthread_rwlockattr_destroy (&a) != 0)
131 printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
132 exit (1);
135 struct timeval tv;
136 (void) gettimeofday (&tv, NULL);
138 struct timespec ts;
139 TIMEVAL_TO_TIMESPEC (&tv, &ts);
141 ++ts.tv_sec;
143 /* Get a read lock. */
144 if (pthread_rwlock_timedrdlock (&r, &ts) != 0)
146 printf ("round %Zu: rwlock_timedrdlock failed\n", cnt);
147 exit (1);
149 printf ("%zu: got timedrdlock\n", cnt);
151 pthread_t th;
152 if (pthread_create (&th, NULL, tf, &r) != 0)
154 printf ("round %Zu: create failed\n", cnt);
155 exit (1);
158 void *status;
159 if (pthread_join (th, &status) != 0)
161 printf ("round %Zu: join failed\n", cnt);
162 exit (1);
164 if (status != NULL)
166 printf ("failure in round %Zu\n", cnt);
167 exit (1);
170 if (pthread_rwlock_destroy (&r) != 0)
172 printf ("round %Zu: rwlock_destroy failed\n", cnt);
173 exit (1);
177 return 0;
180 #define TEST_FUNCTION do_test ()
181 #include "../test-skeleton.c"