(TLS_MULTIPLE_THREADS_IN_TCB): Define.
[glibc/history.git] / nptl / tst-rwlock6.c
blobea7f4ba634f4efe2fd739fcd3debcb054c9e0ef0
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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/time.h>
28 static int kind[] =
30 PTHREAD_RWLOCK_PREFER_READER_NP,
31 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
32 PTHREAD_RWLOCK_PREFER_WRITER_NP,
36 static void *
37 tf (void *arg)
39 pthread_rwlock_t *r = arg;
41 /* Timeout: 0.3 secs. */
42 struct timeval tv;
43 (void) gettimeofday (&tv, NULL);
45 struct timespec ts;
46 TIMEVAL_TO_TIMESPEC (&tv, &ts);
47 ts.tv_nsec += 300000000;
48 if (ts.tv_nsec >= 1000000000)
50 ts.tv_nsec -= 1000000000;
51 ++ts.tv_sec;
54 int err = pthread_rwlock_timedrdlock (r, &ts);
55 if (err == 0)
57 puts ("rwlock_timedrdlock returned");
58 pthread_exit ((void *) 1l);
61 if (err != ETIMEDOUT)
63 printf ("err = %s (%d), expected %s (%d)\n",
64 strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT);
65 pthread_exit ((void *) 1l);
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_timedrdlock (r, &ts);
86 if (err == 0)
88 puts ("2nd timedrdlock succeeded");
89 pthread_exit ((void *) 1l);
91 if (err != EINVAL)
93 puts ("2nd timedrdlock did not return EINVAL");
94 pthread_exit ((void *) 1l);
97 return NULL;
101 static int
102 do_test (void)
104 int cnt;
105 for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
107 pthread_rwlock_t r;
108 pthread_rwlockattr_t a;
110 if (pthread_rwlockattr_init (&a) != 0)
112 printf ("round %d: rwlockattr_t failed\n", cnt);
113 exit (1);
116 if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
118 printf ("round %d: rwlockattr_setkind failed\n", cnt);
119 exit (1);
122 if (pthread_rwlock_init (&r, &a) != 0)
124 printf ("round %d: rwlock_init failed\n", cnt);
125 exit (1);
128 if (pthread_rwlockattr_destroy (&a) != 0)
130 printf ("round %d: rwlockattr_destroy failed\n", cnt);
131 exit (1);
134 struct timeval tv;
135 (void) gettimeofday (&tv, NULL);
137 struct timespec ts;
138 TIMEVAL_TO_TIMESPEC (&tv, &ts);
140 ++ts.tv_sec;
142 /* Get a write lock. */
143 if (pthread_rwlock_timedwrlock (&r, &ts) != 0)
145 printf ("round %d: rwlock_wrlock failed\n", cnt);
146 exit (1);
149 (void) gettimeofday (&tv, NULL);
150 TIMEVAL_TO_TIMESPEC (&tv, &ts);
151 ++ts.tv_sec;
152 int e = pthread_rwlock_timedrdlock (&r, &ts);
153 if (e == 0)
155 puts ("timedrdlock succeeded");
156 exit (1);
158 if (e != EDEADLK)
160 puts ("timedrdlock did not return EDEADLK");
161 exit (1);
164 (void) gettimeofday (&tv, NULL);
165 TIMEVAL_TO_TIMESPEC (&tv, &ts);
166 ++ts.tv_sec;
167 e = pthread_rwlock_timedwrlock (&r, &ts);
168 if (e == 0)
170 puts ("2nd timedwrlock succeeded");
171 exit (1);
173 if (e != EDEADLK)
175 puts ("2nd timedwrlock did not return EDEADLK");
176 exit (1);
179 pthread_t th;
180 if (pthread_create (&th, NULL, tf, &r) != 0)
182 printf ("round %d: create failed\n", cnt);
183 exit (1);
186 void *status;
187 if (pthread_join (th, &status) != 0)
189 printf ("round %d: join failed\n", cnt);
190 exit (1);
192 if (status != NULL)
194 printf ("failure in round %d\n", cnt);
195 exit (1);
198 if (pthread_rwlock_destroy (&r) != 0)
200 printf ("round %d: rwlock_destroy failed\n", cnt);
201 exit (1);
205 return 0;
208 #define TEST_FUNCTION do_test ()
209 #include "../test-skeleton.c"