lock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 the default PTHREADS implementation of the public OpenMP
  21. locking primitives.
  22. Because OpenMP uses different entry points for normal and recursive
  23. locks, and pthreads uses only one entry point, a system may be able
  24. to do better and streamline the locking as well as reduce the size
  25. of the types exported. */
  26. /* We need UNIX98/XPG5 extensions to get recursive locks. Request XPG6 since
  27. Solaris requires this for C99 and later. */
  28. #define _XOPEN_SOURCE 600
  29. #include "libgomp.h"
  30. #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
  31. void
  32. gomp_init_lock_30 (omp_lock_t *lock)
  33. {
  34. pthread_mutex_init (lock, NULL);
  35. }
  36. void
  37. gomp_destroy_lock_30 (omp_lock_t *lock)
  38. {
  39. pthread_mutex_destroy (lock);
  40. }
  41. void
  42. gomp_set_lock_30 (omp_lock_t *lock)
  43. {
  44. pthread_mutex_lock (lock);
  45. }
  46. void
  47. gomp_unset_lock_30 (omp_lock_t *lock)
  48. {
  49. pthread_mutex_unlock (lock);
  50. }
  51. int
  52. gomp_test_lock_30 (omp_lock_t *lock)
  53. {
  54. return pthread_mutex_trylock (lock) == 0;
  55. }
  56. void
  57. gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
  58. {
  59. pthread_mutex_init (&lock->lock, NULL);
  60. lock->count = 0;
  61. lock->owner = NULL;
  62. }
  63. void
  64. gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
  65. {
  66. pthread_mutex_destroy (&lock->lock);
  67. }
  68. void
  69. gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
  70. {
  71. void *me = gomp_icv (true);
  72. if (lock->owner != me)
  73. {
  74. pthread_mutex_lock (&lock->lock);
  75. lock->owner = me;
  76. }
  77. lock->count++;
  78. }
  79. void
  80. gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
  81. {
  82. if (--lock->count == 0)
  83. {
  84. lock->owner = NULL;
  85. pthread_mutex_unlock (&lock->lock);
  86. }
  87. }
  88. int
  89. gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
  90. {
  91. void *me = gomp_icv (true);
  92. if (lock->owner != me)
  93. {
  94. if (pthread_mutex_trylock (&lock->lock) != 0)
  95. return 0;
  96. lock->owner = me;
  97. }
  98. return ++lock->count;
  99. }
  100. #else
  101. void
  102. gomp_init_lock_30 (omp_lock_t *lock)
  103. {
  104. sem_init (lock, 0, 1);
  105. }
  106. void
  107. gomp_destroy_lock_30 (omp_lock_t *lock)
  108. {
  109. sem_destroy (lock);
  110. }
  111. void
  112. gomp_set_lock_30 (omp_lock_t *lock)
  113. {
  114. while (sem_wait (lock) != 0)
  115. ;
  116. }
  117. void
  118. gomp_unset_lock_30 (omp_lock_t *lock)
  119. {
  120. sem_post (lock);
  121. }
  122. int
  123. gomp_test_lock_30 (omp_lock_t *lock)
  124. {
  125. return sem_trywait (lock) == 0;
  126. }
  127. void
  128. gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
  129. {
  130. sem_init (&lock->lock, 0, 1);
  131. lock->count = 0;
  132. lock->owner = NULL;
  133. }
  134. void
  135. gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
  136. {
  137. sem_destroy (&lock->lock);
  138. }
  139. void
  140. gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
  141. {
  142. void *me = gomp_icv (true);
  143. if (lock->owner != me)
  144. {
  145. while (sem_wait (&lock->lock) != 0)
  146. ;
  147. lock->owner = me;
  148. }
  149. lock->count++;
  150. }
  151. void
  152. gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
  153. {
  154. if (--lock->count == 0)
  155. {
  156. lock->owner = NULL;
  157. sem_post (&lock->lock);
  158. }
  159. }
  160. int
  161. gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
  162. {
  163. void *me = gomp_icv (true);
  164. if (lock->owner != me)
  165. {
  166. if (sem_trywait (&lock->lock) != 0)
  167. return 0;
  168. lock->owner = me;
  169. }
  170. return ++lock->count;
  171. }
  172. #endif
  173. #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
  174. void
  175. gomp_init_lock_25 (omp_lock_25_t *lock)
  176. {
  177. pthread_mutex_init (lock, NULL);
  178. }
  179. void
  180. gomp_destroy_lock_25 (omp_lock_25_t *lock)
  181. {
  182. pthread_mutex_destroy (lock);
  183. }
  184. void
  185. gomp_set_lock_25 (omp_lock_25_t *lock)
  186. {
  187. pthread_mutex_lock (lock);
  188. }
  189. void
  190. gomp_unset_lock_25 (omp_lock_25_t *lock)
  191. {
  192. pthread_mutex_unlock (lock);
  193. }
  194. int
  195. gomp_test_lock_25 (omp_lock_25_t *lock)
  196. {
  197. return pthread_mutex_trylock (lock) == 0;
  198. }
  199. void
  200. gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
  201. {
  202. pthread_mutexattr_t attr;
  203. pthread_mutexattr_init (&attr);
  204. pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
  205. pthread_mutex_init (&lock->lock, &attr);
  206. lock->count = 0;
  207. pthread_mutexattr_destroy (&attr);
  208. }
  209. void
  210. gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
  211. {
  212. pthread_mutex_destroy (&lock->lock);
  213. }
  214. void
  215. gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
  216. {
  217. pthread_mutex_lock (&lock->lock);
  218. lock->count++;
  219. }
  220. void
  221. gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
  222. {
  223. lock->count--;
  224. pthread_mutex_unlock (&lock->lock);
  225. }
  226. int
  227. gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
  228. {
  229. if (pthread_mutex_trylock (&lock->lock) == 0)
  230. return ++lock->count;
  231. return 0;
  232. }
  233. omp_lock_symver (omp_init_lock)
  234. omp_lock_symver (omp_destroy_lock)
  235. omp_lock_symver (omp_set_lock)
  236. omp_lock_symver (omp_unset_lock)
  237. omp_lock_symver (omp_test_lock)
  238. omp_lock_symver (omp_init_nest_lock)
  239. omp_lock_symver (omp_destroy_nest_lock)
  240. omp_lock_symver (omp_set_nest_lock)
  241. omp_lock_symver (omp_unset_nest_lock)
  242. omp_lock_symver (omp_test_nest_lock)
  243. #else
  244. ialias (omp_init_lock)
  245. ialias (omp_init_nest_lock)
  246. ialias (omp_destroy_lock)
  247. ialias (omp_destroy_nest_lock)
  248. ialias (omp_set_lock)
  249. ialias (omp_set_nest_lock)
  250. ialias (omp_unset_lock)
  251. ialias (omp_unset_nest_lock)
  252. ialias (omp_test_lock)
  253. ialias (omp_test_nest_lock)
  254. #endif