1
- /* $OpenBSD: uselocale.c,v 1.5 2017/08/16 13 :52:50 schwarze Exp $ */
1
+ /* $OpenBSD: uselocale.c,v 1.6 2022/04/03 16 :52:50 anton Exp $ */
2
2
/*
3
3
* Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
4
4
*
@@ -170,6 +170,10 @@ _test_MB_CUR_MAX(int line, int ee, size_t ar)
170
170
#define TEST_R (Fn , ...) _test_##Fn(__LINE__, 0, __VA_ARGS__)
171
171
#define TEST_ER (Fn , ...) _test_##Fn(__LINE__, __VA_ARGS__)
172
172
173
+ static pthread_mutex_t mtx ;
174
+ static pthread_mutexattr_t mtxattr ;
175
+ static pthread_cond_t cond ;
176
+
173
177
/*
174
178
* SWITCH_SIGNAL wakes the other thread.
175
179
* SWITCH_WAIT goes to sleep.
@@ -179,40 +183,21 @@ _test_MB_CUR_MAX(int line, int ee, size_t ar)
179
183
static void
180
184
switch_thread (int step , int flags )
181
185
{
182
- static pthread_mutexattr_t ma ;
183
- static struct timespec t ;
184
- static pthread_cond_t * c ;
185
- static pthread_mutex_t * m ;
186
- int irc ;
187
-
188
- if (m == NULL ) {
189
- if ((m = malloc (sizeof (* m ))) == NULL )
190
- err (1 , NULL );
191
- if ((irc = pthread_mutexattr_init (& ma )) != 0 )
192
- errc (1 , irc , "pthread_mutexattr_init" );
193
- if ((irc = pthread_mutexattr_settype (& ma ,
194
- PTHREAD_MUTEX_STRICT_NP )) != 0 )
195
- errc (1 , irc , "pthread_mutexattr_settype" );
196
- if ((irc = pthread_mutex_init (m , & ma )) != 0 )
197
- errc (1 , irc , "pthread_mutex_init" );
198
- }
199
- if (c == NULL ) {
200
- if ((c = malloc (sizeof (* c ))) == NULL )
201
- err (1 , NULL );
202
- if ((irc = pthread_cond_init (c , NULL )) != 0 )
203
- errc (1 , irc , "pthread_cond_init" );
204
- }
186
+ struct timespec t ;
187
+ int irc ;
188
+
205
189
if (flags & SWITCH_SIGNAL ) {
206
- if ((irc = pthread_cond_signal (c )) != 0 )
190
+ if ((irc = pthread_cond_signal (& cond )) != 0 )
207
191
errc (1 , irc , "pthread_cond_signal(%d)" , step );
208
192
}
209
193
if (flags & SWITCH_WAIT ) {
210
- if ((irc = pthread_mutex_trylock (m )) != 0 )
194
+ if ((irc = pthread_mutex_trylock (& mtx )) != 0 )
211
195
errc (1 , irc , "pthread_mutex_trylock(%d)" , step );
212
196
t .tv_sec = time (NULL ) + 2 ;
213
- if ((irc = pthread_cond_timedwait (c , m , & t )) != 0 )
197
+ t .tv_nsec = 0 ;
198
+ if ((irc = pthread_cond_timedwait (& cond , & mtx , & t )) != 0 )
214
199
errc (1 , irc , "pthread_cond_timedwait(%d)" , step );
215
- if ((irc = pthread_mutex_unlock (m )) != 0 )
200
+ if ((irc = pthread_mutex_unlock (& mtx )) != 0 )
216
201
errc (1 , irc , "pthread_mutex_unlock(%d)" , step );
217
202
}
218
203
}
@@ -442,6 +427,16 @@ main(void)
442
427
unsetenv ("LC_MESSAGES" );
443
428
unsetenv ("LANG" );
444
429
430
+ if ((irc = pthread_mutexattr_init (& mtxattr )) != 0 )
431
+ errc (1 , irc , "pthread_mutexattr_init" );
432
+ if ((irc = pthread_mutexattr_settype (& mtxattr ,
433
+ PTHREAD_MUTEX_STRICT_NP )) != 0 )
434
+ errc (1 , irc , "pthread_mutexattr_settype" );
435
+ if ((irc = pthread_mutex_init (& mtx , & mtxattr )) != 0 )
436
+ errc (1 , irc , "pthread_mutex_init" );
437
+ if ((irc = pthread_cond_init (& cond , NULL )) != 0 )
438
+ errc (1 , irc , "pthread_cond_init" );
439
+
445
440
/* First let the child do some tests. */
446
441
if ((irc = pthread_create (& child_thread , NULL , child_func , NULL )) != 0 )
447
442
errc (1 , irc , "pthread_create" );
0 commit comments