diff --git a/bsp/mini2440/startup.c b/bsp/mini2440/startup.c index 4632ee9fcb1e25847dc29498a9c2e78866110cd7..466f6743d0784edd795d10371ab40c5120f2e81a 100644 --- a/bsp/mini2440/startup.c +++ b/bsp/mini2440/startup.c @@ -155,6 +155,8 @@ void rtthread_startup(void) #endif #endif + rt_system_timer_thread_init(); + /* init idle thread */ rt_thread_idle_init(); diff --git a/include/rtdef.h b/include/rtdef.h index 442f9ecd5db9f2f221bf0908de82dd77d38c152b..f6c1db41f6922877927eae0c5f1ba377be56f8e3 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -361,6 +361,10 @@ struct rt_thread }; /*@}*/ +/* module clean types */ +#define RT_MODULE_FLAG_AUTO_CLEAN 0x01 /* auto clean */ +#define RT_MODULE_FLAG_MANUAL_CLEAN 0x02 /* manual clean */ + #ifdef RT_USING_MODULE struct rt_module { diff --git a/src/idle.c b/src/idle.c index 2354a21aac347ac2f59cf12532b86c117fbe2f1b..bebd0f7c4808a1d5882eaec839b374f9a1562f52 100644 --- a/src/idle.c +++ b/src/idle.c @@ -65,7 +65,9 @@ static void rt_thread_idle_entry(void* parameter) if (!rt_list_isempty(&rt_thread_defunct)) { rt_base_t lock; - +#ifdef RT_USING_MODULE + rt_module_t module; +#endif struct rt_thread* thread = rt_list_entry(rt_thread_defunct.next, struct rt_thread, tlist); /* disable interrupt */ @@ -79,13 +81,33 @@ static void rt_thread_idle_entry(void* parameter) /* release thread's stack */ rt_free(thread->stack_addr); + rt_kprintf("thread %s was deleted\n", thread->name); + #ifdef RT_USING_MODULE - /* release thread point in module */ - if(thread->module_parent != RT_NULL) - thread->module_parent->module_thread = RT_NULL; -#endif + module = thread->module_parent; + /* delete thread object */ rt_object_delete((rt_object_t)thread); + + if(module != RT_NULL) + { + /* if the thread is module's main thread */ + if(module->module_thread == thread) + { + /* detach module's main thread */ + module->module_thread = RT_NULL; + } + + /* if sub thread list and main thread are null */ + if((module->module_thread == RT_NULL) && + rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) && + (module->parent.flag & RT_MODULE_FLAG_AUTO_CLEAN)) + { + /* unload module */ + rt_module_unload(module); + } + } +#endif } #endif } diff --git a/src/module.c b/src/module.c index af980541cba456b825c9da3fb0ccdef24216bb19..b29ee764d117120cd11c2f61e673b07ad642e61c 100644 --- a/src/module.c +++ b/src/module.c @@ -19,7 +19,7 @@ #include "module.h" #include "kservice.h" -/* #define RT_MODULE_DEBUG */ +/* #define RT_MODULE_DEBUG */ #define elf_module ((Elf32_Ehdr *)module_ptr) #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff)) @@ -174,8 +174,8 @@ static void rt_module_init_object_container(struct rt_module* module) struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) { - rt_uint32_t index; - rt_uint32_t module_addr = 0, module_size = 0, rodata_addr = 0; + rt_uint32_t index, rodata_addr = 0, bss_addr = 0; + rt_uint32_t module_addr = 0, module_size = 0; struct rt_module* module = RT_NULL; rt_uint8_t *ptr, *strtab, *shstrab; @@ -262,6 +262,7 @@ struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) /* load bss section */ if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index])) { + bss_addr = (rt_uint32_t)ptr; rt_memset(ptr, 0, shdr[index].sh_size); } } @@ -298,7 +299,7 @@ struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) { if(ELF_ST_TYPE(sym->st_info) == STT_SECTION) { - if (strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_RODATA, 8) == 0) + if (rt_strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_RODATA, 8) == 0) { /* relocate rodata section */ rt_module_arm_relocate(module, rel, @@ -308,7 +309,7 @@ struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) else if(strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_BSS, 5) == 0) { /* relocate bss section */ - rt_module_arm_relocate(module, rel, (Elf32_Addr)ptr, module_addr); + rt_module_arm_relocate(module, rel, (Elf32_Addr)bss_addr, module_addr); } } else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC ) @@ -328,11 +329,12 @@ struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) } else { + Elf32_Addr addr; #ifdef RT_MODULE_DEBUG rt_kprintf("unresolved relocate symbol: %s\n", strtab + sym->st_name); #endif /* need to resolve symbol in kernel symbol table */ - Elf32_Addr addr = rt_module_symbol_find(strtab + sym->st_name); + addr = rt_module_symbol_find(strtab + sym->st_name); if (addr != (Elf32_Addr)RT_NULL) rt_module_arm_relocate(module, rel, addr, module_addr); else rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name); @@ -346,6 +348,9 @@ struct rt_module* rt_module_load(void* module_ptr, const rt_uint8_t* name) /* init module object container */ rt_module_init_object_container(module); + /* set module defalut clean type */ + module->parent.flag |= RT_MODULE_FLAG_AUTO_CLEAN; + /* create module main thread */ module->module_thread = rt_thread_create((const char*)name, module->module_entry, RT_NULL, @@ -380,7 +385,7 @@ void rt_module_unload(struct rt_module* module) /* release module memory */ rt_free(module->module_space); - rt_free((void *)module); + rt_object_delete((rt_object *)module); } rt_module_t rt_module_find(char* name)