From 5de4836e200132e931988187d5dea0ec4617fd36 Mon Sep 17 00:00:00 2001 From: MechSlayer <0jcrespo1996@gmail.com> Date: Fri, 5 Sep 2025 15:43:05 +0200 Subject: [PATCH] feature: add read and write unlock functions for read-write locks --- Source/DrangPlatform/Include/drang/sync.h | 41 +++++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/Source/DrangPlatform/Include/drang/sync.h b/Source/DrangPlatform/Include/drang/sync.h index bc44602..3a7c95d 100644 --- a/Source/DrangPlatform/Include/drang/sync.h +++ b/Source/DrangPlatform/Include/drang/sync.h @@ -1,5 +1,4 @@ #pragma once -#include "error.h" #include "platform.h" #include #include @@ -400,20 +399,40 @@ DRANG_API int drang_rwlock_rdlock(struct drang_rwlock *rwlock); DRANG_API int drang_rwlock_wrlock(struct drang_rwlock *rwlock); /** - * @brief Releases a previously acquired read or write lock. + * @brief Releases a read lock on the read-write lock. * - * Releases either a read lock or write lock that was previously acquired by - * the calling thread. The type of lock (read or write) is tracked internally. + * Releases a read lock that was previously acquired with drang_rwlock_rdlock(). + * The calling thread must currently hold a read lock on the specified read-write lock. + * Other waiting writers may be awakened when the last reader releases their lock. * - * @param[in] rwlock Pointer to the read-write lock to unlock. + * @param[in] rwlock Pointer to the read-write lock to release the read lock from. * @return 0 on success, negative error code on failure. - * @retval 0 Success - lock released. + * @retval 0 Success - read lock released. * @retval DRANG_EINVAL Invalid parameter (rwlock is NULL or not initialized). - * @retval DRANG_EPERM The calling thread does not hold any lock on this rwlock. - * @remarks Only the thread that acquired the lock should release it. - * Works for both read and write locks. - * Unlocking when no lock is held results in undefined behavior. + * @retval DRANG_EPERM The calling thread does not hold a read lock on this lock. + * @remarks Only the thread that acquired the read lock should release it. + * Releasing a read lock that is not held results in undefined behavior. + * When the last reader releases their lock, waiting writers may be awakened. */ -DRANG_API int drang_rwlock_unlock(struct drang_rwlock *rwlock); +DRANG_API int drang_rwlock_rdunlock(struct drang_rwlock *rwlock); + +/** + * @brief Releases a write lock on the read-write lock. + * + * Releases a write lock that was previously acquired with drang_rwlock_wrlock(). + * The calling thread must currently hold the write lock on the specified read-write lock. + * Waiting readers and writers may be awakened when the write lock is released. + * + * @param[in] rwlock Pointer to the read-write lock to release the write lock from. + * @return 0 on success, negative error code on failure. + * @retval 0 Success - write lock released. + * @retval DRANG_EINVAL Invalid parameter (rwlock is NULL or not initialized). + * @retval DRANG_EPERM The calling thread does not hold the write lock on this lock. + * @remarks Only the thread that acquired the write lock should release it. + * Releasing a write lock that is not held results in undefined behavior. + * When the write lock is released, waiting readers and writers may be awakened. + */ +DRANG_API int drang_rwlock_wrunlock(struct drang_rwlock *rwlock); + DRANG_END_DECLS