Update copyright dates with scripts/update-copyrights
[glibc.git] / sysdeps / pthread / tst-cond5.c
blob1d12def58d2392f014a6f3d2ab9ef977cfb0a2a1
1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <sys/time.h>
27 static pthread_mutex_t mut;
28 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31 static int
32 do_test (void)
34 pthread_mutexattr_t ma;
35 int err;
36 struct timespec ts;
37 struct timeval tv;
39 if (pthread_mutexattr_init (&ma) != 0)
41 puts ("mutexattr_init failed");
42 exit (1);
45 if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
47 puts ("mutexattr_settype failed");
48 exit (1);
51 if (pthread_mutex_init (&mut, &ma) != 0)
53 puts ("mutex_init failed");
54 exit (1);
57 /* Get the mutex. */
58 if (pthread_mutex_lock (&mut) != 0)
60 puts ("mutex_lock failed");
61 exit (1);
64 /* Waiting for the condition will fail. But we want the timeout here. */
65 if (gettimeofday (&tv, NULL) != 0)
67 puts ("gettimeofday failed");
68 exit (1);
71 TIMEVAL_TO_TIMESPEC (&tv, &ts);
72 ts.tv_nsec += 500000000;
73 if (ts.tv_nsec >= 1000000000)
75 ts.tv_nsec -= 1000000000;
76 ++ts.tv_sec;
78 err = pthread_cond_timedwait (&cond, &mut, &ts);
79 if (err == 0)
81 /* This could in theory happen but here without any signal and
82 additional waiter it should not. */
83 puts ("cond_timedwait succeeded");
84 exit (1);
86 else if (err != ETIMEDOUT)
88 printf ("cond_timedwait returned with %s\n", strerror (err));
89 exit (1);
92 err = pthread_mutex_unlock (&mut);
93 if (err != 0)
95 printf ("mutex_unlock failed: %s\n", strerror (err));
96 exit (1);
99 return 0;
103 #define TEST_FUNCTION do_test ()
104 #include "../test-skeleton.c"