浏览代码

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
bernard.xiong@gmail.com 12 年之前
父节点
当前提交
ee11663d87
共有 3 个文件被更改,包括 208 次插入165 次删除
  1. 5 3
      src/idle.c
  2. 203 157
      src/module.c
  3. 0 5
      src/scheduler.c

+ 5 - 3
src/idle.c

@@ -131,7 +131,9 @@ 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
 	}

+ 203 - 157
src/module.c

@@ -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);
-#endif
 
-static rt_module_t rt_current_module = RT_NULL;
 static struct rt_semaphore mod_sem;
+#endif
+
 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
@@ -74,7 +73,7 @@ void rt_system_module_init(void)
 #ifdef __GNUC__
     extern int __rtmsymtab_start;
     extern int __rtmsymtab_end;
-    
+
     _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
     _rt_module_symtab_end   = (struct rt_module_symtab *)&__rtmsymtab_end;
 #elif defined (__CC_ARM)
@@ -82,16 +81,13 @@ void rt_system_module_init(void)
     extern int RTMSymTab$$Limit;
 
     _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
-    _rt_module_symtab_end   = (struct rt_module_symtab *)&RTMSymTab$$Limit;    
+    _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)
 {
-    /* return current module */
-    return rt_current_module;
-}
+	rt_thread_t tid;
 
-/**
- * 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;
+	tid = rt_thread_self();
+	if (tid == RT_NULL) return RT_NULL;
 
-    return RT_EOK;
+    /* return current module */
+    return (rt_module_t)tid->module_id;
 }
 
 static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel,
@@ -161,7 +149,7 @@ static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel,
         break;
     case R_ARM_REL32:
         *where += sym_val - (Elf32_Addr)where;
-        RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_REL32: %x -> %x, sym %x, offset %x\n", 
+        RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
             where, *where, sym_val, rel->r_offset));
         break;
     case R_ARM_V4BX:
@@ -207,7 +195,7 @@ static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel,
                  offset >= (rt_int32_t)0x01000000)
         {
             rt_kprintf("Module: Only Thumb addresses allowed\n");
-            
+
             return -1;
         }
 
@@ -349,30 +337,31 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
         /* rtmlinker finished */
         linked = RT_TRUE;
     }
-    
+
     /* get the ELF image size */
     for (index = 0; index < elf_module->e_phnum; index++)
     {
         if(phdr[index].p_type == PT_LOAD)
             module_size += phdr[index].p_memsz;
-    }    
-    
+    }
+
     if (module_size == 0)
     {
         rt_kprintf("Module: size error\n");
         return RT_NULL;
-    }    
+    }
 
     /* allocate module */
     module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, name);
     if (!module) return RT_NULL;
 
     module->nref = 0;
-    
+
     /* allocate module space */
     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;
@@ -386,15 +375,15 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
     {
         if (phdr[index].p_type == PT_LOAD)
         {
-            rt_memcpy(ptr + phdr[index].p_paddr, 
-                (rt_uint8_t *)elf_module + phdr[index].p_offset, 
+            rt_memcpy(ptr + phdr[index].p_paddr,
+                (rt_uint8_t *)elf_module + phdr[index].p_offset,
                 phdr[index].p_filesz);
-        }        
-    }    
+        }
+    }
 
     /* set module entry */
     module->module_entry = module->module_space + elf_module->e_entry;
-    
+
     /* handle relocation section */
     for (index = 0; index < elf_module->e_shnum; index ++)
     {
@@ -403,16 +392,16 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
         Elf32_Rel *rel;
         rt_uint8_t *strtab;
         static rt_bool_t unsolved = RT_FALSE;
-        
+
         if (!IS_REL(shdr[index])) continue;
 
         /* get relocate item */
         rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
 
         /* locate .rel.plt and .rel.dyn section */
-        symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + 
+        symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr +
             shdr[shdr[index].sh_link].sh_offset);
