starpu_spinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2013, 2014 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. * 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 <common/fxt.h>
  21. #include <common/thread.h>
  22. int _starpu_spin_init(struct _starpu_spinlock *lock)
  23. {
  24. #if defined(STARPU_SPINLOCK_CHECK)
  25. starpu_pthread_mutexattr_t errcheck_attr;
  26. // memcpy(&lock->errcheck_lock, PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, sizeof(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP));
  27. int ret;
  28. ret = starpu_pthread_mutexattr_init(&errcheck_attr);
  29. STARPU_CHECK_RETURN_VALUE(ret, "pthread_mutexattr_init");
  30. ret = starpu_pthread_mutexattr_settype(&errcheck_attr, PTHREAD_MUTEX_ERRORCHECK);
  31. STARPU_ASSERT(!ret);
  32. ret = starpu_pthread_mutex_init(&lock->errcheck_lock, &errcheck_attr);
  33. starpu_pthread_mutexattr_destroy(&errcheck_attr);
  34. return ret;
  35. #else
  36. int ret = starpu_pthread_spin_init(&lock->lock, 0);
  37. STARPU_ASSERT(!ret);
  38. return ret;
  39. #endif
  40. }
  41. int _starpu_spin_destroy(struct _starpu_spinlock *lock STARPU_ATTRIBUTE_UNUSED)
  42. {
  43. #if defined(STARPU_SPINLOCK_CHECK)
  44. return starpu_pthread_mutex_destroy(&lock->errcheck_lock);
  45. #else
  46. return starpu_pthread_spin_destroy(&lock->lock);
  47. #endif
  48. }
  49. #undef _starpu_spin_lock
  50. int _starpu_spin_lock(struct _starpu_spinlock *lock)
  51. {
  52. #if defined(STARPU_SPINLOCK_CHECK)
  53. int ret = starpu_pthread_mutex_lock(&lock->errcheck_lock);
  54. STARPU_ASSERT(!ret);
  55. return ret;
  56. #else
  57. int ret = starpu_pthread_spin_lock(&lock->lock);
  58. STARPU_ASSERT(!ret);
  59. return ret;
  60. #endif
  61. }
  62. int _starpu_spin_checklocked(struct _starpu_spinlock *lock)
  63. {
  64. #if defined(STARPU_SPINLOCK_CHECK)
  65. int ret = starpu_pthread_mutex_trylock(&lock->errcheck_lock);
  66. STARPU_ASSERT(ret != 0);
  67. return ret == 0;
  68. #else
  69. return _starpu_pthread_spin_checklocked(&lock->lock);
  70. #endif
  71. }
  72. #undef _starpu_spin_trylock
  73. int _starpu_spin_trylock(struct _starpu_spinlock *lock)
  74. {
  75. #if defined(STARPU_SPINLOCK_CHECK)
  76. int ret = starpu_pthread_mutex_trylock(&lock->errcheck_lock);
  77. STARPU_ASSERT(!ret || (ret == EBUSY));
  78. return ret;
  79. #else
  80. int ret = starpu_pthread_spin_trylock(&lock->lock);
  81. STARPU_ASSERT(!ret || (ret == EBUSY));
  82. return ret;
  83. #endif
  84. }
  85. #undef _starpu_spin_unlock
  86. int _starpu_spin_unlock(struct _starpu_spinlock *lock STARPU_ATTRIBUTE_UNUSED)
  87. {
  88. #if defined(STARPU_SPINLOCK_CHECK)
  89. int ret = starpu_pthread_mutex_unlock(&lock->errcheck_lock);
  90. STARPU_ASSERT(!ret);
  91. return ret;
  92. #else
  93. int ret = starpu_pthread_spin_unlock(&lock->lock);
  94. STARPU_ASSERT(!ret);
  95. return ret;
  96. #endif
  97. }