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.
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.
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.
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/>. */
19 /* This test is a stripped-down version of
20 tst-single_threaded-pthread.c, without any loading of dynamic
24 #include <support/check.h>
25 #include <support/xthread.h>
26 #include <sys/single_threaded.h>
28 /* First barrier synchronizes main thread, thread 1, thread 2. */
29 static pthread_barrier_t barrier1
;
31 /* Second barrier synchronizes main thread, thread 2. */
32 static pthread_barrier_t barrier2
;
35 threadfunc (void *closure
)
37 TEST_VERIFY (!__libc_single_threaded
);
39 /* Wait for the main thread and the other thread. */
40 xpthread_barrier_wait (&barrier1
);
41 TEST_VERIFY (!__libc_single_threaded
);
43 /* Second thread waits on second barrier, too. */
45 xpthread_barrier_wait (&barrier2
);
46 TEST_VERIFY (!__libc_single_threaded
);
54 TEST_VERIFY (__libc_single_threaded
);
56 /* Two threads plus main thread. */
57 xpthread_barrier_init (&barrier1
, NULL
, 3);
59 /* Main thread and second thread. */
60 xpthread_barrier_init (&barrier2
, NULL
, 2);
62 pthread_t thr1
= xpthread_create (NULL
, threadfunc
, NULL
);
63 TEST_VERIFY (!__libc_single_threaded
);
65 pthread_t thr2
= xpthread_create (NULL
, threadfunc
, &thr2
);
66 TEST_VERIFY (!__libc_single_threaded
);
68 xpthread_barrier_wait (&barrier1
);
69 TEST_VERIFY (!__libc_single_threaded
);
71 /* Join first thread. This should not bring us back into
72 single-threaded mode. */
74 TEST_VERIFY (!__libc_single_threaded
);
76 /* We may be back in single-threaded mode after joining both
77 threads, but this is not guaranteed. */
78 xpthread_barrier_wait (&barrier2
);
80 printf ("info: __libc_single_threaded after joining all threads: %d\n",
81 __libc_single_threaded
);
86 #include <support/test-driver.c>