Skip to content

Commit c956a64

Browse files
committed
feat: ipc: add nested-free flag support to mutex
This patch introduces a `nested-free` control flag to the mutex, allowing more flexible control over mutex hold limits. The change is necessary to handle scenarios where nested mutex acquisition is restricted, since it will introduce possibility of data corruption in a nested access to the critical area. Changes: - Added `ctrl_flags` field in `rt_mutex` structure to replace the reserved field for holding control flags. - Introduced `RT_MUTEX_CTRL_NESTED_FREE` command in `rt_mutex_control` to enable the new flag. - Refactored mutex initialization by extracting common logic into a new `_mutex_init` function, reducing code duplication. - Updated the `_rt_mutex_take` function to respect the `nested-free` flag, preventing nested acquisition when the flag is set. - Modified `rt_mutex_control` to handle the new control command and return appropriate error codes for invalid commands. Signed-off-by: Shell <[email protected]>
1 parent cc1707e commit c956a64

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

include/rtdef.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ struct rt_mutex
10281028
rt_uint8_t ceiling_priority; /**< the priority ceiling of mutexe */
10291029
rt_uint8_t priority; /**< the maximal priority for pending thread */
10301030
rt_uint8_t hold; /**< numbers of thread hold the mutex */
1031-
rt_uint8_t reserved; /**< reserved field */
1031+
rt_uint8_t ctrl_flags; /**< flags to control mutex behavior */
10321032

10331033
struct rt_thread *owner; /**< current owner of mutex */
10341034
rt_list_t taken_list; /**< the object list taken by thread */

include/rtthread.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,18 @@ rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg);
445445
*/
446446

447447
#ifdef RT_USING_MUTEX
448+
449+
#define RT_MTX_CTRL_FLAG_DEFAULT 0x0 /* create a mutex with default behavior */
450+
#define RT_MTX_CTRL_FLAG_RESERVED 0x01 /* RESERVED for legacy IPC flag */
451+
RT_STATIC_ASSERT(ident_to_ipc_flag, RT_MTX_CTRL_FLAG_RESERVED == RT_IPC_FLAG_PRIO);
452+
#define RT_MTX_CTRL_FLAG_NO_RECUR 0x02 /* create a mutex without recursive taken */
448453
/*
449454
* mutex interface
450455
*/
451-
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag);
456+
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t ctrl_flag);
452457
rt_err_t rt_mutex_detach(rt_mutex_t mutex);
453458
#ifdef RT_USING_HEAP
454-
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag);
459+
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t ctrl_flag);
455460
rt_err_t rt_mutex_delete(rt_mutex_t mutex);
456461
#endif /* RT_USING_HEAP */
457462
void rt_mutex_drop_thread(rt_mutex_t mutex, rt_thread_t thread);

src/ipc.c

+23-33
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,23 @@ static void _mutex_before_delete_detach(rt_mutex_t mutex)
980980
* @{
981981
*/
982982

983+
static void _mutex_init(rt_mutex_t mutex, rt_uint8_t ctrl_flags)
984+
{
985+
/* initialize ipc object */
986+
_ipc_object_init(&(mutex->parent));
987+
988+
mutex->owner = RT_NULL;
989+
mutex->priority = 0xFF;
990+
mutex->hold = 0;
991+
mutex->ceiling_priority = 0xFF;
992+
mutex->ctrl_flags = ctrl_flags;
993+
rt_list_init(&(mutex->taken_list));
994+
995+
/* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
996+
mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
997+
rt_spin_lock_init(&(mutex->spinlock));
998+
}
999+
9831000
/**
9841001
* @brief Initialize a static mutex object.
9851002
*
@@ -1004,29 +1021,15 @@ static void _mutex_before_delete_detach(rt_mutex_t mutex)
10041021
*
10051022
* @warning This function can ONLY be called from threads.
10061023
*/
1007-
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
1024+
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t ctrl_flag)
10081025
{
1009-
/* flag parameter has been obsoleted */
1010-
RT_UNUSED(flag);
1011-
10121026
/* parameter check */
10131027
RT_ASSERT(mutex != RT_NULL);
10141028

10151029
/* initialize object */
10161030
rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
10171031

1018-
/* initialize ipc object */
1019-
_ipc_object_init(&(mutex->parent));
1020-
1021-
mutex->owner = RT_NULL;
1022-
mutex->priority = 0xFF;
1023-
mutex->hold = 0;
1024-
mutex->ceiling_priority = 0xFF;
1025-
rt_list_init(&(mutex->taken_list));
1026-
1027-
/* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
1028-
mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
1029-
rt_spin_lock_init(&(mutex->spinlock));
1032+
_mutex_init(mutex, ctrl_flag);
10301033

10311034
return RT_EOK;
10321035
}
@@ -1230,32 +1233,18 @@ RTM_EXPORT(rt_mutex_getprioceiling);
12301233
*
12311234
* @warning This function can ONLY be called from threads.
12321235
*/
1233-
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
1236+
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t ctrl_flags)
12341237
{
12351238
struct rt_mutex *mutex;
12361239

1237-
/* flag parameter has been obsoleted */
1238-
RT_UNUSED(flag);
1239-
12401240
RT_DEBUG_NOT_IN_INTERRUPT;
12411241

12421242
/* allocate object */
12431243
mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
12441244
if (mutex == RT_NULL)
12451245
return mutex;
12461246

1247-
/* initialize ipc object */
1248-
_ipc_object_init(&(mutex->parent));
1249-
1250-
mutex->owner = RT_NULL;
1251-
mutex->priority = 0xFF;
1252-
mutex->hold = 0;
1253-
mutex->ceiling_priority = 0xFF;
1254-
rt_list_init(&(mutex->taken_list));
1255-
1256-
/* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
1257-
mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
1258-
rt_spin_lock_init(&(mutex->spinlock));
1247+
_mutex_init(mutex, ctrl_flags);
12591248

12601249
return mutex;
12611250
}
@@ -1351,7 +1340,8 @@ static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend
13511340

13521341
if (mutex->owner == thread)
13531342
{
1354-
if (mutex->hold < RT_MUTEX_HOLD_MAX)
1343+
if (!(mutex->ctrl_flags & RT_MTX_CTRL_FLAG_NO_RECUR) &&
1344+
mutex->hold < RT_MUTEX_HOLD_MAX)
13551345
{
13561346
/* it's the same thread */
13571347
mutex->hold ++;

0 commit comments

Comments
 (0)