rwlock.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LIBLOCKDEP_RWLOCK_H
  3. #define _LIBLOCKDEP_RWLOCK_H
  4. #include <pthread.h>
  5. #include "common.h"
  6. struct liblockdep_pthread_rwlock {
  7. pthread_rwlock_t rwlock;
  8. struct lockdep_map dep_map;
  9. };
  10. typedef struct liblockdep_pthread_rwlock liblockdep_pthread_rwlock_t;
  11. #define LIBLOCKDEP_PTHREAD_RWLOCK_INITIALIZER(rwl) \
  12. (struct liblockdep_pthread_rwlock) { \
  13. .rwlock = PTHREAD_RWLOCK_INITIALIZER, \
  14. .dep_map = STATIC_LOCKDEP_MAP_INIT(#rwl, &((&(rwl))->dep_map)), \
  15. }
  16. static inline int __rwlock_init(liblockdep_pthread_rwlock_t *lock,
  17. const char *name,
  18. struct lock_class_key *key,
  19. const pthread_rwlockattr_t *attr)
  20. {
  21. lockdep_init_map(&lock->dep_map, name, key, 0);
  22. return pthread_rwlock_init(&lock->rwlock, attr);
  23. }
  24. #define liblockdep_pthread_rwlock_init(lock, attr) \
  25. ({ \
  26. static struct lock_class_key __key; \
  27. \
  28. __rwlock_init((lock), #lock, &__key, (attr)); \
  29. })
  30. static inline int liblockdep_pthread_rwlock_rdlock(liblockdep_pthread_rwlock_t *lock)
  31. {
  32. lock_acquire(&lock->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_);
  33. return pthread_rwlock_rdlock(&lock->rwlock);
  34. }
  35. static inline int liblockdep_pthread_rwlock_unlock(liblockdep_pthread_rwlock_t *lock)
  36. {
  37. lock_release(&lock->dep_map, 0, (unsigned long)_RET_IP_);
  38. return pthread_rwlock_unlock(&lock->rwlock);
  39. }
  40. static inline int liblockdep_pthread_rwlock_wrlock(liblockdep_pthread_rwlock_t *lock)
  41. {
  42. lock_acquire(&lock->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
  43. return pthread_rwlock_wrlock(&lock->rwlock);
  44. }
  45. static inline int liblockdep_pthread_rwlock_tryrdlock(liblockdep_pthread_rwlock_t *lock)
  46. {
  47. lock_acquire(&lock->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_);
  48. return pthread_rwlock_tryrdlock(&lock->rwlock) == 0 ? 1 : 0;
  49. }
  50. static inline int liblockdep_pthread_rwlock_trywlock(liblockdep_pthread_rwlock_t *lock)
  51. {
  52. lock_acquire(&lock->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
  53. return pthread_rwlock_trywlock(&lock->rwlock) == 0 ? 1 : 0;
  54. }
  55. static inline int liblockdep_rwlock_destroy(liblockdep_pthread_rwlock_t *lock)
  56. {
  57. return pthread_rwlock_destroy(&lock->rwlock);
  58. }
  59. #ifdef __USE_LIBLOCKDEP
  60. #define pthread_rwlock_t liblockdep_pthread_rwlock_t
  61. #define pthread_rwlock_init liblockdep_pthread_rwlock_init
  62. #define pthread_rwlock_rdlock liblockdep_pthread_rwlock_rdlock
  63. #define pthread_rwlock_unlock liblockdep_pthread_rwlock_unlock
  64. #define pthread_rwlock_wrlock liblockdep_pthread_rwlock_wrlock
  65. #define pthread_rwlock_tryrdlock liblockdep_pthread_rwlock_tryrdlock
  66. #define pthread_rwlock_trywlock liblockdep_pthread_rwlock_trywlock
  67. #define pthread_rwlock_destroy liblockdep_rwlock_destroy
  68. #endif
  69. #endif