From 9b44535740b7e1deda584e8c7397b40113fa43f0 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Wed, 26 Jan 2022 16:02:24 +0800 Subject: [PATCH 1/2] add RT_DEBUG_SCHEDULER_AVAILABLE check --- components/dfs/filesystems/devfs/devfs.c | 12 +++++- components/drivers/ipc/completion.c | 6 +++ components/drivers/ipc/dataqueue.c | 18 +++++--- components/drivers/ipc/waitqueue.c | 2 +- include/rtdebug.h | 23 ++++++++++ src/ipc.c | 53 +++++++++++++++++++----- 6 files changed, 96 insertions(+), 18 deletions(-) diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index d60f7ddc9b..8e58fb9f53 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -144,11 +144,15 @@ int dfs_device_fs_open(struct dfs_fd *file) { count ++; } + rt_exit_critical(); root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) + count * sizeof(rt_device_t)); if (root_dirent != RT_NULL) { + /* lock scheduler */ + rt_enter_critical(); + root_dirent->devices = (rt_device_t *)(root_dirent + 1); root_dirent->read_index = 0; root_dirent->device_count = count; @@ -156,12 +160,18 @@ int dfs_device_fs_open(struct dfs_fd *file) /* get all device node */ for (node = information->object_list.next; node != &(information->object_list); node = node->next) { + /* avoid memory write through */ + if (count == root_dirent->device_count) + { + rt_kprintf("warning: There are newly added devices that are not displayed!"); + break; + } object = rt_list_entry(node, struct rt_object, list); root_dirent->devices[count] = (rt_device_t)object; count ++; } + rt_exit_critical(); } - rt_exit_critical(); /* set data */ file->data = root_dirent; diff --git a/components/drivers/ipc/completion.c b/components/drivers/ipc/completion.c index f13718f9b3..4983327e1d 100644 --- a/components/drivers/ipc/completion.c +++ b/components/drivers/ipc/completion.c @@ -57,6 +57,12 @@ rt_err_t rt_completion_wait(struct rt_completion *completion, rt_thread_t thread; RT_ASSERT(completion != RT_NULL); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + result = RT_EOK; thread = rt_thread_self(); diff --git a/components/drivers/ipc/dataqueue.c b/components/drivers/ipc/dataqueue.c index 4e52662c44..fc77f83922 100644 --- a/components/drivers/ipc/dataqueue.c +++ b/components/drivers/ipc/dataqueue.c @@ -98,6 +98,12 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue, RT_ASSERT(queue != RT_NULL); RT_ASSERT(queue->magic == DATAQUEUE_MAGIC); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + result = RT_EOK; thread = rt_thread_self(); @@ -112,9 +118,6 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue, goto __exit; } - /* current context checking */ - RT_DEBUG_NOT_IN_INTERRUPT; - /* reset thread error number */ thread->error = RT_EOK; @@ -217,6 +220,12 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue, RT_ASSERT(data_ptr != RT_NULL); RT_ASSERT(size != RT_NULL); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + result = RT_EOK; thread = rt_thread_self(); @@ -230,9 +239,6 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue, goto __exit; } - /* current context checking */ - RT_DEBUG_NOT_IN_INTERRUPT; - /* reset thread error number */ thread->error = RT_EOK; diff --git a/components/drivers/ipc/waitqueue.c b/components/drivers/ipc/waitqueue.c index 6a26604617..f65b34cdcf 100644 --- a/components/drivers/ipc/waitqueue.c +++ b/components/drivers/ipc/waitqueue.c @@ -129,7 +129,7 @@ int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec) rt_base_t level; /* current context checking */ - RT_DEBUG_NOT_IN_INTERRUPT; + RT_DEBUG_SCHEDULER_AVAILABLE; tick = rt_tick_from_millisecond(msec); diff --git a/include/rtdebug.h b/include/rtdebug.h index 0858a5bbf1..143f6c29d4 100644 --- a/include/rtdebug.h +++ b/include/rtdebug.h @@ -110,9 +110,31 @@ do \ rt_hw_interrupt_enable(level); \ } \ while (0) + +/* "scheduler available" means: + * 1) the scheduler has been started. + * 2) not in interrupt context. + * 3) scheduler is not locked. + */ +#define RT_DEBUG_SCHEDULER_AVAILABLE \ +do \ +{ \ + rt_base_t level; \ + level = rt_hw_interrupt_disable(); \ + if (rt_critical_level() != 0) \ + { \ + rt_kprintf("Function[%s]: scheduler is not available\n", \ + __FUNCTION__); \ + RT_ASSERT(0) \ + } \ + RT_DEBUG_IN_THREAD_CONTEXT; \ + rt_hw_interrupt_enable(level); \ +} \ +while (0) #else #define RT_DEBUG_NOT_IN_INTERRUPT #define RT_DEBUG_IN_THREAD_CONTEXT +#define RT_DEBUG_SCHEDULER_AVAILABLE #endif #else /* RT_DEBUG */ @@ -121,6 +143,7 @@ while (0) #define RT_DEBUG_LOG(type, message) #define RT_DEBUG_NOT_IN_INTERRUPT #define RT_DEBUG_IN_THREAD_CONTEXT +#define RT_DEBUG_SCHEDULER_AVAILABLE #endif /* RT_DEBUG */ diff --git a/src/ipc.c b/src/ipc.c index 5d4a2d018f..7e76a752fb 100755 --- a/src/ipc.c +++ b/src/ipc.c @@ -484,6 +484,11 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) /* parameter check */ RT_ASSERT(sem != RT_NULL); RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore); + /* current context checking */ + if (time != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent))); @@ -514,9 +519,6 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) } else { - /* current context checking */ - RT_DEBUG_IN_THREAD_CONTEXT; - /* semaphore is unavailable, push to suspend list */ /* get current thread */ thread = rt_thread_self(); @@ -914,6 +916,12 @@ rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time) /* this function must not be used in interrupt even if time = 0 */ RT_DEBUG_IN_THREAD_CONTEXT; + /* current context checking */ + if (time != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + /* parameter check */ RT_ASSERT(mutex != RT_NULL); RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex); @@ -1566,12 +1574,18 @@ rt_err_t rt_event_recv(rt_event_t event, register rt_ubase_t level; register rt_base_t status; - RT_DEBUG_IN_THREAD_CONTEXT; - /* parameter check */ RT_ASSERT(event != RT_NULL); RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event); + /* current context checking */ + RT_DEBUG_IN_THREAD_CONTEXT; + + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + if (set == 0) return -RT_ERROR; @@ -1993,6 +2007,12 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, RT_ASSERT(mb != RT_NULL); RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + /* initialize delta tick */ tick_delta = 0; /* get current thread */ @@ -2025,7 +2045,6 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, return -RT_EFULL; } - RT_DEBUG_IN_THREAD_CONTEXT; /* suspend current thread */ _ipc_list_suspend(&(mb->suspend_sender_thread), thread, @@ -2236,6 +2255,12 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout) RT_ASSERT(mb != RT_NULL); RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + /* initialize delta tick */ tick_delta = 0; /* get current thread */ @@ -2271,7 +2296,6 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout) return -RT_ETIMEOUT; } - RT_DEBUG_IN_THREAD_CONTEXT; /* suspend current thread */ _ipc_list_suspend(&(mb->parent.suspend_thread), thread, @@ -2744,6 +2768,12 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq, RT_ASSERT(buffer != RT_NULL); RT_ASSERT(size != 0); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + /* greater than one message size */ if (size > mq->msg_size) return -RT_ERROR; @@ -2784,7 +2814,6 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq, return -RT_EFULL; } - RT_DEBUG_IN_THREAD_CONTEXT; /* suspend current thread */ _ipc_list_suspend(&(mq->suspend_sender_thread), thread, @@ -3054,6 +3083,12 @@ rt_err_t rt_mq_recv(rt_mq_t mq, RT_ASSERT(buffer != RT_NULL); RT_ASSERT(size != 0); + /* current context checking */ + if (timeout != 0) + { + RT_DEBUG_SCHEDULER_AVAILABLE; + } + /* initialize delta tick */ tick_delta = 0; /* get current thread */ @@ -3074,8 +3109,6 @@ rt_err_t rt_mq_recv(rt_mq_t mq, /* message queue is empty */ while (mq->entry == 0) { - RT_DEBUG_IN_THREAD_CONTEXT; - /* reset error number in thread */ thread->error = RT_EOK; From 500d26c2570bb3d905e38aed05fd9c9b324a8f6e Mon Sep 17 00:00:00 2001 From: Henson Date: Wed, 26 Jan 2022 21:39:06 +0800 Subject: [PATCH 2/2] optimize code --- components/drivers/ipc/completion.c | 8 +----- components/drivers/ipc/dataqueue.c | 10 ++------ components/drivers/ipc/waitqueue.c | 2 +- include/rtdebug.h | 25 ++++++++++-------- src/ipc.c | 40 ++++++----------------------- 5 files changed, 26 insertions(+), 59 deletions(-) diff --git a/components/drivers/ipc/completion.c b/components/drivers/ipc/completion.c index 4983327e1d..84dfb740b1 100644 --- a/components/drivers/ipc/completion.c +++ b/components/drivers/ipc/completion.c @@ -58,10 +58,7 @@ rt_err_t rt_completion_wait(struct rt_completion *completion, RT_ASSERT(completion != RT_NULL); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); result = RT_EOK; thread = rt_thread_self(); @@ -88,9 +85,6 @@ rt_err_t rt_completion_wait(struct rt_completion *completion, rt_list_insert_before(&(completion->suspended_list), &(thread->tlist)); - /* current context checking */ - RT_DEBUG_NOT_IN_INTERRUPT; - /* start timer */ if (timeout > 0) { diff --git a/components/drivers/ipc/dataqueue.c b/components/drivers/ipc/dataqueue.c index fc77f83922..94e7b087c5 100644 --- a/components/drivers/ipc/dataqueue.c +++ b/components/drivers/ipc/dataqueue.c @@ -99,10 +99,7 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue, RT_ASSERT(queue->magic == DATAQUEUE_MAGIC); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); result = RT_EOK; thread = rt_thread_self(); @@ -221,10 +218,7 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue, RT_ASSERT(size != RT_NULL); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); result = RT_EOK; thread = rt_thread_self(); diff --git a/components/drivers/ipc/waitqueue.c b/components/drivers/ipc/waitqueue.c index f65b34cdcf..7847c6dde2 100644 --- a/components/drivers/ipc/waitqueue.c +++ b/components/drivers/ipc/waitqueue.c @@ -129,7 +129,7 @@ int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec) rt_base_t level; /* current context checking */ - RT_DEBUG_SCHEDULER_AVAILABLE; + RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE); tick = rt_tick_from_millisecond(msec); diff --git a/include/rtdebug.h b/include/rtdebug.h index 143f6c29d4..55d46171a2 100644 --- a/include/rtdebug.h +++ b/include/rtdebug.h @@ -116,25 +116,28 @@ while (0) * 2) not in interrupt context. * 3) scheduler is not locked. */ -#define RT_DEBUG_SCHEDULER_AVAILABLE \ +#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \ do \ { \ - rt_base_t level; \ - level = rt_hw_interrupt_disable(); \ - if (rt_critical_level() != 0) \ + if (need_check) \ { \ - rt_kprintf("Function[%s]: scheduler is not available\n", \ - __FUNCTION__); \ - RT_ASSERT(0) \ + rt_base_t level; \ + level = rt_hw_interrupt_disable(); \ + if (rt_critical_level() != 0) \ + { \ + rt_kprintf("Function[%s]: scheduler is not available\n", \ + __FUNCTION__); \ + RT_ASSERT(0) \ + } \ + RT_DEBUG_IN_THREAD_CONTEXT; \ + rt_hw_interrupt_enable(level); \ } \ - RT_DEBUG_IN_THREAD_CONTEXT; \ - rt_hw_interrupt_enable(level); \ } \ while (0) #else #define RT_DEBUG_NOT_IN_INTERRUPT #define RT_DEBUG_IN_THREAD_CONTEXT -#define RT_DEBUG_SCHEDULER_AVAILABLE +#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) #endif #else /* RT_DEBUG */ @@ -143,7 +146,7 @@ while (0) #define RT_DEBUG_LOG(type, message) #define RT_DEBUG_NOT_IN_INTERRUPT #define RT_DEBUG_IN_THREAD_CONTEXT -#define RT_DEBUG_SCHEDULER_AVAILABLE +#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) #endif /* RT_DEBUG */ diff --git a/src/ipc.c b/src/ipc.c index 7e76a752fb..f634c32dd3 100755 --- a/src/ipc.c +++ b/src/ipc.c @@ -484,11 +484,9 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) /* parameter check */ RT_ASSERT(sem != RT_NULL); RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore); + /* current context checking */ - if (time != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(time != 0); RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent))); @@ -914,13 +912,8 @@ rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time) struct rt_thread *thread; /* this function must not be used in interrupt even if time = 0 */ - RT_DEBUG_IN_THREAD_CONTEXT; - /* current context checking */ - if (time != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE); /* parameter check */ RT_ASSERT(mutex != RT_NULL); @@ -1579,12 +1572,7 @@ rt_err_t rt_event_recv(rt_event_t event, RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event); /* current context checking */ - RT_DEBUG_IN_THREAD_CONTEXT; - - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE); if (set == 0) return -RT_ERROR; @@ -2008,10 +1996,7 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); /* initialize delta tick */ tick_delta = 0; @@ -2256,10 +2241,7 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout) RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); /* initialize delta tick */ tick_delta = 0; @@ -2769,10 +2751,7 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq, RT_ASSERT(size != 0); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); /* greater than one message size */ if (size > mq->msg_size) @@ -3084,10 +3063,7 @@ rt_err_t rt_mq_recv(rt_mq_t mq, RT_ASSERT(size != 0); /* current context checking */ - if (timeout != 0) - { - RT_DEBUG_SCHEDULER_AVAILABLE; - } + RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); /* initialize delta tick */ tick_delta = 0;