diff options
author | anton <> | 2022-04-03 16:52:50 +0000 |
---|---|---|
committer | anton <> | 2022-04-03 16:52:50 +0000 |
commit | 491cd486a581a71008b2ca3b70aeb7c2ef32b32b (patch) | |
tree | 7a7d446722df0be753992106ccaed79ee8205b5e /src/regress/lib | |
parent | 8f38d7689ac702fcf8b05eeacbeec41d2e737624 (diff) | |
download | openbsd-491cd486a581a71008b2ca3b70aeb7c2ef32b32b.tar.gz openbsd-491cd486a581a71008b2ca3b70aeb7c2ef32b32b.tar.bz2 openbsd-491cd486a581a71008b2ca3b70aeb7c2ef32b32b.zip |
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@
Diffstat (limited to 'src/regress/lib')
-rw-r--r-- | src/regress/lib/libc/locale/uselocale/uselocale.c | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/src/regress/lib/libc/locale/uselocale/uselocale.c b/src/regress/lib/libc/locale/uselocale/uselocale.c index 445bb64876..f07a16ad65 100644 --- a/src/regress/lib/libc/locale/uselocale/uselocale.c +++ b/src/regress/lib/libc/locale/uselocale/uselocale.c | |||
@@ -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 $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org> | 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 | #define TEST_R(Fn, ...) _test_##Fn(__LINE__, 0, __VA_ARGS__) | 170 | #define TEST_R(Fn, ...) _test_##Fn(__LINE__, 0, __VA_ARGS__) |
171 | #define TEST_ER(Fn, ...) _test_##Fn(__LINE__, __VA_ARGS__) | 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 | * SWITCH_SIGNAL wakes the other thread. | 178 | * SWITCH_SIGNAL wakes the other thread. |
175 | * SWITCH_WAIT goes to sleep. | 179 | * SWITCH_WAIT goes to sleep. |
@@ -179,40 +183,21 @@ _test_MB_CUR_MAX(int line, int ee, size_t ar) | |||
179 | static void | 183 | static void |
180 | switch_thread(int step, int flags) | 184 | switch_thread(int step, int flags) |
181 | { | 185 | { |
182 | static pthread_mutexattr_t ma; | 186 | struct timespec t; |
183 | static struct timespec t; | 187 | int irc; |
184 | static pthread_cond_t *c; | 188 | |
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 | } | ||
205 | if (flags & SWITCH_SIGNAL) { | 189 | if (flags & SWITCH_SIGNAL) { |
206 | if ((irc = pthread_cond_signal(c)) != 0) | 190 | if ((irc = pthread_cond_signal(&cond)) != 0) |
207 | errc(1, irc, "pthread_cond_signal(%d)", step); | 191 | errc(1, irc, "pthread_cond_signal(%d)", step); |
208 | } | 192 | } |
209 | if (flags & SWITCH_WAIT) { | 193 | if (flags & SWITCH_WAIT) { |
210 | if ((irc = pthread_mutex_trylock(m)) != 0) | 194 | if ((irc = pthread_mutex_trylock(&mtx)) != 0) |
211 | errc(1, irc, "pthread_mutex_trylock(%d)", step); | 195 | errc(1, irc, "pthread_mutex_trylock(%d)", step); |
212 | t.tv_sec = time(NULL) + 2; | 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 | errc(1, irc, "pthread_cond_timedwait(%d)", step); | 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 | errc(1, irc, "pthread_mutex_unlock(%d)", step); | 201 | errc(1, irc, "pthread_mutex_unlock(%d)", step); |
217 | } | 202 | } |
218 | } | 203 | } |
@@ -442,6 +427,16 @@ main(void) | |||
442 | unsetenv("LC_MESSAGES"); | 427 | unsetenv("LC_MESSAGES"); |
443 | unsetenv("LANG"); | 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 | /* First let the child do some tests. */ | 440 | /* First let the child do some tests. */ |
446 | if ((irc = pthread_create(&child_thread, NULL, child_func, NULL)) != 0) | 441 | if ((irc = pthread_create(&child_thread, NULL, child_func, NULL)) != 0) |
447 | errc(1, irc, "pthread_create"); | 442 | errc(1, irc, "pthread_create"); |