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/>. */
28 # define TYPE PTHREAD_MUTEX_NORMAL
40 pthread_mutexattr_t a
;
42 if (pthread_mutexattr_init (&a
) != 0)
44 puts ("mutexattr_init failed");
48 if (pthread_mutexattr_settype (&a
, TYPE
) != 0)
50 puts ("mutexattr_settype failed");
55 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
57 puts ("pthread_mutexattr_setprotocol failed");
62 err
= pthread_mutex_init (&m
, &a
);
68 puts ("PI mutexes unsupported");
72 puts ("mutex_init failed");
76 if (pthread_mutexattr_destroy (&a
) != 0)
78 puts ("mutexattr_destroy failed");
82 if (pthread_mutex_lock (&m
) != 0)
84 puts ("mutex_lock failed");
88 if (pthread_mutex_trylock (&m
) == 0)
90 puts ("mutex_trylock succeeded");
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
);
102 puts ("timedlock succeeded");
105 else if (err
!= ETIMEDOUT
)
107 printf ("timedlock error != ETIMEDOUT: %d\n", err
);
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
;
120 tv2
.tv_usec
+= 1000000;
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;
134 printf ("premature timeout: %ld.%06ld difference\n",
135 tv2
.tv_sec
, tv2
.tv_usec
);
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
);
150 puts ("2nd timedlock succeeded");
153 else if (err
!= EINVAL
)
155 printf ("2nd timedlock error != EINVAL: %d\n", err
);
159 if (pthread_mutex_unlock (&m
) != 0)
161 puts ("mutex_unlock failed");
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");
184 if (pthread_mutex_unlock (&m
) != 0)
186 puts ("final mutex_unlock failed");
190 if (pthread_mutex_destroy (&m
) != 0)
192 puts ("mutex_destroy failed");
200 #define TEST_FUNCTION do_test ()
201 #include "../test-skeleton.c"