From 4e2a6cc36179c1b5ffae707f9485e56c964b8988 Mon Sep 17 00:00:00 2001 From: bernard Date: Mon, 21 Jul 2014 06:28:43 +0800 Subject: [Kernel] The rt_object_find routine can support to find an object inside a module. The usage is: rt_object_find("module_name/object_name"). --- src/object.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/object.c b/src/object.c index ed3173a0f5..32dc0f2384 100644 --- a/src/object.c +++ b/src/object.c @@ -403,22 +403,77 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type) { struct rt_object *object; struct rt_list_node *node; - struct rt_object_information *information; - extern volatile rt_uint8_t rt_interrupt_nest; + struct rt_object_information *information = RT_NULL; /* parameter check */ if ((name == RT_NULL) || (type > RT_Object_Class_Unknown)) return RT_NULL; /* which is invoke in interrupt status */ - if (rt_interrupt_nest != 0) - RT_ASSERT(0); + RT_DEBUG_NOT_IN_INTERRUPT; + +#ifdef RT_USING_MODULE + /* check whether to find a object inside a module. */ + { + const char *name_ptr; + int module_name_length; + + name_ptr = name; + while ((*name_ptr != '\0') && (*name_ptr != '/')) + name_ptr ++; + + if (*name_ptr == '/') + { + struct rt_module* module = RT_NULL; + + /* get the name length of module */ + module_name_length = name_ptr - name; + + /* enter critical */ + rt_enter_critical(); + + /* find module */ + information = &rt_object_container[RT_Object_Class_Module]; + for (node = information->object_list.next; + node != &(information->object_list); + node = node->next) + { + object = rt_list_entry(node, struct rt_object, list); + if ((rt_strncmp(object->name, name, module_name_length) == 0) && + module_name_length == RT_NAME_MAX || object->name[module_name_length] == '\0') + { + /* get module */ + module = (struct rt_module*)object; + break; + } + } + rt_exit_critical(); + + /* there is no this module inside the system */ + if (module == RT_NULL) return RT_NULL; + + /* get the object pool of module */ + information = &(module->module_object[type]); + + /* get object name */ + while ((*name_ptr == '/') && (*name_ptr != '\0')) name_ptr ++; + if (*name_ptr == '\0') + { + if (type == RT_Object_Class_Module) return object; + return RT_NULL; + } + + /* point to the object name */ + name = name_ptr; + } + } +#endif /* enter critical */ rt_enter_critical(); /* try to find object */ - information = &rt_object_container[type]; + if (information == RT_NULL) information = &rt_object_container[type]; for (node = information->object_list.next; node != &(information->object_list); node = node->next) -- cgit v1.3.1 From 33fed1b167e1b2a653d9e58853537e40628f4e2e Mon Sep 17 00:00:00 2001 From: bernard Date: Mon, 21 Jul 2014 06:29:15 +0800 Subject: [Kernel] Better documentation for doxygen. --- src/device.c | 2 +- src/module.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/device.c b/src/device.c index 2bb3cc0f9c..835a517ce3 100644 --- a/src/device.c +++ b/src/device.c @@ -80,7 +80,7 @@ RTM_EXPORT(rt_device_unregister); * * @return the error code, RT_EOK on successfully. * - * @deprecated This function is not needed because the initialization + * @deprecated since 1.2.x, this function is not needed because the initialization * of a device is performed when applicaiton opens it. */ rt_err_t rt_device_init_all(void) diff --git a/src/module.c b/src/module.c index 0603159b91..5d5d02c580 100644 --- a/src/module.c +++ b/src/module.c @@ -1120,8 +1120,8 @@ rt_module_t rt_module_open(const char *path) * This function will do a excutable program with main function and parameters. * * @param path the full path of application module - * @cmd_line the command line of program - * @size the size of command line of program + * @param cmd_line the command line of program + * @param size the size of command line of program * * @return the module object */ -- cgit v1.3.1 From b8bf6bef76bdabe76749c752217afbe5d24ba371 Mon Sep 17 00:00:00 2001 From: Grissiom Date: Wed, 20 Aug 2014 10:16:04 +0800 Subject: kernel: mempool: fix race condition in rt_mp_alloc When thread wake up from waiting for memory, there is a chance that there is no memory available in high pressure. So use a loop to check again. Otherwise, there will be a NULL reference. --- src/mempool.c | 97 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/mempool.c b/src/mempool.c index 05904bfbc7..b6d83597bf 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -323,23 +323,15 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time) rt_uint8_t *block_ptr; register rt_base_t level; struct rt_thread *thread; + rt_uint32_t before_sleep = 0; + + /* get current thread */ + thread = rt_thread_self(); /* disable interrupt */ level = rt_hw_interrupt_disable(); - if (mp->block_free_count) - { - /* memory block is available. decrease the free block counter */ - mp->block_free_count --; - - /* get block from block list */ - block_ptr = mp->block_list; - mp->block_list = *(rt_uint8_t **)block_ptr; - - /* point to memory pool */ - *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; - } - else + while (mp->block_free_count == 0) { /* memory block is unavailable. */ if (time == 0) @@ -347,54 +339,63 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time) /* enable interrupt */ rt_hw_interrupt_enable(level); + rt_set_errno(-RT_ETIMEOUT); + return RT_NULL; } - else - { - RT_DEBUG_NOT_IN_INTERRUPT; - /* get current thread */ - thread = rt_thread_self(); + RT_DEBUG_NOT_IN_INTERRUPT; - thread->error = RT_EOK; + thread->error = RT_EOK; - /* need suspend thread */ - rt_thread_suspend(thread); - rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); - mp->suspend_thread_count ++; + /* need suspend thread */ + rt_thread_suspend(thread); + rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); + mp->suspend_thread_count++; - if (time > 0) - { - /* init thread timer and start it */ - rt_timer_control(&(thread->thread_timer), - RT_TIMER_CTRL_SET_TIME, - &time); - rt_timer_start(&(thread->thread_timer)); - } + if (time > 0) + { + /* get the start tick of timer */ + before_sleep = rt_tick_get(); + + /* init thread timer and start it */ + rt_timer_control(&(thread->thread_timer), + RT_TIMER_CTRL_SET_TIME, + &time); + rt_timer_start(&(thread->thread_timer)); + } - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + /* do a schedule */ + rt_schedule(); - /* do a schedule */ - rt_schedule(); + if (thread->error != RT_EOK) + return RT_NULL; - if (thread->error != RT_EOK) - return RT_NULL; + if (time > 0) + { + time -= rt_tick_get() - before_sleep; + if (time < 0) + time = 0; + } + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + } - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* memory block is available. decrease the free block counter */ + mp->block_free_count--; - /* decrease free block */ - mp->block_free_count --; + /* get block from block list */ + block_ptr = mp->block_list; + RT_ASSERT(block_ptr != RT_NULL); - /* get block from block list */ - block_ptr = mp->block_list; - mp->block_list = *(rt_uint8_t **)block_ptr; + /* Setup the next free node. */ + mp->block_list = *(rt_uint8_t **)block_ptr; - /* point to memory pool */ - *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; - } - } + /* point to memory pool */ + *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; /* enable interrupt */ rt_hw_interrupt_enable(level); -- cgit v1.3.1 From c0629b342e683a1f8a237dfa9788e82493ef5cb3 Mon Sep 17 00:00:00 2001 From: tcz717 Date: Sun, 24 Aug 2014 23:46:31 +0800 Subject: fix:comment spelling mistake relation=>relocation --- src/module.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/module.h b/src/module.h index d2d4c4fcc8..3c45c96d02 100644 --- a/src/module.h +++ b/src/module.h @@ -239,7 +239,7 @@ typedef struct #define SHT_DYNAMIC 6 /* dynamic section */ #define SHT_NOTE 7 /* note section */ #define SHT_NOBITS 8 /* no space section */ -#define SHT_REL 9 /* relation section without addends */ +#define SHT_REL 9 /* relocation section without addends */ #define SHT_SHLIB 10 /* reserved - purpose unknown */ #define SHT_DYNSYM 11 /* dynamic symbol table section */ #define SHT_NUM 12 /* number of section types */ -- cgit v1.3.1 From d4133990b0736fb6b845d80ffe002f8e70881e9e Mon Sep 17 00:00:00 2001 From: Grissiom Date: Tue, 2 Sep 2014 17:21:31 +0800 Subject: kernel/idle: fix rt_thread_idle_excute in high optimization level 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 into a "if". So add the volatile qualifier when test against the rt_thread_defunc list. --- src/idle.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/idle.c b/src/idle.c index 7bca6c28cb..b00367bd5c 100644 --- a/src/idle.c +++ b/src/idle.c @@ -67,6 +67,20 @@ void rt_thread_idle_sethook(void (*hook)(void)) /*@}*/ #endif +/* 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 + * into a "if". + * + * So add the volatile qualifier here. */ + const volatile rt_list_t *l = (const volatile rt_list_t*)&rt_thread_defunct; + + return l->next != l; +} + /** * @ingroup Thread * @@ -76,7 +90,7 @@ void rt_thread_idle_excute(void) { /* Loop until there is no dead thread. So one call to rt_thread_idle_excute * will do all the cleanups. */ - while (!rt_list_isempty(&rt_thread_defunct)) + while (_has_defunct_thread()) { rt_base_t lock; rt_thread_t thread; @@ -89,7 +103,7 @@ void rt_thread_idle_excute(void) lock = rt_hw_interrupt_disable(); /* re-check whether list is empty */ - if (!rt_list_isempty(&rt_thread_defunct)) + if (_has_defunct_thread()) { /* get defunct thread */ thread = rt_list_entry(rt_thread_defunct.next, -- cgit v1.3.1 From ab786679c7040b4bdb7920f6b35b52045e830d8a Mon Sep 17 00:00:00 2001 From: bernard Date: Thu, 11 Sep 2014 12:50:41 +0800 Subject: [Kernel] comments clean up. --- src/idle.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/idle.c b/src/idle.c index 7bca6c28cb..ab0c992212 100644 --- a/src/idle.c +++ b/src/idle.c @@ -44,16 +44,12 @@ static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE]; extern rt_list_t rt_thread_defunct; #ifdef RT_USING_HOOK -/** - * @addtogroup Hook - */ - -/*@{*/ - static void (*rt_thread_idle_hook)(); /** - * This function will set a hook function to idle thread loop. + * @ingroup Hook + * This function sets a hook function to idle thread loop. When the system performs + * idle loop, this hook function should be invoked. * * @param hook the specified hook function * @@ -63,8 +59,6 @@ void rt_thread_idle_sethook(void (*hook)(void)) { rt_thread_idle_hook = hook; } - -/*@}*/ #endif /** @@ -180,7 +174,7 @@ static void rt_thread_idle_entry(void *parameter) } /** - * @ingroup SymstemInit + * @ingroup SystemInit * * This function will initialize idle thread, then start it. * -- cgit v1.3.1 From b23de95ff47298df2bea06346623fa90922bdc98 Mon Sep 17 00:00:00 2001 From: bernard Date: Thu, 11 Sep 2014 12:51:52 +0800 Subject: [Kernel] comments clean up. --- src/mem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mem.c b/src/mem.c index e8aae72489..004de01d94 100644 --- a/src/mem.c +++ b/src/mem.c @@ -176,10 +176,10 @@ static void plug_holes(struct heap_mem *mem) /** * @ingroup SystemInit * - * This function will init system heap + * This function will initialize system heap memory. * - * @param begin_addr the beginning address of system page - * @param end_addr the end address of system page + * @param begin_addr the beginning address of system heap memory. + * @param end_addr the end address of system heap memory. */ void rt_system_heap_init(void *begin_addr, void *end_addr) { -- cgit v1.3.1 From 1f3ea02c5ace84d848a03fb014a664bdf586a07d Mon Sep 17 00:00:00 2001 From: bernard Date: Thu, 11 Sep 2014 12:54:05 +0800 Subject: [Kernel] Use stream mode to open console device. --- src/kservice.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/kservice.c b/src/kservice.c index cb0064f38b..12878e67d1 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1073,7 +1073,7 @@ rt_device_t rt_console_set_device(const char *name) /* set new console device */ _console_device = new; - rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR); + rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM); } return old; -- cgit v1.3.1 From bd6b39cace67fcefccb105e0e6f7609973bbb7ce Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 19 Sep 2014 14:29:03 +0800 Subject: Update device.c --- src/device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/device.c b/src/device.c index 835a517ce3..957c9fc774 100644 --- a/src/device.c +++ b/src/device.c @@ -362,8 +362,8 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg) RTM_EXPORT(rt_device_control); /** - * This function will set the indication callback function when device receives - * data. + * This function will set the reception indication callback function. This callback function + * is invoked when this device receives data. * * @param dev the pointer of device driver structure * @param rx_ind the indication callback function -- cgit v1.3.1 From 8615e2c8e5a0964b871282582127e4a6aad763a5 Mon Sep 17 00:00:00 2001 From: Grissiom Date: Tue, 14 Oct 2014 16:00:42 +0800 Subject: kernel/timer: change row_lvl to unsigned Use signed int as array index is unsecure. --- src/timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/timer.c b/src/timer.c index f71e40fcd6..ba2801530d 100644 --- a/src/timer.c +++ b/src/timer.c @@ -293,7 +293,7 @@ RTM_EXPORT(rt_timer_delete); */ rt_err_t rt_timer_start(rt_timer_t timer) { - int row_lvl; + unsigned int row_lvl; rt_list_t *timer_list; register rt_base_t level; rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL]; -- cgit v1.3.1