thread.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014, 2016-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 CNRS
  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. * StarPr 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. #ifndef __COMMON_THREAD_H__
  18. #define __COMMON_THREAD_H__
  19. #include <starpu.h>
  20. #include <common/utils.h>
  21. #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  22. int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock);
  23. #endif
  24. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
  25. static inline int _starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared STARPU_ATTRIBUTE_UNUSED)
  26. {
  27. lock->taken = 0;
  28. return 0;
  29. }
  30. #define starpu_pthread_spin_init _starpu_pthread_spin_init
  31. static inline int _starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  32. {
  33. /* we don't do anything */
  34. return 0;
  35. }
  36. #define starpu_pthread_spin_destroy _starpu_pthread_spin_destroy
  37. static inline int _starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  38. {
  39. #ifdef STARPU_SIMGRID
  40. while (1)
  41. {
  42. if (STARPU_LIKELY(!lock->taken))
  43. {
  44. lock->taken = 1;
  45. return 0;
  46. }
  47. /* Give hand to another thread, hopefully the one which has the
  48. * spinlock and probably just has also a short-lived mutex. */
  49. MSG_process_sleep(0.000001);
  50. STARPU_UYIELD();
  51. }
  52. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  53. if (STARPU_LIKELY(STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0))
  54. /* Got it on first try! */
  55. return 0;
  56. return _starpu_pthread_spin_do_lock(lock);
  57. #else /* !SIMGRID && !LINUX */
  58. uint32_t prev;
  59. do
  60. {
  61. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  62. if (STARPU_UNLIKELY(prev))
  63. STARPU_UYIELD();
  64. }
  65. while (STARPU_UNLIKELY(prev));
  66. return 0;
  67. #endif
  68. }
  69. #define starpu_pthread_spin_lock _starpu_pthread_spin_lock
  70. static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  71. {
  72. #ifdef STARPU_SIMGRID
  73. STARPU_ASSERT(lock->taken);
  74. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  75. STARPU_ASSERT(lock->taken == 1 || lock->taken == 2);
  76. #else
  77. STARPU_ASSERT(lock->taken);
  78. #endif
  79. }
  80. static inline int _starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  81. {
  82. #ifdef STARPU_SIMGRID
  83. if (STARPU_UNLIKELY(lock->taken))
  84. return EBUSY;
  85. lock->taken = 1;
  86. return 0;
  87. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  88. unsigned prev;
  89. prev = STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1);
  90. return (prev == 0)?0:EBUSY;
  91. #else /* !SIMGRID && !LINUX */
  92. uint32_t prev;
  93. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  94. return (prev == 0)?0:EBUSY;
  95. #endif
  96. }
  97. #define starpu_pthread_spin_trylock _starpu_pthread_spin_trylock
  98. #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  99. void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock);
  100. #endif
  101. static inline int _starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  102. {
  103. #ifdef STARPU_SIMGRID
  104. lock->taken = 0;
  105. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  106. STARPU_ASSERT(lock->taken != 0);
  107. STARPU_SYNCHRONIZE();
  108. unsigned next = STARPU_ATOMIC_ADD(&lock->taken, -1);
  109. if (STARPU_LIKELY(next == 0))
  110. /* Nobody to wake, we are done */
  111. return 0;
  112. _starpu_pthread_spin_do_unlock(lock);
  113. #else /* !SIMGRID && !LINUX */
  114. STARPU_RELEASE(&lock->taken);
  115. #endif
  116. return 0;
  117. }
  118. #define starpu_pthread_spin_unlock _starpu_pthread_spin_unlock
  119. #else /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */
  120. static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  121. {
  122. STARPU_ASSERT(pthread_spin_trylock((pthread_spinlock_t *)lock) != 0);
  123. }
  124. #endif /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */
  125. #endif /* __COMMON_THREAD_H__ */