starpu_spinlock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <common/starpu_spinlock.h>
  18. #include <common/config.h>
  19. #include <common/utils.h>
  20. #include <starpu_util.h>
  21. #ifdef STARPU_SIMGRID
  22. #include <msg/msg.h>
  23. #endif
  24. int _starpu_spin_init(struct _starpu_spinlock *lock)
  25. {
  26. #ifdef STARPU_SIMGRID
  27. lock->taken = 0;
  28. return 0;
  29. #elif defined(STARPU_SPINLOCK_CHECK)
  30. // memcpy(&lock->errcheck_lock, PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, sizeof(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP));
  31. int ret;
  32. ret = pthread_mutexattr_init(&lock->errcheck_attr);
  33. STARPU_CHECK_RETURN_VALUE(ret, "pthread_mutexattr_init");
  34. ret = pthread_mutexattr_settype(&lock->errcheck_attr, PTHREAD_MUTEX_ERRORCHECK);
  35. STARPU_ASSERT(!ret);
  36. ret = pthread_mutex_init(&lock->errcheck_lock, &lock->errcheck_attr);
  37. return ret;
  38. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  39. int ret = pthread_spin_init(&lock->lock, 0);
  40. STARPU_ASSERT(!ret);
  41. return ret;
  42. #else
  43. lock->taken = 0;
  44. return 0;
  45. #endif
  46. }
  47. int _starpu_spin_destroy(struct _starpu_spinlock *lock STARPU_ATTRIBUTE_UNUSED)
  48. {
  49. #ifdef STARPU_SIMGRID
  50. /* we don't do anything */
  51. return 0;
  52. #elif defined(STARPU_SPINLOCK_CHECK)
  53. pthread_mutexattr_destroy(&lock->errcheck_attr);
  54. return pthread_mutex_destroy(&lock->errcheck_lock);
  55. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  56. int ret = pthread_spin_destroy(&lock->lock);
  57. STARPU_ASSERT(!ret);
  58. return ret;
  59. #else
  60. /* we don't do anything */
  61. return 0;
  62. #endif
  63. }
  64. int _starpu_spin_lock(struct _starpu_spinlock *lock)
  65. {
  66. #ifdef STARPU_SIMGRID
  67. while (1)
  68. {
  69. if (!lock->taken)
  70. {
  71. lock->taken = 1;
  72. return 0;
  73. }
  74. /* Give hand to another thread, hopefully the one which has the
  75. * spinlock and probably just has also a short-lived mutex. */
  76. MSG_process_sleep(0.000001);
  77. }
  78. #elif defined(STARPU_SPINLOCK_CHECK)
  79. int ret = pthread_mutex_lock(&lock->errcheck_lock);
  80. STARPU_ASSERT(!ret);
  81. return ret;
  82. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  83. int ret = pthread_spin_lock(&lock->lock);
  84. STARPU_ASSERT(!ret);
  85. return ret;
  86. #else
  87. uint32_t prev;
  88. do
  89. {
  90. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  91. }
  92. while (prev);
  93. return 0;
  94. #endif
  95. }
  96. int _starpu_spin_checklocked(struct _starpu_spinlock *lock)
  97. {
  98. #ifdef STARPU_SIMGRID
  99. STARPU_ASSERT(lock->taken);
  100. return !lock->taken;
  101. #elif defined(STARPU_SPINLOCK_CHECK)
  102. int ret = _STARPU_PTHREAD_MUTEX_TRYLOCK(&lock->errcheck_lock);
  103. STARPU_ASSERT(ret != 0);
  104. return ret == 0;
  105. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  106. int ret = pthread_spin_trylock(&lock->lock);
  107. STARPU_ASSERT(ret != 0);
  108. return ret == 0;
  109. #else
  110. STARPU_ASSERT(lock->taken);
  111. return !lock->taken;
  112. #endif
  113. }
  114. int _starpu_spin_trylock(struct _starpu_spinlock *lock)
  115. {
  116. #ifdef STARPU_SIMGRID
  117. if (lock->taken)
  118. return EBUSY;
  119. lock->taken = 1;
  120. return 0;
  121. #elif defined(STARPU_SPINLOCK_CHECK)
  122. int ret = _STARPU_PTHREAD_MUTEX_TRYLOCK(&lock->errcheck_lock);
  123. STARPU_ASSERT(!ret || (ret == EBUSY));
  124. return ret;
  125. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  126. int ret = pthread_spin_trylock(&lock->lock);
  127. STARPU_ASSERT(!ret || (ret == EBUSY));
  128. return ret;
  129. #else
  130. uint32_t prev;
  131. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  132. return (prev == 0)?0:EBUSY;
  133. #endif
  134. }
  135. int _starpu_spin_unlock(struct _starpu_spinlock *lock STARPU_ATTRIBUTE_UNUSED)
  136. {
  137. #ifdef STARPU_SIMGRID
  138. lock->taken = 0;
  139. return 0;
  140. #elif defined(STARPU_SPINLOCK_CHECK)
  141. int ret = pthread_mutex_unlock(&lock->errcheck_lock);
  142. STARPU_ASSERT(!ret);
  143. return ret;
  144. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  145. int ret = pthread_spin_unlock(&lock->lock);
  146. STARPU_ASSERT(!ret);
  147. return ret;
  148. #else
  149. STARPU_RELEASE(&lock->taken);
  150. return 0;
  151. #endif
  152. }