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.
4 
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.
9 
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.
14 
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/>.  */
18 
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>
25 
26 
27 static int kind[] =
28   {
29     PTHREAD_RWLOCK_PREFER_READER_NP,
30     PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
31     PTHREAD_RWLOCK_PREFER_WRITER_NP,
32   };
33 
34 
35 static void *
tf(void * arg)36 tf (void *arg)
37 {
38   pthread_rwlock_t *r = arg;
39 
40   /* Timeout: 0.3 secs.  */
41   struct timeval tv;
42   (void) gettimeofday (&tv, NULL);
43 
44   struct timespec ts;
45   TIMEVAL_TO_TIMESPEC (&tv, &ts);
46   ts.tv_nsec += 300000000;
47   if (ts.tv_nsec >= 1000000000)
48     {
49       ts.tv_nsec -= 1000000000;
50       ++ts.tv_sec;
51     }
52 
53   int err = pthread_rwlock_timedwrlock (r, &ts);
54   if (err == 0)
55     {
56       puts ("rwlock_timedwrlock returned");
57       pthread_exit ((void *) 1l);
58     }
59 
60   if (err != ETIMEDOUT)
61     {
62       printf ("err = %s (%d), expected %s (%d)\n",
63 	      strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT);
64       pthread_exit ((void *) 1l);
65     }
66 
67   struct timeval tv2;
68   (void) gettimeofday (&tv2, NULL);
69 
70   timersub (&tv2, &tv, &tv);
71 
72   if (tv.tv_usec < 200000)
73     {
74       puts ("timeout too short");
75       pthread_exit ((void *) 1l);
76     }
77 
78   (void) gettimeofday (&tv, NULL);
79   TIMEVAL_TO_TIMESPEC (&tv, &ts);
80   ts.tv_sec += 10;
81   /* Note that the following operation makes ts invalid.  */
82   ts.tv_nsec += 1000000000;
83 
84   err = pthread_rwlock_timedwrlock (r, &ts);
85   if (err == 0)
86     {
87       puts ("2nd timedwrlock succeeded");
88       pthread_exit ((void *) 1l);
89     }
90   if (err != EINVAL)
91     {
92       puts ("2nd timedwrlock did not return EINVAL");
93       pthread_exit ((void *) 1l);
94     }
95 
96   return NULL;
97 }
98 
99 
100 static int
do_test(void)101 do_test (void)
102 {
103   size_t cnt;
104   for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
105     {
106       pthread_rwlock_t r;
107       pthread_rwlockattr_t a;
108 
109       if (pthread_rwlockattr_init (&a) != 0)
110 	{
111 	  printf ("round %Zu: rwlockattr_t failed\n", cnt);
112 	  exit (1);
113 	}
114 
115       if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
116 	{
117 	  printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
118 	  exit (1);
119 	}
120 
121       if (pthread_rwlock_init (&r, &a) != 0)
122 	{
123 	  printf ("round %Zu: rwlock_init failed\n", cnt);
124 	  exit (1);
125 	}
126 
127       if (pthread_rwlockattr_destroy (&a) != 0)
128 	{
129 	  printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
130 	  exit (1);
131 	}
132 
133       struct timeval tv;
134       (void) gettimeofday (&tv, NULL);
135 
136       struct timespec ts;
137       TIMEVAL_TO_TIMESPEC (&tv, &ts);
138 
139       ++ts.tv_sec;
140 
141       /* Get a read lock.  */
142       if (pthread_rwlock_timedrdlock (&r, &ts) != 0)
143 	{
144 	  printf ("round %Zu: rwlock_timedrdlock failed\n", cnt);
145 	  exit (1);
146 	}
147 
148       pthread_t th;
149       if (pthread_create (&th, NULL, tf, &r) != 0)
150 	{
151 	  printf ("round %Zu: create failed\n", cnt);
152 	  exit (1);
153 	}
154 
155       void *status;
156       if (pthread_join (th, &status) != 0)
157 	{
158 	  printf ("round %Zu: join failed\n", cnt);
159 	  exit (1);
160 	}
161       if (status != NULL)
162 	{
163 	  printf ("failure in round %Zu\n", cnt);
164 	  exit (1);
165 	}
166 
167       if (pthread_rwlock_destroy (&r) != 0)
168 	{
169 	  printf ("round %Zu: rwlock_destroy failed\n", cnt);
170 	  exit (1);
171 	}
172     }
173 
174   return 0;
175 }
176 
177 #define TEST_FUNCTION do_test ()
178 #include "../test-skeleton.c"
179