diff --git a/mkrtos_knl/knl/ipc.c b/mkrtos_knl/knl/ipc.c index d9b1d0b9b..a01459197 100755 --- a/mkrtos_knl/knl/ipc.c +++ b/mkrtos_knl/knl/ipc.c @@ -48,6 +48,11 @@ static void add_wait(ipc_t *ipc, thread_t *th) 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) // { // } @@ -72,8 +77,7 @@ static int ipc_send(ipc_t *ipc, entry_frame_t *f) status = spinlock_lock(&ipc->lock); if (!ipc->rcv) { - slist_add_append(&ipc->wait_send, &th->wait); - thread_suspend(th); + add_wait_unlock(ipc, th); } spinlock_set(&ipc->lock, status); if (th->msg.flags & MSG_BUF_HAS_DATA_FLAGS) @@ -237,7 +241,7 @@ again_recv: } else { - th->msg.call_send = NULL; + send_th->msg.call_send = NULL; ipc->rcv = NULL; //! 如果不是call,则清空接收者 } thread_ready(send_th, TRUE); //!< 唤醒发送线程 diff --git a/mkrtos_user/server/init/src/test/ipc_test.c b/mkrtos_user/server/init/src/test/ipc_test.c index d44ce7ec3..db4225d91 100644 --- a/mkrtos_user/server/init/src/test/ipc_test.c +++ b/mkrtos_user/server/init/src/test/ipc_test.c @@ -13,13 +13,16 @@ static umword_t th1_hd = 0; static umword_t th2_hd = 0; +static umword_t th3_hd = 0; static umword_t ipc_hd = 0; static char msg_buf0[MSG_BUG_LEN]; static char msg_buf1[MSG_BUG_LEN]; +static char msg_buf2[MSG_BUG_LEN]; #define STACK_SIZE 1024 static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE]; static __attribute__((aligned(8))) uint8_t stack1[STACK_SIZE]; +static __attribute__((aligned(8))) uint8_t stack2[STACK_SIZE]; #if !DEBUG_IPC_CALL static void thread_test_func(void) @@ -72,7 +75,7 @@ static void thread_test_func(void) { ipc_recv(ipc_hd); printf(buf); - strcpy(buf, "hello thread2.\n"); + strcpy(buf, "thread_test_func.\n"); ipc_send(ipc_hd, strlen(buf), 0); } printf("thread_test_func.\n"); @@ -86,7 +89,7 @@ static void thread_test_func2(void) thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL); while (1) { - strcpy(buf, "hello thread1.\n"); + strcpy(buf, "thread_test_func2.\n"); ipc_send(ipc_hd, strlen(buf), MSG_BUF_CALL_FLAGS); printf(buf); } @@ -94,6 +97,22 @@ static void thread_test_func2(void) task_unmap(TASK_PROT, th2_hd); printf("Error\n"); } +static void thread_test_func3(void) +{ + char *buf; + umword_t len; + thread_msg_buf_get(th3_hd, (umword_t *)(&buf), NULL); + while (1) + { + ipc_recv(ipc_hd); + printf(buf); + strcpy(buf, "thread_test_func3.\n"); + ipc_send(ipc_hd, strlen(buf), 0); + } + printf("thread_test_func2.\n"); + task_unmap(TASK_PROT, th3_hd); + printf("Error\n"); +} #endif /** * @brief 启动两个线程并进行ipc测试 @@ -105,6 +124,8 @@ void ipc_test(void) assert(th1_hd != HANDLER_INVALID); th2_hd = handler_alloc(); assert(th2_hd != HANDLER_INVALID); + th3_hd = handler_alloc(); + assert(th3_hd != HANDLER_INVALID); ipc_hd = handler_alloc(); assert(ipc_hd != HANDLER_INVALID); @@ -119,6 +140,7 @@ void ipc_test(void) tag = thread_bind_task(th1_hd, TASK_THIS); assert(msg_tag_get_prot(tag) >= 0); tag = thread_run(th1_hd); + assert(msg_tag_get_prot(tag) >= 0); tag = factory_create_thread(FACTORY_PROT, th2_hd); assert(msg_tag_get_prot(tag) >= 0); @@ -130,4 +152,16 @@ void ipc_test(void) assert(msg_tag_get_prot(tag) >= 0); 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); }