locks.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LOCKS_H
  3. #define NETDATA_LOCKS_H 1
  4. #include "../libnetdata.h"
  5. #include "../clocks/clocks.h"
  6. typedef pthread_mutex_t netdata_mutex_t;
  7. #define NETDATA_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  8. typedef struct netdata_spinlock {
  9. bool locked;
  10. #ifdef NETDATA_INTERNAL_CHECKS
  11. pid_t locker_pid;
  12. size_t spins;
  13. #endif
  14. } SPINLOCK;
  15. #define NETDATA_SPINLOCK_INITIALIZER \
  16. { .locked = false }
  17. void spinlock_init(SPINLOCK *spinlock);
  18. void spinlock_lock(SPINLOCK *spinlock);
  19. void spinlock_unlock(SPINLOCK *spinlock);
  20. bool spinlock_trylock(SPINLOCK *spinlock);
  21. typedef struct netdata_rw_spinlock {
  22. int32_t readers;
  23. SPINLOCK spinlock;
  24. } RW_SPINLOCK;
  25. #define NETDATA_RW_SPINLOCK_INITIALIZER \
  26. { .readers = 0, .spinlock = NETDATA_SPINLOCK_INITIALIZER }
  27. void rw_spinlock_init(RW_SPINLOCK *rw_spinlock);
  28. void rw_spinlock_read_lock(RW_SPINLOCK *rw_spinlock);
  29. void rw_spinlock_read_unlock(RW_SPINLOCK *rw_spinlock);
  30. void rw_spinlock_write_lock(RW_SPINLOCK *rw_spinlock);
  31. void rw_spinlock_write_unlock(RW_SPINLOCK *rw_spinlock);
  32. bool rw_spinlock_tryread_lock(RW_SPINLOCK *rw_spinlock);
  33. bool rw_spinlock_trywrite_lock(RW_SPINLOCK *rw_spinlock);
  34. #ifdef NETDATA_TRACE_RWLOCKS
  35. typedef enum {
  36. RWLOCK_REQUEST_READ = (1 << 0),
  37. RWLOCK_REQUEST_WRITE = (1 << 1),
  38. RWLOCK_REQUEST_TRYREAD = (1 << 2),
  39. RWLOCK_REQUEST_TRYWRITE = (1 << 3),
  40. } LOCKER_REQUEST;
  41. typedef struct netdata_rwlock_locker {
  42. LOCKER_REQUEST lock;
  43. bool got_it;
  44. pid_t pid;
  45. size_t refcount;
  46. const char *tag;
  47. const char *file;
  48. const char *function;
  49. unsigned long line;
  50. struct netdata_rwlock_locker *next, *prev;
  51. } netdata_rwlock_locker;
  52. typedef struct netdata_rwlock_t {
  53. pthread_rwlock_t rwlock_t; // the lock
  54. size_t readers; // the number of reader on the lock
  55. size_t writers; // the number of writers on the lock
  56. netdata_mutex_t lockers_mutex; // a mutex to protect the linked list of the lock holding threads
  57. netdata_rwlock_locker *lockers; // the linked list of the lock holding threads
  58. Pvoid_t lockers_pid_JudyL;
  59. } netdata_rwlock_t;
  60. #define NETDATA_RWLOCK_INITIALIZER { \
  61. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER, \
  62. .readers = 0, \
  63. .writers = 0, \
  64. .lockers_mutex = NETDATA_MUTEX_INITIALIZER, \
  65. .lockers = NULL, \
  66. .lockers_pid_JudyL = NULL, \
  67. }
  68. #else // NETDATA_TRACE_RWLOCKS
  69. typedef struct netdata_rwlock_t {
  70. pthread_rwlock_t rwlock_t;
  71. } netdata_rwlock_t;
  72. #define NETDATA_RWLOCK_INITIALIZER { \
  73. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER \
  74. }
  75. #endif // NETDATA_TRACE_RWLOCKS
  76. int __netdata_mutex_init(netdata_mutex_t *mutex);
  77. int __netdata_mutex_destroy(netdata_mutex_t *mutex);
  78. int __netdata_mutex_lock(netdata_mutex_t *mutex);
  79. int __netdata_mutex_trylock(netdata_mutex_t *mutex);
  80. int __netdata_mutex_unlock(netdata_mutex_t *mutex);
  81. int __netdata_rwlock_destroy(netdata_rwlock_t *rwlock);
  82. int __netdata_rwlock_init(netdata_rwlock_t *rwlock);
  83. int __netdata_rwlock_rdlock(netdata_rwlock_t *rwlock);
  84. int __netdata_rwlock_wrlock(netdata_rwlock_t *rwlock);
  85. int __netdata_rwlock_unlock(netdata_rwlock_t *rwlock);
  86. int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock);
  87. int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock);
  88. void netdata_thread_disable_cancelability(void);
  89. void netdata_thread_enable_cancelability(void);
  90. #ifdef NETDATA_TRACE_RWLOCKS
  91. int netdata_mutex_init_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  92. int netdata_mutex_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  93. int netdata_mutex_lock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  94. int netdata_mutex_trylock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  95. int netdata_mutex_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  96. int netdata_rwlock_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  97. int netdata_rwlock_init_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  98. int netdata_rwlock_rdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  99. int netdata_rwlock_wrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  100. int netdata_rwlock_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  101. int netdata_rwlock_tryrdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  102. int netdata_rwlock_trywrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  103. #define netdata_mutex_init(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  104. #define netdata_mutex_destroy(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  105. #define netdata_mutex_lock(mutex) netdata_mutex_lock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  106. #define netdata_mutex_trylock(mutex) netdata_mutex_trylock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  107. #define netdata_mutex_unlock(mutex) netdata_mutex_unlock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  108. #define netdata_rwlock_destroy(rwlock) netdata_rwlock_destroy_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  109. #define netdata_rwlock_init(rwlock) netdata_rwlock_init_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  110. #define netdata_rwlock_rdlock(rwlock) netdata_rwlock_rdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  111. #define netdata_rwlock_wrlock(rwlock) netdata_rwlock_wrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  112. #define netdata_rwlock_unlock(rwlock) netdata_rwlock_unlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  113. #define netdata_rwlock_tryrdlock(rwlock) netdata_rwlock_tryrdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  114. #define netdata_rwlock_trywrlock(rwlock) netdata_rwlock_trywrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  115. #else // !NETDATA_TRACE_RWLOCKS
  116. #define netdata_mutex_init(mutex) __netdata_mutex_init(mutex)
  117. #define netdata_mutex_destroy(mutex) __netdata_mutex_destroy(mutex)
  118. #define netdata_mutex_lock(mutex) __netdata_mutex_lock(mutex)
  119. #define netdata_mutex_trylock(mutex) __netdata_mutex_trylock(mutex)
  120. #define netdata_mutex_unlock(mutex) __netdata_mutex_unlock(mutex)
  121. #define netdata_rwlock_destroy(rwlock) __netdata_rwlock_destroy(rwlock)
  122. #define netdata_rwlock_init(rwlock) __netdata_rwlock_init(rwlock)
  123. #define netdata_rwlock_rdlock(rwlock) __netdata_rwlock_rdlock(rwlock)
  124. #define netdata_rwlock_wrlock(rwlock) __netdata_rwlock_wrlock(rwlock)
  125. #define netdata_rwlock_unlock(rwlock) __netdata_rwlock_unlock(rwlock)
  126. #define netdata_rwlock_tryrdlock(rwlock) __netdata_rwlock_tryrdlock(rwlock)
  127. #define netdata_rwlock_trywrlock(rwlock) __netdata_rwlock_trywrlock(rwlock)
  128. #endif // NETDATA_TRACE_RWLOCKS
  129. #endif //NETDATA_LOCKS_H