Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-cond9.c
blob2046029e40e64d9699f58c3d67b191fcf6cf0251
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <stdlib.h>
23 #include <time.h>
24 #include <sys/time.h>
27 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
28 static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
31 static void *
32 tf (void *arg)
34 int err = pthread_cond_wait (&cond, &mut);
35 if (err == 0)
37 puts ("cond_wait did not fail");
38 exit (1);
41 if (err != EPERM)
43 printf ("cond_wait didn't return EPERM but %d\n", err);
44 exit (1);
48 /* Current time. */
49 struct timeval tv;
50 (void) gettimeofday (&tv, NULL);
51 /* +1000 seconds in correct format. */
52 struct timespec ts;
53 TIMEVAL_TO_TIMESPEC (&tv, &ts);
54 ts.tv_sec += 1000;
56 err = pthread_cond_timedwait (&cond, &mut, &ts);
57 if (err == 0)
59 puts ("cond_timedwait did not fail");
60 exit (1);
63 if (err != EPERM)
65 printf ("cond_timedwait didn't return EPERM but %d\n", err);
66 exit (1);
69 return (void *) 1l;
73 static int
74 do_test (void)
76 pthread_t th;
77 int err;
79 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
81 err = pthread_cond_wait (&cond, &mut);
82 if (err == 0)
84 puts ("cond_wait did not fail");
85 exit (1);
88 if (err != EPERM)
90 printf ("cond_wait didn't return EPERM but %d\n", err);
91 exit (1);
95 /* Current time. */
96 struct timeval tv;
97 (void) gettimeofday (&tv, NULL);
98 /* +1000 seconds in correct format. */
99 struct timespec ts;
100 TIMEVAL_TO_TIMESPEC (&tv, &ts);
101 ts.tv_sec += 1000;
103 err = pthread_cond_timedwait (&cond, &mut, &ts);
104 if (err == 0)
106 puts ("cond_timedwait did not fail");
107 exit (1);
110 if (err != EPERM)
112 printf ("cond_timedwait didn't return EPERM but %d\n", err);
113 exit (1);
116 if (pthread_mutex_lock (&mut) != 0)
118 puts ("parent: mutex_lock failed");
119 exit (1);
122 puts ("creating thread");
124 if (pthread_create (&th, NULL, tf, NULL) != 0)
126 puts ("create failed");
127 exit (1);
130 void *r;
131 if (pthread_join (th, &r) != 0)
133 puts ("join failed");
134 exit (1);
136 if (r != (void *) 1l)
138 puts ("thread has wrong return value");
139 exit (1);
142 puts ("done");
144 return 0;
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"