thread.h 4.3 KB

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