ipc support call.

This commit is contained in:
zhangzheng
2023-09-01 22:01:13 +08:00
parent 5ba4cc7fb2
commit a513010c51
9 changed files with 148 additions and 155 deletions

View File

@@ -117,7 +117,8 @@
"u_thread.h": "c",
"u_app.h": "c",
"u_util.h": "c",
"u_mm.h": "c"
"u_mm.h": "c",
"u_ipc.h": "c"
},
"cortex-debug.showRTOS": false,
}

View File

@@ -46,13 +46,15 @@ typedef struct sp_info
#define THREAD_MSG_BUG_LEN 128 //!< 默认的消息寄存器大小
#define MSG_BUF_HAS_DATA_FLAGS 0x01U //!< 已经有数据了
#define MSG_BUF_CALL_FLAGS 0x02U //!< 是CALL
#define MSG_BUF_RECV_R_FLAGS 0x02U //!< 接收上次发送数据的接收者
#define MSG_BUF_REPLY_FLAGS 0x04U //!<
typedef struct msg_buf
{
void *msg; //!< buf长度 @see THREAD_MSG_BUG_LEN
uhmword_t len; //!< 这里不是buf的大小而是存储接收或者发送的长度
uhmword_t flags; //!< 传输标志
thread_t *call_send; //!< 当是call操作时存放call的发起方
void *msg; //!< buf长度 @see THREAD_MSG_BUG_LEN
uhmword_t len; //!< 这里不是buf的大小而是存储接收或者发送的长度
uhmword_t flags; //!< 传输标志
thread_t *send_th; //!< 当是call操作时存放call的发起方
thread_t *recv_th; //!< 当是call操作时存放call的发起方
} msg_buf_t;
#define THREAD_MAIGC 0xdeadead

View File

