starpu_spinlock.c 3.1 KB

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