lock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 generic implementation of the public OpenMP locking primitives in
  21. terms of internal gomp_mutex_t. It is not meant to be compiled on its own.
  22. It is #include'd from config/{linux,nvptx}/lock.c. */
  23. #include <string.h>
  24. #include "libgomp.h"
  25. /* The internal gomp_mutex_t and the external non-recursive omp_lock_t
  26. have the same form. Re-use it. */
  27. void
  28. gomp_init_lock_30 (omp_lock_t *lock)
  29. {
  30. gomp_mutex_init (lock);
  31. }
  32. void
  33. gomp_destroy_lock_30 (omp_lock_t *lock)
  34. {
  35. gomp_mutex_destroy (lock);
  36. }
  37. void
  38. gomp_set_lock_30 (omp_lock_t *lock)
  39. {
  40. gomp_mutex_lock (lock);
  41. }
  42. void
  43. gomp_unset_lock_30 (omp_lock_t *lock)
  44. {
  45. gomp_mutex_unlock (lock);
  46. }
  47. int
  48. gomp_test_lock_30 (omp_lock_t *lock)
  49. {
  50. int oldval = 0;
  51. return __atomic_compare_exchange_n (lock, &oldval, 1, false,
  52. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED);
  53. }
  54. void
  55. gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
  56. {
  57. memset (lock, '\0', sizeof (*lock));
  58. }
  59. void
  60. gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
  61. {
  62. }
  63. void
  64. gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
  65. {
  66. void *me = gomp_icv (true);
  67. if (lock->owner != me)
  68. {
  69. gomp_mutex_lock (&lock->lock);
  70. lock->owner = me;
  71. }
  72. lock->count++;
  73. }
  74. void
  75. gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
  76. {
  77. if (--lock->count == 0)
  78. {
  79. lock->owner = NULL;
  80. gomp_mutex_unlock (&lock->lock);
  81. }
  82. }
  83. int
  84. gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
  85. {
  86. void *me = gomp_icv (true);
  87. int oldval;
  88. if (lock->owner == me)
  89. return ++lock->count;
  90. oldval = 0;
  91. if (__atomic_compare_exchange_n (&lock->lock, &oldval, 1, false,
  92. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  93. {
  94. lock->owner = me;
  95. lock->count = 1;
  96. return 1;
  97. }
  98. return 0;
  99. }