(root)/
glibc-2.38/
sysdeps/
nptl/
dl-thread_gscope_wait.c
       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.
       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  #include <nptl/descr.h>
      20  #include <futex-internal.h>
      21  #include <ldsodefs.h>
      22  #include <list.h>
      23  #include <lowlevellock.h>
      24  
      25  void
      26  __thread_gscope_wait (void)
      27  {
      28    lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
      29  
      30    struct pthread *self = THREAD_SELF;
      31  
      32    /* Iterate over the list with system-allocated threads first.  */
      33    list_t *runp;
      34    list_for_each (runp, &GL (dl_stack_used))
      35      {
      36        struct pthread *t = list_entry (runp, struct pthread, list);
      37        if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
      38          continue;
      39  
      40        int *const gscope_flagp = &t->header.gscope_flag;
      41  
      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;
      49  
      50        do
      51          futex_wait_simple ((unsigned int *) gscope_flagp,
      52                             THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
      53        while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
      54      }
      55  
      56    /* Now the list with threads using user-allocated stacks.  */
      57    list_for_each (runp, &GL (dl_stack_user))
      58      {
      59        struct pthread *t = list_entry (runp, struct pthread, list);
      60        if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
      61          continue;
      62  
      63        int *const gscope_flagp = &t->header.gscope_flag;
      64  
      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;
      72  
      73        do
      74          futex_wait_simple ((unsigned int *) gscope_flagp,
      75                             THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
      76        while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
      77      }
      78  
      79    lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
      80  }