Skip to content

Commit dadd169

Browse files
committed
[components/libc/posix]add comments for rwlock APIs.
1 parent f1d6df9 commit dadd169

File tree

1 file changed

+223
-1
lines changed

1 file changed

+223
-1
lines changed

components/libc/posix/pthreads/pthread_rwlock.c

Lines changed: 223 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -50,6 +50,31 @@ int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
5050
}
5151
RTM_EXPORT(pthread_rwlockattr_setpshared);
5252

53+
/**
54+
* @brief Initializes a read-write lock.
55+
*
56+
* This function initializes the read-write lock object pointed to by `rwlock` with the
57+
* attributes specified by `attr`. If `attr` is `NULL`, the default attributes are used.
58+
* A read-write lock allows multiple threads to read or a single thread to write, but not both simultaneously.
59+
*
60+
* @param rwlock A pointer to the read-write lock object to be initialized.
61+
* Must point to valid memory.
62+
* @param attr A pointer to the attributes for the read-write lock.
63+
* If `NULL`, default attributes are applied.
64+
*
65+
* @return
66+
* - `0` on success.
67+
* - A non-zero error code on failure, including:
68+
* - `EINVAL`: Invalid attributes.
69+
*
70+
* @note
71+
* - The read-write lock must be destroyed using `pthread_rwlock_destroy()` when it is no longer needed.
72+
* - 'rw_mutex' is used for protecting rwlock data.
73+
* 'rw_condreaders' is a condition variable for controlling readers.
74+
* 'rw_condwriters' is a condition variable for controlling writers.
75+
*
76+
* @see pthread_rwlock_destroy, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
77+
*/
5378
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
5479
const pthread_rwlockattr_t *attr)
5580
{
@@ -69,6 +94,30 @@ int pthread_rwlock_init(pthread_rwlock_t *rwlock,
6994
}
7095
RTM_EXPORT(pthread_rwlock_init);
7196

97+
/**
98+
* @brief Destroys a read-write lock.
99+
*
100+
* This function destroys the read-write lock object pointed to by `rwlock`. After
101+
* the lock is destroyed, it cannot be used until it is reinitialized with
102+
* `pthread_rwlock_init`. Any threads currently blocked on the lock are affected by the destruction.
103+
*
104+
* @param rwlock A pointer to the read-write lock object to be destroyed.
105+
* Must point to a valid, initialized read-write lock.
106+
*
107+
* @return
108+
* - `0` on success.
109+
* - A non-zero error code on failure, including:
110+
* - `EINVAL`: The `rwlock` is invalid or uninitialized.
111+
* - `EBUSY`: The lock is currently in use by a thread, and cannot be destroyed.
112+
*
113+
* @note
114+
* - The read-write lock must not be in use (i.e., no threads should be blocked on it)
115+
* when `pthread_rwlock_destroy` is called.
116+
* - Calling this function on an uninitialized or destroyed lock will result in undefined behavior.
117+
* - Ensure that all threads have unlocked the lock before attempting to destroy it.
118+
*
119+
* @see pthread_rwlock_init, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
120+
*/
72121
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
73122
{
74123
int result;
@@ -122,6 +171,29 @@ int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
122171
}
123172
RTM_EXPORT(pthread_rwlock_destroy);
124173

174+
/**
175+
* @brief Acquire a read lock on a read-write lock.
176+
*
177+
* This function locks the specified read-write lock for reading. If the lock
178+
* is already held by one or more threads for reading, the calling thread
179+
* can acquire the lock as well (shared access). However, if the lock is
180+
* held by another writer thread, or other writer thread has been waiting
181+
* for the lock, the calling thread will block until the write lock is released.
182+
*
183+
* @param rwlock A pointer to the read-write lock to be locked.
184+
*
185+
* @return - 0 on success.
186+
* - EINVAL if the rwlock is invalid.
187+
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
188+
*
189+
* @note A thread that has acquired a read lock must eventually release it
190+
* using `pthread_rwlock_unlock`. Multiple read locks can be held
191+
* simultaneously, but a write lock excludes all other locks.
192+
*
193+
* @see pthread_rwlock_unlock
194+
* @see pthread_rwlock_wrlock
195+
* @see pthread_rwlock_tryrdlock
196+
*/
125197
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
126198
{
127199
int result;
@@ -156,6 +228,28 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
156228
}
157229
RTM_EXPORT(pthread_rwlock_rdlock);
158230

231+
/**
232+
* @brief Try to acquire a read lock on a read-write lock without blocking.
233+
*
234+
* This function attempts to acquire a read lock on the specified read-write lock.
235+
* If the lock is already held for writing, the function will return immediately
236+
* without blocking the calling thread. If the lock is available for reading,
237+
* it will be acquired successfully.
238+
*
239+
* @param rwlock A pointer to the read-write lock to attempt to lock.
240+
*
241+
* @return - 0 on success, indicating the read lock was acquired.
242+
* - EBUSY if the lock is currently held for writing by another thread.
243+
* - EINVAL if the rwlock is invalid.
244+
*
245+
* @note This function is non-blocking and returns immediately if the lock
246+
* cannot be acquired. After successfully acquiring the read lock,
247+
* the thread must release it using `pthread_rwlock_unlock`.
248+
*
249+
* @see pthread_rwlock_unlock
250+
* @see pthread_rwlock_rdlock
251+
* @see pthread_rwlock_trywrlock
252+
*/
159253
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
160254
{
161255
int result;
@@ -179,6 +273,35 @@ int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
179273
}
180274
RTM_EXPORT(pthread_rwlock_tryrdlock);
181275

276+
/**
277+
* @brief Acquire a read lock on a read-write lock with a timeout.
278+
*
279+
* This function attempts to lock the specified read-write lock for reading,
280+
* blocking until the lock becomes available or the specified timeout expires.
281+
* If the lock is held for writing by another thread, the calling thread will
282+
* block, but only up to the time specified by `abstime`.
283+
*
284+
* @param rwlock A pointer to the read-write lock to be locked.
285+
* @param abstime A pointer to a `timespec` structure specifying the
286+
* absolute timeout (in seconds and nanoseconds since the
287+
* Epoch, 1970-01-01 00:00:00 UTC).
288+
*
289+
* @return - 0 on success, indicating the read lock was acquired.
290+
* - ETIMEDOUT if the timeout expired before the lock could be acquired.
291+
* - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
292+
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
293+
*
294+
* @note The timeout is specified as an absolute time (not relative). After
295+
* acquiring the read lock, the thread must release it using
296+
* `pthread_rwlock_unlock`.
297+
*
298+
* @warning If the system clock is changed (e.g., via manual adjustment or
299+
* NTP synchronization), the timeout behavior may be affected.
300+
*
301+
* @see pthread_rwlock_unlock
302+
* @see pthread_rwlock_rdlock
303+
* @see pthread_rwlock_tryrdlock
304+
*/
182305
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
183306
const struct timespec *abstime)
184307
{
@@ -214,6 +337,35 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
214337
}
215338
RTM_EXPORT(pthread_rwlock_timedrdlock);
216339

