2019-09-03 06:56:06 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-04-04 09:15:04 -07:00
|
|
|
#include "util.h"
|
|
|
|
#include "rwsem.h"
|
|
|
|
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
#include "mutex.h"
|
|
|
|
#endif
|
|
|
|
|
2017-04-04 09:15:04 -07:00
|
|
|
int init_rwsem(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_init(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return pthread_rwlock_init(&sem->lock, NULL);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int exit_rwsem(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_destroy(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return pthread_rwlock_destroy(&sem->lock);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int down_read(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_lock(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return perf_singlethreaded ? 0 : pthread_rwlock_rdlock(&sem->lock);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int up_read(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_unlock(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int down_write(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_lock(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return perf_singlethreaded ? 0 : pthread_rwlock_wrlock(&sem->lock);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int up_write(struct rw_semaphore *sem)
|
|
|
|
{
|
2023-10-24 15:23:04 -07:00
|
|
|
#if RWS_ERRORCHECK
|
|
|
|
mutex_unlock(&sem->mtx);
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-04-04 09:15:04 -07:00
|
|
|
return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
|
2023-10-24 15:23:04 -07:00
|
|
|
#endif
|
2017-04-04 09:15:04 -07:00
|
|
|
}
|