dl-thread_gscope_wait.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Out-of-line notification function for the GSCOPE locking mechanism.
  2. Copyright (C) 2007-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <nptl/descr.h>
  16. #include <futex-internal.h>
  17. #include <ldsodefs.h>
  18. #include <list.h>
  19. #include <lowlevellock.h>
  20. void
  21. __thread_gscope_wait (void)
  22. {
  23. lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
  24. struct pthread *self = THREAD_SELF;
  25. /* Iterate over the list with system-allocated threads first. */
  26. list_t *runp;
  27. list_for_each (runp, &GL (dl_stack_used))
  28. {
  29. struct pthread *t = list_entry (runp, struct pthread, list);
  30. if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
  31. continue;
  32. int *const gscope_flagp = &t->header.gscope_flag;
  33. /* We have to wait until this thread is done with the global
  34. scope. First tell the thread that we are waiting and
  35. possibly have to be woken. */
  36. if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
  37. THREAD_GSCOPE_FLAG_WAIT,
  38. THREAD_GSCOPE_FLAG_USED))
  39. continue;
  40. do
  41. futex_wait_simple ((unsigned int *) gscope_flagp,
  42. THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
  43. while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
  44. }
  45. /* Now the list with threads using user-allocated stacks. */
  46. list_for_each (runp, &GL (dl_stack_user))
  47. {
  48. struct pthread *t = list_entry (runp, struct pthread, list);
  49. if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
  50. continue;
  51. int *const gscope_flagp = &t->header.gscope_flag;
  52. /* We have to wait until this thread is done with the global
  53. scope. First tell the thread that we are waiting and
  54. possibly have to be woken. */
  55. if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
  56. THREAD_GSCOPE_FLAG_WAIT,
  57. THREAD_GSCOPE_FLAG_USED))
  58. continue;
  59. do
  60. futex_wait_simple ((unsigned int *) gscope_flagp,
  61. THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
  62. while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
  63. }
  64. lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
  65. }