starpu-spinlock.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. return pthread_spin_trylock(&lock->lock);
  53. #else
  54. uint32_t prev;
  55. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  56. return (prev == 0)?0:EBUSY;
  57. #endif
  58. }
  59. int _starpu_spin_unlock(starpu_spinlock_t *lock)
  60. {
  61. #ifdef HAVE_PTHREAD_SPIN_LOCK
  62. return pthread_spin_unlock(&lock->lock);
  63. #else
  64. STARPU_RELEASE(&lock->taken);
  65. return 0;
  66. #endif
  67. }