* @brief Initialize a static mutex object. * * @note For the static mutex object, its memory space is allocated by the compiler during compiling, * and shall placed on the read-write data segment or on the uninitialized data segment. * By contrast, the rt_mutex_create() function will automatically allocate memory space * and initialize the mutex. * * @se
| 1006 | * @warning This function can ONLY be called from threads. |
| 1007 | */ |
| 1008 | rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag) |
| 1009 | { |
| 1010 | /* flag parameter has been obsoleted */ |
| 1011 | RT_UNUSED(flag); |
| 1012 | |
| 1013 | /* parameter check */ |
| 1014 | RT_ASSERT(mutex != RT_NULL); |
| 1015 | |
| 1016 | /* initialize object */ |
| 1017 | rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name); |
| 1018 | |
| 1019 | /* initialize ipc object */ |
| 1020 | _ipc_object_init(&(mutex->parent)); |
| 1021 | |
| 1022 | mutex->owner = RT_NULL; |
| 1023 | mutex->priority = 0xFF; |
| 1024 | mutex->hold = 0; |
| 1025 | mutex->ceiling_priority = 0xFF; |
| 1026 | rt_list_init(&(mutex->taken_list)); |
| 1027 | |
| 1028 | /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */ |
| 1029 | mutex->parent.parent.flag = RT_IPC_FLAG_PRIO; |
| 1030 | rt_spin_lock_init(&(mutex->spinlock)); |
| 1031 | |
| 1032 | return RT_EOK; |
| 1033 | } |
| 1034 | RTM_EXPORT(rt_mutex_init); |
| 1035 | |
| 1036 |