locks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. // ----------------------------------------------------------------------------
  4. // automatic thread cancelability management, based on locks
  5. static __thread int netdata_thread_first_cancelability = 0;
  6. static __thread int netdata_thread_lock_cancelability = 0;
  7. inline void netdata_thread_disable_cancelability(void) {
  8. int old;
  9. int ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
  10. if(ret != 0)
  11. error("THREAD_CANCELABILITY: pthread_setcancelstate() on thread %s returned error %d", netdata_thread_tag(), ret);
  12. else {
  13. if(!netdata_thread_lock_cancelability)
  14. netdata_thread_first_cancelability = old;
  15. netdata_thread_lock_cancelability++;
  16. }
  17. }
  18. inline void netdata_thread_enable_cancelability(void) {
  19. if(netdata_thread_lock_cancelability < 1) {
  20. error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): invalid thread cancelability count %d on thread %s - results will be undefined - please report this!", netdata_thread_lock_cancelability, netdata_thread_tag());
  21. }
  22. else if(netdata_thread_lock_cancelability == 1) {
  23. int old = 1;
  24. int ret = pthread_setcancelstate(netdata_thread_first_cancelability, &old);
  25. if(ret != 0)
  26. error("THREAD_CANCELABILITY: pthread_setcancelstate() on thread %s returned error %d", netdata_thread_tag(), ret);
  27. else {
  28. if(old != PTHREAD_CANCEL_DISABLE)
  29. error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): old thread cancelability on thread %s was changed, expected DISABLED (%d), found %s (%d) - please report this!", netdata_thread_tag(), PTHREAD_CANCEL_DISABLE, (old == PTHREAD_CANCEL_ENABLE)?"ENABLED":"UNKNOWN", old);
  30. }
  31. netdata_thread_lock_cancelability = 0;
  32. }
  33. else
  34. netdata_thread_lock_cancelability--;
  35. }
  36. // ----------------------------------------------------------------------------
  37. // mutex
  38. int __netdata_mutex_init(netdata_mutex_t *mutex) {
  39. int ret = pthread_mutex_init(mutex, NULL);
  40. if(unlikely(ret != 0))
  41. error("MUTEX_LOCK: failed to initialize (code %d).", ret);
  42. return ret;
  43. }
  44. int __netdata_mutex_lock(netdata_mutex_t *mutex) {
  45. netdata_thread_disable_cancelability();
  46. int ret = pthread_mutex_lock(mutex);
  47. if(unlikely(ret != 0)) {
  48. netdata_thread_enable_cancelability();
  49. error("MUTEX_LOCK: failed to get lock (code %d)", ret);
  50. }
  51. return ret;
  52. }
  53. int __netdata_mutex_trylock(netdata_mutex_t *mutex) {
  54. netdata_thread_disable_cancelability();
  55. int ret = pthread_mutex_trylock(mutex);
  56. if(ret != 0)
  57. netdata_thread_enable_cancelability();
  58. return ret;
  59. }
  60. int __netdata_mutex_unlock(netdata_mutex_t *mutex) {
  61. int ret = pthread_mutex_unlock(mutex);
  62. if(unlikely(ret != 0))
  63. error("MUTEX_LOCK: failed to unlock (code %d).", ret);
  64. else
  65. netdata_thread_enable_cancelability();
  66. return ret;
  67. }
  68. int netdata_mutex_init_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  69. const unsigned long line __maybe_unused, netdata_mutex_t *mutex) {
  70. usec_t start = 0;
  71. (void)start;
  72. if(unlikely(debug_flags & D_LOCKS)) {
  73. start = now_boottime_usec();
  74. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_init(0x%p) from %lu@%s, %s()", mutex, line, file, function);
  75. }
  76. int ret = __netdata_mutex_init(mutex);
  77. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_init(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
  78. return ret;
  79. }
  80. int netdata_mutex_lock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  81. const unsigned long line __maybe_unused, netdata_mutex_t *mutex) {
  82. usec_t start = 0;
  83. (void)start;
  84. if(unlikely(debug_flags & D_LOCKS)) {
  85. start = now_boottime_usec();
  86. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_lock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
  87. }
  88. int ret = __netdata_mutex_lock(mutex);
  89. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_lock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
  90. return ret;
  91. }
  92. int netdata_mutex_trylock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  93. const unsigned long line __maybe_unused, netdata_mutex_t *mutex) {
  94. usec_t start = 0;
  95. (void)start;
  96. if(unlikely(debug_flags & D_LOCKS)) {
  97. start = now_boottime_usec();
  98. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_trylock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
  99. }
  100. int ret = __netdata_mutex_trylock(mutex);
  101. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_trylock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
  102. return ret;
  103. }
  104. int netdata_mutex_unlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  105. const unsigned long line __maybe_unused, netdata_mutex_t *mutex) {
  106. usec_t start = 0;
  107. (void)start;
  108. if(unlikely(debug_flags & D_LOCKS)) {
  109. start = now_boottime_usec();
  110. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_unlock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
  111. }
  112. int ret = __netdata_mutex_unlock(mutex);
  113. debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_unlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
  114. return ret;
  115. }
  116. // ----------------------------------------------------------------------------
  117. // r/w lock
  118. int __netdata_rwlock_destroy(netdata_rwlock_t *rwlock) {
  119. int ret = pthread_rwlock_destroy(rwlock);
  120. if(unlikely(ret != 0))
  121. error("RW_LOCK: failed to destroy lock (code %d)", ret);
  122. return ret;
  123. }
  124. int __netdata_rwlock_init(netdata_rwlock_t *rwlock) {
  125. int ret = pthread_rwlock_init(rwlock, NULL);
  126. if(unlikely(ret != 0))
  127. error("RW_LOCK: failed to initialize lock (code %d)", ret);
  128. return ret;
  129. }
  130. int __netdata_rwlock_rdlock(netdata_rwlock_t *rwlock) {
  131. netdata_thread_disable_cancelability();
  132. int ret = pthread_rwlock_rdlock(rwlock);
  133. if(unlikely(ret != 0)) {
  134. netdata_thread_enable_cancelability();
  135. error("RW_LOCK: failed to obtain read lock (code %d)", ret);
  136. }
  137. return ret;
  138. }
  139. int __netdata_rwlock_wrlock(netdata_rwlock_t *rwlock) {
  140. netdata_thread_disable_cancelability();
  141. int ret = pthread_rwlock_wrlock(rwlock);
  142. if(unlikely(ret != 0)) {
  143. error("RW_LOCK: failed to obtain write lock (code %d)", ret);
  144. netdata_thread_enable_cancelability();
  145. }
  146. return ret;
  147. }
  148. int __netdata_rwlock_unlock(netdata_rwlock_t *rwlock) {
  149. int ret = pthread_rwlock_unlock(rwlock);
  150. if(unlikely(ret != 0))
  151. error("RW_LOCK: failed to release lock (code %d)", ret);
  152. else
  153. netdata_thread_enable_cancelability();
  154. return ret;
  155. }
  156. int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock) {
  157. netdata_thread_disable_cancelability();
  158. int ret = pthread_rwlock_tryrdlock(rwlock);
  159. if(ret != 0)
  160. netdata_thread_enable_cancelability();
  161. return ret;
  162. }
  163. int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock) {
  164. netdata_thread_disable_cancelability();
  165. int ret = pthread_rwlock_trywrlock(rwlock);
  166. if(ret != 0)
  167. netdata_thread_enable_cancelability();
  168. return ret;
  169. }
  170. int netdata_rwlock_destroy_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  171. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  172. usec_t start = 0;
  173. (void)start;
  174. if(unlikely(debug_flags & D_LOCKS)) {
  175. start = now_boottime_usec();
  176. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_destroy(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  177. }
  178. int ret = __netdata_rwlock_destroy(rwlock);
  179. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_destroy(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  180. return ret;
  181. }
  182. int netdata_rwlock_init_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  183. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  184. usec_t start = 0;
  185. (void)start;
  186. if(unlikely(debug_flags & D_LOCKS)) {
  187. start = now_boottime_usec();
  188. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_init(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  189. }
  190. int ret = __netdata_rwlock_init(rwlock);
  191. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_init(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  192. return ret;
  193. }
  194. int netdata_rwlock_rdlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  195. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  196. usec_t start = 0;
  197. (void)start;
  198. if(unlikely(debug_flags & D_LOCKS)) {
  199. start = now_boottime_usec();
  200. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_rdlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  201. }
  202. int ret = __netdata_rwlock_rdlock(rwlock);
  203. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_rdlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  204. return ret;
  205. }
  206. int netdata_rwlock_wrlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  207. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  208. usec_t start = 0;
  209. (void)start;
  210. if(unlikely(debug_flags & D_LOCKS)) {
  211. start = now_boottime_usec();
  212. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_wrlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  213. }
  214. int ret = __netdata_rwlock_wrlock(rwlock);
  215. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_wrlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  216. return ret;
  217. }
  218. int netdata_rwlock_unlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  219. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  220. usec_t start = 0;
  221. (void)start;
  222. if(unlikely(debug_flags & D_LOCKS)) {
  223. start = now_boottime_usec();
  224. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_unlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  225. }
  226. int ret = __netdata_rwlock_unlock(rwlock);
  227. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_unlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  228. return ret;
  229. }
  230. int netdata_rwlock_tryrdlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  231. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  232. usec_t start = 0;
  233. (void)start;
  234. if(unlikely(debug_flags & D_LOCKS)) {
  235. start = now_boottime_usec();
  236. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_tryrdlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  237. }
  238. int ret = __netdata_rwlock_tryrdlock(rwlock);
  239. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_tryrdlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  240. return ret;
  241. }
  242. int netdata_rwlock_trywrlock_debug(const char *file __maybe_unused, const char *function __maybe_unused,
  243. const unsigned long line __maybe_unused, netdata_rwlock_t *rwlock) {
  244. usec_t start = 0;
  245. (void)start;
  246. if(unlikely(debug_flags & D_LOCKS)) {
  247. start = now_boottime_usec();
  248. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_trywrlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
  249. }
  250. int ret = __netdata_rwlock_trywrlock(rwlock);
  251. debug(D_LOCKS, "RW_LOCK: netdata_rwlock_trywrlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
  252. return ret;
  253. }