blob: 254d821cc93bb3ac15d9c51836c10e79fa879e39 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
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 <time.h>
24#include <unistd.h>
25
26
27#if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
28static int
29check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
30{
31 clockid_t cl2;
32 if (pthread_condattr_getclock (condattr, &cl2) != 0)
33 {
34 puts ("condattr_getclock failed");
35 return 1;
36 }
37 if (cl != cl2)
38 {
39 printf ("condattr_getclock returned wrong value: %d, expected %d\n",
40 (int) cl2, (int) cl);
41 return 1;
42 }
43
44 int p;
45 if (pthread_condattr_getpshared (condattr, &p) != 0)
46 {
47 puts ("condattr_getpshared failed");
48 return 1;
49 }
50 else if (p != pshared)
51 {
52 printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
53 p, pshared);
54 return 1;
55 }
56
57 return 0;
58}
59
60static int
61run_test (clockid_t cl)
62{
63 pthread_condattr_t condattr;
64
65 printf ("clock = %d\n", (int) cl);
66
67 if (pthread_condattr_init (&condattr) != 0)
68 {
69 puts ("condattr_init failed");
70 return 1;
71 }
72
73 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
74 return 1;
75
76 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
77 {
78 puts ("1st condattr_setpshared failed");
79 return 1;
80 }
81
82 if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
83 return 1;
84
85 if (pthread_condattr_setclock (&condattr, cl) != 0)
86 {
87 puts ("1st condattr_setclock failed");
88 return 1;
89 }
90
91 if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
92 return 1;
93
94 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
95 {
96 puts ("2nd condattr_setpshared failed");
97 return 1;
98 }
99
100 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
101 return 1;
102
103 if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
104 {
105 puts ("2nd condattr_setclock failed");
106 return 1;
107 }
108
109 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
110 return 1;
111
112 if (pthread_condattr_setclock (&condattr, cl) != 0)
113 {
114 puts ("3rd condattr_setclock failed");
115 return 1;
116 }
117
118 if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
119 return 1;
120
121 if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
122 {
123 puts ("3rd condattr_setpshared failed");
124 return 1;
125 }
126
127 if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
128 return 1;
129
130 if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
131 {
132 puts ("4th condattr_setclock failed");
133 return 1;
134 }
135
136 if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
137 return 1;
138
139 if (pthread_condattr_destroy (&condattr) != 0)
140 {
141 puts ("condattr_destroy failed");
142 return 1;
143 }
144
145 return 0;
146}
147#endif
148
149
150static int
151do_test (void)
152{
153#if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
154
155 puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
156 return 0;
157
158#else
159
160 int res = run_test (CLOCK_REALTIME);
161
162# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
163# if _POSIX_MONOTONIC_CLOCK == 0
164 int e = sysconf (_SC_MONOTONIC_CLOCK);
165 if (e < 0)
166 puts ("CLOCK_MONOTONIC not supported");
167 else if (e == 0)
168 {
169 puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
170 res = 1;
171 }
172 else
173# endif
174 res |= run_test (CLOCK_MONOTONIC);
175# else
176 puts ("_POSIX_MONOTONIC_CLOCK not defined");
177# endif
178
179 return res;
180#endif
181}
182
183#define TEST_FUNCTION do_test ()
184#include "../test-skeleton.c"