starpu_spinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <starpu_util.h>
  20. int _starpu_spin_init(starpu_spinlock_t *lock)
  21. {
  22. #ifdef STARPU_SPINLOCK_CHECK
  23. // memcpy(&lock->errcheck_lock, PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, sizeof(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP));
  24. int ret;
  25. ret = pthread_mutexattr_init(&lock->errcheck_attr);
  26. STARPU_ASSERT(!ret);
  27. ret = pthread_mutexattr_settype(&lock->errcheck_attr, PTHREAD_MUTEX_ERRORCHECK);
  28. STARPU_ASSERT(!ret);
  29. ret = pthread_mutex_init(&lock->errcheck_lock, &lock->errcheck_attr);
  30. return ret;
  31. #else
  32. #ifdef HAVE_PTHREAD_SPIN_LOCK
  33. int ret = pthread_spin_init(&lock->lock, 0);
  34. STARPU_ASSERT(!ret);
  35. return ret;
  36. #else
  37. lock->taken = 0;
  38. return 0;
  39. #endif
  40. #endif
  41. }
  42. int _starpu_spin_destroy(starpu_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  43. {
  44. #ifdef STARPU_SPINLOCK_CHECK
  45. pthread_mutexattr_destroy(&lock->errcheck_attr);
  46. return pthread_mutex_destroy(&lock->errcheck_lock);
  47. #else
  48. #ifdef HAVE_PTHREAD_SPIN_LOCK
  49. int ret = pthread_spin_destroy(&lock->lock);
  50. STARPU_ASSERT(!ret);
  51. return ret;
  52. #else
  53. /* we don't do anything */
  54. return 0;
  55. #endif
  56. #endif
  57. }
  58. int _starpu_spin_lock(starpu_spinlock_t *lock)
  59. {
  60. #ifdef STARPU_SPINLOCK_CHECK
  61. int ret = pthread_mutex_lock(&lock->errcheck_lock);
  62. STARPU_ASSERT(!ret);
  63. return ret;
  64. #else
  65. #ifdef HAVE_PTHREAD_SPIN_LOCK
  66. int ret = pthread_spin_lock(&lock->lock);
  67. STARPU_ASSERT(!ret);
  68. return ret;
  69. #else
  70. uint32_t prev;
  71. do {
  72. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  73. } while (prev);
  74. return 0;
  75. #endif
  76. #endif
  77. }
  78. int _starpu_spin_trylock(starpu_spinlock_t *lock)
  79. {
  80. #ifdef STARPU_SPINLOCK_CHECK
  81. int ret = pthread_mutex_trylock(&lock->errcheck_lock);
  82. STARPU_ASSERT(!ret || (ret == EBUSY));
  83. return ret;
  84. #else
  85. #ifdef HAVE_PTHREAD_SPIN_LOCK
  86. int ret = pthread_spin_trylock(&lock->lock);
  87. STARPU_ASSERT(!ret || (ret == EBUSY));
  88. return ret;
  89. #else
  90. uint32_t prev;
  91. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  92. return (prev == 0)?0:EBUSY;
  93. #endif
  94. #endif
  95. }
  96. int _starpu_spin_unlock(starpu_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  97. {
  98. #ifdef STARPU_SPINLOCK_CHECK
  99. int ret = pthread_mutex_unlock(&lock->errcheck_lock);
  100. STARPU_ASSERT(!ret);
  101. return ret;
  102. #else
  103. #ifdef HAVE_PTHREAD_SPIN_LOCK
  104. int ret = pthread_spin_unlock(&lock->lock);
  105. STARPU_ASSERT(!ret);
  106. return ret;
  107. #else
  108. STARPU_RELEASE(&lock->taken);
  109. return 0;
  110. #endif
  111. #endif
  112. }