修复服务端并发错误&内核内存分配错误问题

This commit is contained in:
zhangzheng
2024-01-05 23:08:42 +08:00
parent 1e8d407664
commit a2723c39c8
19 changed files with 197 additions and 101 deletions

View File

@@ -36,3 +36,4 @@ int meta_reg_svr_obj(rpc_svr_obj_t *svr_obj, umword_t prot);
int meta_reg_svr_obj_raw(meta_t *meta, rpc_svr_obj_t *svr_obj, umword_t prot);
int rpc_creaite_bind_ipc(obj_handler_t th, void *obj, obj_handler_t *ipc_hd);
void rpc_loop(void);
int rpc_mtd_loop(void);

View File

@@ -34,7 +34,7 @@ int u_intr_bind(int irq_no, u_irq_prio_t prio, int th_prio,
return msg_tag_get_val(tag);
}
ret = u_thread_create(&th_hd, stack, stack_size, msg_buf, thread_func);
ret = u_thread_create(&th_hd, (char *)stack + stack_size, msg_buf, thread_func);
if (ret < 0)
{

View File

@@ -182,10 +182,12 @@ void rpc_loop(void)
thread_ipc_reply(tag, ipc_timeout_create2(0, 0));
}
}
#define RPC_MTD_TH_STACK_SIZE 1024
#define RPC_MTD_TH_STACK_SIZE (1024+256)
typedef struct mtd_params
{
rpc_svr_obj_t *obj;
void *stack;
int is_del;
msg_tag_t in_tag;
obj_handler_t ipc_obj;
obj_handler_t th_obj;
@@ -200,32 +202,39 @@ static void rpc_mtc_thread(void *arg)
ipc_msg_t *msg;
mtd_params_t *params = (mtd_params_t *)arg;
thread_msg_buf_get(-1, (umword_t *)(&msg), NULL);
svr_obj = (rpc_svr_obj_t *)params->obj;
if (svr_obj->dispatch)
{
tag = svr_obj->dispatch(svr_obj, params->in_tag, msg);
}
thread_ipc_send(tag, params->ipc_obj, ipc_timeout_create2(0, 0));
handler_free_umap(params->ipc_obj);
params->ipc_obj = HANDLER_INVALID;
u_thread_del(params->th_obj);
params->is_del = 1;
while (1)
;
{
u_sleep_ms(1000);
}
}
static void check_release_stack_mem(void)
{
mtd_params_t *pos;
pthread_spin_lock(&lock);
slist_foreach_not_next(pos, &th_head, node)
{
mtd_params_t *next = slist_next_entry(pos, &th_head, node);
slist_del(&pos->node);
void *stack = (void *)((char *)pos - (RPC_MTD_TH_STACK_SIZE + MSG_BUG_LEN));
free(stack);
if (pos->is_del == 1)
{
slist_del(&pos->node);
// void *stack = (void *)((char *)pos - (RPC_MTD_TH_STACK_SIZE + MSG_BUG_LEN));
handler_del_umap(pos->ipc_obj);
u_thread_del(pos->th_obj);
free(pos->stack);
}
pos = next;
}
pthread_spin_unlock(&lock);
}
extern void __pthread_new_thread_entry__(void);
int rpc_mtd_loop(void)
@@ -234,7 +243,9 @@ int rpc_mtd_loop(void)
msg_tag_t tag;
umword_t buf;
obj_handler_t ipc_hd;
uint8_t *main_msg_buf;
thread_msg_buf_get(-1, (umword_t *)(&main_msg_buf), NULL);
slist_init(&th_head);
while (1)
{
@@ -243,38 +254,43 @@ int rpc_mtd_loop(void)
ipc_hd = handler_alloc();
if (ipc_hd == HANDLER_INVALID)
{
cons_write_str("mtd alloc is fial.\n");
u_sleep_ms(1000);
// cons_write_str("mtd alloc is fial.\n");
// u_sleep_ms(1000);
continue;
}
tag = factory_create_ipc(FACTORY_PROT, vpage_create_raw3(0, 0, ipc_hd));
if (msg_tag_get_val(tag) < 0)
{
cons_write_str("mtd factory ipc fail.\n");
// cons_write_str("mtd factory ipc fail.\n");
handler_free(ipc_hd);
u_sleep_ms(1000);
// u_sleep_ms(1000);
continue;
}
tag = thread_ipc_wait(ipc_timeout_create2(0, 0), &obj, ipc_hd);
if (msg_tag_get_val(tag) < 0)
{
handler_free_umap(ipc_hd);
continue;
}
again_create:;
obj_handler_t th_obj;
void *stack = malloc(RPC_MTD_TH_STACK_SIZE + MSG_BUG_LEN + sizeof(mtd_params_t));
void *stack;
stack = memalign(sizeof(void *) * 2,
RPC_MTD_TH_STACK_SIZE + MSG_BUG_LEN + sizeof(mtd_params_t));
if (!stack)
{
cons_write_str("mtd no stack mem.\n");
// cons_write_str("mtd no stack mem.\n");
check_release_stack_mem();
u_sleep_ms(1000);
// u_sleep_ms(1000);
goto again_create;
}
int ret_val;
uint8_t *msg_buf = (uint8_t *)stack + RPC_MTD_TH_STACK_SIZE;
umword_t *stack_tmp = (umword_t *)stack;
int ret_val;
umword_t *stack_tmp = (umword_t *)((uint8_t *)stack + RPC_MTD_TH_STACK_SIZE);
mtd_params_t *params = (mtd_params_t *)((char *)stack + RPC_MTD_TH_STACK_SIZE + MSG_BUG_LEN);
// 设置调用参数等
@@ -282,26 +298,35 @@ int rpc_mtd_loop(void)
*(--stack_tmp) = (umword_t)0; // 保留
*(--stack_tmp) = (umword_t)rpc_mtc_thread;
//
params->in_tag = tag;
params->ipc_obj = ipc_hd;
params->obj = (rpc_svr_obj_t *)obj;
params->stack = stack;
params->is_del = 0;
slist_init(&params->node);
pthread_spin_lock(&lock);
slist_add(&th_head, &params->node);
pthread_spin_unlock(&lock);
again_th_create:
ret_val = u_thread_create(&params->th_obj,
stack,
RPC_MTD_TH_STACK_SIZE,
(char *)stack_tmp,
(char *)stack + RPC_MTD_TH_STACK_SIZE,
(void (*)(void))__pthread_new_thread_entry__);
if (ret_val < 0)
{
cons_write_str("mtd no mem.\n");
// cons_write_str("mtd no mem.\n");
check_release_stack_mem();
u_sleep_ms(1000);
// u_sleep_ms(1000);
goto again_th_create;
}
memcpy(msg_buf, main_msg_buf, MSG_BUG_LEN - IPC_USER_SIZE);
ipc_msg_t *msg = (ipc_msg_t *)msg_buf;
msg->user[2] = thread_get_src_pid();
u_thread_run(params->th_obj, 2);
// thread_ipc_reply(tag, ipc_timeout_create2(0, 0));
}

