module developing

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@666 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
qiuyiuestc 2010-04-23 14:23:23 +00:00
parent c4a21b4456
commit b87f6d562b
4 changed files with 45 additions and 12 deletions

View File

@ -155,6 +155,8 @@ void rtthread_startup(void)
#endif
#endif
rt_system_timer_thread_init();
/* init idle thread */
rt_thread_idle_init();

View File

@ -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
{

View File

@ -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
}

View File

@ -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)