From 302f179c851d3a8c953400ca0d2789546c50491c Mon Sep 17 00:00:00 2001 From: liruncong Date: Tue, 9 Feb 2021 23:18:45 +0800 Subject: 修正rt_thread_delay_until时间不准问题,及错误使用无符号比较问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 0db9dd8e4d..627b7e898e 100644 --- a/src/thread.c +++ b/src/thread.c @@ -585,6 +585,7 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) { register rt_base_t level; struct rt_thread *thread; + rt_tick_t cur_tick; RT_ASSERT(tick != RT_NULL); @@ -596,15 +597,19 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) /* disable interrupt */ level = rt_hw_interrupt_disable(); - if (rt_tick_get() - *tick < inc_tick) + cur_tick = rt_tick_get(); + if ((rt_int32_t)(cur_tick - *tick) < (rt_int32_t)inc_tick) { - *tick = *tick + inc_tick - rt_tick_get(); + rt_tick_t left_tick; + + *tick += inc_tick; + left_tick = *tick - cur_tick; /* suspend thread */ rt_thread_suspend(thread); /* reset the timeout of thread timer and start it */ - rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick); + rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick); rt_timer_start(&(thread->thread_timer)); /* enable interrupt */ @@ -620,12 +625,10 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) } else { + *tick = cur_tick; rt_hw_interrupt_enable(level); } - /* get the wakeup tick */ - *tick = rt_tick_get(); - return RT_EOK; } RTM_EXPORT(rt_thread_delay_until); -- cgit v1.3.1 From 2b98ac215a4e07401464bfcd6ad00957ff03bd72 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Wed, 24 Feb 2021 16:29:43 +0800 Subject: [kernel] rearrange rt_thread_control() - schedule the thread when close it --- components/libc/compilers/common/stdlib.c | 10 +-- src/thread.c | 141 ++++++++++++++++-------------- 2 files changed, 77 insertions(+), 74 deletions(-) (limited to 'src/thread.c') diff --git a/components/libc/compilers/common/stdlib.c b/components/libc/compilers/common/stdlib.c index 9f801ab966..48a86098cd 100644 --- a/components/libc/compilers/common/stdlib.c +++ b/components/libc/compilers/common/stdlib.c @@ -14,18 +14,10 @@ void __rt_libc_exit(int status) { rt_thread_t self = rt_thread_self(); -#ifdef RT_USING_MODULE - if (dlmodule_self()) - { - dlmodule_exit(status); - } -#endif - if (self != RT_NULL) { rt_kprintf("thread:%s exit:%d!\n", self->name, status); - rt_thread_suspend(self); - rt_schedule(); + rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL); } } diff --git a/src/thread.c b/src/thread.c index 0db9dd8e4d..2681eca59f 100644 --- a/src/thread.c +++ b/src/thread.c @@ -26,6 +26,7 @@ * bug when thread has not startup. * 2018-11-22 Jesven yield is same to rt_schedule * add support for tasks bound to cpu + * 2021-02-24 Meco Man rearrange rt_thread_control() - schedule the thread when close it */ #include @@ -670,83 +671,93 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) switch (cmd) { - case RT_THREAD_CTRL_CHANGE_PRIORITY: - /* disable interrupt */ - temp = rt_hw_interrupt_disable(); - - /* for ready thread, change queue */ - if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + case RT_THREAD_CTRL_CHANGE_PRIORITY: { - /* remove thread from schedule queue first */ - rt_schedule_remove_thread(thread); - - /* change thread priority */ - thread->current_priority = *(rt_uint8_t *)arg; - - /* recalculate priority attribute */ -#if RT_THREAD_PRIORITY_MAX > 32 - thread->number = thread->current_priority >> 3; /* 5bit */ - thread->number_mask = 1 << thread->number; - thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ -#else - thread->number_mask = 1 << thread->current_priority; -#endif - - /* insert thread to schedule queue again */ - rt_schedule_insert_thread(thread); + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + + /* for ready thread, change queue */ + if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + { + /* remove thread from schedule queue first */ + rt_schedule_remove_thread(thread); + + /* change thread priority */ + thread->current_priority = *(rt_uint8_t *)arg; + + /* recalculate priority attribute */ + #if RT_THREAD_PRIORITY_MAX > 32 + thread->number = thread->current_priority >> 3; /* 5bit */ + thread->number_mask = 1 << thread->number; + thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ + #else + thread->number_mask = 1 << thread->current_priority; + #endif + + /* insert thread to schedule queue again */ + rt_schedule_insert_thread(thread); + } + else + { + thread->current_priority = *(rt_uint8_t *)arg; + + /* recalculate priority attribute */ + #if RT_THREAD_PRIORITY_MAX > 32 + thread->number = thread->current_priority >> 3; /* 5bit */ + thread->number_mask = 1 << thread->number; + thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ + #else + thread->number_mask = 1 << thread->current_priority; + #endif + } + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); + break; } - else - { - thread->current_priority = *(rt_uint8_t *)arg; - /* recalculate priority attribute */ -#if RT_THREAD_PRIORITY_MAX > 32 - thread->number = thread->current_priority >> 3; /* 5bit */ - thread->number_mask = 1 << thread->number; - thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ -#else - thread->number_mask = 1 << thread->current_priority; -#endif + case RT_THREAD_CTRL_STARTUP: + { + return rt_thread_startup(thread); } - /* enable interrupt */ - rt_hw_interrupt_enable(temp); - break; - - case RT_THREAD_CTRL_STARTUP: - return rt_thread_startup(thread); - - case RT_THREAD_CTRL_CLOSE: - - if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) + case RT_THREAD_CTRL_CLOSE: { - return rt_thread_detach(thread); + rt_err_t rt_err; + + if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) + { + rt_err = rt_thread_detach(thread); + } + #ifdef RT_USING_HEAP + else + { + rt_err = rt_thread_delete(thread); + } + #endif + rt_schedule(); + return rt_err; } -#ifdef RT_USING_HEAP - else + + #ifdef RT_USING_SMP + case RT_THREAD_CTRL_BIND_CPU: { - return rt_thread_delete(thread); - } -#endif + rt_uint8_t cpu; -#ifdef RT_USING_SMP - case RT_THREAD_CTRL_BIND_CPU: - { - rt_uint8_t cpu; + if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT) + { + /* we only support bind cpu before started phase. */ + return RT_ERROR; + } - if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT) - { - /* we only support bind cpu before started phase. */ - return RT_ERROR; + cpu = (rt_uint8_t)(size_t)arg; + thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu; + break; } + #endif /*RT_USING_SMP*/ - cpu = (rt_uint8_t)(size_t)arg; - thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu; - break; - } -#endif /*RT_USING_SMP*/ - default: - break; + default: + break; } return RT_EOK; -- cgit v1.3.1 From 62137eec9ab5c689686bb705304964d7451bbcdf Mon Sep 17 00:00:00 2001 From: 张世争 Date: Fri, 26 Feb 2021 10:38:31 +0800 Subject: [update][kernel]规范rt_object_detach的使用 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread.c | 15 ++------------- src/timer.c | 2 +- 2 files changed, 3 insertions(+), 14 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 2681eca59f..18b5227f5a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -382,19 +382,8 @@ rt_err_t rt_thread_detach(rt_thread_t thread) /* change stat */ thread->stat = RT_THREAD_CLOSE; - if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) - { - rt_object_detach((rt_object_t)thread); - } - else - { - /* disable interrupt */ - lock = rt_hw_interrupt_disable(); - /* insert to defunct thread list */ - rt_list_insert_after(&rt_thread_defunct, &(thread->tlist)); - /* enable interrupt */ - rt_hw_interrupt_enable(lock); - } + /* detach thread object */ + rt_object_detach((rt_object_t)thread); return RT_EOK; } diff --git a/src/timer.c b/src/timer.c index d882711151..747c28c8bd 100644 --- a/src/timer.c +++ b/src/timer.c @@ -233,7 +233,7 @@ rt_err_t rt_timer_detach(rt_timer_t timer) /* enable interrupt */ rt_hw_interrupt_enable(level); - rt_object_detach((rt_object_t)timer); + rt_object_detach(&(timer->parent)); return RT_EOK; } -- cgit v1.3.1 From 72d7f6c0adf6ef251e7662f09c9dd32579041a19 Mon Sep 17 00:00:00 2001 From: 张世争 Date: Sun, 28 Feb 2021 12:00:44 +0800 Subject: [update][kernel]规范rt_object_t强制转换的使用,消除上次提交的编译警告 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread.c | 2 -- src/timer.c | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 18b5227f5a..beef93f46a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -358,8 +358,6 @@ RTM_EXPORT(rt_thread_startup); */ rt_err_t rt_thread_detach(rt_thread_t thread) { - rt_base_t lock; - /* thread check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); diff --git a/src/timer.c b/src/timer.c index 747c28c8bd..2eaca225bc 100644 --- a/src/timer.c +++ b/src/timer.c @@ -201,7 +201,7 @@ void rt_timer_init(rt_timer_t timer, RT_ASSERT(timer != RT_NULL); /* timer object initialization */ - rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name); + rt_object_init(&(timer->parent), RT_Object_Class_Timer, name); _rt_timer_init(timer, timeout, parameter, time, flag); } @@ -298,7 +298,7 @@ rt_err_t rt_timer_delete(rt_timer_t timer) /* enable interrupt */ rt_hw_interrupt_enable(level); - rt_object_delete((rt_object_t)timer); + rt_object_delete(&(timer->parent)); return RT_EOK; } -- cgit v1.3.1 From 5ce889ea820e1c5bbadc41a0464b2dcdf3e607fc Mon Sep 17 00:00:00 2001 From: liruncong Date: Thu, 4 Mar 2021 19:33:28 +0800 Subject: 撤销有符号比较,仍应使用无符号比较 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 627b7e898e..c2a3e2e797 100644 --- a/src/thread.c +++ b/src/thread.c @@ -598,7 +598,7 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) level = rt_hw_interrupt_disable(); cur_tick = rt_tick_get(); - if ((rt_int32_t)(cur_tick - *tick) < (rt_int32_t)inc_tick) + if (cur_tick - *tick < inc_tick) { rt_tick_t left_tick; -- cgit v1.3.1 From 678306d300faa44b6cd099464bd978afb2c5c9a6 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Mon, 8 Mar 2021 11:25:38 +0800 Subject: [kernel]auto formatted --- src/clock.c | 4 ++-- src/components.c | 2 +- src/cpu.c | 2 +- src/device.c | 2 +- src/idle.c | 4 ++-- src/ipc.c | 20 ++++++++++---------- src/irq.c | 6 +++--- src/kservice.c | 8 ++++---- src/mem.c | 4 ++-- src/memheap.c | 6 +++--- src/mempool.c | 2 +- src/object.c | 10 +++++----- src/scheduler.c | 8 ++++---- src/signal.c | 2 +- src/slab.c | 2 +- src/thread.c | 2 +- src/timer.c | 2 +- 17 files changed, 43 insertions(+), 43 deletions(-) (limited to 'src/thread.c') diff --git a/src/clock.c b/src/clock.c index 66923e5c00..0880d8d241 100644 --- a/src/clock.c +++ b/src/clock.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -111,7 +111,7 @@ rt_tick_t rt_tick_from_millisecond(rt_int32_t ms) tick = RT_TICK_PER_SECOND * (ms / 1000); tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000; } - + /* return the calculated tick */ return tick; } diff --git a/src/components.c b/src/components.c index 8ea5654d0c..9709d0aae2 100644 --- a/src/components.c +++ b/src/components.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/cpu.c b/src/cpu.c index 96e585713b..48ddb12c46 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/device.c b/src/device.c index a4f004fb69..388c2ecf12 100644 --- a/src/device.c +++ b/src/device.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/idle.c b/src/idle.c index aa14552a49..24dfffbc6d 100644 --- a/src/idle.c +++ b/src/idle.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -211,7 +211,7 @@ static void rt_thread_idle_entry(void *parameter) #endif rt_thread_idle_excute(); -#ifdef RT_USING_PM +#ifdef RT_USING_PM rt_system_power_manager(); #endif } diff --git a/src/ipc.c b/src/ipc.c index 37b05b9ae3..111d724c23 100755 --- a/src/ipc.c +++ b/src/ipc.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -34,7 +34,7 @@ * 2013-09-14 Grissiom add an option check in rt_event_recv * 2018-10-02 Bernard add 64bit support for mailbox * 2019-09-16 tyx add send wait support for message queue - * 2020-07-29 Meco Man fix thread->event_set/event_info when received an + * 2020-07-29 Meco Man fix thread->event_set/event_info when received an * event without pending * 2020-10-11 Meco Man add value overflow-check code * 2021-01-03 Meco Man add rt_mb_urgent() @@ -124,7 +124,7 @@ rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, break; default: - break; + break; } return RT_EOK; @@ -959,7 +959,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex) rt_hw_interrupt_enable(temp); /* enable interrupt */ return -RT_EFULL; /* value overflowed */ } - + /* clear owner */ mutex->owner = RT_NULL; mutex->original_priority = 0xff; @@ -1280,11 +1280,11 @@ rt_err_t rt_event_recv(rt_event_t event, /* set received event */ if (recved) *recved = (event->set & set); - - /* fill thread event info */ + + /* fill thread event info */ thread->event_set = (event->set & set); thread->event_info = option; - + /* received event */ if (option & RT_EVENT_FLAG_CLEAR) event->set &= ~set; @@ -1649,7 +1649,7 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, ++ mb->in_offset; if (mb->in_offset >= mb->size) mb->in_offset = 0; - + if(mb->entry < RT_MB_ENTRY_MAX) { /* increase message entry */ @@ -1660,7 +1660,7 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_hw_interrupt_enable(temp); /* enable interrupt */ return -RT_EFULL; /* value overflowed */ } - + /* resume suspended thread */ if (!rt_list_isempty(&mb->parent.suspend_thread)) { @@ -2409,7 +2409,7 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size) rt_hw_interrupt_enable(temp); /* enable interrupt */ return -RT_EFULL; /* value overflowed */ } - + /* resume suspended thread */ if (!rt_list_isempty(&mq->parent.suspend_thread)) { diff --git a/src/irq.c b/src/irq.c index 311d297bfe..177c1a9ff5 100644 --- a/src/irq.c +++ b/src/irq.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -21,7 +21,7 @@ static void (*rt_interrupt_leave_hook)(void); /** * @ingroup Hook - * This function set a hook function when the system enter a interrupt + * This function set a hook function when the system enter a interrupt * * @note the hook function must be simple and never be blocked or suspend. */ @@ -31,7 +31,7 @@ void rt_interrupt_enter_sethook(void (*hook)(void)) } /** * @ingroup Hook - * This function set a hook function when the system exit a interrupt. + * This function set a hook function when the system exit a interrupt. * * @note the hook function must be simple and never be blocked or suspend. */ diff --git a/src/kservice.c b/src/kservice.c index 9fc3971c7e..79ee4c2b9d 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -456,7 +456,7 @@ RTM_EXPORT(rt_strncmp); rt_int32_t rt_strcmp(const char *cs, const char *ct) { while (*cs && *cs == *ct) - { + { cs++; ct++; } @@ -1127,10 +1127,10 @@ rt_device_t rt_console_set_device(const char *name) /* find new console device */ new_device = rt_device_find(name); - + /* check whether it's a same device */ if (new_device == old_device) return RT_NULL; - + if (new_device != RT_NULL) { if (_console_device != RT_NULL) diff --git a/src/mem.c b/src/mem.c index c03c8b3452..ac0d27f2c3 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -489,7 +489,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize) { ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2; } - + if (mem2 < lfree) { /* the splited struct is now the lowest */ diff --git a/src/memheap.c b/src/memheap.c index 449e66e975..a5d86f6347 100644 --- a/src/memheap.c +++ b/src/memheap.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 */ @@ -130,7 +130,7 @@ rt_err_t rt_memheap_detach(struct rt_memheap *heap) RT_ASSERT(heap); RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap); RT_ASSERT(rt_object_is_systemobject(&heap->parent)); - + rt_sem_detach(&heap->lock); rt_object_detach(&(heap->parent)); @@ -765,7 +765,7 @@ void dump_used_memheap(struct rt_memheap *mh) rt_kprintf("\nmemory heap address:\n"); rt_kprintf("heap_ptr: 0x%08x\n", mh->start_addr); - rt_kprintf("free : 0x%08x\n", mh->available_size); + rt_kprintf("free : 0x%08x\n", mh->available_size); rt_kprintf("max_used: 0x%08x\n", mh->max_used_size); rt_kprintf("size : 0x%08x\n", mh->pool_size); diff --git a/src/mempool.c b/src/mempool.c index db614032aa..1de48940d7 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/object.c b/src/object.c index 56263618b3..013e2d1e33 100644 --- a/src/object.c +++ b/src/object.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -200,7 +200,7 @@ void rt_object_put_sethook(void (*hook)(struct rt_object *object)) /** * This function will return the specified type of object information. * - * @param type the type of object, which can be + * @param type the type of object, which can be * RT_Object_Class_Thread/Semaphore/Mutex... etc * * @return the object type information or RT_NULL @@ -220,7 +220,7 @@ RTM_EXPORT(rt_object_get_information); /** * This function will return the length of object list in object container. * - * @param type the type of object, which can be + * @param type the type of object, which can be * RT_Object_Class_Thread/Semaphore/Mutex... etc * @return the length of object list */ @@ -247,10 +247,10 @@ int rt_object_get_length(enum rt_object_class_type type) RTM_EXPORT(rt_object_get_length); /** - * This function will copy the object pointer of the specified type, + * This function will copy the object pointer of the specified type, * with the maximum size specified by maxlen. * - * @param type the type of object, which can be + * @param type the type of object, which can be * RT_Object_Class_Thread/Semaphore/Mutex... etc * @param pointers the pointers will be saved to * @param maxlen the maximum number of pointers can be saved diff --git a/src/scheduler.c b/src/scheduler.c index 06f305ea0b..c03c20d030 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -270,10 +270,10 @@ void rt_system_scheduler_start(void) #ifdef RT_USING_SMP /** * This function will handle IPI interrupt and do a scheduling in system; - * + * * @param vector, the number of IPI interrupt for system scheduling * @param param, use RT_NULL - * + * * NOTE: this function should be invoke or register as ISR in BSP. */ void rt_scheduler_ipi_handler(int vector, void *param) @@ -283,7 +283,7 @@ void rt_scheduler_ipi_handler(int vector, void *param) /** * This function will perform one scheduling. It will select one thread - * with the highest priority level in global ready queue or local ready queue, + * with the highest priority level in global ready queue or local ready queue, * then switch to it. */ void rt_schedule(void) diff --git a/src/signal.c b/src/signal.c index bb9339ab6c..c2c3e975cb 100644 --- a/src/signal.c +++ b/src/signal.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/slab.c b/src/slab.c index 0872ecf092..12ca1a938c 100644 --- a/src/slab.c +++ b/src/slab.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/src/thread.c b/src/thread.c index beef93f46a..402ed66acb 100644 --- a/src/thread.c +++ b/src/thread.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/src/timer.c b/src/timer.c index 2eaca225bc..836df51e06 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * -- cgit v1.3.1 From d9367468061113b31df5e3488dced157e1d0dabf Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Fri, 28 May 2021 10:28:00 +0800 Subject: [kernel] Improve kernel stability --- src/ipc.c | 1 + src/thread.c | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src/thread.c') diff --git a/src/ipc.c b/src/ipc.c index 111d724c23..22e0f06bc6 100755 --- a/src/ipc.c +++ b/src/ipc.c @@ -124,6 +124,7 @@ rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, break; default: + RT_ASSERT(0); break; } diff --git a/src/thread.c b/src/thread.c index 34e96077d1..e03c027eff 100644 --- a/src/thread.c +++ b/src/thread.c @@ -358,6 +358,8 @@ RTM_EXPORT(rt_thread_startup); */ rt_err_t rt_thread_detach(rt_thread_t thread) { + rt_base_t lock; + /* thread check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); @@ -377,12 +379,18 @@ rt_err_t rt_thread_detach(rt_thread_t thread) /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); + /* disable interrupt */ + lock = rt_hw_interrupt_disable(); + /* change stat */ thread->stat = RT_THREAD_CLOSE; /* detach thread object */ rt_object_detach((rt_object_t)thread); + /* enable interrupt */ + rt_hw_interrupt_enable(lock); + return RT_EOK; } RTM_EXPORT(rt_thread_detach); -- cgit v1.3.1 From 039d7979d83666e96d7f6efb84255de0047e3e67 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 29 May 2021 17:30:18 +0800 Subject: [kernel][thread.c] 规范内部函数命名方式 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index e03c027eff..6304a4ebe6 100644 --- a/src/thread.c +++ b/src/thread.c @@ -79,7 +79,7 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) #endif /* must be invoke witch rt_hw_interrupt_disable */ -static void _thread_cleanup_execute(rt_thread_t thread) +static void _rt_thread_cleanup_execute(rt_thread_t thread) { register rt_base_t level; #ifdef RT_USING_MODULE @@ -103,7 +103,7 @@ static void _thread_cleanup_execute(rt_thread_t thread) rt_hw_interrupt_enable(level); } -void rt_thread_exit(void) +static void _rt_thread_exit(void) { struct rt_thread *thread; register rt_base_t level; @@ -114,7 +114,7 @@ void rt_thread_exit(void) /* disable interrupt */ level = rt_hw_interrupt_disable(); - _thread_cleanup_execute(thread); + _rt_thread_cleanup_execute(thread); /* remove from schedule */ rt_schedule_remove_thread(thread); @@ -165,11 +165,11 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, #ifdef ARCH_CPU_STACK_GROWS_UPWARD thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (void *)((char *)thread->stack_addr), - (void *)rt_thread_exit); + (void *)_rt_thread_exit); #else thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), - (void *)rt_thread_exit); + (void *)_rt_thread_exit); #endif /* priority init */ @@ -374,7 +374,7 @@ rt_err_t rt_thread_detach(rt_thread_t thread) rt_schedule_remove_thread(thread); } - _thread_cleanup_execute(thread); + _rt_thread_cleanup_execute(thread); /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); @@ -472,7 +472,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread) rt_schedule_remove_thread(thread); } - _thread_cleanup_execute(thread); + _rt_thread_cleanup_execute(thread); /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); -- cgit v1.3.1 From 8b53609f574209b11f649f946466fd0e0244eb60 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 10 Jun 2021 18:33:47 +0800 Subject: [kernel] 补充endif后缀注释(3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memheap.c | 30 +++++++++++------------ src/mempool.c | 7 +++--- src/object.c | 14 +++++------ src/scheduler.c | 76 ++++++++++++++++++++++++++++----------------------------- src/signal.c | 12 ++++----- src/slab.c | 22 ++++++++--------- src/thread.c | 34 +++++++++++++------------- src/timer.c | 20 +++++++-------- 8 files changed, 107 insertions(+), 108 deletions(-) (limited to 'src/thread.c') diff --git a/src/memheap.c b/src/memheap.c index 35a04c14b1..feac8bb4aa 100644 --- a/src/memheap.c +++ b/src/memheap.c @@ -74,7 +74,7 @@ void rt_mem_set_tag(void *ptr, const char *name) rt_memheap_setname(item, name); } } -#endif +#endif /* RT_USING_MEMTRACE */ /* * The initialized memory pool will be: @@ -127,7 +127,7 @@ rt_err_t rt_memheap_init(struct rt_memheap *memheap, #ifdef RT_USING_MEMTRACE rt_memset(item->owner_thread_name, ' ', sizeof(item->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ item->next = (struct rt_memheap_item *) ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE); @@ -252,7 +252,7 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size) #ifdef RT_USING_MEMTRACE rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ /* break down the block list */ new_ptr->prev = header_ptr; @@ -310,7 +310,7 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size) rt_memcpy(header_ptr->owner_thread_name, rt_thread_self()->name, sizeof(header_ptr->owner_thread_name)); else rt_memcpy(header_ptr->owner_thread_name, "NONE", sizeof(header_ptr->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ /* release lock */ rt_sem_release(&(heap->lock)); @@ -439,7 +439,7 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize) #ifdef RT_USING_MEMTRACE rt_memset(next_ptr->owner_thread_name, ' ', sizeof(next_ptr->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ next_ptr->prev = header_ptr; next_ptr->next = header_ptr->next; @@ -507,7 +507,7 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize) #ifdef RT_USING_MEMTRACE rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ /* break down the block list */ new_ptr->prev = header_ptr; @@ -660,7 +660,7 @@ void rt_memheap_free(void *ptr) #ifdef RT_USING_MEMTRACE rt_memset(header_ptr->owner_thread_name, ' ', sizeof(header_ptr->owner_thread_name)); -#endif +#endif /* RT_USING_MEMTRACE */ /* release lock */ rt_sem_release(&(heap->lock)); @@ -750,7 +750,7 @@ int memheaptrace(void) return 0; } MSH_CMD_EXPORT(memheaptrace, dump memory trace information); -#endif +#endif /* RT_USING_FINSH */ #ifdef RT_USING_MEMHEAP_AS_HEAP static struct rt_memheap _heap; @@ -816,7 +816,7 @@ void *rt_malloc(rt_size_t size) RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("malloc => 0x%08x : %d", ptr, size)); } -#endif +#endif /* RT_USING_MEMTRACE */ return ptr; } @@ -882,7 +882,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize) RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("realloc => 0x%08x : %d", new_ptr, newsize)); } -#endif +#endif /* RT_USING_MEMTRACE */ return new_ptr; } @@ -912,7 +912,7 @@ void *rt_calloc(rt_size_t count, rt_size_t size) RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("calloc => 0x%08x : %d", ptr, count * size)); } -#endif +#endif /* RT_USING_MEMTRACE */ return ptr; } @@ -932,7 +932,7 @@ void rt_memory_info(rt_uint32_t *total, *max_used = _heap.max_used_size; } -#endif +#endif /* RT_USING_MEMHEAP_AS_HEAP */ #ifdef RT_USING_MEMTRACE @@ -1004,8 +1004,8 @@ void memtrace_heap() #ifdef RT_USING_FINSH #include MSH_CMD_EXPORT(memtrace_heap, dump memory trace for heap); -#endif /* end of RT_USING_FINSH */ +#endif /* RT_USING_FINSH */ -#endif /* end of RT_USING_MEMTRACE */ +#endif /* RT_USING_MEMTRACE */ -#endif /* end of RT_USING_MEMHEAP */ +#endif /* RT_USING_MEMHEAP */ diff --git a/src/mempool.c b/src/mempool.c index 1de48940d7..5ac87d2bc8 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -54,7 +54,7 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block)) } /**@}*/ -#endif +#endif /* RT_USING_HOOK */ /** * @addtogroup MM @@ -290,7 +290,7 @@ rt_err_t rt_mp_delete(rt_mp_t mp) return RT_EOK; } RTM_EXPORT(rt_mp_delete); -#endif +#endif /* RT_USING_HEAP */ /** * This function will allocate a block from memory pool @@ -451,5 +451,4 @@ RTM_EXPORT(rt_mp_free); /**@}*/ -#endif - +#endif /* RT_USING_MEMPOOL */ diff --git a/src/object.c b/src/object.c index 013e2d1e33..ff96037c52 100644 --- a/src/object.c +++ b/src/object.c @@ -20,7 +20,7 @@ #ifdef RT_USING_MODULE #include -#endif +#endif /* RT_USING_MODULE */ /* * define object_info for the number of rt_object_container items. @@ -189,7 +189,7 @@ void rt_object_put_sethook(void (*hook)(struct rt_object *object)) } /**@}*/ -#endif +#endif /* RT_USING_HOOK */ /** * @addtogroup KernelObject @@ -305,7 +305,7 @@ void rt_object_init(struct rt_object *object, struct rt_object_information *information; #ifdef RT_USING_MODULE struct rt_dlmodule *module = dlmodule_self(); -#endif +#endif /* RT_USING_MODULE */ /* get object information */ information = rt_object_get_information(type); @@ -349,7 +349,7 @@ void rt_object_init(struct rt_object *object, object->module_id = (void *)module; } else -#endif +#endif /* RT_USING_MODULE */ { /* insert object into information object list */ rt_list_insert_after(&(information->object_list), &(object->list)); @@ -403,7 +403,7 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) struct rt_object_information *information; #ifdef RT_USING_MODULE struct rt_dlmodule *module = dlmodule_self(); -#endif +#endif /* RT_USING_MODULE */ RT_DEBUG_NOT_IN_INTERRUPT; @@ -444,7 +444,7 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) object->module_id = (void *)module; } else -#endif +#endif /* RT_USING_MODULE */ { /* insert object into information object list */ rt_list_insert_after(&(information->object_list), &(object->list)); @@ -487,7 +487,7 @@ void rt_object_delete(rt_object_t object) /* free the memory of object */ RT_KERNEL_FREE(object); } -#endif +#endif /* RT_USING_HEAP */ /** * This function will judge the object is system object or not. diff --git a/src/scheduler.c b/src/scheduler.c index ed0f30fc81..eec660a8aa 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -38,14 +38,14 @@ rt_uint32_t rt_thread_ready_priority_group; #if RT_THREAD_PRIORITY_MAX > 32 /* Maximum priority level, 256 */ rt_uint8_t rt_thread_ready_table[32]; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ #ifndef RT_USING_SMP extern volatile rt_uint8_t rt_interrupt_nest; static rt_int16_t rt_scheduler_lock_nest; struct rt_thread *rt_current_thread = RT_NULL; rt_uint8_t rt_current_priority; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ rt_list_t rt_thread_defunct; @@ -71,18 +71,18 @@ rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) } /**@}*/ -#endif +#endif /* RT_USING_HOOK */ #ifdef RT_USING_OVERFLOW_CHECK static void _rt_scheduler_stack_check(struct rt_thread *thread) { RT_ASSERT(thread != RT_NULL); -#if defined(ARCH_CPU_STACK_GROWS_UPWARD) +#ifdef ARCH_CPU_STACK_GROWS_UPWARD if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' || #else if (*((rt_uint8_t *)thread->stack_addr) != '#' || -#endif +#endif /* ARCH_CPU_STACK_GROWS_UPWARD */ (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr || (rt_ubase_t)thread->sp > (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size) @@ -94,7 +94,7 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } -#if defined(ARCH_CPU_STACK_GROWS_UPWARD) +#ifdef ARCH_CPU_STACK_GROWS_UPWARD else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) { rt_kprintf("warning: %s stack is close to the top of stack address.\n", @@ -106,9 +106,9 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name); } -#endif +#endif /* ARCH_CPU_STACK_GROWS_UPWARD */ } -#endif +#endif /* RT_USING_OVERFLOW_CHECK */ /* * get the highest priority thread in ready queue @@ -129,7 +129,7 @@ static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio) #else highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* get highest ready priority thread */ if (highest_ready_priority < local_highest_ready_priority) @@ -162,7 +162,7 @@ static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio) highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* get highest ready priority thread */ highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, @@ -173,7 +173,7 @@ static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio) return highest_priority_thread; } -#endif +#endif /* RT_USING_SMP */ /** * @ingroup SystemInit @@ -183,12 +183,12 @@ void rt_system_scheduler_init(void) { #ifdef RT_USING_SMP int cpu; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ register rt_base_t offset; #ifndef RT_USING_SMP rt_scheduler_lock_nest = 0; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n", RT_THREAD_PRIORITY_MAX)); @@ -213,9 +213,9 @@ void rt_system_scheduler_init(void) #if RT_THREAD_PRIORITY_MAX > 32 rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table)); -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /* initialize ready priority group */ rt_thread_ready_priority_group = 0; @@ -223,7 +223,7 @@ void rt_system_scheduler_init(void) #if RT_THREAD_PRIORITY_MAX > 32 /* initialize ready table */ rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* initialize thread defunct */ rt_list_init(&rt_thread_defunct); @@ -245,7 +245,7 @@ void rt_system_scheduler_start(void) to_thread->oncpu = rt_hw_cpu_id(); #else rt_current_thread = to_thread; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ rt_schedule_remove_thread(to_thread); to_thread->stat = RT_THREAD_RUNNING; @@ -255,7 +255,7 @@ void rt_system_scheduler_start(void) rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread); #else rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp); -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /* never come back */ } @@ -319,7 +319,7 @@ void rt_schedule(void) rt_thread_resume(current_thread); } } -#endif +#endif /* RT_USING_SIGNALS */ if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */ { @@ -367,7 +367,7 @@ void rt_schedule(void) #ifdef RT_USING_OVERFLOW_CHECK _rt_scheduler_stack_check(to_thread); -#endif +#endif /* RT_USING_OVERFLOW_CHECK */ rt_hw_context_switch((rt_ubase_t)¤t_thread->sp, (rt_ubase_t)&to_thread->sp, to_thread); @@ -396,7 +396,7 @@ void rt_schedule(void) { rt_hw_interrupt_enable(level); } -#endif +#endif /* RT_USING_SIGNALS */ __exit: return ; @@ -472,7 +472,7 @@ void rt_schedule(void) #ifdef RT_USING_OVERFLOW_CHECK _rt_scheduler_stack_check(to_thread); -#endif +#endif /* RT_USING_OVERFLOW_CHECK */ if (rt_interrupt_nest == 0) { @@ -502,7 +502,7 @@ void rt_schedule(void) { rt_hw_interrupt_enable(level); } -#endif +#endif /* RT_USING_SIGNALS */ goto __exit; } else @@ -527,7 +527,7 @@ void rt_schedule(void) __exit: return; } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /** * This function checks if a scheduling is needed after IRQ context. If yes, @@ -559,7 +559,7 @@ void rt_scheduler_do_irq_switch(void *context) rt_thread_resume(current_thread); } } -#endif +#endif /* RT_USING_SIGNALS */ if (pcpu->irq_switch_flag == 0) { @@ -608,7 +608,7 @@ void rt_scheduler_do_irq_switch(void *context) #ifdef RT_USING_OVERFLOW_CHECK _rt_scheduler_stack_check(to_thread); -#endif +#endif /* RT_USING_OVERFLOW_CHECK */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); current_thread->cpus_lock_nest--; @@ -621,7 +621,7 @@ void rt_scheduler_do_irq_switch(void *context) } rt_hw_interrupt_enable(level); } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /* * This function will insert a thread to system ready queue. The state of @@ -661,7 +661,7 @@ void rt_schedule_insert_thread(struct rt_thread *thread) { #if RT_THREAD_PRIORITY_MAX > 32 rt_thread_ready_table[thread->number] |= thread->high_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ rt_thread_ready_priority_group |= thread->number_mask; rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), @@ -675,7 +675,7 @@ void rt_schedule_insert_thread(struct rt_thread *thread) #if RT_THREAD_PRIORITY_MAX > 32 pcpu->ready_table[thread->number] |= thread->high_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ pcpu->priority_group |= thread->number_mask; rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]), @@ -724,14 +724,14 @@ void rt_schedule_insert_thread(struct rt_thread *thread) /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 rt_thread_ready_table[thread->number] |= thread->high_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ rt_thread_ready_priority_group |= thread->number_mask; __exit: /* enable interrupt */ rt_hw_interrupt_enable(temp); } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /* * This function will remove a thread from system ready queue. @@ -768,7 +768,7 @@ void rt_schedule_remove_thread(struct rt_thread *thread) } #else rt_thread_ready_priority_group &= ~thread->number_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ } } else @@ -785,7 +785,7 @@ void rt_schedule_remove_thread(struct rt_thread *thread) } #else pcpu->priority_group &= ~thread->number_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ } } @@ -818,13 +818,13 @@ void rt_schedule_remove_thread(struct rt_thread *thread) } #else rt_thread_ready_priority_group &= ~thread->number_mask; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ } /* enable interrupt */ rt_hw_interrupt_enable(level); } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /** * This function will lock the thread scheduler. @@ -885,7 +885,7 @@ void rt_enter_critical(void) /* enable interrupt */ rt_hw_interrupt_enable(level); } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ RTM_EXPORT(rt_enter_critical); /** @@ -959,7 +959,7 @@ void rt_exit_critical(void) rt_hw_interrupt_enable(level); } } -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ RTM_EXPORT(rt_exit_critical); /** @@ -975,7 +975,7 @@ rt_uint16_t rt_critical_level(void) return current_thread->critical_lock_nest; #else return rt_scheduler_lock_nest; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ } RTM_EXPORT(rt_critical_level); diff --git a/src/signal.c b/src/signal.c index c2c3e975cb..bb7706d9ef 100644 --- a/src/signal.c +++ b/src/signal.c @@ -20,7 +20,7 @@ #ifndef RT_SIG_INFO_MAX #define RT_SIG_INFO_MAX 32 -#endif +#endif /* RT_SIG_INFO_MAX */ #define DBG_TAG "SIGN" #define DBG_LVL DBG_WARNING @@ -67,7 +67,7 @@ static void _signal_entry(void *parameter) /* return to thread */ tid->sp = tid->sig_ret; tid->sig_ret = RT_NULL; -#endif +#endif /* RT_USING_SMP */ LOG_D("switch back to: 0x%08x\n", tid->sp); tid->stat &= ~RT_THREAD_STAT_SIGNAL; @@ -76,7 +76,7 @@ static void _signal_entry(void *parameter) rt_hw_context_switch_to((rt_base_t)¶meter, tid); #else rt_hw_context_switch_to((rt_ubase_t)&(tid->sp)); -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ } /* @@ -153,7 +153,7 @@ static void _signal_deliver(rt_thread_t tid) tid->sig_ret = tid->sp; tid->sp = rt_hw_stack_init((void *)_signal_entry, RT_NULL, (void *)((char *)tid->sig_ret - 32), RT_NULL); -#endif +#endif /* RT_USING_SMP */ rt_hw_interrupt_enable(level); LOG_D("signal stack pointer @ 0x%08x", tid->sp); @@ -204,7 +204,7 @@ void *rt_signal_check(void* context) rt_hw_interrupt_enable(level); return context; } -#endif +#endif /* RT_USING_SMP */ rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler) { @@ -586,4 +586,4 @@ int rt_system_signal_init(void) return 0; } -#endif +#endif /* RT_USING_SIGNALS */ diff --git a/src/slab.c b/src/slab.c index 114259002f..bec826ef18 100644 --- a/src/slab.c +++ b/src/slab.c @@ -61,7 +61,7 @@ /* some statistical variable */ #ifdef RT_MEM_STATS static rt_size_t used_mem, max_mem; -#endif +#endif /* RT_MEM_STATS */ #ifdef RT_USING_HOOK static void (*rt_malloc_hook)(void *ptr, rt_size_t size); @@ -99,7 +99,7 @@ RTM_EXPORT(rt_free_sethook); /**@}*/ -#endif +#endif /* RT_USING_HOOK */ /* * slab allocator implementation @@ -516,7 +516,7 @@ void *rt_malloc(rt_size_t size) used_mem += size; if (used_mem > max_mem) max_mem = used_mem; -#endif +#endif /* RT_MEM_STATS */ goto done; } @@ -571,7 +571,7 @@ void *rt_malloc(rt_size_t size) used_mem += z->z_chunksize; if (used_mem > max_mem) max_mem = used_mem; -#endif +#endif /* RT_MEM_STATS */ goto done; } @@ -655,7 +655,7 @@ void *rt_malloc(rt_size_t size) used_mem += z->z_chunksize; if (used_mem > max_mem) max_mem = used_mem; -#endif +#endif /* RT_MEM_STATS */ } done: @@ -790,7 +790,7 @@ void rt_free(void *ptr) (rt_ubase_t)addr, ((rt_ubase_t)(addr) - heap_start) >> RT_MM_PAGE_BITS)); } -#endif +#endif /* RT_DEBUG_SLAB */ kup = btokup((rt_ubase_t)ptr & ~RT_MM_PAGE_MASK); /* release large allocation */ @@ -806,7 +806,7 @@ void rt_free(void *ptr) #ifdef RT_MEM_STATS used_mem -= size * RT_MM_PAGE_SIZE; -#endif +#endif /* RT_MEM_STATS */ rt_sem_release(&heap_sem); RT_DEBUG_LOG(RT_DEBUG_SLAB, @@ -833,7 +833,7 @@ void rt_free(void *ptr) #ifdef RT_MEM_STATS used_mem -= z->z_chunksize; -#endif +#endif /* RT_MEM_STATS */ /* * Bump the number of free chunks. If it becomes non-zero the zone @@ -929,9 +929,9 @@ void list_mem(void) rt_kprintf("maximum allocated memory: %d\n", max_mem); } FINSH_FUNCTION_EXPORT(list_mem, list memory usage information) -#endif -#endif +#endif /* RT_USING_FINSH */ +#endif /* RT_MEM_STATS */ /**@}*/ -#endif +#endif /* defined (RT_USING_HEAP) && defined (RT_USING_SLAB) */ diff --git a/src/thread.c b/src/thread.c index 6304a4ebe6..c0a8f0ee74 100644 --- a/src/thread.c +++ b/src/thread.c @@ -76,7 +76,7 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) rt_thread_inited_hook = hook; } -#endif +#endif /* RT_USING_HOOK */ /* must be invoke witch rt_hw_interrupt_disable */ static void _rt_thread_cleanup_execute(rt_thread_t thread) @@ -84,7 +84,7 @@ static void _rt_thread_cleanup_execute(rt_thread_t thread) register rt_base_t level; #ifdef RT_USING_MODULE struct rt_dlmodule *module = RT_NULL; -#endif +#endif /* RT_USING_MODULE */ level = rt_hw_interrupt_disable(); #ifdef RT_USING_MODULE module = (struct rt_dlmodule*)thread->module_id; @@ -92,14 +92,14 @@ static void _rt_thread_cleanup_execute(rt_thread_t thread) { dlmodule_destroy(module); } -#endif +#endif /* RT_USING_MODULE */ /* invoke thread cleanup */ if (thread->cleanup != RT_NULL) thread->cleanup(thread); #ifdef RT_USING_SIGNALS rt_thread_free_sig(thread); -#endif +#endif /* RT_USING_SIGNALS */ rt_hw_interrupt_enable(level); } @@ -170,7 +170,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), (void *)_rt_thread_exit); -#endif +#endif /* ARCH_CPU_STACK_GROWS_UPWARD */ /* priority init */ RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); @@ -181,7 +181,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, #if RT_THREAD_PRIORITY_MAX > 32 thread->number = 0; thread->high_mask = 0; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* tick init */ thread->init_tick = tick; @@ -200,7 +200,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->scheduler_lock_nest = 0; thread->cpus_lock_nest = 0; thread->critical_lock_nest = 0; -#endif /*RT_USING_SMP*/ +#endif /* RT_USING_SMP */ /* initialize cleanup function and user data */ thread->cleanup = 0; @@ -221,14 +221,14 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, #ifndef RT_USING_SMP thread->sig_ret = RT_NULL; -#endif +#endif /* RT_USING_SMP */ thread->sig_vectors = RT_NULL; thread->si_list = RT_NULL; -#endif +#endif /* RT_USING_SIGNALS */ #ifdef RT_USING_LWP thread->lwp = RT_NULL; -#endif +#endif /* RT_USING_LWP */ RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); @@ -302,7 +302,7 @@ rt_thread_t rt_thread_self(void) extern rt_thread_t rt_current_thread; return rt_current_thread; -#endif +#endif /* RT_USING_SMP */ } RTM_EXPORT(rt_thread_self); @@ -330,7 +330,7 @@ rt_err_t rt_thread_startup(rt_thread_t thread) thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1L << thread->current_priority; -#endif +#endif /* RT_THREAD_PRIORITY_MAX > 32 */ RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority)); @@ -492,7 +492,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread) return RT_EOK; } RTM_EXPORT(rt_thread_delete); -#endif +#endif /* RT_USING_HEAP */ /** * This function will let current thread yield processor, and scheduler will @@ -690,7 +690,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1 << thread->current_priority; - #endif + #endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* insert thread to schedule queue again */ rt_schedule_insert_thread(thread); @@ -706,7 +706,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1 << thread->current_priority; - #endif + #endif /* RT_THREAD_PRIORITY_MAX > 32 */ } /* enable interrupt */ @@ -732,7 +732,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) { rt_err = rt_thread_delete(thread); } - #endif + #endif /* RT_USING_HEAP */ rt_schedule(); return rt_err; } @@ -752,7 +752,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu; break; } - #endif /*RT_USING_SMP*/ + #endif /* RT_USING_SMP */ default: break; diff --git a/src/timer.c b/src/timer.c index 9b0db6c6a5..1c8e70fccc 100644 --- a/src/timer.c +++ b/src/timer.c @@ -31,11 +31,11 @@ static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL]; #ifndef RT_TIMER_THREAD_STACK_SIZE #define RT_TIMER_THREAD_STACK_SIZE 512 -#endif +#endif /* RT_TIMER_THREAD_STACK_SIZE */ #ifndef RT_TIMER_THREAD_PRIO #define RT_TIMER_THREAD_PRIO 0 -#endif +#endif /* RT_TIMER_THREAD_PRIO */ /* soft timer status */ static rt_uint8_t soft_timer_status = RT_SOFT_TIMER_IDLE; @@ -44,7 +44,7 @@ static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL]; static struct rt_thread timer_thread; ALIGN(RT_ALIGN_SIZE) static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE]; -#endif +#endif /* RT_USING_TIMER_SOFT */ #ifdef RT_USING_HOOK extern void (*rt_object_take_hook)(struct rt_object *object); @@ -81,7 +81,7 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer)) } /**@}*/ -#endif +#endif /* RT_USING_HOOK */ static void _rt_timer_init(rt_timer_t timer, void (*timeout)(void *parameter), @@ -171,7 +171,7 @@ void rt_timer_dump(rt_list_t timer_heads[]) } rt_kprintf("\n"); } -#endif +#endif /* RT_DEBUG_TIMER */ /** * @addtogroup Clock @@ -303,7 +303,7 @@ rt_err_t rt_timer_delete(rt_timer_t timer) return RT_EOK; } RTM_EXPORT(rt_timer_delete); -#endif +#endif /* RT_USING_HEAP */ /** * This function will start the timer @@ -348,7 +348,7 @@ rt_err_t rt_timer_start(rt_timer_t timer) timer_list = rt_soft_timer_list; } else -#endif +#endif /* RT_USING_TIMER_SOFT */ { /* insert timer to system timer list */ timer_list = rt_timer_list; @@ -422,7 +422,7 @@ rt_err_t rt_timer_start(rt_timer_t timer) rt_schedule(); } } -#endif +#endif /* RT_USING_TIMER_SOFT */ return RT_EOK; } @@ -718,7 +718,7 @@ static void rt_thread_timer_entry(void *parameter) rt_soft_timer_check(); } } -#endif +#endif /* RT_USING_TIMER_SOFT */ /** * @ingroup SystemInit @@ -764,7 +764,7 @@ void rt_system_timer_thread_init(void) /* startup */ rt_thread_startup(&timer_thread); -#endif +#endif /* RT_USING_TIMER_SOFT */ } /**@}*/ -- cgit v1.3.1 From 05bb1a50b0d26b7c60c82ccb4ff8a66a501feac3 Mon Sep 17 00:00:00 2001 From: fenghuijie Date: Sat, 3 Jul 2021 17:54:58 +0800 Subject: 添加idle回收资源的修改 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtthread.h | 6 ++- src/Kconfig | 5 ++ src/idle.c | 154 +++++++++++++++++++++++++++++++++++++++++++++-------- src/scheduler.c | 5 -- src/thread.c | 40 ++++++++------ 5 files changed, 168 insertions(+), 42 deletions(-) (limited to 'src/thread.c') diff --git a/include/rtthread.h b/include/rtthread.h index 0583ce6434..f9d0b45e86 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -168,7 +168,6 @@ void rt_thread_idle_init(void); rt_err_t rt_thread_idle_sethook(void (*hook)(void)); rt_err_t rt_thread_idle_delhook(void (*hook)(void)); #endif -void rt_thread_idle_excute(void); rt_thread_t rt_thread_idle_gethandler(void); /* @@ -187,6 +186,7 @@ rt_uint16_t rt_critical_level(void); #ifdef RT_USING_HOOK void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to)); +void rt_scheduler_switchto_sethook(void (*hook)(struct rt_thread *tid)); #endif #ifdef RT_USING_SMP @@ -396,6 +396,10 @@ rt_err_t rt_mq_recv(rt_mq_t mq, rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg); #endif +/* defunct */ +void rt_thread_defunct_enqueue(rt_thread_t thread); +rt_thread_t rt_thread_defunct_dequeue(void); + /* * spinlock */ diff --git a/src/Kconfig b/src/Kconfig index 9a49146dff..e0582f5663 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -98,6 +98,11 @@ config IDLE_THREAD_STACK_SIZE int "The stack size of idle thread" default 256 +config SYSTEM_THREAD_STACK_SIZE + int "The stack size of system thread (for defunct etc.)" + depends on RT_USING_SMP + default IDLE_THREAD_STACK_SIZE + config RT_USING_TIMER_SOFT bool "Enable software timer with a timer thread" default y diff --git a/src/idle.c b/src/idle.c index db256a6010..a571e487cf 100644 --- a/src/idle.c +++ b/src/idle.c @@ -44,12 +44,22 @@ #define _CPUS_NR 1 #endif /* RT_USING_SMP */ -extern rt_list_t rt_thread_defunct; +static rt_list_t _rt_thread_defunct = RT_LIST_OBJECT_INIT(_rt_thread_defunct);; static struct rt_thread idle[_CPUS_NR]; ALIGN(RT_ALIGN_SIZE) static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE]; +#ifdef RT_USING_SMP +#ifndef SYSTEM_THREAD_STACK_SIZE +#define SYSTEM_THREAD_STACK_SIZE IDLE_THREAD_STACK_SIZE +#endif +static struct rt_thread rt_system_thread; +ALIGN(RT_ALIGN_SIZE) +static rt_uint8_t rt_system_stack[SYSTEM_THREAD_STACK_SIZE]; +static struct rt_semaphore system_sem; +#endif + #ifdef RT_USING_IDLE_HOOK #ifndef RT_IDLE_HOOK_LIST_SIZE #define RT_IDLE_HOOK_LIST_SIZE 4 @@ -127,43 +137,75 @@ rt_err_t rt_thread_idle_delhook(void (*hook)(void)) #endif /* RT_USING_IDLE_HOOK */ -#ifdef RT_USING_HEAP +#ifdef RT_USING_MODULE /* Return whether there is defunctional thread to be deleted. */ rt_inline int _has_defunct_thread(void) { /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)". - * So the compiler has a good reason that the rt_thread_defunct list does - * not change within rt_thread_idle_excute thus optimize the "while" loop + * So the compiler has a good reason that the _rt_thread_defunct list does + * not change within rt_thread_defunct_exceute thus optimize the "while" loop * into a "if". * * So add the volatile qualifier here. */ - const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct; + const volatile rt_list_t *l = (const volatile rt_list_t *)&_rt_thread_defunct; return l->next != l; } -#endif /* RT_USING_HEAP */ +#endif /* RT_USING_MODULE */ + +/* enqueue a thread to defunct queue + * it must be called between rt_hw_interrupt_disable and rt_hw_interrupt_enable + */ +void rt_thread_defunct_enqueue(rt_thread_t thread) +{ + rt_list_insert_after(&_rt_thread_defunct, &thread->tlist); +#ifdef RT_USING_SMP + rt_sem_release(&system_sem); +#endif +} + +/* dequeue a thread from defunct queue + * it must be called between rt_hw_interrupt_disable and rt_hw_interrupt_enable + */ +rt_thread_t rt_thread_defunct_dequeue(void) +{ + rt_thread_t thread = RT_NULL; + rt_list_t *l = &_rt_thread_defunct; + + if (l->next != l) + { + thread = rt_list_entry(l->next, + struct rt_thread, + tlist); + rt_list_remove(&(thread->tlist)); + } + return thread; +} /** * @ingroup Thread * * This function will perform system background job when system idle. */ -void rt_thread_idle_excute(void) +static void rt_defunct_execute(void) { - /* Loop until there is no dead thread. So one call to rt_thread_idle_excute + /* Loop until there is no dead thread. So one call to rt_defunct_execute * will do all the cleanups. */ - /* disable interrupt */ - - RT_DEBUG_NOT_IN_INTERRUPT; - -#ifdef RT_USING_HEAP while (1) { rt_base_t lock; rt_thread_t thread; + void (*cleanup)(struct rt_thread *tid); +#ifdef RT_USING_MODULE + struct rt_dlmodule *module = RT_NULL; +#endif + RT_DEBUG_NOT_IN_INTERRUPT; + + /* disable interrupt */ lock = rt_hw_interrupt_disable(); +#ifdef RT_USING_MODULE /* check whether list is empty */ if (!_has_defunct_thread()) { @@ -171,18 +213,56 @@ void rt_thread_idle_excute(void) break; } /* get defunct thread */ - thread = rt_list_entry(rt_thread_defunct.next, + thread = rt_list_entry(_rt_thread_defunct.next, struct rt_thread, tlist); + module = (struct rt_dlmodule*)thread->module_id; + if (module) + { + dlmodule_destroy(module); + } /* remove defunct thread */ rt_list_remove(&(thread->tlist)); - /* release thread's stack */ - RT_KERNEL_FREE(thread->stack_addr); - /* delete thread object */ - rt_object_delete((rt_object_t)thread); - rt_hw_interrupt_enable(lock); +#else + thread = rt_thread_defunct_dequeue(); + if (!thread) + { + rt_hw_interrupt_enable(lock); + break; + } +#endif + /* invoke thread cleanup */ + cleanup = thread->cleanup; + if (cleanup != RT_NULL) + { + rt_hw_interrupt_enable(lock); + cleanup(thread); + lock = rt_hw_interrupt_disable(); + } + +#ifdef RT_USING_SIGNALS + rt_thread_free_sig(thread); +#endif + + /* if it's a system object, not delete it */ + if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) + { + /* detach this object */ + rt_object_detach((rt_object_t)thread); + /* enable interrupt */ + rt_hw_interrupt_enable(lock); + } + else + { + rt_hw_interrupt_enable(lock); +#ifdef RT_USING_HEAP + /* release thread's stack */ + RT_KERNEL_FREE(thread->stack_addr); + /* delete thread object */ + rt_object_delete((rt_object_t)thread); +#endif + } } -#endif /* RT_USING_HEAP */ } extern void rt_system_power_manager(void); @@ -214,13 +294,27 @@ static void rt_thread_idle_entry(void *parameter) } #endif /* RT_USING_IDLE_HOOK */ - rt_thread_idle_excute(); +#ifndef RT_USING_SMP + rt_defunct_execute(); +#endif /* RT_USING_SMP */ + #ifdef RT_USING_PM rt_system_power_manager(); #endif /* RT_USING_PM */ } } +#ifdef RT_USING_SMP +static void rt_thread_system_entry(void *parameter) +{ + while (1) + { + rt_sem_take(&system_sem, RT_WAITING_FOREVER); + rt_defunct_execute(); + } +} +#endif + /** * @ingroup SystemInit * @@ -250,6 +344,24 @@ void rt_thread_idle_init(void) /* startup */ rt_thread_startup(&idle[i]); } + +#ifdef RT_USING_SMP + RT_ASSERT(RT_THREAD_PRIORITY_MAX > 2); + + rt_sem_init(&system_sem, "defunct", 1, RT_IPC_FLAG_FIFO); + + /* create defunct thread */ + rt_thread_init(&rt_system_thread, + "tsystem", + rt_thread_system_entry, + RT_NULL, + rt_system_stack, + sizeof(rt_system_stack), + RT_THREAD_PRIORITY_MAX - 2, + 32); + /* startup */ + rt_thread_startup(&rt_system_thread); +#endif } /** diff --git a/src/scheduler.c b/src/scheduler.c index eec660a8aa..956d95d7cc 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -47,8 +47,6 @@ struct rt_thread *rt_current_thread = RT_NULL; rt_uint8_t rt_current_priority; #endif /* RT_USING_SMP */ -rt_list_t rt_thread_defunct; - #ifdef RT_USING_HOOK static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); @@ -224,9 +222,6 @@ void rt_system_scheduler_init(void) /* initialize ready table */ rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - - /* initialize thread defunct */ - rt_list_init(&rt_thread_defunct); } /** diff --git a/src/thread.c b/src/thread.c index c0a8f0ee74..88cc7b61ba 100644 --- a/src/thread.c +++ b/src/thread.c @@ -32,8 +32,6 @@ #include #include -extern rt_list_t rt_thread_defunct; - #ifdef RT_USING_HOOK static void (*rt_thread_suspend_hook)(rt_thread_t thread); static void (*rt_thread_resume_hook) (rt_thread_t thread); @@ -131,7 +129,7 @@ static void _rt_thread_exit(void) else { /* insert to defunct thread list */ - rt_list_insert_after(&rt_thread_defunct, &(thread->tlist)); + rt_thread_defunct_enqueue(thread); } /* switch to next task */ @@ -379,17 +377,22 @@ rt_err_t rt_thread_detach(rt_thread_t thread) /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); - /* disable interrupt */ - lock = rt_hw_interrupt_disable(); - /* change stat */ thread->stat = RT_THREAD_CLOSE; - /* detach thread object */ - rt_object_detach((rt_object_t)thread); - - /* enable interrupt */ - rt_hw_interrupt_enable(lock); + if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) + { + rt_object_detach((rt_object_t)thread); + } + else + { + /* disable interrupt */ + lock = rt_hw_interrupt_disable(); + /* insert to defunct thread list */ + rt_thread_defunct_enqueue(thread); + /* enable interrupt */ + rt_hw_interrupt_enable(lock); + } return RT_EOK; } @@ -484,7 +487,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread) thread->stat = RT_THREAD_CLOSE; /* insert to defunct thread list */ - rt_list_insert_after(&rt_thread_defunct, &(thread->tlist)); + rt_thread_defunct_enqueue(thread); /* enable interrupt */ rt_hw_interrupt_enable(lock); @@ -847,12 +850,12 @@ rt_err_t rt_thread_resume(rt_thread_t thread) rt_timer_stop(&thread->thread_timer); - /* enable interrupt */ - rt_hw_interrupt_enable(temp); - /* insert to schedule ready list */ rt_schedule_insert_thread(thread); + /* enable interrupt */ + rt_hw_interrupt_enable(temp); + RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread)); return RT_EOK; } @@ -867,6 +870,7 @@ RTM_EXPORT(rt_thread_resume); void rt_thread_timeout(void *parameter) { struct rt_thread *thread; + register rt_base_t temp; thread = (struct rt_thread *)parameter; @@ -875,6 +879,9 @@ void rt_thread_timeout(void *parameter) RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + /* set error number */ thread->error = -RT_ETIMEOUT; @@ -884,6 +891,9 @@ void rt_thread_timeout(void *parameter) /* insert to schedule ready list */ rt_schedule_insert_thread(thread); + /* enable interrupt */ + rt_hw_interrupt_enable(temp); + /* do schedule */ rt_schedule(); } -- cgit v1.3.1 From 0015af02e4f08ab47f2d2fefcb7c1aca1682bfda Mon Sep 17 00:00:00 2001 From: fenghuijie Date: Mon, 5 Jul 2021 18:33:22 +0800 Subject: 调整代码,以支持cpu usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 4 ++++ include/rtthread.h | 1 + libcpu/arm/cortex-a/interrupt.c | 14 ++++++++++++++ libcpu/arm/cortex-a/start_gcc.S | 2 ++ src/irq.c | 2 +- src/scheduler.c | 13 +++++++++++++ src/thread.c | 4 ++++ 7 files changed, 39 insertions(+), 1 deletion(-) (limited to 'src/thread.c') diff --git a/include/rtdef.h b/include/rtdef.h index 2eb2cc1963..e1a64a433a 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -663,6 +663,10 @@ struct rt_thread rt_ubase_t init_tick; /**< thread's initialized tick */ rt_ubase_t remaining_tick; /**< remaining tick */ +#ifdef RT_USING_CPU_USAGE + rt_uint64_t duration_tick; /**< cpu usage tick */ +#endif + struct rt_timer thread_timer; /**< built-in thread timer */ void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */ diff --git a/include/rtthread.h b/include/rtthread.h index f2f4091b8a..cf47a48efe 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -186,6 +186,7 @@ rt_uint16_t rt_critical_level(void); #ifdef RT_USING_HOOK void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to)); +void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)); #endif #ifdef RT_USING_SMP diff --git a/libcpu/arm/cortex-a/interrupt.c b/libcpu/arm/cortex-a/interrupt.c index 474a23fee1..f90f1c0094 100644 --- a/libcpu/arm/cortex-a/interrupt.c +++ b/libcpu/arm/cortex-a/interrupt.c @@ -27,6 +27,20 @@ struct rt_irq_desc isr_table[MAX_HANDLERS]; rt_uint32_t rt_interrupt_from_thread = 0; rt_uint32_t rt_interrupt_to_thread = 0; rt_uint32_t rt_thread_switch_interrupt_flag = 0; + +#ifdef RT_USING_HOOK +static void (*rt_interrupt_switch_hook)(void); + +void rt_interrupt_switch_sethook(void (*hook)(void)) +{ + rt_interrupt_switch_hook = hook; +} +#endif + +void rt_interrupt_hook(void) +{ + RT_OBJECT_HOOK_CALL(rt_interrupt_switch_hook, ()); +} #endif const unsigned int VECTOR_BASE = 0x00; diff --git a/libcpu/arm/cortex-a/start_gcc.S b/libcpu/arm/cortex-a/start_gcc.S index 6c5dd81785..908468476e 100644 --- a/libcpu/arm/cortex-a/start_gcc.S +++ b/libcpu/arm/cortex-a/start_gcc.S @@ -341,6 +341,8 @@ rt_hw_context_switch_interrupt_do: ldr r6, [r6] ldr sp, [r6] @ get new task's stack pointer + bl rt_interrupt_hook + #ifdef RT_USING_FPU /* fpu context */ ldmfd sp!, {r6} diff --git a/src/irq.c b/src/irq.c index e89b8504dc..6ddcb3dd4a 100644 --- a/src/irq.c +++ b/src/irq.c @@ -89,8 +89,8 @@ void rt_interrupt_leave(void) rt_interrupt_nest)); level = rt_hw_interrupt_disable(); - rt_interrupt_nest --; RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,()); + rt_interrupt_nest --; rt_hw_interrupt_enable(level); } RTM_EXPORT(rt_interrupt_leave); diff --git a/src/scheduler.c b/src/scheduler.c index 956d95d7cc..353fdad5cb 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -49,6 +49,7 @@ rt_uint8_t rt_current_priority; #ifdef RT_USING_HOOK static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); +static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); /** * @addtogroup Hook @@ -68,6 +69,12 @@ rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) rt_scheduler_hook = hook; } +void +rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid)) +{ + rt_scheduler_switch_hook = hook; +} + /**@}*/ #endif /* RT_USING_HOOK */ @@ -364,6 +371,8 @@ void rt_schedule(void) _rt_scheduler_stack_check(to_thread); #endif /* RT_USING_OVERFLOW_CHECK */ + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread)); + rt_hw_context_switch((rt_ubase_t)¤t_thread->sp, (rt_ubase_t)&to_thread->sp, to_thread); } @@ -473,6 +482,8 @@ void rt_schedule(void) { extern void rt_thread_handle_sig(rt_bool_t clean_state); + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread)); + rt_hw_context_switch((rt_ubase_t)&from_thread->sp, (rt_ubase_t)&to_thread->sp); @@ -609,6 +620,8 @@ void rt_scheduler_do_irq_switch(void *context) current_thread->cpus_lock_nest--; current_thread->scheduler_lock_nest--; + RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread)); + rt_hw_context_switch_interrupt(context, (rt_ubase_t)¤t_thread->sp, (rt_ubase_t)&to_thread->sp, to_thread); } diff --git a/src/thread.c b/src/thread.c index 88cc7b61ba..60e6ab91d1 100644 --- a/src/thread.c +++ b/src/thread.c @@ -228,6 +228,10 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->lwp = RT_NULL; #endif /* RT_USING_LWP */ +#ifdef RT_USING_CPU_USAGE + thread->duration_tick = 0; +#endif + RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); return RT_EOK; -- cgit v1.3.1 From 67f2f323a2d7bfa7ba8efbd291691c12269088a2 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 6 Aug 2021 02:56:42 +0800 Subject: [kernel] 规范内部函数命名 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cpu.c | 18 +++++++++--------- src/object.c | 9 +++++---- src/signal.c | 8 ++++---- src/thread.c | 48 ++++++++++++++++++++++++------------------------ 4 files changed, 42 insertions(+), 41 deletions(-) (limited to 'src/thread.c') diff --git a/src/cpu.c b/src/cpu.c index a7d56ba999..fc1fed5a06 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -11,13 +11,13 @@ #include #ifdef RT_USING_SMP -static struct rt_cpu rt_cpus[RT_CPUS_NR]; +static struct rt_cpu _cpus[RT_CPUS_NR]; rt_hw_spinlock_t _cpus_lock; /* * disable scheduler */ -static void rt_preempt_disable(void) +static void _cpu_preempt_disable(void) { register rt_base_t level; struct rt_thread *current_thread; @@ -42,7 +42,7 @@ static void rt_preempt_disable(void) /* * enable scheduler */ -static void rt_preempt_enable(void) +static void _cpu_preempt_enable(void) { register rt_base_t level; struct rt_thread *current_thread; @@ -73,7 +73,7 @@ RTM_EXPORT(rt_spin_lock_init) void rt_spin_lock(struct rt_spinlock *lock) { - rt_preempt_disable(); + _cpu_preempt_disable(); rt_hw_spin_lock(&lock->lock); } RTM_EXPORT(rt_spin_lock) @@ -81,7 +81,7 @@ RTM_EXPORT(rt_spin_lock) void rt_spin_unlock(struct rt_spinlock *lock) { rt_hw_spin_unlock(&lock->lock); - rt_preempt_enable(); + _cpu_preempt_enable(); } RTM_EXPORT(rt_spin_unlock) @@ -89,7 +89,7 @@ rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock) { unsigned long level; - rt_preempt_disable(); + _cpu_preempt_disable(); level = rt_hw_local_irq_disable(); rt_hw_spin_lock(&lock->lock); @@ -103,7 +103,7 @@ void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level) rt_hw_spin_unlock(&lock->lock); rt_hw_local_irq_enable(level); - rt_preempt_enable(); + _cpu_preempt_enable(); } RTM_EXPORT(rt_spin_unlock_irqrestore) @@ -112,12 +112,12 @@ RTM_EXPORT(rt_spin_unlock_irqrestore) */ struct rt_cpu *rt_cpu_self(void) { - return &rt_cpus[rt_hw_cpu_id()]; + return &_cpus[rt_hw_cpu_id()]; } struct rt_cpu *rt_cpu_index(int index) { - return &rt_cpus[index]; + return &_cpus[index]; } /** diff --git a/src/object.c b/src/object.c index ff96037c52..718b0aa5b2 100644 --- a/src/object.c +++ b/src/object.c @@ -23,7 +23,7 @@ #endif /* RT_USING_MODULE */ /* - * define object_info for the number of rt_object_container items. + * define object_info for the number of _object_container items. */ enum rt_object_info_type { @@ -60,8 +60,9 @@ enum rt_object_info_type }; #define _OBJ_CONTAINER_LIST_INIT(c) \ - {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)} -static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] = + {&(_object_container[c].object_list), &(_object_container[c].object_list)} + +static struct rt_object_information _object_container[RT_Object_Info_Unknown] = { /* initialize object container - thread */ {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)}, @@ -211,7 +212,7 @@ rt_object_get_information(enum rt_object_class_type type) int index; for (index = 0; index < RT_Object_Info_Unknown; index ++) - if (rt_object_container[index].type == type) return &rt_object_container[index]; + if (_object_container[index].type == type) return &_object_container[index]; return RT_NULL; } diff --git a/src/signal.c b/src/signal.c index bb7706d9ef..c5ae2a709a 100644 --- a/src/signal.c +++ b/src/signal.c @@ -35,7 +35,7 @@ struct siginfo_node struct rt_slist_node list; }; -static struct rt_mempool *_rt_siginfo_pool; +static struct rt_mempool *_siginfo_pool; static void _signal_deliver(rt_thread_t tid); void rt_thread_handle_sig(rt_bool_t clean_state); @@ -538,7 +538,7 @@ int rt_thread_kill(rt_thread_t tid, int sig) } rt_hw_interrupt_enable(level); - si_node = (struct siginfo_node *) rt_mp_alloc(_rt_siginfo_pool, 0); + si_node = (struct siginfo_node *) rt_mp_alloc(_siginfo_pool, 0); if (si_node) { rt_slist_init(&(si_node->list)); @@ -576,8 +576,8 @@ int rt_thread_kill(rt_thread_t tid, int sig) int rt_system_signal_init(void) { - _rt_siginfo_pool = rt_mp_create("signal", RT_SIG_INFO_MAX, sizeof(struct siginfo_node)); - if (_rt_siginfo_pool == RT_NULL) + _siginfo_pool = rt_mp_create("signal", RT_SIG_INFO_MAX, sizeof(struct siginfo_node)); + if (_siginfo_pool == RT_NULL) { LOG_E("create memory pool for signal info failed."); RT_ASSERT(0); diff --git a/src/thread.c b/src/thread.c index 60e6ab91d1..7e0c10bfef 100644 --- a/src/thread.c +++ b/src/thread.c @@ -77,7 +77,7 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) #endif /* RT_USING_HOOK */ /* must be invoke witch rt_hw_interrupt_disable */ -static void _rt_thread_cleanup_execute(rt_thread_t thread) +static void _thread_cleanup_execute(rt_thread_t thread) { register rt_base_t level; #ifdef RT_USING_MODULE @@ -101,7 +101,7 @@ static void _rt_thread_cleanup_execute(rt_thread_t thread) rt_hw_interrupt_enable(level); } -static void _rt_thread_exit(void) +static void _thread_exit(void) { struct rt_thread *thread; register rt_base_t level; @@ -112,7 +112,7 @@ static void _rt_thread_exit(void) /* disable interrupt */ level = rt_hw_interrupt_disable(); - _rt_thread_cleanup_execute(thread); + _thread_cleanup_execute(thread); /* remove from schedule */ rt_schedule_remove_thread(thread); @@ -139,14 +139,14 @@ static void _rt_thread_exit(void) rt_hw_interrupt_enable(level); } -static rt_err_t _rt_thread_init(struct rt_thread *thread, - const char *name, - void (*entry)(void *parameter), - void *parameter, - void *stack_start, - rt_uint32_t stack_size, - rt_uint8_t priority, - rt_uint32_t tick) +static rt_err_t _thread_init(struct rt_thread *thread, + const char *name, + void (*entry)(void *parameter), + void *parameter, + void *stack_start, + rt_uint32_t stack_size, + rt_uint8_t priority, + rt_uint32_t tick) { /* init thread list */ rt_list_init(&(thread->tlist)); @@ -163,11 +163,11 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, #ifdef ARCH_CPU_STACK_GROWS_UPWARD thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (void *)((char *)thread->stack_addr), - (void *)_rt_thread_exit); + (void *)_thread_exit); #else thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), - (void *)_rt_thread_exit); + (void *)_thread_exit); #endif /* ARCH_CPU_STACK_GROWS_UPWARD */ /* priority init */ @@ -274,14 +274,14 @@ rt_err_t rt_thread_init(struct rt_thread *thread, /* initialize thread object */ rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name); - return _rt_thread_init(thread, - name, - entry, - parameter, - stack_start, - stack_size, - priority, - tick); + return _thread_init(thread, + name, + entry, + parameter, + stack_start, + stack_size, + priority, + tick); } RTM_EXPORT(rt_thread_init); @@ -376,7 +376,7 @@ rt_err_t rt_thread_detach(rt_thread_t thread) rt_schedule_remove_thread(thread); } - _rt_thread_cleanup_execute(thread); + _thread_cleanup_execute(thread); /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); @@ -440,7 +440,7 @@ rt_thread_t rt_thread_create(const char *name, return RT_NULL; } - _rt_thread_init(thread, + _thread_init(thread, name, entry, parameter, @@ -479,7 +479,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread) rt_schedule_remove_thread(thread); } - _rt_thread_cleanup_execute(thread); + _thread_cleanup_execute(thread); /* release thread timer */ rt_timer_detach(&(thread->thread_timer)); -- cgit v1.3.1 From 48c7a455e69c007cd166f3732bd5aa5971e31702 Mon Sep 17 00:00:00 2001 From: liukangcc Date: Fri, 10 Sep 2021 15:32:06 +0800 Subject: [update] src/thread.c function description. --- src/thread.c | 205 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 117 insertions(+), 88 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 7e0c10bfef..3cf9e015cb 100644 --- a/src/thread.c +++ b/src/thread.c @@ -38,12 +38,11 @@ static void (*rt_thread_resume_hook) (rt_thread_t thread); static void (*rt_thread_inited_hook) (rt_thread_t thread); /** - * @ingroup Hook - * This function sets a hook function when the system suspend a thread. + * @brief This function sets a hook function when the system suspend a thread. * - * @param hook the specified hook function + * @note the hook function must be simple and never be blocked or suspend. * - * @note the hook function must be simple and never be blocked or suspend. + * @param hook the specified hook function. */ void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)) { @@ -51,12 +50,11 @@ void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)) } /** - * @ingroup Hook - * This function sets a hook function when the system resume a thread. + * @brief This function sets a hook function when the system resume a thread. * - * @param hook the specified hook function + * @note the hook function must be simple and never be blocked or suspend. * - * @note the hook function must be simple and never be blocked or suspend. + * @param hook the specified hook function. */ void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)) { @@ -64,10 +62,9 @@ void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)) } /** - * @ingroup Hook - * This function sets a hook function when a thread is initialized. + * @brief This function sets a hook function when a thread is initialized. * - * @param hook the specified hook function + * @param hook the specified hook function. */ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) { @@ -76,7 +73,6 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) #endif /* RT_USING_HOOK */ -/* must be invoke witch rt_hw_interrupt_disable */ static void _thread_cleanup_execute(rt_thread_t thread) { register rt_base_t level; @@ -244,19 +240,27 @@ static rt_err_t _thread_init(struct rt_thread *thread, /**@{*/ /** - * This function will initialize a thread, normally it's used to initialize a - * static thread object. - * - * @param thread the static thread object - * @param name the name of thread, which shall be unique - * @param entry the entry function of thread - * @param parameter the parameter of thread enter function - * @param stack_start the start address of thread stack - * @param stack_size the size of thread stack - * @param priority the priority of thread - * @param tick the time slice if there are same priority thread - * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @brief This function will initialize a thread, normally it's used to initialize a + * static thread object. + * + * @param thread the static thread object. + * + * @param name the name of thread, which shall be unique. + * + * @param entry the entry function of thread. + * + * @param parameter the parameter of thread enter function. + * + * @param stack_start the start address of thread stack. + * + * @param stack_size the size of thread stack. + * + * @param priority the priority of thread. + * + * @param tick the time slice if there are same priority thread. + * + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_init(struct rt_thread *thread, const char *name, @@ -286,9 +290,9 @@ rt_err_t rt_thread_init(struct rt_thread *thread, RTM_EXPORT(rt_thread_init); /** - * This function will return self thread object + * @brief This function will return self thread object. * - * @return the self thread object + * @return the self thread object. */ rt_thread_t rt_thread_self(void) { @@ -309,11 +313,12 @@ rt_thread_t rt_thread_self(void) RTM_EXPORT(rt_thread_self); /** - * This function will start a thread and put it to system ready queue + * @brief This function will start a thread and put it to system ready queue. * - * @param thread the thread to be started + * @param thread the thread to be started. * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_startup(rt_thread_t thread) { @@ -351,12 +356,13 @@ rt_err_t rt_thread_startup(rt_thread_t thread) RTM_EXPORT(rt_thread_startup); /** - * This function will detach a thread. The thread object will be removed from - * thread queue and detached/deleted from system object management. + * @brief This function will detach a thread. The thread object will be removed from + * thread queue and detached/deleted from system object management. * - * @param thread the thread to be deleted + * @param thread the thread to be deleted. * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_detach(rt_thread_t thread) { @@ -404,17 +410,22 @@ RTM_EXPORT(rt_thread_detach); #ifdef RT_USING_HEAP /** - * This function will create a thread object and allocate thread object memory - * and stack. + * @brief This function will create a thread object and allocate thread object memory. + * and stack. + * + * @param name the name of thread, which shall be unique. + * + * @param entry the entry function of thread. * - * @param name the name of thread, which shall be unique - * @param entry the entry function of thread - * @param parameter the parameter of thread enter function - * @param stack_size the size of thread stack - * @param priority the priority of thread - * @param tick the time slice if there are same priority thread + * @param parameter the parameter of thread enter function. * - * @return the created thread object + * @param stack_size the size of thread stack. + * + * @param priority the priority of thread. + * + * @param tick the time slice if there are same priority thread. + * + * @return the created thread object. */ rt_thread_t rt_thread_create(const char *name, void (*entry)(void *parameter), @@ -454,12 +465,13 @@ rt_thread_t rt_thread_create(const char *name, RTM_EXPORT(rt_thread_create); /** - * This function will delete a thread. The thread object will be removed from - * thread queue and deleted from system object management in the idle thread. + * @brief This function will delete a thread. The thread object will be removed from + * thread queue and deleted from system object management in the idle thread. * - * @param thread the thread to be deleted + * @param thread the thread to be deleted. * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delete(rt_thread_t thread) { @@ -502,11 +514,12 @@ RTM_EXPORT(rt_thread_delete); #endif /* RT_USING_HEAP */ /** - * This function will let current thread yield processor, and scheduler will - * choose a highest thread to run. After yield processor, the current thread - * is still in READY state. + * @brief This function will let current thread yield processor, and scheduler will + * choose a highest thread to run. After yield processor, the current thread + * is still in READY state. * - * @return RT_EOK + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_yield(void) { @@ -525,11 +538,13 @@ rt_err_t rt_thread_yield(void) RTM_EXPORT(rt_thread_yield); /** - * This function will let current thread sleep for some ticks. + * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend, + * when the thread timer reaches the tick value, scheduler will awaken this thread. * - * @param tick the sleep ticks + * @param tick the sleep ticks. * - * @return RT_EOK + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_sleep(rt_tick_t tick) { @@ -564,11 +579,12 @@ rt_err_t rt_thread_sleep(rt_tick_t tick) } /** - * This function will let current thread delay for some ticks. + * @brief This function will let current thread delay for some ticks. * - * @param tick the delay ticks + * @param tick the delay ticks. * - * @return RT_EOK + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delay(rt_tick_t tick) { @@ -577,12 +593,14 @@ rt_err_t rt_thread_delay(rt_tick_t tick) RTM_EXPORT(rt_thread_delay); /** - * This function will let current thread delay until (*tick + inc_tick). + * @brief This function will let current thread delay until (*tick + inc_tick). * - * @param tick the tick of last wakeup. - * @param inc_tick the increment tick + * @param tick the tick of last wakeup. * - * @return RT_EOK + * @param inc_tick the increment tick. + * + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) { @@ -637,11 +655,12 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) RTM_EXPORT(rt_thread_delay_until); /** - * This function will let current thread delay for some milliseconds. + * @brief This function will let current thread delay for some milliseconds. * - * @param ms the delay ms time + * @param ms the delay ms time. * - * @return RT_EOK + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_mdelay(rt_int32_t ms) { @@ -654,17 +673,24 @@ rt_err_t rt_thread_mdelay(rt_int32_t ms) RTM_EXPORT(rt_thread_mdelay); /** - * This function will control thread behaviors according to control command. + * @brief This function will control thread behaviors according to control command. + * + * @param thread the specified thread to be controlled. + * + * @param cmd the control command, which includes. * - * @param thread the specified thread to be controlled - * @param cmd the control command, which includes - * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread; - * RT_THREAD_CTRL_STARTUP for starting a thread; - * RT_THREAD_CTRL_CLOSE for delete a thread; - * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. - * @param arg the argument of control command + * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread. * - * @return RT_EOK + * RT_THREAD_CTRL_STARTUP for starting a thread. + * + * RT_THREAD_CTRL_CLOSE for delete a thread. + * + * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. + * + * @param arg the argument of control command. + * + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) { @@ -770,14 +796,15 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) RTM_EXPORT(rt_thread_control); /** - * This function will suspend the specified thread. + * @brief This function will suspend the specified thread and change it to suspend state. * - * @param thread the thread to be suspended + * @note if suspend self thread, after this function call, the + * rt_schedule() must be invoked. * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @param thread the thread to be suspended. * - * @note if suspend self thread, after this function call, the - * rt_schedule() must be invoked. + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_suspend(rt_thread_t thread) { @@ -822,11 +849,12 @@ rt_err_t rt_thread_suspend(rt_thread_t thread) RTM_EXPORT(rt_thread_suspend); /** - * This function will resume a thread and put it to system ready queue. + * @brief This function will resume a thread and put it to system ready queue. * - * @param thread the thread to be resumed + * @param thread the thread to be resumed. * - * @return the operation status, RT_EOK on OK, -RT_ERROR on error + * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. + * When the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_resume(rt_thread_t thread) { @@ -866,10 +894,10 @@ rt_err_t rt_thread_resume(rt_thread_t thread) RTM_EXPORT(rt_thread_resume); /** - * This function is the timeout function for thread, normally which is invoked - * when thread is timeout to wait some resource. + * @brief This function is the timeout function for thread, normally which is invoked + * when thread is timeout to wait some resource. * - * @param parameter the parameter of thread timeout function + * @param parameter the parameter of thread timeout function */ void rt_thread_timeout(void *parameter) { @@ -904,18 +932,19 @@ void rt_thread_timeout(void *parameter) RTM_EXPORT(rt_thread_timeout); /** - * This function will find the specified thread. + * @brief This function will find the specified thread. * - * @param name the name of thread finding + * @note please don't invoke this function in interrupt status. * - * @return the found thread + * @param name the name of thread finding. * - * @note please don't invoke this function in interrupt status. + * @return the found thread. */ rt_thread_t rt_thread_find(char *name) { return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread); } + RTM_EXPORT(rt_thread_find); /**@}*/ -- cgit v1.3.1 From 202b23371d6ea028648fa820c4977c7d12217f35 Mon Sep 17 00:00:00 2001 From: liukangcc Date: Sat, 11 Sep 2021 13:37:37 +0800 Subject: [update] thread.c api annotation. --- src/thread.c | 138 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 68 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index 3cf9e015cb..0c7ae4beaa 100644 --- a/src/thread.c +++ b/src/thread.c @@ -40,9 +40,9 @@ static void (*rt_thread_inited_hook) (rt_thread_t thread); /** * @brief This function sets a hook function when the system suspend a thread. * - * @note the hook function must be simple and never be blocked or suspend. + * @note The hook function must be simple and never be blocked or suspend. * - * @param hook the specified hook function. + * @param hook is the specified hook function. */ void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)) { @@ -52,9 +52,9 @@ void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)) /** * @brief This function sets a hook function when the system resume a thread. * - * @note the hook function must be simple and never be blocked or suspend. + * @note The hook function must be simple and never be blocked or suspend. * - * @param hook the specified hook function. + * @param hook is the specified hook function. */ void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)) { @@ -64,7 +64,7 @@ void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)) /** * @brief This function sets a hook function when a thread is initialized. * - * @param hook the specified hook function. + * @param hook is the specified hook function. */ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) { @@ -240,27 +240,27 @@ static rt_err_t _thread_init(struct rt_thread *thread, /**@{*/ /** - * @brief This function will initialize a thread, normally it's used to initialize a + * @brief This function will initialize a thread. It's used to initialize a * static thread object. * - * @param thread the static thread object. + * @param thread is the static thread object. * - * @param name the name of thread, which shall be unique. + * @param name is the name of thread, which shall be unique. * - * @param entry the entry function of thread. + * @param entry is the entry function of thread. * - * @param parameter the parameter of thread enter function. + * @param parameter is the parameter of thread enter function. * - * @param stack_start the start address of thread stack. + * @param stack_start is the start address of thread stack. * - * @param stack_size the size of thread stack. + * @param stack_size is the size of thread stack. * - * @param priority the priority of thread. + * @param priority is the priority of thread. * - * @param tick the time slice if there are same priority thread. + * @param tick is the time slice if there are same priority thread. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_init(struct rt_thread *thread, const char *name, @@ -292,7 +292,7 @@ RTM_EXPORT(rt_thread_init); /** * @brief This function will return self thread object. * - * @return the self thread object. + * @return The self thread object. */ rt_thread_t rt_thread_self(void) { @@ -315,10 +315,10 @@ RTM_EXPORT(rt_thread_self); /** * @brief This function will start a thread and put it to system ready queue. * - * @param thread the thread to be started. + * @param thread is the thread to be started. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_startup(rt_thread_t thread) { @@ -357,12 +357,12 @@ RTM_EXPORT(rt_thread_startup); /** * @brief This function will detach a thread. The thread object will be removed from - * thread queue and detached/deleted from system object management. + * thread queue and detached/deleted from the system object management. * - * @param thread the thread to be deleted. + * @param thread is the thread to be deleted. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_detach(rt_thread_t thread) { @@ -413,19 +413,20 @@ RTM_EXPORT(rt_thread_detach); * @brief This function will create a thread object and allocate thread object memory. * and stack. * - * @param name the name of thread, which shall be unique. + * @param name is the name of thread, which shall be unique. * - * @param entry the entry function of thread. + * @param entry is the entry function of thread. * - * @param parameter the parameter of thread enter function. + * @param parameter is the parameter of thread enter function. * - * @param stack_size the size of thread stack. + * @param stack_size is the size of thread stack. * - * @param priority the priority of thread. + * @param priority is the priority of thread. * - * @param tick the time slice if there are same priority thread. + * @param tick is the time slice if there are same priority thread. * - * @return the created thread object. + * @return If the return value is a rt_thread structure pointer, the function is successfully executed. + * If the return value is RT_NULL, it means this operation failed. */ rt_thread_t rt_thread_create(const char *name, void (*entry)(void *parameter), @@ -468,10 +469,10 @@ RTM_EXPORT(rt_thread_create); * @brief This function will delete a thread. The thread object will be removed from * thread queue and deleted from system object management in the idle thread. * - * @param thread the thread to be deleted. + * @param thread is the thread to be deleted. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delete(rt_thread_t thread) { @@ -514,12 +515,12 @@ RTM_EXPORT(rt_thread_delete); #endif /* RT_USING_HEAP */ /** - * @brief This function will let current thread yield processor, and scheduler will - * choose a highest thread to run. After yield processor, the current thread - * is still in READY state. + * @brief This function will let current thread yield processor, and scheduler will + * choose the highest thread to run. After yield processor, the current thread + * is still in READY state. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_yield(void) { @@ -541,10 +542,10 @@ RTM_EXPORT(rt_thread_yield); * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend, * when the thread timer reaches the tick value, scheduler will awaken this thread. * - * @param tick the sleep ticks. + * @param tick is the sleep ticks. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_sleep(rt_tick_t tick) { @@ -581,10 +582,10 @@ rt_err_t rt_thread_sleep(rt_tick_t tick) /** * @brief This function will let current thread delay for some ticks. * - * @param tick the delay ticks. + * @param tick is the delay ticks. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delay(rt_tick_t tick) { @@ -595,12 +596,12 @@ RTM_EXPORT(rt_thread_delay); /** * @brief This function will let current thread delay until (*tick + inc_tick). * - * @param tick the tick of last wakeup. + * @param tick is the tick of last wakeup. * - * @param inc_tick the increment tick. + * @param inc_tick is the increment tick. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick) { @@ -657,10 +658,10 @@ RTM_EXPORT(rt_thread_delay_until); /** * @brief This function will let current thread delay for some milliseconds. * - * @param ms the delay ms time. + * @param ms is the delay ms time. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_mdelay(rt_int32_t ms) { @@ -675,9 +676,9 @@ RTM_EXPORT(rt_thread_mdelay); /** * @brief This function will control thread behaviors according to control command. * - * @param thread the specified thread to be controlled. + * @param thread is the specified thread to be controlled. * - * @param cmd the control command, which includes. + * @param cmd is the control command, which includes. * * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread. * @@ -687,10 +688,10 @@ RTM_EXPORT(rt_thread_mdelay); * * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. * - * @param arg the argument of control command. + * @param arg is the argument of control command. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) { @@ -798,13 +799,13 @@ RTM_EXPORT(rt_thread_control); /** * @brief This function will suspend the specified thread and change it to suspend state. * - * @note if suspend self thread, after this function call, the + * @note If suspend self thread, after this function call, the * rt_schedule() must be invoked. * - * @param thread the thread to be suspended. + * @param thread is the thread to be suspended. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_suspend(rt_thread_t thread) { @@ -851,10 +852,10 @@ RTM_EXPORT(rt_thread_suspend); /** * @brief This function will resume a thread and put it to system ready queue. * - * @param thread the thread to be resumed. + * @param thread is the thread to be resumed. * - * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed. - * When the return value is any other values, it means this operation failed. + * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. + * If the return value is any other values, it means this operation failed. */ rt_err_t rt_thread_resume(rt_thread_t thread) { @@ -897,7 +898,7 @@ RTM_EXPORT(rt_thread_resume); * @brief This function is the timeout function for thread, normally which is invoked * when thread is timeout to wait some resource. * - * @param parameter the parameter of thread timeout function + * @param parameter is the parameter of thread timeout function */ void rt_thread_timeout(void *parameter) { @@ -934,11 +935,12 @@ RTM_EXPORT(rt_thread_timeout); /** * @brief This function will find the specified thread. * - * @note please don't invoke this function in interrupt status. + * @note Please don't invoke this function in interrupt status. * - * @param name the name of thread finding. + * @param name is the name of thread finding. * - * @return the found thread. + * @return If the return value is a rt_thread structure pointer, the function is successfully executed. + * If the return value is RT_NULL, it means this operation failed. */ rt_thread_t rt_thread_find(char *name) { -- cgit v1.3.1