* @brief Initializes a condition variable. * * This function initializes the condition variable pointed to by `cond` with the attributes * specified by `attr`. If `attr` is NULL, the condition variable is initialized with the * default attributes. * * @param cond A pointer to the condition variable to be initialized. * Must point to valid memory. * @param attr A pointer to the
| 97 | * @see pthread_cond_destroy, pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast |
| 98 | */ |
| 99 | int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) |
| 100 | { |
| 101 | rt_err_t result; |
| 102 | char cond_name[RT_NAME_MAX]; |
| 103 | static rt_uint16_t cond_num = 0; |
| 104 | |
| 105 | /* parameter check */ |
| 106 | if (cond == RT_NULL) |
| 107 | return EINVAL; |
| 108 | if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE)) |
| 109 | return EINVAL; |
| 110 | |
| 111 | rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++); |
| 112 | |
| 113 | /* use default value */ |
| 114 | if (attr == RT_NULL) |
| 115 | { |
| 116 | cond->attr = PTHREAD_PROCESS_PRIVATE; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | cond->attr = *attr; |
| 121 | } |
| 122 | |
| 123 | result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO); |
| 124 | if (result != RT_EOK) |
| 125 | { |
| 126 | return EINVAL; |
| 127 | } |
| 128 | |
| 129 | /* detach the object from system object container */ |
| 130 | rt_object_detach(&(cond->sem.parent.parent)); |
| 131 | cond->sem.parent.parent.type = RT_Object_Class_Semaphore; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | RTM_EXPORT(pthread_cond_init); |
| 136 | |
| 137 | /** |