lock.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This is a Linux specific implementation of the public OpenMP locking
  21. primitives. This implementation uses atomic instructions and the futex
  22. syscall. */
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/syscall.h>
  26. #include "wait.h"
  27. /* Reuse the generic implementation in terms of gomp_mutex_t. */
  28. #include "../../lock.c"
  29. #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
  30. /* gomp_mutex_* can be safely locked in one thread and
  31. unlocked in another thread, so the OpenMP 2.5 and OpenMP 3.0
  32. non-nested locks can be the same. */
  33. strong_alias (gomp_init_lock_30, gomp_init_lock_25)
  34. strong_alias (gomp_destroy_lock_30, gomp_destroy_lock_25)
  35. strong_alias (gomp_set_lock_30, gomp_set_lock_25)
  36. strong_alias (gomp_unset_lock_30, gomp_unset_lock_25)
  37. strong_alias (gomp_test_lock_30, gomp_test_lock_25)
  38. /* The external recursive omp_nest_lock_25_t form requires additional work. */
  39. /* We need an integer to uniquely identify this thread. Most generally
  40. this is the thread's TID, which ideally we'd get this straight from
  41. the TLS block where glibc keeps it. Unfortunately, we can't get at
  42. that directly.
  43. If we don't support (or have disabled) TLS, one function call is as
  44. good (or bad) as any other. Use the syscall all the time.
  45. On an ILP32 system (defined here as not LP64), we can make do with
  46. any thread-local pointer. Ideally we'd use the TLS base address,
  47. since that requires the least amount of arithmetic, but that's not
  48. always available directly. Make do with the gomp_thread pointer
  49. since it's handy. */
  50. # if !defined (HAVE_TLS)
  51. static inline int gomp_tid (void)
  52. {
  53. return syscall (SYS_gettid);
  54. }
  55. # elif !defined(__LP64__)
  56. static inline int gomp_tid (void)
  57. {
  58. return (int) gomp_thread ();
  59. }
  60. # else
  61. static __thread int tid_cache;
  62. static inline int gomp_tid (void)
  63. {
  64. int tid = tid_cache;
  65. if (__builtin_expect (tid == 0, 0))
  66. tid_cache = tid = syscall (SYS_gettid);
  67. return tid;
  68. }
  69. # endif
  70. void
  71. gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
  72. {
  73. memset (lock, 0, sizeof (*lock));
  74. }
  75. void
  76. gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
  77. {
  78. }
  79. void
  80. gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
  81. {
  82. int otid, tid = gomp_tid ();
  83. while (1)
  84. {
  85. otid = 0;
  86. if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
  87. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  88. {
  89. lock->count = 1;
  90. return;
  91. }
  92. if (otid == tid)
  93. {
  94. lock->count++;
  95. return;
  96. }
  97. do_wait (&lock->owner, otid);
  98. }
  99. }
  100. void
  101. gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
  102. {
  103. /* ??? Validate that we own the lock here. */
  104. if (--lock->count == 0)
  105. {
  106. __atomic_store_n (&lock->owner, 0, MEMMODEL_RELEASE);
  107. futex_wake (&lock->owner, 1);
  108. }
  109. }
  110. int
  111. gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
  112. {
  113. int otid, tid = gomp_tid ();
  114. otid = 0;
  115. if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
  116. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  117. {
  118. lock->count = 1;
  119. return 1;
  120. }
  121. if (otid == tid)
  122. return ++lock->count;
  123. return 0;
  124. }
  125. omp_lock_symver (omp_init_lock)
  126. omp_lock_symver (omp_destroy_lock)
  127. omp_lock_symver (omp_set_lock)
  128. omp_lock_symver (omp_unset_lock)
  129. omp_lock_symver (omp_test_lock)
  130. omp_lock_symver (omp_init_nest_lock)
  131. omp_lock_symver (omp_destroy_nest_lock)
  132. omp_lock_symver (omp_set_nest_lock)
  133. omp_lock_symver (omp_unset_nest_lock)
  134. omp_lock_symver (omp_test_nest_lock)
  135. #else
  136. ialias (omp_init_lock)
  137. ialias (omp_init_nest_lock)
  138. ialias (omp_destroy_lock)
  139. ialias (omp_destroy_nest_lock)
  140. ialias (omp_set_lock)
  141. ialias (omp_set_nest_lock)
  142. ialias (omp_unset_lock)
  143. ialias (omp_unset_nest_lock)
  144. ialias (omp_test_lock)
  145. ialias (omp_test_nest_lock)
  146. #endif