* @brief Initialize a static messagequeue object. * * @note For the static messagequeue 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_mq_create() function will allocate memory space automatically * and initialize the messa
| 3095 | * @warning This function can ONLY be called from threads. |
| 3096 | */ |
| 3097 | rt_err_t rt_mq_init(rt_mq_t mq, |
| 3098 | const char *name, |
| 3099 | void *msgpool, |
| 3100 | rt_size_t msg_size, |
| 3101 | rt_size_t pool_size, |
| 3102 | rt_uint8_t flag) |
| 3103 | { |
| 3104 | struct rt_mq_message *head; |
| 3105 | rt_base_t temp; |
| 3106 | register rt_size_t msg_align_size; |
| 3107 | |
| 3108 | /* parameter check */ |
| 3109 | RT_ASSERT(mq != RT_NULL); |
| 3110 | RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO)); |
| 3111 | |
| 3112 | /* initialize object */ |
| 3113 | rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name); |
| 3114 | |
| 3115 | /* set parent flag */ |
| 3116 | mq->parent.parent.flag = flag; |
| 3117 | |
| 3118 | /* initialize ipc object */ |
| 3119 | _ipc_object_init(&(mq->parent)); |
| 3120 | |
| 3121 | /* set message pool */ |
| 3122 | mq->msg_pool = msgpool; |
| 3123 | |
| 3124 | /* get correct message size */ |
| 3125 | msg_align_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE); |
| 3126 | mq->msg_size = msg_size; |
| 3127 | mq->max_msgs = pool_size / (msg_align_size + sizeof(struct rt_mq_message)); |
| 3128 | |
| 3129 | if (0 == mq->max_msgs) |
| 3130 | { |
| 3131 | return -RT_EINVAL; |
| 3132 | } |
| 3133 | |
| 3134 | /* initialize message list */ |
| 3135 | mq->msg_queue_head = RT_NULL; |
| 3136 | mq->msg_queue_tail = RT_NULL; |
| 3137 | |
| 3138 | /* initialize message empty list */ |
| 3139 | mq->msg_queue_free = RT_NULL; |
| 3140 | for (temp = 0; temp < mq->max_msgs; temp ++) |
| 3141 | { |
| 3142 | head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool + |
| 3143 | temp * (msg_align_size + sizeof(struct rt_mq_message))); |
| 3144 | head->next = (struct rt_mq_message *)mq->msg_queue_free; |
| 3145 | mq->msg_queue_free = head; |
| 3146 | } |
| 3147 | |
| 3148 | /* the initial entry is zero */ |
| 3149 | mq->entry = 0; |
| 3150 | |
| 3151 | /* initialize an additional list of sender suspend thread */ |
| 3152 | rt_list_init(&(mq->suspend_sender_thread)); |
| 3153 | rt_spin_lock_init(&(mq->spinlock)); |
| 3154 |