lock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright (C) 2005-2015 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. /* The internal gomp_mutex_t and the external non-recursive omp_lock_t
  28. have the same form. Re-use it. */
  29. void
  30. gomp_init_lock_30 (omp_lock_t *lock)
  31. {
  32. gomp_mutex_init (lock);
  33. }
  34. void
  35. gomp_destroy_lock_30 (omp_lock_t *lock)
  36. {
  37. gomp_mutex_destroy (lock);
  38. }
  39. void
  40. gomp_set_lock_30 (omp_lock_t *lock)
  41. {
  42. gomp_mutex_lock (lock);
  43. }
  44. void
  45. gomp_unset_lock_30 (omp_lock_t *lock)
  46. {
  47. gomp_mutex_unlock (lock);
  48. }
  49. int
  50. gomp_test_lock_30 (omp_lock_t *lock)
  51. {
  52. int oldval = 0;
  53. return __atomic_compare_exchange_n (lock, &oldval, 1, false,
  54. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED);
  55. }
  56. void
  57. gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
  58. {
  59. memset (lock, '\0', sizeof (*lock));
  60. }
  61. void
  62. gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
  63. {
  64. }
  65. void
  66. gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
  67. {
  68. void *me = gomp_icv (true);
  69. if (lock->owner != me)
  70. {
  71. gomp_mutex_lock (&lock->lock);
  72. lock->owner = me;
  73. }
  74. lock->count++;
  75. }
  76. void
  77. gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
  78. {
  79. if (--lock->count == 0)
  80. {
  81. lock->owner = NULL;
  82. gomp_mutex_unlock (&lock->lock);
  83. }
  84. }
  85. int
  86. gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
  87. {
  88. void *me = gomp_icv (true);
  89. int oldval;
  90. if (lock->owner == me)
  91. return ++lock->count;
  92. oldval = 0;
  93. if (__atomic_compare_exchange_n (&lock->lock, &oldval, 1, false,
  94. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  95. {
  96. lock->owner = me;
  97. lock->count = 1;
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
  103. /* gomp_mutex_* can be safely locked in one thread and
  104. unlocked in another thread, so the OpenMP 2.5 and OpenMP 3.0
  105. non-nested locks can be the same. */
  106. strong_alias (gomp_init_lock_30, gomp_init_lock_25)
  107. strong_alias (gomp_destroy_lock_30, gomp_destroy_lock_25)
  108. strong_alias (gomp_set_lock_30, gomp_set_lock_25)
  109. strong_alias (gomp_unset_lock_30, gomp_unset_lock_25)
  110. strong_alias (gomp_test_lock_30, gomp_test_lock_25)
  111. /* The external recursive omp_nest_lock_25_t form requires additional work. */
  112. /* We need an integer to uniquely identify this thread. Most generally
  113. this is the thread's TID, which ideally we'd get this straight from
  114. the TLS block where glibc keeps it. Unfortunately, we can't get at
  115. that directly.
  116. If we don't support (or have disabled) TLS, one function call is as
  117. good (or bad) as any other. Use the syscall all the time.
  118. On an ILP32 system (defined here as not LP64), we can make do with
  119. any thread-local pointer. Ideally we'd use the TLS base address,
  120. since that requires the least amount of arithmetic, but that's not
  121. always available directly. Make do with the gomp_thread pointer
  122. since it's handy. */
  123. # if !defined (HAVE_TLS)
  124. static inline int gomp_tid (void)
  125. {
  126. return syscall (SYS_gettid);
  127. }
  128. # elif !defined(__LP64__)
  129. static inline int gomp_tid (void)
  130. {
  131. return (int) gomp_thread ();
  132. }
  133. # else
  134. static __thread int tid_cache;
  135. static inline int gomp_tid (void)
  136. {
  137. int tid = tid_cache;
  138. if (__builtin_expect (tid == 0, 0))
  139. tid_cache = tid = syscall (SYS_gettid);
  140. return tid;
  141. }
  142. # endif
  143. void
  144. gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
  145. {
  146. memset (lock, 0, sizeof (*lock));
  147. }
  148. void
  149. gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
  150. {
  151. }
  152. void
  153. gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
  154. {
  155. int otid, tid = gomp_tid ();
  156. while (1)
  157. {
  158. otid = 0;
  159. if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
  160. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  161. {
  162. lock->count = 1;
  163. return;
  164. }
  165. if (otid == tid)
  166. {
  167. lock->count++;
  168. return;
  169. }
  170. do_wait (&lock->owner, otid);
  171. }
  172. }
  173. void
  174. gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
  175. {
  176. /* ??? Validate that we own the lock here. */
  177. if (--lock->count == 0)
  178. {
  179. __atomic_store_n (&lock->owner, 0, MEMMODEL_RELEASE);
  180. futex_wake (&lock->owner, 1);
  181. }
  182. }
  183. int
  184. gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
  185. {
  186. int otid, tid = gomp_tid ();
  187. otid = 0;
  188. if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
  189. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  190. {
  191. lock->count = 1;
  192. return 1;
  193. }
  194. if (otid == tid)
  195. return ++lock->count;
  196. return 0;
  197. }
  198. omp_lock_symver (omp_init_lock)
  199. omp_lock_symver (omp_destroy_lock)
  200. omp_lock_symver (omp_set_lock)
  201. omp_lock_symver (omp_unset_lock)
  202. omp_lock_symver (omp_test_lock)
  203. omp_lock_symver (omp_init_nest_lock)
  204. omp_lock_symver (omp_destroy_nest_lock)
  205. omp_lock_symver (omp_set_nest_lock)
  206. omp_lock_symver (omp_unset_nest_lock)
  207. omp_lock_symver (omp_test_nest_lock)
  208. #else
  209. ialias (omp_init_lock)
  210. ialias (omp_init_nest_lock)
  211. ialias (omp_destroy_lock)
  212. ialias (omp_destroy_nest_lock)
  213. ialias (omp_set_lock)
  214. ialias (omp_set_nest_lock)
  215. ialias (omp_unset_lock)
  216. ialias (omp_unset_nest_lock)
  217. ialias (omp_test_lock)
  218. ialias (omp_test_nest_lock)
  219. #endif