thread.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __COMMON_THREAD_H__
  17. #define __COMMON_THREAD_H__
  18. #include <common/utils.h>
  19. #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  20. int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock);
  21. #endif
  22. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
  23. static inline int _starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared STARPU_ATTRIBUTE_UNUSED)
  24. {
  25. lock->taken = 0;
  26. return 0;
  27. }
  28. #define starpu_pthread_spin_init _starpu_pthread_spin_init
  29. static inline int _starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  30. {
  31. /* we don't do anything */
  32. return 0;
  33. }
  34. #define starpu_pthread_spin_destroy _starpu_pthread_spin_destroy
  35. static inline int _starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  36. {
  37. #ifdef STARPU_SIMGRID
  38. while (1)
  39. {
  40. if (STARPU_LIKELY(!lock->taken))
  41. {
  42. lock->taken = 1;
  43. return 0;
  44. }
  45. /* Give hand to another thread, hopefully the one which has the
  46. * spinlock and probably just has also a short-lived mutex. */
  47. starpu_sleep(0.000001);
  48. STARPU_UYIELD();
  49. }
  50. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  51. if (STARPU_LIKELY(STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0))
  52. /* Got it on first try! */
  53. return 0;
  54. return _starpu_pthread_spin_do_lock(lock);
  55. #else /* !SIMGRID && !LINUX */
  56. uint32_t prev;
  57. do
  58. {
  59. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  60. if (STARPU_UNLIKELY(prev))
  61. STARPU_UYIELD();
  62. }
  63. while (STARPU_UNLIKELY(prev));
  64. return 0;
  65. #endif
  66. }
  67. #define starpu_pthread_spin_lock _starpu_pthread_spin_lock
  68. static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  69. {
  70. #ifdef STARPU_SIMGRID
  71. STARPU_ASSERT(lock->taken);
  72. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  73. STARPU_ASSERT(lock->taken == 1 || lock->taken == 2);
  74. #else
  75. STARPU_ASSERT(lock->taken);
  76. #endif
  77. }
  78. static inline int _starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  79. {
  80. #ifdef STARPU_SIMGRID
  81. if (STARPU_UNLIKELY(lock->taken))
  82. return EBUSY;
  83. lock->taken = 1;
  84. return 0;
  85. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  86. unsigned prev;
  87. prev = STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1);
  88. return (prev == 0)?0:EBUSY;
  89. #else /* !SIMGRID && !LINUX */
  90. uint32_t prev;
  91. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  92. return (prev == 0)?0:EBUSY;
  93. #endif
  94. }
  95. #define starpu_pthread_spin_trylock _starpu_pthread_spin_trylock
  96. #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  97. void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock);
  98. #endif
  99. static inline int _starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  100. {
  101. #ifdef STARPU_SIMGRID
  102. lock->taken = 0;
  103. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  104. STARPU_ASSERT(lock->taken != 0);
  105. STARPU_SYNCHRONIZE();
  106. unsigned next = STARPU_ATOMIC_ADD(&lock->taken, -1);
  107. if (STARPU_LIKELY(next == 0))
  108. /* Nobody to wake, we are done */
  109. return 0;
  110. _starpu_pthread_spin_do_unlock(lock);
  111. #else /* !SIMGRID && !LINUX */
  112. STARPU_RELEASE(&lock->taken);
  113. #endif
  114. return 0;
  115. }
  116. #define starpu_pthread_spin_unlock _starpu_pthread_spin_unlock
  117. #else /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */
  118. static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  119. {
  120. STARPU_ASSERT(pthread_spin_trylock((pthread_spinlock_t *)lock) != 0);
  121. }
  122. #endif /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */
  123. #endif /* __COMMON_THREAD_H__ */