1099d7439SLiam R. Howlett /* SPDX-License-Identifier: GPL-2.0+ */ 2099d7439SLiam R. Howlett #ifndef _TOOLS__RWSEM_H 3099d7439SLiam R. Howlett #define _TOOLS__RWSEM_H 4099d7439SLiam R. Howlett 5099d7439SLiam R. Howlett #include <pthread.h> 6099d7439SLiam R. Howlett 7099d7439SLiam R. Howlett struct rw_semaphore { 8099d7439SLiam R. Howlett pthread_rwlock_t lock; 9099d7439SLiam R. Howlett }; 10099d7439SLiam R. Howlett init_rwsem(struct rw_semaphore * sem)11099d7439SLiam R. Howlettstatic inline int init_rwsem(struct rw_semaphore *sem) 12099d7439SLiam R. Howlett { 13099d7439SLiam R. Howlett return pthread_rwlock_init(&sem->lock, NULL); 14099d7439SLiam R. Howlett } 15099d7439SLiam R. Howlett exit_rwsem(struct rw_semaphore * sem)16099d7439SLiam R. Howlettstatic inline int exit_rwsem(struct rw_semaphore *sem) 17099d7439SLiam R. Howlett { 18099d7439SLiam R. Howlett return pthread_rwlock_destroy(&sem->lock); 19099d7439SLiam R. Howlett } 20099d7439SLiam R. Howlett down_read(struct rw_semaphore * sem)21099d7439SLiam R. Howlettstatic inline int down_read(struct rw_semaphore *sem) 22099d7439SLiam R. Howlett { 23099d7439SLiam R. Howlett return pthread_rwlock_rdlock(&sem->lock); 24099d7439SLiam R. Howlett } 25099d7439SLiam R. Howlett up_read(struct rw_semaphore * sem)26099d7439SLiam R. Howlettstatic inline int up_read(struct rw_semaphore *sem) 27099d7439SLiam R. Howlett { 28099d7439SLiam R. Howlett return pthread_rwlock_unlock(&sem->lock); 29099d7439SLiam R. Howlett } 30099d7439SLiam R. Howlett down_write(struct rw_semaphore * sem)31099d7439SLiam R. Howlettstatic inline int down_write(struct rw_semaphore *sem) 32099d7439SLiam R. Howlett { 33099d7439SLiam R. Howlett return pthread_rwlock_wrlock(&sem->lock); 34099d7439SLiam R. Howlett } 35099d7439SLiam R. Howlett up_write(struct rw_semaphore * sem)36099d7439SLiam R. Howlettstatic inline int up_write(struct rw_semaphore *sem) 37099d7439SLiam R. Howlett { 38099d7439SLiam R. Howlett return pthread_rwlock_unlock(&sem->lock); 39099d7439SLiam R. Howlett } 40*446e1867SPeng Zhang 41*446e1867SPeng Zhang #define down_read_nested(sem, subclass) down_read(sem) 42*446e1867SPeng Zhang #define down_write_nested(sem, subclass) down_write(sem) 43*446e1867SPeng Zhang 44099d7439SLiam R. Howlett #endif /* _TOOLS_RWSEM_H */ 45