OSDN Git Service

efa207b5dca891b4f43229e3727868d7665766b5
[uclinux-h8/uClibc.git] / test / nptl / tst-cond5.c
1 /* Copyright (C) 2002 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <sys/time.h>
27
28
29 static pthread_mutex_t mut;
30 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31
32
33 static int
34 do_test (void)
35 {
36   pthread_mutexattr_t ma;
37   int err;
38   struct timespec ts;
39   struct timeval tv;
40
41   if (pthread_mutexattr_init (&ma) != 0)
42     {
43       puts ("mutexattr_init failed");
44       exit (1);
45     }
46
47   if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
48     {
49       puts ("mutexattr_settype failed");
50       exit (1);
51     }
52
53   if (pthread_mutex_init (&mut, &ma) != 0)
54     {
55       puts ("mutex_init failed");
56       exit (1);
57     }
58
59   /* Get the mutex.  */
60   if (pthread_mutex_lock (&mut) != 0)
61     {
62       puts ("mutex_lock failed");
63       exit (1);
64     }
65
66   /* Waiting for the condition will fail.  But we want the timeout here.  */
67   if (gettimeofday (&tv, NULL) != 0)
68     {
69       puts ("gettimeofday failed");
70       exit (1);
71     }
72
73   TIMEVAL_TO_TIMESPEC (&tv, &ts);
74   ts.tv_nsec += 500000000;
75   if (ts.tv_nsec >= 1000000000)
76     {
77       ts.tv_nsec -= 1000000000;
78       ++ts.tv_sec;
79     }
80   err = pthread_cond_timedwait (&cond, &mut, &ts);
81   if (err == 0)
82     {
83       /* This could in theory happen but here without any signal and
84          additional waiter it should not.  */
85       puts ("cond_timedwait succeeded");
86       exit (1);
87     }
88   else if (err != ETIMEDOUT)
89     {
90       printf ("cond_timedwait returned with %s\n", strerror (err));
91       exit (1);
92     }
93
94   err = pthread_mutex_unlock (&mut);
95   if (err != 0)
96     {
97       printf ("mutex_unlock failed: %s\n", strerror (err));
98       exit (1);
99     }
100
101   return 0;
102 }
103
104
105 #define TEST_FUNCTION do_test ()
106 #include "../test-skeleton.c"