* @brief Initializes a mutex with optional attributes. * * This function initializes a mutex object pointed to by `mutex`. The mutex * can optionally be initialized with attributes specified by `attr`. If * `attr` is `NULL`, default attributes are used. * * @param[in,out] mutex Pointer to the mutex to be initialized. * @param[in] attr Pointer to the mutex attributes object. Pass `NULL` to u
| 294 | * @see pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock |
| 295 | */ |
| 296 | int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) |
| 297 | { |
| 298 | rt_err_t result; |
| 299 | char name[RT_NAME_MAX]; |
| 300 | static rt_uint16_t pthread_mutex_number = 0; |
| 301 | |
| 302 | if (!mutex) |
| 303 | return EINVAL; |
| 304 | |
| 305 | /* build mutex name */ |
| 306 | rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++); |
| 307 | if (attr == RT_NULL) |
| 308 | mutex->attr = pthread_default_mutexattr; |
| 309 | else |
| 310 | mutex->attr = *attr; |
| 311 | |
| 312 | /* init mutex lock */ |
| 313 | result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_PRIO); |
| 314 | if (result != RT_EOK) |
| 315 | return EINVAL; |
| 316 | |
| 317 | /* detach the object from system object container */ |
| 318 | rt_object_detach(&(mutex->lock.parent.parent)); |
| 319 | mutex->lock.parent.parent.type = RT_Object_Class_Mutex; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | RTM_EXPORT(pthread_mutex_init); |
| 324 | |
| 325 | /** |