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 *
1616
1717const pthread_mutexattr_t pthread_default_mutexattr = PTHREAD_PROCESS_PRIVATE ;
1818
19+ /**
20+ * @brief Initializes a mutex attributes object.
21+ *
22+ * This function initializes a mutex attributes object pointed to by `attr` with
23+ * default attribute values. Once initialized, the attributes object can be used
24+ * to customize the behavior of mutexes created using it.
25+ *
26+ * @param[out] attr Pointer to the mutex attributes object to be initialized.
27+ *
28+ * @return
29+ * - 0 on success.
30+ * - Non-zero error code on failure.
31+ *
32+ * @note
33+ * After initialization, the mutex attributes object must be destroyed with
34+ * `pthread_mutexattr_destroy()` when it is no longer needed.
35+ *
36+ * @warning
37+ * Using an uninitialized mutex attributes object may result in undefined behavior.
38+ *
39+ * @see pthread_mutexattr_destroy, pthread_mutex_init
40+ */
1941int pthread_mutexattr_init (pthread_mutexattr_t * attr )
2042{
2143 if (attr )
@@ -29,6 +51,29 @@ int pthread_mutexattr_init(pthread_mutexattr_t *attr)
2951}
3052RTM_EXPORT (pthread_mutexattr_init );
3153
54+ /**
55+ * @brief Destroys a mutex attributes object.
56+ *
57+ * This function releases any resources associated with the mutex attributes object
58+ * pointed to by `attr`. After the attributes object is destroyed, it should not
59+ * be used unless it is re-initialized with `pthread_mutexattr_init()`.
60+ *
61+ * @param[in,out] attr Pointer to the mutex attributes object to be destroyed.
62+ *
63+ * @return
64+ * - 0 on success.
65+ * - Non-zero error code on failure, including:
66+ * - `EINVAL`: The attributes object is invalid or uninitialized.
67+ *
68+ * @note
69+ * Destroying an uninitialized or already destroyed attributes object results in undefined behavior.
70+ *
71+ * @warning
72+ * Ensure that no mutexes are being initialized or created using this attributes object
73+ * at the time of its destruction.
74+ *
75+ * @see pthread_mutexattr_init, pthread_mutex_init
76+ */
3277int pthread_mutexattr_destroy (pthread_mutexattr_t * attr )
3378{
3479 if (attr )
@@ -42,6 +87,30 @@ int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
4287}
4388RTM_EXPORT (pthread_mutexattr_destroy );
4489
90+ /**
91+ * @brief Retrieves the type attribute of a mutex attributes object.
92+ *
93+ * This function retrieves the mutex type attribute from the attributes object
94+ * pointed to by `attr` and stores it in the integer pointed to by `type`.
95+ *
96+ * @param[in] attr Pointer to the mutex attributes object.
97+ * @param[out] type Pointer to an integer where the mutex type will be stored.
98+ * Possible values include:
99+ * - `PTHREAD_MUTEX_NORMAL`: Default mutex type.
100+ * - `PTHREAD_MUTEX_ERRORCHECK`: Mutex with error-checking.
101+ * - `PTHREAD_MUTEX_RECURSIVE`: Recursive mutex.
102+ *
103+ * @return
104+ * - 0 on success.
105+ * - Non-zero error code on failure, including:
106+ * - `EINVAL`: The attributes object or the `type` pointer is invalid.
107+ *
108+ * @note
109+ * Use this function to check the type of a mutex attributes object that has
110+ * already been initialized or configured.
111+ *
112+ * @see pthread_mutexattr_settype, pthread_mutexattr_init
113+ */
45114int pthread_mutexattr_gettype (const pthread_mutexattr_t * attr , int * type )
46115{
47116 if (attr && type )
@@ -60,6 +129,41 @@ int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
60129}
61130RTM_EXPORT (pthread_mutexattr_gettype );
62131
132+ /**
133+ * @brief Sets the type attribute of a mutex attributes object.
134+ *
135+ * This function sets the type of the mutex to be initialized using the
136+ * attributes object pointed to by `attr`. The `type` can be one of the
137+ * following values:
138+ * - `PTHREAD_MUTEX_NORMAL`: Default mutex type. The mutex does not allow
139+ * a thread to unlock it if it was not locked by that thread (this results
140+ * in undefined behavior).
141+ * - `PTHREAD_MUTEX_ERRORCHECK`: Error-checking mutex type. A thread trying to
142+ * lock a mutex it already holds will return an error.
143+ * - `PTHREAD_MUTEX_RECURSIVE`: Recursive mutex type. The same thread can lock
144+ * the mutex multiple times without causing a deadlock, but it must unlock it
145+ * the same number of times.
146+ *
147+ * @param[in,out] attr Pointer to the mutex attributes object.
148+ * @param[in] type The type to set for the mutex. One of the following:
149+ * - `PTHREAD_MUTEX_NORMAL`
150+ * - `PTHREAD_MUTEX_ERRORCHECK`
151+ * - `PTHREAD_MUTEX_RECURSIVE`
152+ *
153+ * @return
154+ * - 0 on success.
155+ * - Non-zero error code on failure, including:
156+ * - `EINVAL`: The specified type is invalid.
157+ *
158+ * @note
159+ * The type must be set before the mutex attributes object is used to
160+ * initialize a mutex with `pthread_mutex_init()`.
161+ *
162+ * @warning
163+ * Attempting to set an invalid mutex type will result in an error.
164+ *
165+ * @see pthread_mutexattr_gettype, pthread_mutexattr_init, pthread_mutex_init
166+ */
63167int pthread_mutexattr_settype (pthread_mutexattr_t * attr , int type )
64168{
65169 if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_ERRORCHECK )
@@ -73,6 +177,37 @@ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
73177}
74178RTM_EXPORT (pthread_mutexattr_settype );
75179
180+ /**
181+ * @brief Sets the shared attribute of a mutex attributes object.
182+ *
183+ * This function sets the `pshared` attribute of the mutex attributes object
184+ * pointed to by `attr`. The `pshared` attribute determines whether the mutex
185+ * is shared between threads of the same process or can be shared between
186+ * threads of different processes.
187+ *
188+ * @param[in,out] attr Pointer to the mutex attributes object.
189+ * @param[in] pshared The sharing behavior of the mutex. This can be one of the following:
190+ * - `PTHREAD_PROCESS_PRIVATE`: The mutex is only shared between threads
191+ * of the same process (this is the default behavior).
192+ * - `PTHREAD_PROCESS_SHARED`: The mutex can be shared between threads
193+ * of different processes (requires the mutex to be allocated in
194+ * shared memory).
195+ *
196+ * @return
197+ * - 0 on success.
198+ * - Non-zero error code on failure, including:
199+ * - `EINVAL`: Invalid `pshared` value or invalid attributes object.
200+ *
201+ * @note
202+ * The `pshared` attribute must be set before the mutex attributes object is
203+ * used to initialize a mutex with `pthread_mutex_init()`. For shared mutexes
204+ * (`PTHREAD_PROCESS_SHARED`), the mutex object must be allocated in shared memory.
205+ *
206+ * @warning
207+ * Attempting to set an invalid `pshared` value will result in an error.
208+ *
209+ * @see pthread_mutexattr_getpshared, pthread_mutexattr_init, pthread_mutex_init
210+ */
76211int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr , int pshared )
77212{
78213 if (!attr )
@@ -93,6 +228,35 @@ int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
93228}
94229RTM_EXPORT (pthread_mutexattr_setpshared );
95230
231+ /**
232+ * @brief Retrieves the shared attribute of a mutex attributes object.
233+ *
234+ * This function retrieves the `pshared` attribute from the mutex attributes
235+ * object pointed to by `attr` and stores it in the integer pointed to by `pshared`.
236+ * The `pshared` attribute indicates whether the mutex can be shared between threads
237+ * of different processes or only within the same process.
238+ *
239+ * @param[in] attr Pointer to the mutex attributes object.
240+ * @param[out] pshared Pointer to an integer where the shared attribute will be stored.
241+ * Possible values are:
242+ * - `PTHREAD_PROCESS_PRIVATE`: Mutex is shared only within the same process.
243+ * - `PTHREAD_PROCESS_SHARED`: Mutex can be shared between threads of different processes.
244+ *
245+ * @return
246+ * - 0 on success.
247+ * - Non-zero error code on failure, including:
248+ * - `EINVAL`: Invalid attributes object or the `pshared` pointer is NULL.
249+ *
250+ * @note
251+ * Use this function to check the shared attribute of an already initialized
252+ * mutex attributes object.
253+ *
254+ * @warning
255+ * Attempting to get the `pshared` attribute of an uninitialized or invalid
256+ * attributes object will result in an error.
257+ *
258+ * @see pthread_mutexattr_setpshared, pthread_mutexattr_init, pthread_mutex_init
259+ */
96260int pthread_mutexattr_getpshared (pthread_mutexattr_t * attr , int * pshared )
97261{
98262 if (!attr || !pshared )
@@ -104,6 +268,31 @@ int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
104268}
105269RTM_EXPORT (pthread_mutexattr_getpshared );
106270
271+ /**
272+ * @brief Initializes a mutex with optional attributes.
273+ *
274+ * This function initializes a mutex object pointed to by `mutex`. The mutex
275+ * can optionally be initialized with attributes specified by `attr`. If
276+ * `attr` is `NULL`, default attributes are used.
277+ *
278+ * @param[in,out] mutex Pointer to the mutex to be initialized.
279+ * @param[in] attr Pointer to the mutex attributes object. Pass `NULL` to use
280+ * default attributes.
281+ *
282+ * @return
283+ * - 0 on success.
284+ * - Non-zero error code on failure, including:
285+ * - `EINVAL`: Invalid parameters or result.
286+ *
287+ * @note
288+ * The mutex object must be destroyed using `pthread_mutex_destroy()` after it
289+ * is no longer needed to free associated resources.
290+ *
291+ * @warning
292+ * A mutex should not be re-initialized while it is already in use.
293+ *
294+ * @see pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock
295+ */
107296int pthread_mutex_init (pthread_mutex_t * mutex , const pthread_mutexattr_t * attr )
108297{
109298 rt_err_t result ;
@@ -133,6 +322,31 @@ int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
133322}
134323RTM_EXPORT (pthread_mutex_init );
135324
325+ /**
326+ * @brief Destroys a mutex object.
327+ *
328+ * This function releases any resources associated with the mutex object
329+ * pointed to by `mutex`. After the mutex has been destroyed, it cannot
330+ * be used unless it is re-initialized with `pthread_mutex_init()`.
331+ *
332+ * @param[in,out] mutex Pointer to the mutex to be destroyed.
333+ *
334+ * @return
335+ * - 0 on success.
336+ * - Non-zero error code on failure, including:
337+ * - `EBUSY`: The mutex is currently locked or being used by another thread.
338+ * - `EINVAL`: The mutex is invalid or has not been initialized.
339+ *
340+ * @note
341+ * Before calling this function, ensure that the mutex is not locked or in use
342+ * by any thread. Destroying a locked mutex results in undefined behavior.
343+ *
344+ * @warning
345+ * Attempting to destroy a mutex that is still in use can cause resource leaks
346+ * or undefined behavior.
347+ *
348+ * @see pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock
349+ */
136350int pthread_mutex_destroy (pthread_mutex_t * mutex )
137351{
138352 if (!mutex || mutex -> attr == -1 )
@@ -149,6 +363,34 @@ int pthread_mutex_destroy(pthread_mutex_t *mutex)
149363}
150364RTM_EXPORT (pthread_mutex_destroy );
151365
366+ /**
367+ * @brief Locks a mutex.
368+ *
369+ * This function locks the mutex object pointed to by `mutex`. If the mutex is
370+ * already locked by another thread, the calling thread will block until the
371+ * mutex becomes available.
372+ *
373+ * @param[in,out] mutex Pointer to the mutex to be locked.
374+ *
375+ * @return
376+ * - 0 on success.
377+ * - Non-zero error code on failure, including:
378+ * - `EDEADLK`: A deadlock condition was detected (e.g., the current thread
379+ * already holds the mutex in a recursive locking scenario).
380+ * - `EINVAL`: The mutex is invalid or uninitialized.
381+ *
382+ * @note
383+ * If the mutex is initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute,
384+ * the same thread can lock the mutex multiple times without causing a deadlock.
385+ * However, the mutex must be unlocked an equal number of times before it
386+ * becomes available to other threads.
387+ *
388+ * @warning
389+ * Attempting to lock an uninitialized or already destroyed mutex results in
390+ * undefined behavior.
391+ *
392+ * @see pthread_mutex_unlock, pthread_mutex_trylock, pthread_mutex_init
393+ */
152394int pthread_mutex_lock (pthread_mutex_t * mutex )
153395{
154396 int mtype ;
@@ -182,6 +424,33 @@ int pthread_mutex_lock(pthread_mutex_t *mutex)
182424}
183425RTM_EXPORT (pthread_mutex_lock );
184426
427+ /**
428+ * @brief Unlocks a mutex.
429+ *
430+ * This function unlocks the mutex object pointed to by `mutex`. If other threads
431+ * are blocked waiting for the mutex, one of them will acquire the lock once it is
432+ * released. The calling thread must hold the lock on the mutex before calling
433+ * this function.
434+ *
435+ * @param[in,out] mutex Pointer to the mutex to be unlocked.
436+ *
437+ * @return
438+ * - 0 on success.
439+ * - Non-zero error code on failure, including:
440+ * - `EPERM`: The current thread does not hold the lock on the mutex.
441+ * - `EINVAL`: The mutex is invalid or uninitialized.
442+ *
443+ * @note
444+ * If the mutex was initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute,
445+ * the mutex will only be unlocked after the calling thread unlocks it as many
446+ * times as it was locked.
447+ *
448+ * @warning
449+ * Attempting to unlock an uninitialized, destroyed, or unlocked mutex results
450+ * in undefined behavior.
451+ *
452+ * @see pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_init
453+ */
185454int pthread_mutex_unlock (pthread_mutex_t * mutex )
186455{
187456 rt_err_t result ;
@@ -216,6 +485,31 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
216485}
217486RTM_EXPORT (pthread_mutex_unlock );
218487
488+ /**
489+ * @brief Attempts to lock a mutex without blocking.
490+ *
491+ * This function attempts to lock the mutex object pointed to by `mutex`. If the mutex
492+ * is already locked by another thread, the function returns immediately with an error
493+ * code instead of blocking.
494+ *
495+ * @param[in,out] mutex Pointer to the mutex to be locked.
496+ *
497+ * @return
498+ * - 0 on success (the mutex was successfully locked).
499+ * - Non-zero error code on failure, including:
500+ * - `EBUSY`: The mutex is already locked by another thread.
501+ * - `EINVAL`: The mutex is invalid or uninitialized.
502+ *
503+ * @note
504+ * This function is useful for implementing non-blocking mutex acquisition. If the mutex
505+ * was initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute, the calling thread can
506+ * lock it multiple times, but must unlock it the same number of times.
507+ *
508+ * @warning
509+ * Attempting to trylock an uninitialized or destroyed mutex results in undefined behavior.
510+ *
511+ * @see pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_init
512+ */
219513int pthread_mutex_trylock (pthread_mutex_t * mutex )
220514{
221515 rt_err_t result ;
0 commit comments