starpu_spinlock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2013 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 <starpu_thread.h>
  22. #include <common/config.h>
  23. #include <common/thread.h>
  24. struct _starpu_spinlock
  25. {
  26. #ifdef STARPU_SIMGRID
  27. int taken;
  28. #elif defined(STARPU_SPINLOCK_CHECK)
  29. starpu_pthread_mutexattr_t errcheck_attr;
  30. starpu_pthread_mutex_t errcheck_lock;
  31. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  32. _starpu_pthread_spinlock_t lock;
  33. #else
  34. /* we only have a trivial implementation yet ! */
  35. uint32_t taken STARPU_ATTRIBUTE_ALIGNED(16);
  36. #endif
  37. #ifdef STARPU_SPINLOCK_CHECK
  38. const char *last_taker;
  39. #endif
  40. };
  41. #ifdef STARPU_SPINLOCK_CHECK
  42. #define STARPU_RECORD_LOCK(lock) do { \
  43. (lock)->last_taker = __starpu_func__; \
  44. } while(0)
  45. #else // !STARPU_SPINLOCK_CHECK
  46. #define STARPU_RECORD_LOCK(lock) do {} while(0)
  47. #endif // STARPU_SPINLOCK_CHECK
  48. int _starpu_spin_init(struct _starpu_spinlock *lock);
  49. int _starpu_spin_destroy(struct _starpu_spinlock *lock);
  50. int _starpu_spin_lock(struct _starpu_spinlock *lock);
  51. #define _starpu_spin_lock(lock) ({ \
  52. const char *file; \
  53. if (starpu_worker_get_type(starpu_worker_get_id()) == STARPU_CUDA_WORKER) \
  54. { \
  55. file = strrchr(__FILE__,'/'); \
  56. file += sizeof(char);\
  57. _STARPU_TRACE_LOCKING_SPINLOCK(file,__LINE__); \
  58. }\
  59. _starpu_spin_lock(lock); \
  60. if (starpu_worker_get_type(starpu_worker_get_id()) == STARPU_CUDA_WORKER) \
  61. { \
  62. file = strrchr(__FILE__,'/'); \
  63. file += sizeof(char);\
  64. _STARPU_TRACE_SPINLOCK_LOCKED(file,__LINE__); \
  65. }\
  66. STARPU_RECORD_LOCK(lock); \
  67. 0; \
  68. })
  69. int _starpu_spin_trylock(struct _starpu_spinlock *lock);
  70. #define _starpu_spin_trylock(lock) ({ \
  71. const char *file; \
  72. if (starpu_worker_get_type(starpu_worker_get_id()) == STARPU_CUDA_WORKER) \
  73. { \
  74. file = strrchr(__FILE__,'/'); \
  75. file += sizeof(char);\
  76. _STARPU_TRACE_TRYLOCK_SPINLOCK(file,__LINE__); \
  77. }\
  78. int err = _starpu_spin_trylock(lock); \
  79. if (!err) \
  80. STARPU_RECORD_LOCK(lock); \
  81. err; \
  82. })
  83. int _starpu_spin_checklocked(struct _starpu_spinlock *lock);
  84. int _starpu_spin_unlock(struct _starpu_spinlock *lock);
  85. #define _starpu_spin_unlock(lock) ({ \
  86. const char *file; \
  87. if (starpu_worker_get_type(starpu_worker_get_id()) == STARPU_CUDA_WORKER) \
  88. { \
  89. file = strrchr(__FILE__,'/'); \
  90. file += sizeof(char);\
  91. _STARPU_TRACE_UNLOCKING_SPINLOCK(file,__LINE__); \
  92. }\
  93. _starpu_spin_unlock(lock); \
  94. if (starpu_worker_get_type(starpu_worker_get_id()) == STARPU_CUDA_WORKER) \
  95. { \
  96. file = strrchr(__FILE__,'/'); \
  97. file += sizeof(char);\
  98. _STARPU_TRACE_SPINLOCK_UNLOCKED(file,__LINE__); \
  99. }\
  100. 0; \
  101. })
  102. #define STARPU_SPIN_MAXTRY 10
  103. #endif // __STARPU_SPINLOCK_H__