diff --git a/components/drivers/cputime/cputimer.c b/components/drivers/cputime/cputimer.c index 12be40356e4..41c75e082ca 100644 --- a/components/drivers/cputime/cputimer.c +++ b/components/drivers/cputime/cputimer.c @@ -357,7 +357,7 @@ rt_err_t rt_cputime_sleep(rt_uint64_t tick) /* enable interrupt */ rt_hw_interrupt_enable(level); - thread->error = -RT_EINTR; + thread->error = RT_EOK; rt_schedule(); if (thread->error == -RT_ETIMEOUT) diff --git a/components/drivers/ipc/workqueue.c b/components/drivers/ipc/workqueue.c index 9a09aae403c..61002d708c6 100644 --- a/components/drivers/ipc/workqueue.c +++ b/components/drivers/ipc/workqueue.c @@ -81,6 +81,8 @@ static void _workqueue_thread_entry(void *parameter) /* do work */ work->work_func(work, work->work_data); + + RT_ASSERT(rt_thread_self()->error == RT_EOK); /* clean current work */ queue->work_current = RT_NULL; @@ -112,6 +114,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, { /* resume work thread */ rt_thread_resume(queue->work_thread); + queue->work_thread->error = RT_EOK; rt_hw_interrupt_enable(level); rt_schedule(); } @@ -194,6 +197,7 @@ static void _delayed_work_timeout_handler(void *parameter) { /* resume work thread */ rt_thread_resume(queue->work_thread); + queue->work_thread->error = RT_EOK; rt_hw_interrupt_enable(level); rt_schedule(); } @@ -352,6 +356,7 @@ rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *wo { /* resume work thread */ rt_thread_resume(queue->work_thread); + queue->work_thread->error = RT_EOK; rt_hw_interrupt_enable(level); rt_schedule(); } diff --git a/include/rtdef.h b/include/rtdef.h index 6941045c936..da0fdc348b1 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -369,6 +369,7 @@ typedef int (*init_fn_t)(void); #define RT_ETRAP 11 /**< Trap event */ #define RT_ENOENT 12 /**< No entry */ #define RT_ENOSPC 13 /**< No space left */ +#define RT_EINSCHE 14 /**< Scheduling in progress */ /**@}*/ diff --git a/src/ipc.c b/src/ipc.c index be220439b86..f901287e60e 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -540,7 +540,7 @@ static rt_err_t _rt_sem_take(rt_sem_t sem, rt_int32_t timeout, int suspend_flag) thread = rt_thread_self(); /* reset thread error number */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name)); @@ -1913,7 +1913,7 @@ static rt_err_t _rt_event_recv(rt_event_t event, /* get current thread */ thread = rt_thread_self(); /* reset thread error */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent))); @@ -2388,7 +2388,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, while (mb->entry == mb->size) { /* reset error number in thread */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; /* no waiting, return timeout */ if (timeout == 0) @@ -2681,7 +2681,7 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo while (mb->entry == 0) { /* reset error number in thread */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; /* no waiting, return timeout */ if (timeout == 0) @@ -3230,7 +3230,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL) { /* reset error number in thread */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; /* no waiting, return timeout */ if (timeout == 0) @@ -3583,7 +3583,7 @@ static rt_err_t _rt_mq_recv(rt_mq_t mq, while (mq->entry == 0) { /* reset error number in thread */ - thread->error = -RT_EINTR; + thread->error = RT_EOK; /* no waiting, return timeout */ if (timeout == 0) diff --git a/src/kservice.c b/src/kservice.c index e706d70247d..784118a7e19 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -75,6 +75,10 @@ static const char* rt_errno_strs[] = "EIO", "EINTRPT", "EINVAL", + "ETRAP", + "ENOENT", + "ENOSPC", + "OK", /* EINSCHE is a temporary state, in fact no error. */ "EUNKNOW" }; @@ -90,8 +94,8 @@ const char *rt_strerror(rt_err_t error) if (error < 0) error = -error; - return (error > RT_EINVAL + 1) ? - rt_errno_strs[RT_EINVAL + 1] : + return (error > RT_EINSCHE + 1) ? + rt_errno_strs[RT_EINSCHE + 1] : rt_errno_strs[error]; } RTM_EXPORT(rt_strerror); diff --git a/src/scheduler.c b/src/scheduler.c index c760fa8ed9c..1458c77266d 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -453,6 +453,7 @@ void rt_schedule(void) rt_base_t level; struct rt_thread *to_thread; struct rt_thread *from_thread; + struct rt_thread *from_thread_bak = rt_thread_self(); /* disable interrupt */ level = rt_hw_interrupt_disable(); @@ -522,6 +523,10 @@ void rt_schedule(void) RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + // RT_ASSERT(from_thread_bak->error == RT_EOK); + /* Need schedule, set a temporary state, means scheduling in progress*/ + from_thread_bak->error = -RT_EINSCHE; + rt_hw_context_switch((rt_ubase_t)&from_thread->sp, (rt_ubase_t)&to_thread->sp); @@ -569,6 +574,11 @@ void rt_schedule(void) rt_hw_interrupt_enable(level); __exit: + + if (rt_thread_self()->error != RT_EOK && rt_thread_self()->error != -RT_ETIMEOUT) + { + rt_thread_self()->error = -RT_EINTR; + } return; } #endif /* RT_USING_SMP */ diff --git a/src/thread.c b/src/thread.c index 5614edea4da..a9b3192f93a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -641,7 +641,7 @@ rt_err_t rt_thread_sleep(rt_tick_t tick) /* enable interrupt */ rt_hw_interrupt_enable(level); - thread->error = -RT_EINTR; + thread->error = RT_EOK; rt_schedule();