starpu_spinlock.c 3.0 KB

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