Do check-localplt test using readelf rather than a build-time C program.
[glibc.git] / nptl / tst-mutex5.c
bloba055e2aea7cf6e79ead9c8746445051559892253
1 /* Copyright (C) 2002, 2003, 2004, 2006 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 <stdio.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <sys/time.h>
27 #ifndef TYPE
28 # define TYPE PTHREAD_MUTEX_NORMAL
29 #endif
32 static int
33 do_test (void)
35 pthread_mutex_t m;
36 struct timespec ts;
37 struct timeval tv;
38 struct timeval tv2;
39 int err;
40 pthread_mutexattr_t a;
42 if (pthread_mutexattr_init (&a) != 0)
44 puts ("mutexattr_init failed");
45 return 1;
48 if (pthread_mutexattr_settype (&a, TYPE) != 0)
50 puts ("mutexattr_settype failed");
51 return 1;
54 #ifdef ENABLE_PI
55 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
57 puts ("pthread_mutexattr_setprotocol failed");
58 return 1;
60 #endif
62 err = pthread_mutex_init (&m, &a);
63 if (err != 0)
65 #ifdef ENABLE_PI
66 if (err == ENOTSUP)
68 puts ("PI mutexes unsupported");
69 return 0;
71 #endif
72 puts ("mutex_init failed");
73 return 1;
76 if (pthread_mutexattr_destroy (&a) != 0)
78 puts ("mutexattr_destroy failed");
79 return 1;
82 if (pthread_mutex_lock (&m) != 0)
84 puts ("mutex_lock failed");
85 return 1;
88 if (pthread_mutex_trylock (&m) == 0)
90 puts ("mutex_trylock succeeded");
91 return 1;
94 gettimeofday (&tv, NULL);
95 TIMEVAL_TO_TIMESPEC (&tv, &ts);
97 ts.tv_sec += 2; /* Wait 2 seconds. */
99 err = pthread_mutex_timedlock (&m, &ts);
100 if (err == 0)
102 puts ("timedlock succeeded");
103 return 1;
105 else if (err != ETIMEDOUT)
107 printf ("timedlock error != ETIMEDOUT: %d\n", err);
108 return 1;
110 else
112 int clk_tck = sysconf (_SC_CLK_TCK);
114 gettimeofday (&tv2, NULL);
116 tv2.tv_sec -= tv.tv_sec;
117 tv2.tv_usec -= tv.tv_usec;
118 if (tv2.tv_usec < 0)
120 tv2.tv_usec += 1000000;
121 tv2.tv_sec -= 1;
124 /* Be a bit tolerant, add one CLK_TCK. */
125 tv2.tv_usec += 1000000 / clk_tck;
126 if (tv2.tv_usec >= 1000000)
128 tv2.tv_usec -= 1000000;
129 ++tv2.tv_sec;
132 if (tv2.tv_sec < 2)
134 printf ("premature timeout: %ld.%06ld difference\n",
135 tv2.tv_sec, tv2.tv_usec);
136 return 1;
140 (void) gettimeofday (&tv, NULL);
141 TIMEVAL_TO_TIMESPEC (&tv, &ts);
143 ts.tv_sec += 2; /* Wait 2 seconds. */
144 /* The following makes the ts value invalid. */
145 ts.tv_nsec += 1000000000;
147 err = pthread_mutex_timedlock (&m, &ts);
148 if (err == 0)
150 puts ("2nd timedlock succeeded");
151 return 1;
153 else if (err != EINVAL)
155 printf ("2nd timedlock error != EINVAL: %d\n", err);
156 return 1;
159 if (pthread_mutex_unlock (&m) != 0)
161 puts ("mutex_unlock failed");
162 return 1;
165 (void) gettimeofday (&tv, NULL);
166 TIMEVAL_TO_TIMESPEC (&tv, &ts);
168 ts.tv_sec += 2; /* Wait 2 seconds. */
169 if (pthread_mutex_timedlock (&m, &ts) != 0)
171 puts ("3rd timedlock failed");
174 (void) gettimeofday (&tv2, NULL);
176 /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */
177 timersub (&tv2, &tv, &tv2);
178 if (tv2.tv_sec > 0 || tv2.tv_usec > 100000)
180 puts ("3rd timedlock didn't return right away");
181 return 1;
184 if (pthread_mutex_unlock (&m) != 0)
186 puts ("final mutex_unlock failed");
187 return 1;
190 if (pthread_mutex_destroy (&m) != 0)
192 puts ("mutex_destroy failed");
193 return 1;
196 return 0;
199 #define TIMEOUT 4
200 #define TEST_FUNCTION do_test ()
201 #include "../test-skeleton.c"