View File

@@ -71,7 +71,7 @@ void sig_init(void)
{
return;
}
u_thread_create(&sig_th, sig_stack, sizeof(sig_stack), sig_msg_buf, sig_func);
u_thread_create(&sig_th, (char *)sig_stack + sizeof(sig_stack), sig_msg_buf, sig_func);
u_thread_run(sig_th, CONFIG_SIG_THREAD_PRIO);
}
#endif

View File

@@ -2,5 +2,5 @@
#include <u_types.h>
void u_thread_del(obj_handler_t th_hd);
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void));
int u_thread_create(obj_handler_t *th_hd, void *stack, void *msg_buf, void (*thread_func)(void));
void u_thread_run(obj_handler_t th_hd, int prio);

View File

@@ -9,9 +9,9 @@
#include <u_thread_util.h>
void u_thread_del(obj_handler_t th_hd)
{
handler_free_umap(th_hd);
handler_del_umap(th_hd);
}
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void))
int u_thread_create(obj_handler_t *th_hd, void *stack, void *msg_buf, void (*thread_func)(void))
{
assert(th_hd);
msg_tag_t tag;
@@ -30,7 +30,7 @@ int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void
return msg_tag_get_prot(tag);
}
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *), RAM_BASE(), 0);
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack, RAM_BASE(), 0);
if (msg_tag_get_prot(tag) < 0)
{
handler_free_umap(th1_hd);

View File

@@ -386,7 +386,7 @@ void rt_timer_init(rt_timer_t timer,
assert(timeout);
if (timer_hd == HANDLER_INVALID)
{
int ret = u_thread_create(&timer_hd, timer_stack, STACK_SIZE, timer_thmsg_buf, timer_func);
int ret = u_thread_create(&timer_hd, (char *)timer_stack + STACK_SIZE, timer_thmsg_buf, timer_func);
if (ret < 0)
{

View File

@@ -15,7 +15,6 @@ int main(int argv, char *args[])
{
obj_handler_t hd;
int ret;
printf("args[0]:%s\n", args[0]);
ret = rpc_meta_init(THREAD_MAIN, &hd);

View File

@@ -48,7 +48,7 @@ void console_init(void)
{
cons_svr_obj_init(&cons_obj);
meta_reg_svr_obj(&cons_obj.svr, CONS_PROT);
u_thread_create(&cons_th, cons_stack, sizeof(cons_stack), NULL, console_read_func);
u_thread_create(&cons_th, (char *)cons_stack + sizeof(cons_stack), NULL, console_read_func);
u_thread_run(cons_th, 3);
printf("cons svr init...\n");
}

View File

@@ -1,15 +1,15 @@
/**
* @file heap_stack.c
* @author ATShining (1358745329@qq.com)
* @brief
* @brief
* @version 0.1
* @date 2023-11-28
*
*
* @copyright Copyright (c) 2023
*
*
*/
#define HEAP_SIZE 1024
#define STACK_SIZE 1024 * 2
#define HEAP_SIZE ((1024 + 256) * 3 + 1024)
#define STACK_SIZE (1024 + 256)
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))

View File

@@ -1,5 +1,5 @@
#一次读取一行,每行代表启动的应用程序,暂时不支持参数
fatfs
# fatfs
cpiofs
sh

View File

@@ -22,15 +22,25 @@
#include "namespace.h"
#include "u_rpc_svr.h"
#include "file_desc.h"
#include <pthread.h>
#include <malloc.h>
#include <fcntl.h>
#include <sys/types.h>
static ns_t ns;
static fs_t ns_fs;
static pthread_spinlock_t lock;
int ns_reg(const char *path, obj_handler_t hd, enum node_type type);
int ns_node_free(ns_node_t *node);
static int find_path(const char *name);
static void ns_lock(void)
{
pthread_spin_lock(&lock);
}
static void ns_unlock(void)
{
pthread_spin_unlock(&lock);
}
/**
* @brief 查找每一个节点,并进行删除
*
@@ -76,7 +86,9 @@ static void _ns_node_del_by_pid(slist_head_t *head, pid_t pid, int to_del)
*/
void ns_node_del_by_pid(pid_t pid, int to_del)
{
ns_lock();
_ns_node_del_by_pid(&ns.root_node.sub_dir, pid, to_del);
ns_unlock();
}
/**
* @brief 初始化一个节点
@@ -557,7 +569,9 @@ int namespace_register(const char *path, obj_handler_t hd, int type)
handler_free_umap(hd);
return -ECANCELED;
}
ns_lock();
int ret = ns_reg(path, hd, type);
ns_unlock();
printf("register svr, name is %s, hd is %d\n", path, hd);
return ret;
}
@@ -580,27 +594,32 @@ int namespace_query(const char *path, obj_handler_t *hd)
*hd = ns_hd;
return 0;
}
ns_lock();
node = node_lookup(&ns.root_node, path, &ret_inx);
if (!node)
{
ns_unlock();
return -EEXIST;
}
if (node && ret_inx == strlen(path))
{
ns_unlock();
return -EEXIST;
}
if (node->type == DIR_NODE)
{
ns_unlock();
return -ENOENT;
}
*hd = node->node_hd;
ns_unlock();
return ret_inx;
}
void namespace_loop(void)
{
rpc_loop();
// rpc_mtd_loop();
}
int fs_svr_read(int fd, void *buf, size_t len)