starpu_spinlock.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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. #ifndef __STARPU_SPINLOCK_H__
  18. #define __STARPU_SPINLOCK_H__
  19. #include <errno.h>
  20. #include <stdint.h>
  21. #include <common/config.h>
  22. #include <starpu.h>
  23. struct _starpu_spinlock
  24. {
  25. #if defined(STARPU_SPINLOCK_CHECK)
  26. starpu_pthread_mutexattr_t errcheck_attr;
  27. starpu_pthread_mutex_t errcheck_lock;
  28. const char *last_taker;
  29. #else
  30. starpu_pthread_spinlock_t lock;
  31. #endif
  32. };
  33. #ifdef STARPU_SPINLOCK_CHECK
  34. #define STARPU_RECORD_LOCK(lock) do { \
  35. (lock)->last_taker = __starpu_func__; \
  36. } while(0)
  37. #else // !STARPU_SPINLOCK_CHECK
  38. #define STARPU_RECORD_LOCK(lock) do {} while(0)
  39. #endif // STARPU_SPINLOCK_CHECK
  40. int _starpu_spin_init(struct _starpu_spinlock *lock);
  41. int _starpu_spin_destroy(struct _starpu_spinlock *lock);
  42. int _starpu_spin_lock(struct _starpu_spinlock *lock);
  43. #define _starpu_spin_lock(lock) ({ \
  44. _STARPU_TRACE_LOCKING_SPINLOCK(); \
  45. _starpu_spin_lock(lock); \
  46. _STARPU_TRACE_SPINLOCK_LOCKED(); \
  47. STARPU_RECORD_LOCK(lock); \
  48. 0; \
  49. })
  50. int _starpu_spin_trylock(struct _starpu_spinlock *lock);
  51. #define _starpu_spin_trylock(lock) ({ \
  52. _STARPU_TRACE_TRYLOCK_SPINLOCK(); \
  53. int err = _starpu_spin_trylock(lock); \
  54. if (!err) { \
  55. STARPU_RECORD_LOCK(lock); \
  56. _STARPU_TRACE_SPINLOCK_LOCKED(); \
  57. } \
  58. err; \
  59. })
  60. int _starpu_spin_checklocked(struct _starpu_spinlock *lock);
  61. int _starpu_spin_unlock(struct _starpu_spinlock *lock);
  62. #define _starpu_spin_unlock(lock) ({ \
  63. _STARPU_TRACE_UNLOCKING_SPINLOCK(); \
  64. _starpu_spin_unlock(lock); \
  65. _STARPU_TRACE_SPINLOCK_UNLOCKED(); \
  66. 0; \
  67. })
  68. #define STARPU_SPIN_MAXTRY 10
  69. #endif // __STARPU_SPINLOCK_H__