@@ -20,10 +20,11 @@ typedef struct ipc
kobject_t kobj; //!< 内核对象
spinlock_t lock; //!< 操作的锁
slist_head_t wait_send; //!< 发送等待队列
slist_head_t wait_recv; //!< 接收等待队列
//!< 等待回复的链表
//!< 发送消息后立刻进入挂起状态并标记flags在等待中这时rcv变量将不能改变直到接受者reply后清空。
thread_t *rcv; //!< 只有一个接受者
thread_t *call_send;
// thread_t *call_send;
} ipc_t;
enum ipc_op
@@ -32,14 +33,6 @@ enum ipc_op
IPC_REVC, //!< 接受IPC消息
};
static bool_t is_call_send(ipc_t *ipc)
{
if (ipc->rcv->msg.call_send)
{
return TRUE;
}
return FALSE;
}
static void add_wait(ipc_t *ipc, thread_t *th)
{
umword_t status;
@@ -48,16 +41,32 @@ static void add_wait(ipc_t *ipc, thread_t *th)
thread_suspend(th);
spinlock_set(&ipc->lock, status);
}
static void add_wait_recv(ipc_t *ipc, thread_t *th)
{
umword_t status;
status = spinlock_lock(&ipc->lock);
slist_add_append(&ipc->wait_recv, &th->wait);
thread_suspend(th);
spinlock_set(&ipc->lock, status);
}
static void add_wait_unlock(ipc_t *ipc, thread_t *th)
{
slist_add_append(&ipc->wait_send, &th->wait);
thread_suspend(th);
}
// if (ipc->rcv->msg.call_send == th)
// static void add_wait_unlock_recv(ipc_t *ipc, thread_t *th)
// {
// slist_add_append(&ipc->wait_recv, &th->wait);
// thread_suspend(th);
// }
// static void wake_up_recv(ipc_t *ipc)
// {
// thread_t *send_th;
// slist_foreach(send_th, &ipc->wait_recv, wait)
// {
// thread_ready(send_th, TRUE);
// }
// }
static int ipc_recv(ipc_t *ipc, entry_frame_t *f);
static int ipc_send(ipc_t *ipc, entry_frame_t *f)
{
umword_t status;
@@ -67,11 +76,7 @@ static int ipc_send(ipc_t *ipc, entry_frame_t *f)
void *send_addr = th->msg.msg;
th->msg.len = f->r[1];
if (send_flag & MSG_BUF_CALL_FLAGS)
{
th->msg.flags |= MSG_BUF_CALL_FLAGS;
th->msg.call_send = NULL;
}
again_send:
if (!ipc->rcv)
{
status = spinlock_lock(&ipc->lock);
@@ -83,18 +88,9 @@ static int ipc_send(ipc_t *ipc, entry_frame_t *f)
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
/*如果是call则需要吧发送者转变为接收者*/
/*TODO:call等待接收者回复消息*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
return ret;
}
return th->msg.len;
}
}
again_send:
assert(ipc->rcv);
if (ipc->rcv->status != THREAD_SUSPEND)
{
@@ -102,18 +98,12 @@ again_send:
{
return -EACCES;
}
add_wait(ipc, th);
status = spinlock_lock(&ipc->lock);
add_wait_unlock(ipc, th);
spinlock_set(&ipc->lock, status);
if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS)
{
th->msg.flags &= ~MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
/*如果是call则需要吧发送者转变为接收者*/
/*TODO:call等待接收者回复消息*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
return ret;
}
return th->msg.len;
}
goto again_send;
@@ -121,7 +111,8 @@ again_send:
else
{
status = spinlock_lock(&ipc->lock);
if (ipc->rcv->msg.call_send == th || ipc->rcv->msg.call_send == NULL)
if (
((send_flag & MSG_BUF_REPLY_FLAGS) == 0 || ((ipc->rcv->msg.flags & MSG_BUF_RECV_R_FLAGS) && ipc->rcv == th->msg.recv_th)))
{
if (ipc->rcv->status == THREAD_SUSPEND)
{
@@ -130,51 +121,34 @@ again_send:
// 接收线程正在等待中,直接复制数据到目标缓存中,然后唤醒目标线程
memcpy(ipc->rcv->msg.msg, th->msg.msg, send_len_c); //!< 拷贝数据
ipc->rcv->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
// ipc->rcv->msg.flags |= MSG_BUF_CALL_FLAGS;
ipc->rcv->msg.call_send = th; //! 当前线程是call发起方
}
else
{
ipc->rcv->msg.call_send = NULL;
}
ipc->rcv->msg.len = send_len_c;
thread_ready(ipc->rcv, TRUE); //!< 直接唤醒接受者
spinlock_set(&ipc->lock, status);
if (th->msg.flags & MSG_BUF_CALL_FLAGS)
{
ipc->rcv = th;
/*如果是call则需要吧发送者转变为接收者*/
int ret = ipc_recv(ipc, f);
th->msg.flags &= ~MSG_BUF_CALL_FLAGS;
send_len_c = ret;
}
else
{
ipc->rcv->msg.call_send = NULL;
ipc->rcv = NULL; //! 如果不是call则清空接收者
}
ipc->rcv->msg.send_th = th; //!< 设置接收者的消息来源
ipc->rcv = NULL; //! 如果不是call则清空接收者
th->msg.recv_th = NULL;
}
else
{
assert(0);
}
}
else if (ipc->rcv->msg.call_send != th && ipc->rcv->msg.call_send != NULL)
{
spinlock_set(&ipc->lock, status);
add_wait(ipc, th);
goto again_send;
}
else
{
thread_sched();
spinlock_set(&ipc->lock, status);
goto again_send;
}
return send_len_c;
}
return send_len_c;
}
static int ipc_recv(ipc_t *ipc, entry_frame_t *f)
{
umword_t recv_flags = f->r[1];
thread_t *th = thread_get_current();
if (!ipc->rcv)
again_recv:
if (ipc->rcv != th)
{
umword_t status = spinlock_lock(&ipc->lock);
if (!ipc->rcv)
@@ -183,13 +157,14 @@ static int ipc_recv(ipc_t *ipc, entry_frame_t *f)
}
else
{
thread_sched();
spinlock_set(&ipc->lock, status);
return -EAGAIN;
goto again_recv;
}
spinlock_set(&ipc->lock, status);
}
th->msg.flags |= (recv_flags & MSG_BUF_RECV_R_FLAGS);
void *recv_addr = th->msg.msg;
again_recv:
// 没有数据则睡眠
while (slist_is_empty(&ipc->wait_send))
{
@@ -203,47 +178,47 @@ again_recv:
}
size_t recv_len_c = 0;
umword_t status = spinlock_lock(&ipc->lock);
if (slist_is_empty(&ipc->wait_send))
{
spinlock_set(&ipc->lock, status);
goto again_recv;
}
/*************************/
umword_t status = spinlock_lock(&ipc->lock);
/*************************/
thread_t *send_th = NULL;
slist_foreach(send_th, &ipc->wait_send, wait)
int find = 0;
if (th->msg.flags & MSG_BUF_RECV_R_FLAGS)
{
if (send_th->msg.call_send == th)
slist_foreach(send_th, &ipc->wait_send, wait)
{
break;
if (th == send_th->msg.send_th || th->msg.recv_th == send_th)
{
find = 1;
send_th->msg.send_th = NULL;
break;
}
}
if (find == 0)
{
thread_sched();
spinlock_set(&ipc->lock, status);
goto again_recv;
}
slist_del(&send_th->wait);
th->msg.flags &= ~MSG_BUF_RECV_R_FLAGS;
}
if (send_th == NULL)
else
{
slist_head_t *mslist = slist_first(&ipc->wait_send); // TODO从链表中找到与th相同的
slist_head_t *mslist = slist_first(&ipc->wait_send);
slist_del(mslist);
send_th = container_of(mslist, thread_t, wait);
} else {
slist_del(&send_th->wait);
}
/*************************/
// slist_head_t *mslist = slist_first(&ipc->wait_send); //TODO从链表中找到与th相同的
// slist_del(mslist);
// thread_t *send_th = container_of(mslist, thread_t, wait);
recv_len_c = MIN(THREAD_MSG_BUG_LEN, send_th->msg.len);
memcpy(recv_addr, send_th->msg.msg, recv_len_c); //!< 拷贝数据
send_th->msg.flags |= MSG_BUF_HAS_DATA_FLAGS;
send_th->msg.len = recv_len_c;
if ((send_th->msg.flags & MSG_BUF_CALL_FLAGS))
{
th->msg.call_send = send_th; // 当前线程的callsend设置为发送方。
ipc->rcv = send_th; // 直接切换接收者,上下文切换之后发送者就变为接收者了
}
else
{
send_th->msg.call_send = NULL;
ipc->rcv = NULL; //! 如果不是call则清空接收者
}
send_th->msg.recv_th = th; //!< 设置消息发送给了谁
ipc->rcv = NULL; //!< 如果不是call则清空接收者
thread_ready(send_th, TRUE); //!< 唤醒发送线程
spinlock_set(&ipc->lock, status);
return recv_len_c;

