1 | /* Test support for single-thread optimizations. With threads, static version. |
2 | Copyright (C) 2020-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
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, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | /* This test is a stripped-down version of |
20 | tst-single_threaded-pthread.c, without any loading of dynamic |
21 | objects. */ |
22 | |
23 | #include <stdio.h> |
24 | #include <support/check.h> |
25 | #include <support/xthread.h> |
26 | #include <sys/single_threaded.h> |
27 | |
28 | /* First barrier synchronizes main thread, thread 1, thread 2. */ |
29 | static pthread_barrier_t barrier1; |
30 | |
31 | /* Second barrier synchronizes main thread, thread 2. */ |
32 | static pthread_barrier_t barrier2; |
33 | |
34 | static void * |
35 | threadfunc (void *closure) |
36 | { |
37 | TEST_VERIFY (!__libc_single_threaded); |
38 | |
39 | /* Wait for the main thread and the other thread. */ |
40 | xpthread_barrier_wait (barrier: &barrier1); |
41 | TEST_VERIFY (!__libc_single_threaded); |
42 | |
43 | /* Second thread waits on second barrier, too. */ |
44 | if (closure != NULL) |
45 | xpthread_barrier_wait (barrier: &barrier2); |
46 | TEST_VERIFY (!__libc_single_threaded); |
47 | |
48 | return NULL; |
49 | } |
50 | |
51 | static int |
52 | do_test (void) |
53 | { |
54 | TEST_VERIFY (__libc_single_threaded); |
55 | |
56 | /* Two threads plus main thread. */ |
57 | xpthread_barrier_init (barrier: &barrier1, NULL, count: 3); |
58 | |
59 | /* Main thread and second thread. */ |
60 | xpthread_barrier_init (barrier: &barrier2, NULL, count: 2); |
61 | |
62 | pthread_t thr1 = xpthread_create (NULL, thread_func: threadfunc, NULL); |
63 | TEST_VERIFY (!__libc_single_threaded); |
64 | |
65 | pthread_t thr2 = xpthread_create (NULL, thread_func: threadfunc, closure: &thr2); |
66 | TEST_VERIFY (!__libc_single_threaded); |
67 | |
68 | xpthread_barrier_wait (barrier: &barrier1); |
69 | TEST_VERIFY (!__libc_single_threaded); |
70 | |
71 | /* Join first thread. This should not bring us back into |
72 | single-threaded mode. */ |
73 | xpthread_join (thr: thr1); |
74 | TEST_VERIFY (!__libc_single_threaded); |
75 | |
76 | /* We may be back in single-threaded mode after joining both |
77 | threads, but this is not guaranteed. */ |
78 | xpthread_barrier_wait (barrier: &barrier2); |
79 | xpthread_join (thr: thr2); |
80 | printf (format: "info: __libc_single_threaded after joining all threads: %d\n" , |
81 | __libc_single_threaded); |
82 | |
83 | return 0; |
84 | } |
85 | |
86 | #include <support/test-driver.c> |
87 | |