340+
/**
341+
* @brief Acquire a write lock on a read-write lock with a timeout.
342+
*
343+
* This function attempts to acquire a write lock on the specified read-write lock,
344+
* blocking until the lock becomes available or the specified timeout expires.
345+
* If the lock is already held for reading or writing by another thread, the
346+
* calling thread will block, but only up to the time specified by `abstime`.
347+
*
348+
* @param rwlock A pointer to the read-write lock to be locked.
349+
* @param abstime A pointer to a `timespec` structure specifying the
350+
* absolute timeout (in seconds and nanoseconds since the
351+
* Epoch, 1970-01-01 00:00:00 UTC).
352+
*
353+
* @return
354+
* - 0 on success, indicating the write lock was acquired.
355+
* - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
356+
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
357+
*
358+
* @note The timeout is specified as an absolute time (not relative). After
359+
* acquiring the write lock, the thread must release it using
360+
* `pthread_rwlock_unlock`.
361+
*
362+
* @warning If the system clock is adjusted (e.g., manually or via NTP synchronization),
363+
* the timeout behavior may be affected.
364+
*
365+
* @see pthread_rwlock_unlock
366+
* @see pthread_rwlock_wrlock
367+
* @see pthread_rwlock_trywrlock
368+
*/
217369
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
218370
const struct timespec *abstime)
219371
{
@@ -248,6 +400,29 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
248400
}
249401
RTM_EXPORT(pthread_rwlock_timedwrlock);
250402