View File

@@ -261,6 +261,7 @@ static kobject_t *thread_create_func(ram_limit_t *lim, umword_t arg0, umword_t a
{
return NULL;
}
printk("thread 0x%x\n", kobj);
return kobj;
}

View File

@@ -3,7 +3,8 @@
#include "u_prot.h"
#define MSG_BUG_LEN 128
#define MSG_BUF_CALL_FLAGS 0x02U //!< 是CALL
#define MSG_BUF_RECV_R_FLAGS 0x02U //!< 接收上次发送数据的接收者
#define MSG_BUF_REPLY_FLAGS 0x04U //!<
msg_tag_t ipc_recv(obj_handler_t obj);
msg_tag_t ipc_recv(obj_handler_t obj, umword_t flags);
msg_tag_t ipc_send(obj_handler_t obj, umword_t len, umword_t flags);

View File

@@ -1,16 +1,17 @@
#include "u_types.h"
#include "u_prot.h"
#include "u_ipc.h"
enum ipc_op
{
IPC_SEND, //!< 发送IPC消息
IPC_REVC, //!< 接受IPC消息
};
msg_tag_t ipc_recv(obj_handler_t obj)
msg_tag_t ipc_recv(obj_handler_t obj, umword_t flags)
{
register volatile umword_t r0 asm("r0");
syscall(obj, msg_tag_init3(IPC_REVC, 0, IPC_PROT).raw,
0,
syscall(obj, msg_tag_init3(IPC_REVC, 1, IPC_PROT).raw,
flags,
0,
0,
0,

View File

@@ -16,10 +16,10 @@ int main(int argc, char *args[])
mm_test();
ulog_test();
factory_test();
thread_test();
app_test();
mpu_test();
printf_test();
thread_test();
#endif
ipc_test();
printf("exit init.\n");

View File

@@ -66,6 +66,12 @@ static void thread_test_func2(void)
printf("Error\n");
}
#else
static void hard_sleep(void)
{
for (volatile int i; i < 10000000; i++)
;
}
static void thread_test_func(void)
{
char *buf;
@@ -73,10 +79,10 @@ static void thread_test_func(void)
thread_msg_buf_get(th1_hd, (umword_t *)(&buf), NULL);
while (1)
{
ipc_recv(ipc_hd);
printf(buf);
strcpy(buf, "thread_test_func.\n");
ipc_send(ipc_hd, strlen(buf), 0);
ipc_recv(ipc_hd, 0);
printf("srv recv:%s", buf);
hard_sleep();
ipc_send(ipc_hd, strlen(buf), MSG_BUF_REPLY_FLAGS);
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, th1_hd);
@@ -89,14 +95,16 @@ static void thread_test_func2(void)
thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "thread_test_func2.\n");
ipc_send(ipc_hd, strlen(buf), MSG_BUF_CALL_FLAGS);
printf(buf);
strcpy(buf, "I am th2.");
ipc_send(ipc_hd, strlen(buf), 0);
ipc_recv(ipc_hd, MSG_BUF_RECV_R_FLAGS);
printf("th2:%s", buf);
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, th2_hd);
printf("Error\n");
}
static void thread_test_func3(void)
{
char *buf;
@@ -104,10 +112,10 @@ static void thread_test_func3(void)
thread_msg_buf_get(th3_hd, (umword_t *)(&buf), NULL);
while (1)
{
ipc_recv(ipc_hd);
printf(buf);
strcpy(buf, "thread_test_func3.\n");
strcpy(buf, "I am th3.\n");
ipc_send(ipc_hd, strlen(buf), 0);
ipc_recv(ipc_hd, MSG_BUF_RECV_R_FLAGS);
printf("th3:%s", buf);
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, th3_hd);
@@ -153,15 +161,15 @@ void ipc_test(void)
tag = thread_run(th2_hd);
assert(msg_tag_get_prot(tag) >= 0);
// assert(msg_tag_get_prot(tag) >= 0);
// tag = factory_create_thread(FACTORY_PROT, th3_hd);
// assert(msg_tag_get_prot(tag) >= 0);
// tag = thread_msg_buf_set(th3_hd, msg_buf2);
// assert(msg_tag_get_prot(tag) >= 0);
// tag = thread_exec_regs(th3_hd, (umword_t)thread_test_func3, (umword_t)stack2 + STACK_SIZE, RAM_BASE());
// assert(msg_tag_get_prot(tag) >= 0);
// tag = thread_bind_task(th3_hd, TASK_THIS);
// assert(msg_tag_get_prot(tag) >= 0);
// tag = thread_run(th3_hd);
// assert(msg_tag_get_prot(tag) >= 0);
assert(msg_tag_get_prot(tag) >= 0);
tag = factory_create_thread(FACTORY_PROT, th3_hd);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_msg_buf_set(th3_hd, msg_buf2);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_exec_regs(th3_hd, (umword_t)thread_test_func3, (umword_t)stack2 + STACK_SIZE, RAM_BASE());
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_bind_task(th3_hd, TASK_THIS);
assert(msg_tag_get_prot(tag) >= 0);
tag = thread_run(th3_hd);
assert(msg_tag_get_prot(tag) >= 0);
}

