starpu_spinlock.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/starpu_spinlock.h>
  17. #include <common/config.h>
  18. #include <starpu_util.h>
  19. int _starpu_spin_init(starpu_spinlock_t *lock)
  20. {
  21. #ifdef HAVE_PTHREAD_SPIN_LOCK
  22. return pthread_spin_init(&lock->lock, 0);
  23. #else
  24. lock->taken = 0;
  25. return 0;
  26. #endif
  27. }
  28. int _starpu_spin_destroy(starpu_spinlock_t *lock)
  29. {
  30. #ifdef HAVE_PTHREAD_SPIN_LOCK
  31. return pthread_spin_destroy(&lock->lock);
  32. #else
  33. /* we don't do anything */
  34. return 0;
  35. #endif
  36. }
  37. int _starpu_spin_lock(starpu_spinlock_t *lock)
  38. {
  39. #ifdef HAVE_PTHREAD_SPIN_LOCK
  40. return pthread_spin_lock(&lock->lock);
  41. #else
  42. uint32_t prev;
  43. do {
  44. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  45. } while (prev);
  46. return 0;
  47. #endif
  48. }
  49. int _starpu_spin_trylock(starpu_spinlock_t *lock)
  50. {
  51. #ifdef HAVE_PTHREAD_SPIN_LOCK
  52. int ret = pthread_spin_trylock(&lock->lock);
  53. STARPU_ASSERT(!ret || (ret == EBUSY));
  54. return ret;
  55. #else
  56. uint32_t prev;
  57. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  58. return (prev == 0)?0:EBUSY;
  59. #endif
  60. }
  61. int _starpu_spin_unlock(starpu_spinlock_t *lock)
  62. {
  63. #ifdef HAVE_PTHREAD_SPIN_LOCK
  64. return pthread_spin_unlock(&lock->lock);
  65. #else
  66. STARPU_RELEASE(&lock->taken);
  67. return 0;
  68. #endif
  69. }