Skip to content

Commit 6948bf3

Browse files
author
anton
committed
Initialize the mutex before making us of it from many threads. Prevents
a race in which one thread is currently initializing the mutex which is not an atomic operation whereas another thread tries to use it too early. With and ok schwarze@
1 parent faf318b commit 6948bf3

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/regress/lib/libc/locale/uselocale/uselocale.c

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 $ */
22
/*
33
* Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
44
*
@@ -170,6 +170,10 @@ _test_MB_CUR_MAX(int line, int ee, size_t ar)
170170
#define TEST_R(Fn, ...) _test_##Fn(__LINE__, 0, __VA_ARGS__)
171171
#define TEST_ER(Fn, ...) _test_##Fn(__LINE__, __VA_ARGS__)
172172

173+
static pthread_mutex_t mtx;
174+
static pthread_mutexattr_t mtxattr;
175+
static pthread_cond_t cond;
176+
173177
/*
174178
* SWITCH_SIGNAL wakes the other thread.
175179
* SWITCH_WAIT goes to sleep.
@@ -179,40 +183,21 @@ _test_MB_CUR_MAX(int line, int ee, size_t ar)
179183
static void
180184
switch_thread(int step, int flags)
181185
{
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+
205189
if (flags & SWITCH_SIGNAL) {
206-
if ((irc = pthread_cond_signal(c)) != 0)
190+
if ((irc = pthread_cond_signal(&cond)) != 0)
207191
errc(1, irc, "pthread_cond_signal(%d)", step);
208192
}
209193
if (flags & SWITCH_WAIT) {
210-
if ((irc = pthread_mutex_trylock(m)) != 0)
194+
if ((irc = pthread_mutex_trylock(&mtx)) != 0)
211195
errc(1, irc, "pthread_mutex_trylock(%d)", step);
212196
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)
214199
errc(1, irc, "pthread_cond_timedwait(%d)", step);
215-
if ((irc = pthread_mutex_unlock(m)) != 0)
200+
if ((irc = pthread_mutex_unlock(&mtx)) != 0)
216201
errc(1, irc, "pthread_mutex_unlock(%d)", step);
217202
}
218203
}
@@ -442,6 +427,16 @@ main(void)
442427
unsetenv("LC_MESSAGES");
443428
unsetenv("LANG");
444429

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+
445440
/* First let the child do some tests. */
446441
if ((irc = pthread_create(&child_thread, NULL, child_func, NULL)) != 0)
447442
errc(1, irc, "pthread_create");

0 commit comments

Comments
 (0)