-        strtab = (rt_uint8_t*) module_ptr + 
+        strtab = (rt_uint8_t*) module_ptr +
             shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
         nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
 
@@ -421,15 +410,15 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
         {
             Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
 
-            RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n", 
+            RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
                 strtab + sym->st_name, sym->st_shndx));
 
-            if((sym->st_shndx != SHT_NULL) || 
+            if((sym->st_shndx != SHT_NULL) ||
                 (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
             {
-                rt_module_arm_relocate(module, rel,  
+                rt_module_arm_relocate(module, rel,
                     (Elf32_Addr)(module->module_space + sym->st_value));
-            }    
+            }
             else if(!linked)
             {
                 Elf32_Addr addr;
@@ -441,10 +430,10 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
                 addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
                 if (addr == 0)
                 {
-                    rt_kprintf("Module: can't find %s in kernel symbol table\n", 
+                    rt_kprintf("Module: can't find %s in kernel symbol table\n",
                         strtab + sym->st_name);
                     unsolved = RT_TRUE;
-                }    
+                }
                 else
                     rt_module_arm_relocate(module, rel, addr);
             }
@@ -456,14 +445,14 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
             rt_object_delete(&(module->parent));
 
             return RT_NULL;
-        }    
+        }
     }
 
     /* construct module symbol table */
     for (index = 0; index < elf_module->e_shnum; index ++)
-    {    
+    {
         /* find .dynsym section */
-        rt_uint8_t *shstrab; 
+        rt_uint8_t *shstrab;
         shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
         if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
             break;
@@ -481,7 +470,7 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
 
         for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
         {
-            if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && 
+            if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
                 (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
                 count ++;
         }
@@ -492,20 +481,20 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
         for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
         {
             rt_size_t length;
-            
-            if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) || 
+
+            if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
                 (ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC)) continue;
 
             length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
 
-            module->symtab[count].addr = 
+            module->symtab[count].addr =
                 (void *)(module->module_space + symtab[i].st_value);
             module->symtab[count].name = rt_malloc(length);
             rt_memset((void *)module->symtab[count].name, 0, length);
-            rt_memcpy((void *)module->symtab[count].name, 
+            rt_memcpy((void *)module->symtab[count].name,
                 strtab + symtab[i].st_name, length);
             count ++;
-        }    
+        }
     }
 
     return module;
@@ -549,7 +538,7 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         return RT_NULL;
 
     /* allocate module */
-    module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, 
+    module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
         (const char *)name);
     if (module == RT_NULL)
         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;
@@ -573,9 +563,9 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         /* load text section */
         if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
         {
-            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, 
+            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
                 shdr[index].sh_size);
-            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load text 0x%x, size %d\n", 
+            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load text 0x%x, size %d\n",
                 ptr, shdr[index].sh_size));
             ptr += shdr[index].sh_size;
         }
@@ -583,10 +573,10 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         /* load rodata section */
         if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
         {
-            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, 
+            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
                 shdr[index].sh_size);
             rodata_addr = (rt_uint32_t)ptr;
-            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load rodata 0x%x, size %d, rodata 0x%x\n", 
+            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load rodata 0x%x, size %d, rodata 0x%x\n",
                 ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
             ptr += shdr[index].sh_size;
         }
@@ -594,10 +584,10 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         /* load data section */
         if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
         {
-            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, 
+            rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
                 shdr[index].sh_size);
             data_addr = (rt_uint32_t)ptr;
-            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load data 0x%x, size %d, data 0x%x\n", 
+            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load data 0x%x, size %d, data 0x%x\n",
                 ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
             ptr += shdr[index].sh_size;
         }
@@ -607,13 +597,13 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         {
             rt_memset(ptr, 0, shdr[index].sh_size);
             bss_addr = (rt_uint32_t)ptr;
-            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n", 
+            RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n",
                 ptr, shdr[index].sh_size));
         }
     }
 
     /* set module entry */
