locks.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 netdata_spinlock_init(SPINLOCK *spinlock);
  18. void netdata_spinlock_lock(SPINLOCK *spinlock);
  19. void netdata_spinlock_unlock(SPINLOCK *spinlock);
  20. bool netdata_spinlock_trylock(SPINLOCK *spinlock);
  21. #ifdef NETDATA_TRACE_RWLOCKS
  22. typedef enum {
  23. RWLOCK_REQUEST_READ = (1 << 0),
  24. RWLOCK_REQUEST_WRITE = (1 << 1),
  25. RWLOCK_REQUEST_TRYREAD = (1 << 2),
  26. RWLOCK_REQUEST_TRYWRITE = (1 << 3),
  27. } LOCKER_REQUEST;
  28. typedef struct netdata_rwlock_locker {
  29. LOCKER_REQUEST lock;
  30. bool got_it;
  31. pid_t pid;
  32. size_t refcount;
  33. const char *tag;
  34. const char *file;
  35. const char *function;
  36. unsigned long line;
  37. struct netdata_rwlock_locker *next, *prev;
  38. } netdata_rwlock_locker;
  39. typedef struct netdata_rwlock_t {
  40. pthread_rwlock_t rwlock_t; // the lock
  41. size_t readers; // the number of reader on the lock
  42. size_t writers; // the number of writers on the lock
  43. netdata_mutex_t lockers_mutex; // a mutex to protect the linked list of the lock holding threads
  44. netdata_rwlock_locker *lockers; // the linked list of the lock holding threads
  45. Pvoid_t lockers_pid_JudyL;
  46. } netdata_rwlock_t;
  47. #define NETDATA_RWLOCK_INITIALIZER { \
  48. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER, \
  49. .readers = 0, \
  50. .writers = 0, \
  51. .lockers_mutex = NETDATA_MUTEX_INITIALIZER, \
  52. .lockers = NULL, \
  53. .lockers_pid_JudyL = NULL, \
  54. }
  55. #else // NETDATA_TRACE_RWLOCKS
  56. typedef struct netdata_rwlock_t {
  57. pthread_rwlock_t rwlock_t;
  58. } netdata_rwlock_t;
  59. #define NETDATA_RWLOCK_INITIALIZER { \
  60. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER \
  61. }
  62. #endif // NETDATA_TRACE_RWLOCKS
  63. int __netdata_mutex_init(netdata_mutex_t *mutex);
  64. int __netdata_mutex_destroy(netdata_mutex_t *mutex);
  65. int __netdata_mutex_lock(netdata_mutex_t *mutex);
  66. int __netdata_mutex_trylock(netdata_mutex_t *mutex);
  67. int __netdata_mutex_unlock(netdata_mutex_t *mutex);
  68. int __netdata_rwlock_destroy(netdata_rwlock_t *rwlock);
  69. int __netdata_rwlock_init(netdata_rwlock_t *rwlock);
  70. int __netdata_rwlock_rdlock(netdata_rwlock_t *rwlock);
  71. int __netdata_rwlock_wrlock(netdata_rwlock_t *rwlock);
  72. int __netdata_rwlock_unlock(netdata_rwlock_t *rwlock);
  73. int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock);
  74. int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock);
  75. void netdata_thread_disable_cancelability(void);
  76. void netdata_thread_enable_cancelability(void);
  77. #ifdef NETDATA_TRACE_RWLOCKS
  78. int netdata_mutex_init_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  79. int netdata_mutex_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  80. int netdata_mutex_lock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  81. int netdata_mutex_trylock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  82. int netdata_mutex_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  83. int netdata_rwlock_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  84. int netdata_rwlock_init_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  85. int netdata_rwlock_rdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  86. int netdata_rwlock_wrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  87. int netdata_rwlock_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  88. int netdata_rwlock_tryrdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  89. int netdata_rwlock_trywrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  90. #define netdata_mutex_init(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  91. #define netdata_mutex_destroy(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  92. #define netdata_mutex_lock(mutex) netdata_mutex_lock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  93. #define netdata_mutex_trylock(mutex) netdata_mutex_trylock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  94. #define netdata_mutex_unlock(mutex) netdata_mutex_unlock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  95. #define netdata_rwlock_destroy(rwlock) netdata_rwlock_destroy_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  96. #define netdata_rwlock_init(rwlock) netdata_rwlock_init_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  97. #define netdata_rwlock_rdlock(rwlock) netdata_rwlock_rdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  98. #define netdata_rwlock_wrlock(rwlock) netdata_rwlock_wrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  99. #define netdata_rwlock_unlock(rwlock) netdata_rwlock_unlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  100. #define netdata_rwlock_tryrdlock(rwlock) netdata_rwlock_tryrdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  101. #define netdata_rwlock_trywrlock(rwlock) netdata_rwlock_trywrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  102. #else // !NETDATA_TRACE_RWLOCKS
  103. #define netdata_mutex_init(mutex) __netdata_mutex_init(mutex)
  104. #define netdata_mutex_destroy(mutex) __netdata_mutex_destroy(mutex)
  105. #define netdata_mutex_lock(mutex) __netdata_mutex_lock(mutex)
  106. #define netdata_mutex_trylock(mutex) __netdata_mutex_trylock(mutex)
  107. #define netdata_mutex_unlock(mutex) __netdata_mutex_unlock(mutex)
  108. #define netdata_rwlock_destroy(rwlock) __netdata_rwlock_destroy(rwlock)
  109. #define netdata_rwlock_init(rwlock) __netdata_rwlock_init(rwlock)
  110. #define netdata_rwlock_rdlock(rwlock) __netdata_rwlock_rdlock(rwlock)
  111. #define netdata_rwlock_wrlock(rwlock) __netdata_rwlock_wrlock(rwlock)
  112. #define netdata_rwlock_unlock(rwlock) __netdata_rwlock_unlock(rwlock)
  113. #define netdata_rwlock_tryrdlock(rwlock) __netdata_rwlock_tryrdlock(rwlock)
  114. #define netdata_rwlock_trywrlock(rwlock) __netdata_rwlock_trywrlock(rwlock)
  115. #endif // NETDATA_TRACE_RWLOCKS
  116. #endif //NETDATA_LOCKS_H