Update to Unicode 15.1.0 [BZ #30854]
[glibc.git] / sysdeps / nptl / dl-thread_gscope_wait.c
blob90432f399e20466bb4f5b800e4b1ceba034e0584
1 /* Out-of-line notification function for the GSCOPE locking mechanism.
2 Copyright (C) 2007-2023 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 #include <nptl/descr.h>
20 #include <futex-internal.h>
21 #include <ldsodefs.h>
22 #include <list.h>
23 #include <lowlevellock.h>
25 void
26 __thread_gscope_wait (void)
28 lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
30 struct pthread *self = THREAD_SELF;
32 /* Iterate over the list with system-allocated threads first. */
33 list_t *runp;
34 list_for_each (runp, &GL (dl_stack_used))
36 struct pthread *t = list_entry (runp, struct pthread, list);
37 if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
38 continue;
40 int *const gscope_flagp = &t->header.gscope_flag;
42 /* We have to wait until this thread is done with the global
43 scope. First tell the thread that we are waiting and
44 possibly have to be woken. */
45 if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
46 THREAD_GSCOPE_FLAG_WAIT,
47 THREAD_GSCOPE_FLAG_USED))
48 continue;
51 futex_wait_simple ((unsigned int *) gscope_flagp,
52 THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
53 while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
56 /* Now the list with threads using user-allocated stacks. */
57 list_for_each (runp, &GL (dl_stack_user))
59 struct pthread *t = list_entry (runp, struct pthread, list);
60 if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
61 continue;
63 int *const gscope_flagp = &t->header.gscope_flag;
65 /* We have to wait until this thread is done with the global
66 scope. First tell the thread that we are waiting and
67 possibly have to be woken. */
68 if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
69 THREAD_GSCOPE_FLAG_WAIT,
70 THREAD_GSCOPE_FLAG_USED))
71 continue;
74 futex_wait_simple ((unsigned int *) gscope_flagp,
75 THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
76 while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
79 lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);