1 | /* Copyright (C) 2014-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
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. |
8 | |
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. |
13 | |
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/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <stdio.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | #include <unistd.h> |
23 | #include <limits.h> |
24 | #include <pthread.h> |
25 | #include <sys/prctl.h> |
26 | |
27 | #if __mips_fpr != 0 || _MIPS_SPFPSET != 16 |
28 | # error This test requires -mfpxx -mno-odd-spreg |
29 | #endif |
30 | |
31 | /* This test verifies that all threads in a process see a mode |
32 | change when any thread causes a mode change. */ |
33 | |
34 | static int mode[6] = |
35 | { |
36 | 0, |
37 | PR_FP_MODE_FR, |
38 | PR_FP_MODE_FR | PR_FP_MODE_FRE, |
39 | PR_FP_MODE_FR, |
40 | 0, |
41 | PR_FP_MODE_FR | PR_FP_MODE_FRE |
42 | }; |
43 | static volatile int current_mode; |
44 | static volatile int finished; |
45 | static pthread_barrier_t barr_ready; |
46 | static pthread_barrier_t barr_cont; |
47 | |
48 | static void * |
49 | thread_function (void * arg __attribute__ ((unused))) |
50 | { |
51 | while (!finished) |
52 | { |
53 | int res = pthread_barrier_wait (barrier: &barr_ready); |
54 | |
55 | if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD) |
56 | { |
57 | printf (format: "barrier wait failed: %m\n" ); |
58 | exit (1); |
59 | } |
60 | |
61 | int mode = prctl (PR_GET_FP_MODE); |
62 | |
63 | if (mode != current_mode) |
64 | { |
65 | printf (format: "unexpected mode: %d != %d\n" , mode, current_mode); |
66 | exit (1); |
67 | } |
68 | |
69 | res = pthread_barrier_wait (barrier: &barr_cont); |
70 | |
71 | if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD) |
72 | { |
73 | printf (format: "barrier wait failed: %m\n" ); |
74 | exit (1); |
75 | } |
76 | } |
77 | return NULL; |
78 | } |
79 | |
80 | static int |
81 | do_test (void) |
82 | { |
83 | int count = sysconf (_SC_NPROCESSORS_ONLN); |
84 | if (count <= 0) |
85 | count = 1; |
86 | count *= 4; |
87 | |
88 | pthread_t th[count]; |
89 | int i; |
90 | int result = 0; |
91 | |
92 | if (pthread_barrier_init (barrier: &barr_ready, NULL, count: count + 1) != 0) |
93 | { |
94 | printf (format: "failed to initialize barrier: %m\n" ); |
95 | exit (1); |
96 | } |
97 | |
98 | if (pthread_barrier_init (barrier: &barr_cont, NULL, count: count + 1) != 0) |
99 | { |
100 | printf (format: "failed to initialize barrier: %m\n" ); |
101 | exit (1); |
102 | } |
103 | |
104 | for (i = 0; i < count; ++i) |
105 | if (pthread_create (newthread: &th[i], NULL, start_routine: thread_function, arg: 0) != 0) |
106 | { |
107 | printf (format: "creation of thread %d failed\n" , i); |
108 | exit (1); |
109 | } |
110 | |
111 | for (i = 0 ; i < 7 ; i++) |
112 | { |
113 | if (prctl (PR_SET_FP_MODE, mode[i % 6]) != 0) |
114 | { |
115 | if (errno != ENOTSUP) |
116 | { |
117 | printf (format: "prctl PR_SET_FP_MODE failed: %m" ); |
118 | exit (1); |
119 | } |
120 | } |
121 | else |
122 | current_mode = mode[i % 6]; |
123 | |
124 | |
125 | int res = pthread_barrier_wait (barrier: &barr_ready); |
126 | |
127 | if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD) |
128 | { |
129 | printf (format: "barrier wait failed: %m\n" ); |
130 | exit (1); |
131 | } |
132 | |
133 | if (i == 6) |
134 | finished = 1; |
135 | |
136 | res = pthread_barrier_wait (barrier: &barr_cont); |
137 | |
138 | if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD) |
139 | { |
140 | printf (format: "barrier wait failed: %m\n" ); |
141 | exit (1); |
142 | } |
143 | } |
144 | |
145 | for (i = 0; i < count; ++i) |
146 | { |
147 | void *v; |
148 | if (pthread_join (th: th[i], thread_return: &v) != 0) |
149 | { |
150 | printf (format: "join of thread %d failed\n" , i); |
151 | result = 1; |
152 | } |
153 | else if (v != NULL) |
154 | { |
155 | printf (format: "join %d successful, but child failed\n" , i); |
156 | result = 1; |
157 | } |
158 | else |
159 | printf (format: "join %d successful\n" , i); |
160 | } |
161 | |
162 | return result; |
163 | } |
164 | |
165 | #define TEST_FUNCTION do_test () |
166 | #include "../../test-skeleton.c" |
167 | |