remove rt_current_module and user can use rt_module_unload to remove a module.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2458 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2012-11-28 14:24:22 +00:00
parent 0f72824eda
commit ee11663d87
3 changed files with 209 additions and 166 deletions

View File

@ -132,6 +132,8 @@ void rt_thread_idle_excute(void)
#ifdef RT_USING_MODULE
if (module != RT_NULL)
{
extern rt_err_t rt_module_destroy(rt_module_t module);
/* if sub thread list and main thread are all empty */
if ((module->module_thread == RT_NULL) &&
rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) )
@ -139,9 +141,9 @@ void rt_thread_idle_excute(void)
module->nref --;
}
/* unload module */
/* destroy module */
if (module->nref == 0)
rt_module_unload(module);
rt_module_destroy(module);
}
#endif
}

View File

@ -15,14 +15,14 @@
* 2011-05-25 yi.qiu implement module hook function
* 2011-06-23 yi.qiu rewrite module memory allocator
* 2012-11-23 Bernard using RT_DEBUG_LOG instead of rt_kprintf.
* 2012-11-28 Bernard remove rt_current_module and user
* can use rt_module_unload to remove a module.
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtm.h>
#include "string.h"
#ifdef RT_USING_MODULE
#include "module.h"
@ -38,6 +38,7 @@
#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
#ifdef RT_USING_SLAB
#define PAGE_COUNT_MAX 256
/* module memory allocator */
@ -53,16 +54,14 @@ struct rt_page_info
rt_uint32_t npage;
};
#ifdef RT_USING_SLAB
static void *rt_module_malloc_page(rt_size_t npages);
static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t npages);
static struct rt_semaphore mod_sem;
#endif
static rt_module_t rt_current_module = RT_NULL;
static struct rt_semaphore mod_sem;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
rt_list_t rt_module_symbol_list;
/**
* @ingroup SystemInit
@ -85,13 +84,10 @@ void rt_system_module_init(void)
_rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
#endif
rt_list_init(&rt_module_symbol_list);
#ifdef RT_USING_SLAB
/* initialize heap semaphore */
rt_sem_init(&mod_sem, "module", 1, RT_IPC_FLAG_FIFO);
/* init current module */
rt_current_module = RT_NULL;
#endif
}
static rt_uint32_t rt_module_symbol_find(const char *sym_str)
@ -114,21 +110,13 @@ static rt_uint32_t rt_module_symbol_find(const char *sym_str)
*/
rt_module_t rt_module_self(void)
{
rt_thread_t tid;
tid = rt_thread_self();
if (tid == RT_NULL) return RT_NULL;
/* return current module */
return rt_current_module;
}
/**
* This function will set current module object
*
* @return RT_EOK
*/
rt_err_t rt_module_set(rt_module_t module)
{
/* set current module */
rt_current_module = module;
return RT_EOK;
return (rt_module_t)tid->module_id;
}
static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel,
@ -373,6 +361,7 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
module->module_space = rt_malloc(module_size);
if (module->module_space == RT_NULL)
{
rt_kprintf("Module: allocate space failed.\n");
rt_object_delete(&(module->parent));
return RT_NULL;
@ -558,6 +547,7 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
module->module_space = rt_malloc(module_size);
if (module->module_space == RT_NULL)
{
rt_kprintf("Module: allocate space failed.\n");
rt_object_delete(&(module->parent));
return RT_NULL;
@ -774,6 +764,9 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
if (elf_module->e_entry != 0)
{
rt_uint32_t *stack_size;
rt_uint8_t *priority;
#ifdef RT_USING_SLAB
/* init module memory allocator */
module->mem_list = RT_NULL;
@ -784,15 +777,18 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
module->page_cnt = 0;
#endif
/* create module thread */
/* get the main thread stack size */
module->stack_size = 2048;
module->thread_priority = RT_THREAD_PRIORITY_MAX - 2;
/* create module thread */
module->module_thread = rt_thread_create(name,
(void(*)(void *))module->module_entry, RT_NULL,
module->stack_size,
module->thread_priority, 10);
RT_DEBUG_LOG(RT_DEBUG_MODULE,("thread entry 0x%x\n", module->module_entry));
/* set module id */
module->module_thread->module_id = (void*)module;
module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
@ -815,12 +811,18 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
return module;
}
static char* module_name(const char *path)
{
char *first, *end, *name;
int size;
char *ptr = (char*)path;
#ifdef RT_USING_DFS
#include <dfs_posix.h>
static char* _module_name(const char *path)
{
const char *first, *end, *ptr;
char *name;
int size;
ptr = (char*)path;
first = ptr;
end = path + rt_strlen(path);
while(*ptr != '\0')
{
if(*ptr == '/') first = ptr + 1;
@ -837,8 +839,6 @@ static char* module_name(const char *path)
return name;
}
#ifdef RT_USING_DFS
#include <dfs_posix.h>
/**
* This function will load a module from a file
*
@ -903,7 +903,7 @@ rt_module_t rt_module_open(const char *path)
return RT_NULL;
}
name = module_name(path);
name = _module_name(path);
module = rt_module_load(name,(void *)buffer);
rt_free(buffer);
rt_free(name);
@ -913,18 +913,18 @@ rt_module_t rt_module_open(const char *path)
#if defined(RT_USING_FINSH)
#include <finsh.h>
FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file);
FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
#endif
#endif
/**
* This function will unload a module from memory and release resources
* This function will destroy a module and release its resource.
*
* @param module the module to be unloaded
* @param module the module to be destroyed.
*
* @return the operation status, RT_EOK on OK; -RT_ERROR on error
*/
rt_err_t rt_module_unload(rt_module_t module)
rt_err_t rt_module_destroy(rt_module_t module)
{
int i;
struct rt_object *object;
@ -934,36 +934,13 @@ rt_err_t rt_module_unload(rt_module_t module)
/* check parameter */
RT_ASSERT(module != RT_NULL);
RT_ASSERT(module->nref == 0);
RT_DEBUG_LOG(RT_DEBUG_MODULE,("rt_module_unload: %s\n", module->parent.name));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("rt_module_destroy: %8.*s\n", RT_NAME_MAX, module->parent.name));
/* module has entry point */
if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
{
/* suspend module main thread */
if (module->module_thread != RT_NULL)
{
if (module->module_thread->stat == RT_THREAD_READY)
rt_thread_suspend(module->module_thread);
}
/* delete threads */
list = &module->module_object[RT_Object_Class_Thread].object_list;
while (list->next != list)
{
object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_TRUE)
{
/* detach static object */
rt_thread_detach((rt_thread_t)object);
}
else
{
/* delete dynamic object */
rt_thread_delete((rt_thread_t)object);
}
}
#ifdef RT_USING_SEMAPHORE
/* delete semaphores */
list = &module->module_object[RT_Object_Class_Thread].object_list;
@ -1125,17 +1102,12 @@ rt_err_t rt_module_unload(rt_module_t module)
/* release module symbol table */
for (i=0; i<module->nsym; i++)
{
rt_free((void *)module->symtab[i].name);
}
if (module->symtab != RT_NULL)
rt_free(module->symtab);
#ifdef RT_USING_HOOK
if (rt_module_unload_hook != RT_NULL)
{
rt_module_unload_hook(module);
}
#endif
#ifdef RT_USING_SLAB
if(module->page_array != RT_NULL)
rt_free(module->page_array);
@ -1147,6 +1119,64 @@ rt_err_t rt_module_unload(rt_module_t module)
return RT_EOK;
}
/**
* This function will unload a module from memory and release resources
*
* @param module the module to be unloaded
*
* @return the operation status, RT_EOK on OK; -RT_ERROR on error
*/
rt_err_t rt_module_unload(rt_module_t module)
{
int i;
rt_err_t result;
struct rt_object *object;
struct rt_list_node *list;
RT_DEBUG_NOT_IN_INTERRUPT;
/* check parameter */
if (module == RT_NULL)
return -RT_ERROR;
rt_enter_critical();
if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
{
/* delete all sub-threads */
list = &module->module_object[RT_Object_Class_Thread].object_list;
while (list->next != list)
{
object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_TRUE)
{
/* detach static object */
rt_thread_detach((rt_thread_t)object);
}
else
{
/* delete dynamic object */
rt_thread_delete((rt_thread_t)object);
}
}
/* delete the main thread of module */
if (module->module_thread != RT_NULL)
{
rt_thread_delete(module->module_thread);
}
}
rt_exit_critical();
#ifdef RT_USING_HOOK
if (rt_module_unload_hook != RT_NULL)
{
rt_module_unload_hook(module);
}
#endif
return RT_EOK;
}
/**
* This function will find the specified module.
*
@ -1201,17 +1231,21 @@ static void *rt_module_malloc_page(rt_size_t npages)
{
void *chunk;
struct rt_page_info *page;
rt_module_t self_module;
self_module = rt_module_self();
RT_ASSERT(self_module != RT_NULL);
chunk = rt_page_alloc(npages);
if (chunk == RT_NULL)
return RT_NULL;
page = (struct rt_page_info *)rt_current_module->page_array;
page[rt_current_module->page_cnt].page_ptr = chunk;
page[rt_current_module->page_cnt].npage = npages;
rt_current_module->page_cnt ++;
page = (struct rt_page_info *)self_module->page_array;
page[self_module->page_cnt].page_ptr = chunk;
page[self_module->page_cnt].npage = npages;
self_module->page_cnt ++;
RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX);
RT_ASSERT(self_module->page_cnt <= PAGE_COUNT_MAX);
RT_DEBUG_LOG(RT_DEBUG_MODULE,"rt_module_malloc_page 0x%x %d\n", chunk, npages);
return chunk;
@ -1230,6 +1264,10 @@ static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t np
{
int i, index;
struct rt_page_info *page;
rt_module_t self_module;
self_module = rt_module_self();
RT_ASSERT(self_module != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_MODULE,"rt_module_free_page 0x%x %d\n", page_ptr, npages);
rt_page_free(page_ptr, npages);
@ -1259,7 +1297,7 @@ static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t np
}
else
RT_ASSERT(RT_FALSE);
rt_current_module->page_cnt--;
self_module->page_cnt--;
return;
}
@ -1278,6 +1316,10 @@ void *rt_module_malloc(rt_size_t size)
struct rt_mem_head **prev;
rt_uint32_t npage;
rt_size_t nunits;
rt_module_t self_module;
self_module = rt_module_self();
RT_ASSERT(self_module != RT_NULL);
RT_DEBUG_NOT_IN_INTERRUPT;
@ -1288,7 +1330,7 @@ void *rt_module_malloc(rt_size_t size)
rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list;
for (prev = (struct rt_mem_head **)&self_module->mem_list;
(b = *prev) != RT_NULL; prev = &(b->next))
{
if (b->size > nunits)
@ -1326,7 +1368,7 @@ void *rt_module_malloc(rt_size_t size)
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list;
for (prev = (struct rt_mem_head **)&self_module->mem_list;
(b = *prev) != RT_NULL; prev = &(b->next))
{
if (b > up + up->size)
@ -1483,6 +1525,10 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
{
struct rt_mem_head *b, *p, *prev, *tmpp;
rt_size_t nunits;
rt_module_t self_module;
self_module = rt_module_self();
RT_ASSERT(self_module != RT_NULL);
RT_DEBUG_NOT_IN_INTERRUPT;
@ -1490,7 +1536,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
return rt_module_malloc(size);
if (size == 0)
{
rt_module_free(rt_current_module, ptr);
rt_module_free(self_module, ptr);
return RT_NULL;
}
@ -1508,7 +1554,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
p = b + nunits;
p->size = b->size - nunits;
b->size = nunits;
rt_module_free(rt_current_module, (void *)(p + 1));
rt_module_free(self_module, (void *)(p + 1));
return (void *)(b + 1);
}
@ -1516,7 +1562,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
else
{
/* more space then required */
prev = (struct rt_mem_head *)rt_current_module->mem_list;
prev = (struct rt_mem_head *)self_module->mem_list;
for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next)
break;
@ -1543,7 +1589,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
b->size = nunits;
prev->next = p;
}
rt_current_module->mem_list = (void *)prev;
self_module->mem_list = (void *)prev;
return (void *)(b + 1);
}
@ -1551,7 +1597,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
{
if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL;
rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
rt_module_free(rt_current_module, (void *)(b + 1));
rt_module_free(self_module, (void *)(b + 1));
return (void *)(p);
}

View File

@ -266,11 +266,6 @@ void rt_schedule(void)
from_thread = rt_current_thread;
rt_current_thread = to_thread;
#ifdef RT_USING_MODULE
rt_module_set((rt_current_thread->module_id != RT_NULL) ?
(rt_module_t)rt_current_thread->module_id : RT_NULL);
#endif
RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
/* switch to new thread */