rwsem.h 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _TOOLS__RWSEM_H
  3. #define _TOOLS__RWSEM_H
  4. #include <pthread.h>
  5. struct rw_semaphore {
  6. pthread_rwlock_t lock;
  7. };
  8. static inline int init_rwsem(struct rw_semaphore *sem)
  9. {
  10. return pthread_rwlock_init(&sem->lock, NULL);
  11. }
  12. static inline int exit_rwsem(struct rw_semaphore *sem)
  13. {
  14. return pthread_rwlock_destroy(&sem->lock);
  15. }
  16. static inline int down_read(struct rw_semaphore *sem)
  17. {
  18. return pthread_rwlock_rdlock(&sem->lock);
  19. }
  20. static inline int up_read(struct rw_semaphore *sem)
  21. {
  22. return pthread_rwlock_unlock(&sem->lock);
  23. }
  24. static inline int down_write(struct rw_semaphore *sem)
  25. {
  26. return pthread_rwlock_wrlock(&sem->lock);
  27. }
  28. static inline int up_write(struct rw_semaphore *sem)
  29. {
  30. return pthread_rwlock_unlock(&sem->lock);
  31. }
  32. #define down_read_nested(sem, subclass) down_read(sem)
  33. #define down_write_nested(sem, subclass) down_write(sem)
  34. #endif /* _TOOLS_RWSEM_H */