-    module->module_entry = (rt_uint8_t*)module->module_space + 
+    module->module_entry = (rt_uint8_t*)module->module_space +
         elf_module->e_entry - module_addr;
 
     /* handle relocation section */
@@ -622,18 +612,18 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         rt_uint32_t i, nr_reloc;
         Elf32_Sym *symtab;
         Elf32_Rel *rel;
-            
+
         if (!IS_REL(shdr[index])) continue;
-        
+
         /* get relocate item */
         rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
 
         /* locate .dynsym and .dynstr */
-        symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + 
+        symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr +
             shdr[shdr[index].sh_link].sh_offset);
-        strtab = (rt_uint8_t*) module_ptr + 
+        strtab = (rt_uint8_t*) module_ptr +
             shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
-        shstrab = (rt_uint8_t*) module_ptr + 
+        shstrab = (rt_uint8_t*) module_ptr +
             shdr[elf_module->e_shstrndx].sh_offset;
         nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
 
@@ -641,8 +631,8 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
         for (i = 0; i < nr_reloc; i ++)
         {
             Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
-            
-            RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", 
+
+            RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n",
                 strtab + sym->st_name));
 
             if (sym->st_shndx != STN_UNDEF)
@@ -650,7 +640,7 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
                 if((ELF_ST_TYPE(sym->st_info) == STT_SECTION)
                     || (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
                 {
-                    if (rt_strncmp((const char*)(shstrab + 
+                    if (rt_strncmp((const char*)(shstrab +
                         shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
                     {
                         /* relocate rodata section */
@@ -663,15 +653,15 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
                     {
                         /* relocate bss section */
                         RT_DEBUG_LOG(RT_DEBUG_MODULE,("bss\n"));
-                        rt_module_arm_relocate(module, rel, 
+                        rt_module_arm_relocate(module, rel,
                             (Elf32_Addr)bss_addr + sym->st_value);
                     }
-                    else if(rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name), 
+                    else if(rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name),
                         ELF_DATA, 6) == 0)
                     {
                         /* relocate data section */
                         RT_DEBUG_LOG(RT_DEBUG_MODULE,("data\n"));
-                        rt_module_arm_relocate(module, rel, 
+                        rt_module_arm_relocate(module, rel,
                             (Elf32_Addr)data_addr + sym->st_value);
                     }
                 }
@@ -688,9 +678,9 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
 
                 if(ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
                 {
-                    RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", 
+                    RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n",
                         strtab + sym->st_name));
-                    
+
                     /* need to resolve symbol in kernel symbol table */
                     addr = rt_module_symbol_find((const char*)(strtab + sym->st_name));
                     if (addr != (Elf32_Addr)RT_NULL)
@@ -699,7 +689,7 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
                         RT_DEBUG_LOG(RT_DEBUG_MODULE,("symbol addr 0x%x\n", addr));
                     }
                     else
-                        rt_kprintf("Module: can't find %s in kernel symbol table\n", 
+                        rt_kprintf("Module: can't find %s in kernel symbol table\n",
                             strtab + sym->st_name);
                 }
                 else
@@ -768,12 +758,15 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
 
     /* init module object container */
     rt_module_init_object_container(module);
-        
+
     /* increase module reference count */
     module->nref ++;
 
     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,26 +777,29 @@ 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;
+		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;
 
         /* startup module thread */
         rt_thread_startup(module->module_thread);
-    }    
+    }
     else
     {
         /* without entry point */
         module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
-    }    
+    }
 
 #ifdef RT_USING_HOOK
     if (rt_module_load_hook != RT_NULL)
@@ -813,20 +809,26 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
 #endif
 
     return module;
-}    
+}
+
+#ifdef RT_USING_DFS
+#include <dfs_posix.h>
 
-static char* module_name(const char *path)
+static char* _module_name(const char *path)
 {
-    char *first, *end, *name;
+    const char *first, *end, *ptr;
+	char *name;
     int size;
-    char *ptr = (char*)path;
-    
+
+	ptr = (char*)path;
+	first = ptr;
+	end = path + rt_strlen(path);
     while(*ptr != '\0')
     {
         if(*ptr == '/') first = ptr + 1;
         if(*ptr == '.') end = ptr - 1;
-        
-        ptr++;    
+
+        ptr++;
     }
 
     size = end - first + 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,39 +934,16 @@ 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;    
+        list = &module->module_object[RT_Object_Class_Thread].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -985,7 +962,7 @@ rt_err_t rt_module_unload(rt_module_t module)
 
 #ifdef RT_USING_MUTEX
         /* delete mutexs*/
-        list = &module->module_object[RT_Object_Class_Mutex].object_list;    
+        list = &module->module_object[RT_Object_Class_Mutex].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -1004,7 +981,7 @@ rt_err_t rt_module_unload(rt_module_t module)
 
 #ifdef RT_USING_EVENT
         /* delete mailboxs */
-        list = &module->module_object[RT_Object_Class_Event].object_list;    
+        list = &module->module_object[RT_Object_Class_Event].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -1023,7 +1000,7 @@ rt_err_t rt_module_unload(rt_module_t module)
 
 #ifdef RT_USING_MAILBOX
         /* delete mailboxs */
-        list = &module->module_object[RT_Object_Class_MailBox].object_list;    
+        list = &module->module_object[RT_Object_Class_MailBox].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -1061,7 +1038,7 @@ rt_err_t rt_module_unload(rt_module_t module)
 
 #ifdef RT_USING_MEMPOOL
         /* delete mempools */
-        list = &module->module_object[RT_Object_Class_MemPool].object_list;    
+        list = &module->module_object[RT_Object_Class_MemPool].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -1080,7 +1057,7 @@ rt_err_t rt_module_unload(rt_module_t module)
 
 #ifdef RT_USING_DEVICE
         /* delete devices */
-        list = &module->module_object[RT_Object_Class_Device].object_list;    
+        list = &module->module_object[RT_Object_Class_Device].object_list;
         while (list->next != list)
         {
             object = rt_list_entry(list->next, struct rt_object, list);
@@ -1125,19 +1102,14 @@ 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) 
+    if(module->page_array != RT_NULL)
         rt_free(module->page_array);
 #endif
 
@@ -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.
  *
@@ -1169,7 +1199,7 @@ rt_module_t rt_module_find(const char *name)
 
     /* try to find device object */
     information = &rt_object_container[RT_Object_Class_Module];
-    for (node = information->object_list.next; 
+    for (node = information->object_list.next;
         node != &(information->object_list); node = node->next)
     {
         object = rt_list_entry(node, struct rt_object, list);
@@ -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,15 +1562,15 @@ 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;
 
-        /* available block after ap in freelist */ 
-        if (p != RT_NULL && (p->size >= (nunits - (b->size))) &&  p == (b + b->size))  
+        /* available block after ap in freelist */
+        if (p != RT_NULL && (p->size >= (nunits - (b->size))) &&  p == (b + b->size))
         {
             /* perfect match */
-            if (p->size == (nunits - (b->size)))  
+            if (p->size == (nunits - (b->size)))
             {
                 b->size = nunits;
                 prev->next = p->next;
@@ -1537,13 +1583,13 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
 
                 /* restoring old pointer */
                 p->next = tmpp->next;
-                
+
                 /* new size for p */
                 p->size = tmpp->size + b->size - nunits;
                 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);
         }
@@ -1570,7 +1616,7 @@ void list_memlist(const char *name)
     if (module == RT_NULL)
         return;
 
-    for (prev = (struct rt_mem_head **)&module->mem_list; 
+    for (prev = (struct rt_mem_head **)&module->mem_list;
         (b = *prev) != RT_NULL; prev = &(b->next))
     {
         rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));

+ 0 - 5
src/scheduler.c

@@ -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 */