locks.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifdef NETDATA_TRACE_RWLOCKS
  9. typedef struct netdata_rwlock_locker {
  10. pid_t pid;
  11. const char *tag;
  12. char lock; // 'R', 'W'
  13. const char *file;
  14. const char *function;
  15. unsigned long line;
  16. size_t callers;
  17. usec_t start_s;
  18. struct netdata_rwlock_t **all_caller_locks;
  19. struct netdata_rwlock_locker *next;
  20. } netdata_rwlock_locker;
  21. typedef struct netdata_rwlock_t {
  22. pthread_rwlock_t rwlock_t; // the lock
  23. size_t readers; // the number of reader on the lock
  24. size_t writers; // the number of writers on the lock
  25. netdata_mutex_t lockers_mutex; // a mutex to protect the linked list of the lock holding threads
  26. netdata_rwlock_locker *lockers; // the linked list of the lock holding threads
  27. } netdata_rwlock_t;
  28. #define NETDATA_RWLOCK_INITIALIZER { \
  29. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER, \
  30. .readers = 0, \
  31. .writers = 0, \
  32. .lockers_mutex = NETDATA_MUTEX_INITIALIZER, \
  33. .lockers = NULL \
  34. }
  35. #else // NETDATA_TRACE_RWLOCKS
  36. typedef struct netdata_rwlock_t {
  37. pthread_rwlock_t rwlock_t;
  38. } netdata_rwlock_t;
  39. #define NETDATA_RWLOCK_INITIALIZER { \
  40. .rwlock_t = PTHREAD_RWLOCK_INITIALIZER \
  41. }
  42. #endif // NETDATA_TRACE_RWLOCKS
  43. extern int __netdata_mutex_init(netdata_mutex_t *mutex);
  44. extern int __netdata_mutex_destroy(netdata_mutex_t *mutex);
  45. extern int __netdata_mutex_lock(netdata_mutex_t *mutex);
  46. extern int __netdata_mutex_trylock(netdata_mutex_t *mutex);
  47. extern int __netdata_mutex_unlock(netdata_mutex_t *mutex);
  48. extern int __netdata_rwlock_destroy(netdata_rwlock_t *rwlock);
  49. extern int __netdata_rwlock_init(netdata_rwlock_t *rwlock);
  50. extern int __netdata_rwlock_rdlock(netdata_rwlock_t *rwlock);
  51. extern int __netdata_rwlock_wrlock(netdata_rwlock_t *rwlock);
  52. extern int __netdata_rwlock_unlock(netdata_rwlock_t *rwlock);
  53. extern int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock);
  54. extern int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock);
  55. extern void netdata_thread_disable_cancelability(void);
  56. extern void netdata_thread_enable_cancelability(void);
  57. #ifdef NETDATA_TRACE_RWLOCKS
  58. extern int netdata_mutex_init_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  59. extern int netdata_mutex_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  60. extern int netdata_mutex_lock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  61. extern int netdata_mutex_trylock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  62. extern int netdata_mutex_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex);
  63. extern int netdata_rwlock_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  64. extern int netdata_rwlock_init_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  65. extern int netdata_rwlock_rdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  66. extern int netdata_rwlock_wrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  67. extern int netdata_rwlock_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  68. extern int netdata_rwlock_tryrdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  69. extern int netdata_rwlock_trywrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock);
  70. #define netdata_mutex_init(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  71. #define netdata_mutex_destroy(mutex) netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  72. #define netdata_mutex_lock(mutex) netdata_mutex_lock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  73. #define netdata_mutex_trylock(mutex) netdata_mutex_trylock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  74. #define netdata_mutex_unlock(mutex) netdata_mutex_unlock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
  75. #define netdata_rwlock_destroy(rwlock) netdata_rwlock_destroy_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  76. #define netdata_rwlock_init(rwlock) netdata_rwlock_init_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  77. #define netdata_rwlock_rdlock(rwlock) netdata_rwlock_rdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  78. #define netdata_rwlock_wrlock(rwlock) netdata_rwlock_wrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  79. #define netdata_rwlock_unlock(rwlock) netdata_rwlock_unlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  80. #define netdata_rwlock_tryrdlock(rwlock) netdata_rwlock_tryrdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  81. #define netdata_rwlock_trywrlock(rwlock) netdata_rwlock_trywrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
  82. #else // !NETDATA_TRACE_RWLOCKS
  83. #define netdata_mutex_init(mutex) __netdata_mutex_init(mutex)
  84. #define netdata_mutex_destroy(mutex) __netdata_mutex_destroy(mutex)
  85. #define netdata_mutex_lock(mutex) __netdata_mutex_lock(mutex)
  86. #define netdata_mutex_trylock(mutex) __netdata_mutex_trylock(mutex)
  87. #define netdata_mutex_unlock(mutex) __netdata_mutex_unlock(mutex)
  88. #define netdata_rwlock_destroy(rwlock) __netdata_rwlock_destroy(rwlock)
  89. #define netdata_rwlock_init(rwlock) __netdata_rwlock_init(rwlock)
  90. #define netdata_rwlock_rdlock(rwlock) __netdata_rwlock_rdlock(rwlock)
  91. #define netdata_rwlock_wrlock(rwlock) __netdata_rwlock_wrlock(rwlock)
  92. #define netdata_rwlock_unlock(rwlock) __netdata_rwlock_unlock(rwlock)
  93. #define netdata_rwlock_tryrdlock(rwlock) __netdata_rwlock_tryrdlock(rwlock)
  94. #define netdata_rwlock_trywrlock(rwlock) __netdata_rwlock_trywrlock(rwlock)
  95. #endif // NETDATA_TRACE_RWLOCKS
  96. #endif //NETDATA_LOCKS_H