* @brief This function will create a mutex object. * * @note For the mutex object, its memory space is automatically allocated. * By contrast, the rt_mutex_init() function will initialize a static mutex object. * * @see rt_mutex_init() * * @param name is a pointer to the name that given to the mutex. * * @param flag is the mutex flag, which determines the queui
| 1232 | * @warning This function can ONLY be called from threads. |
| 1233 | */ |
| 1234 | rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag) |
| 1235 | { |
| 1236 | struct rt_mutex *mutex; |
| 1237 | |
| 1238 | /* flag parameter has been obsoleted */ |
| 1239 | RT_UNUSED(flag); |
| 1240 | |
| 1241 | RT_DEBUG_NOT_IN_INTERRUPT; |
| 1242 | |
| 1243 | /* allocate object */ |
| 1244 | mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name); |
| 1245 | if (mutex == RT_NULL) |
| 1246 | return mutex; |
| 1247 | |
| 1248 | /* initialize ipc object */ |
| 1249 | _ipc_object_init(&(mutex->parent)); |
| 1250 | |
| 1251 | mutex->owner = RT_NULL; |
| 1252 | mutex->priority = 0xFF; |
| 1253 | mutex->hold = 0; |
| 1254 | mutex->ceiling_priority = 0xFF; |
| 1255 | rt_list_init(&(mutex->taken_list)); |
| 1256 | |
| 1257 | /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */ |
| 1258 | mutex->parent.parent.flag = RT_IPC_FLAG_PRIO; |
| 1259 | rt_spin_lock_init(&(mutex->spinlock)); |
| 1260 | |
| 1261 | return mutex; |
| 1262 | } |
| 1263 | RTM_EXPORT(rt_mutex_create); |
| 1264 | |
| 1265 |