403+
/**
404+
* @brief Try to acquire a write lock on a read-write lock without blocking.
405+
*
406+
* This function attempts to acquire a write lock on the specified read-write lock.
407+
* If the lock is already held for reading or writing by another thread, the function
408+
* will return immediately without blocking the calling thread. If the lock is
409+
* available, it will be acquired successfully.
410+
*
411+
* @param rwlock A pointer to the read-write lock to attempt to lock.
412+
*
413+
* @return
414+
* - 0 on success, indicating the write lock was acquired.
415+
* - EBUSY if the lock is currently held by another thread (read or write lock).
416+
* - EINVAL if the `rwlock` is invalid.
417+
*
418+
* @note This function is non-blocking and returns immediately if the lock cannot
419+
* be acquired. After successfully acquiring the write lock, the thread must
420+
* release it using `pthread_rwlock_unlock`.
421+
*
422+
* @see pthread_rwlock_unlock
423+
* @see pthread_rwlock_wrlock
424+
* @see pthread_rwlock_timedwrlock
425+
*/
251426
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
252427
{
253428
int result;
@@ -271,6 +446,28 @@ int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
271446
}
272447
RTM_EXPORT(pthread_rwlock_trywrlock);
273448

449+
/**
450+
* @brief Release a read or write lock on a read-write lock.
451+
*
452+
* This function unlocks the specified read-write lock, releasing either a read
453+
* lock or a write lock held by the calling thread. If the calling thread does
454+
* not hold the lock, the behavior is undefined.
455+
*
456+
* @param rwlock A pointer to the read-write lock to be unlocked.
457+
*
458+
* @return
459+
* - 0 on success.
460+
* - EINVAL if the `rwlock` is invalid.
461+
*
462+
* @note
463+
* - This function must only be called by the thread that successfully acquired
464+
* the lock.
465+
*
466+
* @see pthread_rwlock_rdlock
467+
* @see pthread_rwlock_wrlock
468+
* @see pthread_rwlock_tryrdlock
469+
* @see pthread_rwlock_trywrlock
470+
*/
274471
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
275472
{
276473
int result;
@@ -305,6 +502,31 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
305502
}
306503
RTM_EXPORT(pthread_rwlock_unlock);
307504

505+
/**
506+
* @brief Acquire a write lock on a read-write lock.
507+
*
508+
* This function locks the specified read-write lock for writing. If the lock
509+
* is already held by another thread for reading or writing, the calling thread
510+
* blocks until the lock becomes available.
511+
*
512+
* @param rwlock A pointer to the read-write lock to be locked.
513+
*
514+
* @return
515+
* - 0 on success, indicating the write lock was acquired.
516+
* - EINVAL if the `rwlock` is invalid.
517+
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
518+
*
519+
* @note
520+
* - A write lock is exclusive, meaning no other thread can acquire a read or
521+
* write lock while a write lock is held.
522+
* - The thread that successfully acquires the write lock must release it using
523+
* `pthread_rwlock_unlock`.
524+
*
525+
* @see pthread_rwlock_unlock
526+
* @see pthread_rwlock_trywrlock
527+
* @see pthread_rwlock_timedwrlock
528+
* @see pthread_rwlock_rdlock
529+
*/
308530
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
309531
{
310532
int result;

0 commit comments

Comments
 (0)