优化发布逻辑,修复by_module链表逻辑错误
This commit is contained in:
@@ -194,7 +194,7 @@ int has_msg_init_module(has_module_ID_e module_id)
|
||||
/* 初始化该模块订阅表 */
|
||||
for (i = 0; i < (sizeof(g_sub_list) / sizeof(g_sub_list[0])); i++)
|
||||
{
|
||||
if (g_sub_list[i].module_id == module_id) {
|
||||
if (g_sub_list[i].module_id == (unsigned char)module_id) {
|
||||
// g_sub_list_index[module_id] = MODULE_MAX;
|
||||
// g_pub_list[module_id].pub_num = 0;
|
||||
/* 已经被初始化了,配置错误 */
|
||||
@@ -283,7 +283,7 @@ static inline void msg_check_and_free_message(has_msg_node_t *node)
|
||||
}
|
||||
msg_mutex_unlock(target_mutex);
|
||||
if (temp) {
|
||||
msg_debug("free node addr:0x%p\n", temp);
|
||||
msg_debug("free node addr:%p\n", temp);
|
||||
msg_free(temp);
|
||||
}
|
||||
}
|
||||
@@ -306,46 +306,48 @@ int has_msg_publish(has_module_ID_e module_id, void *buffer, unsigned int length
|
||||
// return -1;
|
||||
// }
|
||||
has_msg_buffer_t *msg_buffer;
|
||||
unsigned char sub_index;
|
||||
has_static_pub_list_t *pub_list;
|
||||
has_static_sub_list_t *sub_list;
|
||||
|
||||
MSG_CHECK_MODULE_ID(module_id, -1);
|
||||
pub_list = &g_pub_list[module_id];
|
||||
// MSG_CHECK_ID_AND_INDEX TODO:如果使用这个,未初始化的模块也不能发布
|
||||
if (g_pub_list[module_id].pub_num == 0) {
|
||||
if (pub_list->pub_num == 0) {
|
||||
// msg_printf("no one subscribe you:%d\n", module_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 根据订阅表分配内存 */
|
||||
node = (has_msg_node_t *)msg_malloc((sizeof(has_msg_node_t) * g_pub_list[module_id].pub_num)
|
||||
node = (has_msg_node_t *)msg_malloc((sizeof(has_msg_node_t) * pub_list->pub_num)
|
||||
+ sizeof(has_msg_buffer_t) + length);
|
||||
if (node == NULL) {
|
||||
msg_printf("malloc fail,pub id:%d\n", module_id);
|
||||
return -1;
|
||||
}
|
||||
msg_debug("publish: id:%d node addr:0x%p\n", module_id, node);
|
||||
msg_buffer = (has_msg_buffer_t *)(&node[g_pub_list[module_id].pub_num]);
|
||||
msg_buffer->node_cnt = g_pub_list[module_id].pub_num;
|
||||
msg_debug("publish: id:%d node addr:%p\n", module_id, node);
|
||||
msg_buffer = (has_msg_buffer_t *)(&node[pub_list->pub_num]);
|
||||
msg_buffer->node_cnt = pub_list->pub_num;
|
||||
msg_buffer->length = length;
|
||||
msg_buffer->module_id = module_id;
|
||||
memcpy(msg_buffer->buffer, buffer, length);
|
||||
/* 查订阅表添加消息 */
|
||||
for (int i = 0; i < g_pub_list[module_id].pub_num; i++) {
|
||||
sub_index = g_sub_list_index[g_pub_list[module_id].pub_module[i]];
|
||||
for (int i = 0; i < pub_list->pub_num; i++) {
|
||||
sub_list = &g_sub_list[g_sub_list_index[pub_list->pub_module[i]]];
|
||||
node[i].msg_buff = msg_buffer;
|
||||
msg_mutex_lock(&g_sub_list[sub_index].msg_mutex);
|
||||
list_add_tail(&node[i].list, &g_sub_list[sub_index].msg_list); // 加入消息链表
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
list_add_tail(&node[i].list, &sub_list->msg_list); // 加入消息链表
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
/* 通知等待线程唤醒 */
|
||||
if (msg_sem_notify(&g_sub_list[sub_index].sem) == -1) {
|
||||
if (msg_sem_notify(&sub_list->sem) == -1) {
|
||||
list_del(&node[i].list); // 添加失败,删除链表
|
||||
msg_mutex_unlock(&g_sub_list[sub_index].msg_mutex);
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
msg_check_and_free_message(&node[i]);
|
||||
msg_printf("warning:Failed to add msg on module:%d !!!\n"
|
||||
, g_sub_list[sub_index].module_id);
|
||||
, sub_list->module_id);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
msg_mutex_unlock(&g_sub_list[sub_index].msg_mutex);
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -415,7 +417,7 @@ int has_msg_handle_by_module(has_module_ID_e module_id, has_msg_handle_cb cb,
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
if (!list_empty(&sub_list->msg_list)) {
|
||||
list_for_each_entry_safe(node, temp, &sub_list->msg_list, list) {
|
||||
if (node->msg_buff->module_id == pub_module_id) {
|
||||
if (node->msg_buff->module_id == (unsigned char)pub_module_id) {
|
||||
#ifdef MSG_OPT_MUTEX_SEM
|
||||
if (msg_sem_wait(&sub_list->sem, 0) != WAIT_MSG_COME) {
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
@@ -424,23 +426,19 @@ int has_msg_handle_by_module(has_module_ID_e module_id, has_msg_handle_cb cb,
|
||||
}
|
||||
#endif
|
||||
list_del(&node->list);
|
||||
break;
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
if (cb != NULL) {
|
||||
cb(node->msg_buff->module_id, node->msg_buff->buffer, node->msg_buff->length);
|
||||
}
|
||||
msg_debug("by handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
} else { // 没有消息需要处理
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cb != NULL) {
|
||||
cb(node->msg_buff->module_id, node->msg_buff->buffer, node->msg_buff->length);
|
||||
}
|
||||
msg_debug("handle:pub id:%d handle id:%d\n", node->msg_buff->module_id, module_id);
|
||||
msg_check_and_free_message(node);
|
||||
|
||||
|
||||
return 0;
|
||||
// 没有消息需要处理
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -686,7 +684,7 @@ int has_msg_subscribe(has_module_ID_e module_id, has_module_ID_e sub_id)
|
||||
if (sub_list->sub_module_cnt < (MODULE_MAX - 1)) {
|
||||
/* 检查是否已经订阅 */
|
||||
for (j = 0; j < sub_list->sub_module_cnt; j++) {
|
||||
if (sub_id == sub_list->sub_module[j]) {
|
||||
if ((unsigned char)sub_id == sub_list->sub_module[j]) {
|
||||
msg_mutex_unlock(&sub_list->msg_mutex);
|
||||
msg_printf("ID:%d has been sub by module:%d\n", sub_id, module_id);
|
||||
return -1;
|
||||
@@ -716,7 +714,7 @@ int has_msg_unsubscribe(has_module_ID_e module_id, has_module_ID_e sub_id)
|
||||
|
||||
msg_mutex_lock(&sub_list->msg_mutex);
|
||||
for (int j = 0; j < sub_list->sub_module_cnt; j++) {
|
||||
if (sub_id == sub_list->sub_module[j]) {
|
||||
if ((unsigned char)sub_id == sub_list->sub_module[j]) {
|
||||
sub_list->sub_module[j] = INVALID_ID;
|
||||
sub_list->sub_module_cnt --;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user