View File

@@ -5,9 +5,12 @@
#include "u_thread.h"
#include "u_task.h"
#include "u_ipc.h"
#include "u_hd_man.h"
#include <assert.h>
#include <stdio.h>
static umword_t th1_hd = 0;
static umword_t th2_hd = 0;
static umword_t ipc_hd = 0;
static char msg_buf0[MSG_BUG_LEN];
static char msg_buf1[MSG_BUG_LEN];
#define STACK_SIZE 1024
@@ -17,32 +20,32 @@ static void thread_test_func(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(11, (umword_t *)(&buf), NULL);
thread_msg_buf_get(th1_hd, (umword_t *)(&buf), NULL);
while (1)
{
msg_tag_t tag = ipc_recv(12);
msg_tag_t tag = ipc_recv(ipc_hd, 0);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
printf("recv data is %s\n", buf);
}
strcpy(buf, "reply");
ipc_send(12, strlen("reply"), 0);
ipc_send(ipc_hd, strlen("reply"), 0);
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, 11);
task_unmap(TASK_PROT, th1_hd);
printf("Error\n");
}
static void thread_test_func2(void)
{
char *buf;
umword_t len;
thread_msg_buf_get(10, (umword_t *)(&buf), NULL);
thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL);
while (1)
{
strcpy(buf, "1234");
ipc_send(12, strlen(buf), 0);
msg_tag_t tag = ipc_recv(12);
ipc_send(ipc_hd, strlen(buf), 0);
msg_tag_t tag = ipc_recv(ipc_hd, 0);
if (msg_tag_get_prot(tag) > 0)
{
buf[msg_tag_get_prot(tag)] = 0;
@@ -50,7 +53,7 @@ static void thread_test_func2(void)
}
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, 10);
task_unmap(TASK_PROT, th2_hd);
printf("Error\n");
}
/**
@@ -59,27 +62,28 @@ static void thread_test_func2(void)
*/
void thread_test(void)
{
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, 12);
if (msg_tag_get_prot(tag) < 0)
{
printf("factory_create_ipc no memory\n");
return;
}
tag = factory_create_thread(FACTORY_PROT, 11);
th1_hd = handler_alloc();
assert(th1_hd != HANDLER_INVALID);
th2_hd = handler_alloc();
assert(th2_hd != HANDLER_INVALID);
ipc_hd = handler_alloc();
assert(ipc_hd != HANDLER_INVALID);
if (msg_tag_get_prot(tag) < 0)
{
printf("factory_create_thread no memory\n");
return;
}
thread_msg_buf_set(11, msg_buf0);
thread_exec_regs(11, (umword_t)thread_test_func, (umword_t)stack0 + STACK_SIZE, RAM_BASE());
thread_bind_task(11, TASK_PROT);
thread_run(11);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, ipc_hd);
assert(msg_tag_get_prot(tag) >= 0);
factory_create_thread(FACTORY_PROT, 10);
thread_msg_buf_set(10, msg_buf1);
thread_exec_regs(10, (umword_t)thread_test_func2, (umword_t)stack1 + STACK_SIZE, RAM_BASE());
thread_bind_task(10, TASK_PROT);
thread_run(10);
tag = factory_create_thread(FACTORY_PROT, th1_hd);
assert(msg_tag_get_prot(tag) >= 0);
thread_msg_buf_set(th1_hd, msg_buf0);
thread_exec_regs(th1_hd, (umword_t)thread_test_func, (umword_t)stack0 + STACK_SIZE, RAM_BASE());
thread_bind_task(th1_hd, TASK_THIS);
thread_run(th1_hd);
factory_create_thread(FACTORY_PROT, th2_hd);
assert(msg_tag_get_prot(tag) >= 0);
thread_msg_buf_set(th2_hd, msg_buf1);
thread_exec_regs(th2_hd, (umword_t)thread_test_func2, (umword_t)stack1 + STACK_SIZE, RAM_BASE());
thread_bind_task(th2_hd, TASK_THIS);
thread_run(th2_hd);
}