Update copyright dates with scripts/update-copyrights.
[glibc.git] / nptl / tst-cond23.c
blob3be8020ff2b2d57a72fe573ff532cc69ca780cbe
1 /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
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>
26 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
27 static int
28 check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
30 clockid_t cl2;
31 if (pthread_condattr_getclock (condattr, &cl2) != 0)
33 puts ("condattr_getclock failed");
34 return 1;
36 if (cl != cl2)
38 printf ("condattr_getclock returned wrong value: %d, expected %d\n",
39 (int) cl2, (int) cl);
40 return 1;
43 int p;
44 if (pthread_condattr_getpshared (condattr, &p) != 0)
46 puts ("condattr_getpshared failed");
47 return 1;
49 else if (p != pshared)
51 printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
52 p, pshared);
53 return 1;
56 return 0;
59 static int
60 run_test (clockid_t cl)
62 pthread_condattr_t condattr;
64 printf ("clock = %d\n", (int) cl);
66 if (pthread_condattr_init (&condattr) != 0)
68 puts ("condattr_init failed");
69 return 1;
72 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
73 return 1;
75 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
77 puts ("1st condattr_setpshared failed");
78 return 1;
81 if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
82 return 1;
84 if (pthread_condattr_setclock (&condattr, cl) != 0)
86 puts ("1st condattr_setclock failed");
87 return 1;
90 if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
91 return 1;
93 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
95 puts ("2nd condattr_setpshared failed");
96 return 1;
99 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
100 return 1;
102 if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
104 puts ("2nd condattr_setclock failed");
105 return 1;
108 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
109 return 1;
111 if (pthread_condattr_setclock (&condattr, cl) != 0)
113 puts ("3rd condattr_setclock failed");
114 return 1;
117 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
118 return 1;
120 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
122 puts ("3rd condattr_setpshared failed");
123 return 1;
126 if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
127 return 1;
129 if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
131 puts ("4th condattr_setclock failed");
132 return 1;
135 if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
136 return 1;
138 if (pthread_condattr_destroy (&condattr) != 0)
140 puts ("condattr_destroy failed");
141 return 1;
144 return 0;
146 #endif
149 static int
150 do_test (void)
152 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
154 puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
155 return 0;
157 #else
159 int res = run_test (CLOCK_REALTIME);
161 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
162 # if _POSIX_MONOTONIC_CLOCK == 0
163 int e = sysconf (_SC_MONOTONIC_CLOCK);
164 if (e < 0)
165 puts ("CLOCK_MONOTONIC not supported");
166 else if (e == 0)
168 puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
169 res = 1;
171 else
172 # endif
173 res |= run_test (CLOCK_MONOTONIC);
174 # else
175 puts ("_POSIX_MONOTONIC_CLOCK not defined");
176 # endif
178 return res;
179 #endif
182 #define TEST_FUNCTION do_test ()
183 #include "../test-skeleton.c"