From 864bd1180237c952beefda21266c3f76f288b99a Mon Sep 17 00:00:00 2001 From: "mbbill@gmail.com" Date: Sun, 12 Jun 2011 10:01:48 +0000 Subject: [PATCH] Add reentrance check. Put kernel debug switch together. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1494 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/mini2440/startup.c | 12 ++- bsp/sam7x/startup.c | 4 + examples/kernel/mbox_send_wait.c | 2 +- include/rtdebug.h | 66 ++++++++++++++ include/rtdef.h | 6 -- include/rtthread.h | 19 ++++ src/debug.c | 28 ++++++ src/idle.c | 6 +- src/ipc.c | 148 ++++++++++++++++++------------- src/irq.c | 12 +-- src/kservice.c | 4 + src/mem.c | 68 +++++++++----- src/mempool.c | 10 ++- src/module.c | 63 ++++++++----- src/object.c | 53 +++++++++-- src/scheduler.c | 28 +++--- src/slab.c | 57 ++++++------ src/thread.c | 27 +++--- src/timer.c | 42 ++++----- 19 files changed, 434 insertions(+), 221 deletions(-) create mode 100644 include/rtdebug.h create mode 100644 src/debug.c diff --git a/bsp/mini2440/startup.c b/bsp/mini2440/startup.c index e9e237987d..915eced9af 100644 --- a/bsp/mini2440/startup.c +++ b/bsp/mini2440/startup.c @@ -69,6 +69,8 @@ extern void finsh_system_init(void); */ void rtthread_startup(void) { + RT_DEBUG_REENT_IN + /* enable cpu cache */ rt_hw_cpu_icache_enable(); rt_hw_cpu_dcache_enable(); @@ -81,7 +83,7 @@ void rtthread_startup(void) /* show version */ rt_show_version(); - + /* init tick */ rt_system_tick_init(); @@ -116,11 +118,11 @@ void rtthread_startup(void) rt_hw_serial_register(&uart2_device, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, &uart2); - + #ifdef RT_USING_DFS rt_hw_sdcard_init(); -#ifdef RT_USING_DFS_UFFS - rt_hw_nand_init(); +#ifdef RT_USING_DFS_UFFS + rt_hw_nand_init(); #endif #endif @@ -147,6 +149,8 @@ void rtthread_startup(void) /* init idle thread */ rt_thread_idle_init(); + RT_DEBUG_REENT_OUT + /* start scheduler */ rt_system_scheduler_start(); diff --git a/bsp/sam7x/startup.c b/bsp/sam7x/startup.c index abf9b58c51..fd822c71ed 100644 --- a/bsp/sam7x/startup.c +++ b/bsp/sam7x/startup.c @@ -74,6 +74,8 @@ void led_flash() */ void rtthread_startup(void) { + RT_DEBUG_REENT_IN + /* init hardware interrupt */ rt_hw_interrupt_init(); @@ -133,6 +135,8 @@ void rtthread_startup(void) /* init idle thread */ rt_thread_idle_init(); + RT_DEBUG_REENT_OUT + /* start scheduler */ rt_system_scheduler_start(); diff --git a/examples/kernel/mbox_send_wait.c b/examples/kernel/mbox_send_wait.c index 569e8f0745..dc1e6295ee 100644 --- a/examples/kernel/mbox_send_wait.c +++ b/examples/kernel/mbox_send_wait.c @@ -41,7 +41,7 @@ static void thread1_entry(void* parameter) static void thread2_entry(void* parameter) { rt_uint8_t count; - unsigned char *str; + char *str; count = 0; while (1) diff --git a/include/rtdebug.h b/include/rtdebug.h new file mode 100644 index 0000000000..735f5dba6f --- /dev/null +++ b/include/rtdebug.h @@ -0,0 +1,66 @@ +#ifndef __RTDEBUG_H__ +#define __RTDEBUG_H__ + +#include + +/* Using this macro to control all kernel debug features. */ +#ifdef RT_DEBUG + +/* Turn on some of these (set to non-zero) to debug kernel */ +#define RT_DEBUG_MEM 0 +#define RT_DEBUG_MODULE 0 +#define RT_DEBUG_SCHEDULER 0 +#define RT_DEBUG_SLAB 0 +#define RT_DEBUG_THREAD 0 +#define RT_DEBUG_TIMER 0 +#define RT_DEBUG_IRQ 0 +#define RT_DEBUG_IPC 0 + +/* Turn on this to enable reentrance check */ +#define RT_DEBUG_REENT_CHK 1 + +#define RT_DEBUG_LOG(type,message) do{ if(type) rt_kprintf message;}while(0) + +#define RT_ASSERT(EX) if (!(EX)) {volatile char dummy=0;\ + rt_kprintf("(%s) assert failed at %s:%d \n", \ + #EX, __FUNCTION__, __LINE__); while (dummy==0);} + +/* Reentrance check */ +/* counter */ +extern rt_uint8_t rt_debug_reent_cnt; + +#define RT_DEBUG_REENT_IN if(RT_DEBUG_REENT_CHK){\ + rt_base_t level;\ + level = rt_hw_interrupt_disable();\ + rt_debug_reent_cnt++;\ + rt_hw_interrupt_enable(level);} + +#define RT_DEBUG_REENT_OUT if(RT_DEBUG_REENT_CHK){\ + rt_base_t level;\ + level = rt_hw_interrupt_disable();\ + rt_debug_reent_cnt--;\ + rt_hw_interrupt_enable(level);} + +/* Mark those non-reentrant functions with this macro */ +#define RT_DEBUG_NOT_REENT if(RT_DEBUG_REENT_CHK){\ + rt_base_t level;\ + level = rt_hw_interrupt_disable();\ + if(rt_debug_reent_cnt != 0){\ + rt_kprintf("Non-reentrant function used in critical area!\n");\ + RT_ASSERT(0)}\ + rt_hw_interrupt_enable(level);} + + +#else /* RT_DEBUG */ + +#define RT_ASSERT(EX) +#define RT_DEGUB_LOG(type,message) +#define RT_DEBUG_REENT_IN +#define RT_DEBUG_REENT_OUT +#define RT_DEBUG_NOT_REENT + +#endif /* RT_DEBUG */ + + + +#endif /* __RTDEBUG_H__ */ diff --git a/include/rtdef.h b/include/rtdef.h index f5d1449d45..50a02c56a3 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -149,12 +149,6 @@ typedef rt_base_t rt_off_t; /**< Type for offset */ #define RT_EBUSY 7 /**< Busy */ /*@}*/ -#ifdef RT_DEBUG -#define RT_ASSERT(EX) if (!(EX)) {volatile char dummy=0; rt_kprintf("(%s) assert failed at %s:%d \n", \ - #EX, __FUNCTION__, __LINE__); while (dummy==0);} -#else -#define RT_ASSERT(EX) -#endif /** * @ingroup BasicDef diff --git a/include/rtthread.h b/include/rtthread.h index 7793663873..95ab191397 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -21,6 +21,7 @@ #define __RT_THREAD_H__ #include +#include #ifdef __cplusplus extern "C" { @@ -48,6 +49,24 @@ void rt_object_detach_sethook(void (*hook)(struct rt_object* object)); void rt_object_trytake_sethook(void (*hook)(struct rt_object* object)); void rt_object_take_sethook(void (*hook)(struct rt_object* object)); void rt_object_put_sethook(void (*hook)(struct rt_object* object)); +#define RT_OBJECT_HOOK_CALL(hookfunc)\ + do{\ + register rt_base_t temp;\ + temp = rt_hw_interrupt_disable();\ + RT_DEBUG_REENT_IN\ + if (hookfunc != RT_NULL) hookfunc();\ + RT_DEBUG_REENT_OUT\ + rt_hw_interrupt_enable(temp);\ + }while(0) +#define RT_OBJECT_HOOK_CALL2(hookfunc,...)\ + do{\ + register rt_base_t temp;\ + temp = rt_hw_interrupt_disable();\ + RT_DEBUG_REENT_IN\ + if (hookfunc != RT_NULL) hookfunc(__VA_ARGS__);\ + RT_DEBUG_REENT_OUT\ + rt_hw_interrupt_enable(temp);\ + }while(0) #endif /*@}*/ diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000000..3edbdf5dc7 --- /dev/null +++ b/src/debug.c @@ -0,0 +1,28 @@ +/* + * File : debug.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-06 11 mbbill first version + */ + +#include +#include + + +/** + * @addtogroup Kernel + */ + +/*@{*/ +#ifdef RT_DEBUG +rt_uint8_t rt_debug_reent_cnt = 0; +#endif + +/*@}*/ diff --git a/src/idle.c b/src/idle.c index 2c5d929111..1b2f464a2a 100644 --- a/src/idle.c +++ b/src/idle.c @@ -60,6 +60,9 @@ void rt_thread_idle_sethook(void (*hook)()) */ void rt_thread_idle_excute(void) { + + RT_DEBUG_NOT_REENT + /* check the defunct thread list */ if (!rt_list_isempty(&rt_thread_defunct)) { @@ -147,8 +150,7 @@ static void rt_thread_idle_entry(void* parameter) while (1) { #ifdef RT_USING_HOOK - /* if there is an idle thread hook */ - if (rt_thread_idle_hook != RT_NULL) rt_thread_idle_hook(); + RT_OBJECT_HOOK_CALL(rt_thread_idle_hook); #endif rt_thread_idle_excute(); diff --git a/src/ipc.c b/src/ipc.c index 762d6e6d9c..932212c4df 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -135,9 +135,7 @@ rt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list) /* get thread entry */ thread = rt_list_entry(list->next, struct rt_thread, tlist); -#ifdef RT_IPC_DEBUG - rt_kprintf("resume thread:%s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC,("resume thread:%s\n", thread->name)); /* resume it */ rt_thread_resume(thread); @@ -253,6 +251,8 @@ rt_sem_t rt_sem_create (const char* name, rt_uint32_t value, rt_uint8_t flag) { rt_sem_t sem; + RT_DEBUG_NOT_REENT + /* allocate object */ sem = (rt_sem_t) rt_object_allocate(RT_Object_Class_Semaphore, name); if (sem == RT_NULL) return sem; @@ -280,6 +280,8 @@ rt_sem_t rt_sem_create (const char* name, rt_uint32_t value, rt_uint8_t flag) */ rt_err_t rt_sem_delete (rt_sem_t sem) { + RT_DEBUG_NOT_REENT + RT_ASSERT(sem != RT_NULL); /* wakeup all suspend threads */ @@ -309,16 +311,16 @@ rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time) RT_ASSERT(sem != RT_NULL); #ifdef RT_USING_HOOK - if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(sem->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_trytake_hook,&(sem->parent.parent)); #endif /* disable interrupt */ temp = rt_hw_interrupt_disable(); -#ifdef RT_IPC_DEBU - rt_kprintf("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name, - ((struct rt_object*)sem)->name, sem->value); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name, + ((struct rt_object*)sem)->name, sem->value)); + if (sem->value > 0) { /* semaphore is available */ @@ -337,6 +339,9 @@ rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time) } else { + /* time = 0 is ok */ + RT_DEBUG_NOT_REENT + /* semaphore is unavailable, push to suspend list */ /* get current thread */ thread = rt_thread_self(); @@ -344,9 +349,8 @@ rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time) /* reset thread error number */ thread->error = RT_EOK; -#ifdef RT_IPC_DEBUG - rt_kprintf("sem take: suspend thread - %s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("sem take: suspend thread - %s\n", thread->name)); /* suspend thread */ rt_ipc_list_suspend(&(sem->parent.suspend_thread), @@ -355,9 +359,8 @@ rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time) /* has waiting time, start thread timer */ if (time > 0) { -#ifdef RT_IPC_DEBUG - rt_kprintf("set thread:%s to timer list\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC,("set thread:%s to timer list\n", thread->name)); + /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time); rt_timer_start(&(thread->thread_timer)); @@ -377,7 +380,7 @@ rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time) } #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(sem->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(sem->parent.parent)); #endif return RT_EOK; @@ -409,7 +412,7 @@ rt_err_t rt_sem_release(rt_sem_t sem) register rt_bool_t need_schedule; #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(sem->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(sem->parent.parent)); #endif need_schedule = RT_FALSE; @@ -417,10 +420,10 @@ rt_err_t rt_sem_release(rt_sem_t sem) /* disable interrupt */ temp = rt_hw_interrupt_disable(); -#ifdef RT_IPC_DEBUG - rt_kprintf("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name, - ((struct rt_object*)sem)->name, sem->value); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name, + ((struct rt_object*)sem)->name, sem->value)); + if ( !rt_list_isempty(&sem->parent.suspend_thread) ) { @@ -549,6 +552,8 @@ rt_mutex_t rt_mutex_create (const char* name, rt_uint8_t flag) { struct rt_mutex *mutex; + RT_DEBUG_NOT_REENT + /* allocate object */ mutex = (rt_mutex_t) rt_object_allocate(RT_Object_Class_Mutex, name); if (mutex == RT_NULL) return mutex; @@ -578,6 +583,8 @@ rt_mutex_t rt_mutex_create (const char* name, rt_uint8_t flag) */ rt_err_t rt_mutex_delete (rt_mutex_t mutex) { + RT_DEBUG_NOT_REENT + RT_ASSERT(mutex != RT_NULL); /* wakeup all suspend threads */ @@ -604,6 +611,9 @@ rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time) register rt_base_t temp; struct rt_thread* thread; + /* this function must not be used in interrupt even if time = 0 */ + RT_DEBUG_NOT_REENT + RT_ASSERT(mutex != RT_NULL); /* disable interrupt */ @@ -613,13 +623,12 @@ rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time) thread = rt_thread_self(); #ifdef RT_USING_HOOK - if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mutex->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_trytake_hook,&(mutex->parent.parent)); #endif -#ifdef RT_IPC_DEBUG - rt_kprintf("mutex_take: current thread %s, mutex value: %d, hold: %d\n", - thread->name, mutex->value, mutex->hold); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mutex_take: current thread %s, mutex value: %d, hold: %d\n", + thread->name, mutex->value, mutex->hold)); /* reset thread error */ thread->error = RT_EOK; @@ -660,9 +669,9 @@ rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time) else { /* mutex is unavailable, push to suspend list */ -#ifdef RT_IPC_DEBUG - rt_kprintf("mutex_take: suspend thread: %s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mutex_take: suspend thread: %s\n", thread->name)); + /* change the owner thread priority of mutex */ if (thread->current_priority < mutex->owner->current_priority) { @@ -678,9 +687,9 @@ rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time) /* has waiting time, start thread timer */ if (time > 0) { -#ifdef RT_IPC_DEBUG - rt_kprintf("mutex_take: start the timer of thread:%s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mutex_take: start the timer of thread:%s\n", thread->name)); + /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time); rt_timer_start(&(thread->thread_timer)); @@ -711,7 +720,7 @@ rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time) rt_hw_interrupt_enable(temp); #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mutex->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(mutex->parent.parent)); #endif return RT_EOK; @@ -739,13 +748,12 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex) /* disable interrupt */ temp = rt_hw_interrupt_disable(); -#ifdef RT_IPC_DEBUG - rt_kprintf("mutex_release:current thread %s, mutex value: %d, hold: %d\n", - thread->name, mutex->value, mutex->hold); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mutex_release:current thread %s, mutex value: %d, hold: %d\n", + thread->name, mutex->value, mutex->hold)); #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mutex->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(mutex->parent.parent)); #endif /* mutex only can be released by owner */ @@ -777,9 +785,9 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex) /* get suspended thread */ thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist); -#ifdef RT_IPC_DEBUG - rt_kprintf("mutex_release: resume thread: %s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mutex_release: resume thread: %s\n", thread->name)); + /* set new owner and priority */ mutex->owner = thread; mutex->original_priority = thread->current_priority; @@ -891,6 +899,8 @@ rt_event_t rt_event_create (const char* name, rt_uint8_t flag) { rt_event_t event; + RT_DEBUG_NOT_REENT + /* allocate object */ event = (rt_event_t) rt_object_allocate(RT_Object_Class_Event, name); if (event == RT_NULL) return event; @@ -919,6 +929,8 @@ rt_err_t rt_event_delete (rt_event_t event) /* parameter check */ RT_ASSERT(event != RT_NULL); + RT_DEBUG_NOT_REENT + /* resume all suspended thread */ rt_ipc_list_resume_all(&(event->parent.suspend_thread)); @@ -952,7 +964,7 @@ rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set) need_schedule = RT_FALSE; #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(event->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(event->parent.parent)); #endif /* disable interrupt */ @@ -1039,6 +1051,8 @@ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_ register rt_ubase_t level; register rt_base_t status; + RT_DEBUG_NOT_REENT + /* parameter check */ RT_ASSERT(event != RT_NULL); if (set == 0) return -RT_ERROR; @@ -1051,7 +1065,7 @@ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_ thread->error = RT_EOK; #ifdef RT_USING_HOOK - if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(event->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_trytake_hook,&(event->parent.parent)); #endif /* disable interrupt */ @@ -1121,7 +1135,7 @@ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_ rt_hw_interrupt_enable(level); #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(event->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(event->parent.parent)); #endif return thread->error; @@ -1240,6 +1254,8 @@ rt_mailbox_t rt_mb_create (const char* name, rt_size_t size, rt_uint8_t flag) { rt_mailbox_t mb; + RT_DEBUG_NOT_REENT + /* allocate object */ mb = (rt_mailbox_t) rt_object_allocate(RT_Object_Class_MailBox, name); if (mb == RT_NULL) return mb; @@ -1279,6 +1295,8 @@ rt_mailbox_t rt_mb_create (const char* name, rt_size_t size, rt_uint8_t flag) */ rt_err_t rt_mb_delete (rt_mailbox_t mb) { + RT_DEBUG_NOT_REENT + /* parameter check */ RT_ASSERT(mb != RT_NULL); @@ -1325,7 +1343,7 @@ rt_err_t rt_mb_send_wait (rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout RT_ASSERT(mb != RT_NULL); #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mb->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(mb->parent.parent)); #endif /* disable interrupt */ @@ -1350,6 +1368,8 @@ rt_err_t rt_mb_send_wait (rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout return -RT_EFULL; } + RT_DEBUG_NOT_REENT + /* suspend current thread */ rt_ipc_list_suspend(&(mb->suspend_sender_thread), thread, mb->parent.parent.flag); @@ -1360,9 +1380,9 @@ rt_err_t rt_mb_send_wait (rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout /* get the start tick of timer */ tick_delta = rt_tick_get(); -#ifdef RT_IPC_DEBUG - rt_kprintf("mb_send_wait: start timer of thread:%s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mb_send_wait: start timer of thread:%s\n", thread->name)); + /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout); rt_timer_start(&(thread->thread_timer)); @@ -1456,7 +1476,7 @@ rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) tick_delta = 0; #ifdef RT_USING_HOOK - if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mb->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_trytake_hook,&(mb->parent.parent)); #endif /* disable interrupt */ @@ -1481,6 +1501,8 @@ rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) return -RT_ETIMEOUT; } + RT_DEBUG_NOT_REENT + /* suspend current thread */ rt_ipc_list_suspend(&(mb->parent.suspend_thread), thread, mb->parent.parent.flag); @@ -1491,9 +1513,9 @@ rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) /* get the start tick of timer */ tick_delta = rt_tick_get(); -#ifdef RT_IPC_DEBUG - rt_kprintf("mb_recv: start timer of thread:%s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("mb_recv: start timer of thread:%s\n", thread->name)); + /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout); rt_timer_start(&(thread->thread_timer)); @@ -1542,7 +1564,7 @@ rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) rt_hw_interrupt_enable(temp); #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mb->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(mb->parent.parent)); #endif rt_schedule(); @@ -1553,7 +1575,7 @@ rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) rt_hw_interrupt_enable(temp); #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mb->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(mb->parent.parent)); #endif return RT_EOK; @@ -1702,6 +1724,8 @@ rt_mq_t rt_mq_create (const char* name, rt_size_t msg_size, rt_size_t max_msgs, struct rt_mq_message* head; register rt_base_t temp; + RT_DEBUG_NOT_REENT + /* allocate object */ mq = (rt_mq_t) rt_object_allocate(RT_Object_Class_MessageQueue, name); if (mq == RT_NULL) return mq; @@ -1755,6 +1779,8 @@ rt_mq_t rt_mq_create (const char* name, rt_size_t msg_size, rt_size_t max_msgs, */ rt_err_t rt_mq_delete (rt_mq_t mq) { + RT_DEBUG_NOT_REENT + /* parameter check */ RT_ASSERT(mq != RT_NULL); @@ -1797,7 +1823,7 @@ rt_err_t rt_mq_send (rt_mq_t mq, void* buffer, rt_size_t size) if (size > mq->msg_size) return -RT_ERROR; #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mq->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(mq->parent.parent)); #endif /* disable interrupt */ @@ -1880,7 +1906,7 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, void* buffer, rt_size_t size) if (size > mq->msg_size) return -RT_ERROR; #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mq->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(mq->parent.parent)); #endif /* disable interrupt */ @@ -1955,7 +1981,7 @@ rt_err_t rt_mq_recv (rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeou rt_uint32_t tick_delta; #ifdef RT_USING_HOOK - if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mq->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_trytake_hook,&(mq->parent.parent)); #endif tick_delta = 0; @@ -1981,6 +2007,8 @@ rt_err_t rt_mq_recv (rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeou return -RT_ETIMEOUT; } + RT_DEBUG_NOT_REENT + /* suspend current thread */ rt_ipc_list_suspend(&(mq->parent.suspend_thread), thread, mq->parent.parent.flag); @@ -1991,9 +2019,9 @@ rt_err_t rt_mq_recv (rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeou /* get the start tick of timer */ tick_delta = rt_tick_get(); -#ifdef RT_IPC_DEBUG - rt_kprintf("set thread:%s to timer list\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_IPC, + ("set thread:%s to timer list\n", thread->name)); + /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout); rt_timer_start(&(thread->thread_timer)); @@ -2051,7 +2079,7 @@ rt_err_t rt_mq_recv (rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeou rt_hw_interrupt_enable(temp); #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mq->parent.parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(mq->parent.parent)); #endif return RT_EOK; diff --git a/src/irq.c b/src/irq.c index 4effde32a0..91dc405ad6 100644 --- a/src/irq.c +++ b/src/irq.c @@ -37,9 +37,9 @@ void rt_interrupt_enter() { rt_base_t level; -#ifdef IRQ_DEBUG - rt_kprintf("irq comming..., irq nest:%d\n", rt_interrupt_nest); -#endif + RT_DEBUG_REENT_IN + + RT_DEBUG_LOG(RT_DEBUG_IRQ,("irq comming..., irq nest:%d\n", rt_interrupt_nest)); level = rt_hw_interrupt_disable(); rt_interrupt_nest ++; @@ -57,13 +57,13 @@ void rt_interrupt_leave() { rt_base_t level; -#ifdef IRQ_DEBUG - rt_kprintf("irq leave, irq nest:%d\n", rt_interrupt_nest); -#endif + RT_DEBUG_LOG(RT_DEBUG_IRQ,("irq leave, irq nest:%d\n", rt_interrupt_nest)); level = rt_hw_interrupt_disable(); rt_interrupt_nest --; rt_hw_interrupt_enable(level); + + RT_DEBUG_REENT_OUT } /*@}*/ diff --git a/src/kservice.c b/src/kservice.c index 5336a9dd9c..90a3ff5b65 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -44,6 +44,8 @@ rt_err_t rt_get_errno(void) { rt_thread_t tid; + RT_DEBUG_NOT_REENT + tid = rt_thread_self(); if (tid == RT_NULL) return errno; @@ -59,6 +61,8 @@ void rt_set_errno(rt_err_t error) { rt_thread_t tid; + RT_DEBUG_NOT_REENT + tid = rt_thread_self(); if (tid == RT_NULL) { errno = error; return; } diff --git a/src/mem.c b/src/mem.c index bc4dba4cc5..787e37f423 100644 --- a/src/mem.c +++ b/src/mem.c @@ -72,7 +72,15 @@ static void (*rt_free_hook)(void *ptr); */ void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_malloc_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** @@ -83,7 +91,15 @@ void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size)) */ void rt_free_sethook(void (*hook)(void *ptr)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_free_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /*@}*/ @@ -124,6 +140,8 @@ static void plug_holes(struct heap_mem *mem) struct heap_mem *nmem; struct heap_mem *pmem; + RT_DEBUG_NOT_REENT + RT_ASSERT((rt_uint8_t *)mem >= heap_ptr); RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end); RT_ASSERT(mem->used == 0); @@ -170,6 +188,8 @@ void rt_system_heap_init(void* begin_addr, void* end_addr) rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE); + RT_DEBUG_NOT_REENT + /* alignment addr */ if((end_align > (2 * SIZEOF_STRUCT_MEM) ) && ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align )) { @@ -184,9 +204,8 @@ void rt_system_heap_init(void* begin_addr, void* end_addr) /* point to begin address of heap */ heap_ptr = (rt_uint8_t *)begin_align; -#ifdef RT_MEM_DEBUG - rt_kprintf("mem init, heap begin address 0x%x, size %d\n", (rt_uint32_t)heap_ptr, mem_size_aligned); -#endif + RT_DEBUG_LOG(RT_DEBUG_MEM, + ("mem init, heap begin address 0x%x, size %d\n", (rt_uint32_t)heap_ptr, mem_size_aligned)); /* initialize the start of the heap */ mem = (struct heap_mem *)heap_ptr; @@ -226,9 +245,11 @@ void *rt_malloc(rt_size_t size) rt_size_t ptr, ptr2; struct heap_mem *mem, *mem2; + RT_DEBUG_NOT_REENT + if (size == 0) return RT_NULL; -#ifdef RT_MEM_DEBUG +#if RT_DEBUG_MEM if (size != RT_ALIGN(size, RT_ALIGN_SIZE)) rt_kprintf("malloc size %d, but align to %d\n", size, RT_ALIGN(size, RT_ALIGN_SIZE)); else @@ -240,9 +261,8 @@ void *rt_malloc(rt_size_t size) if (size > mem_size_aligned) { -#ifdef RT_MEM_DEBUG - rt_kprintf("no memory\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_MEM,("no memory\n")); + return RT_NULL; } @@ -327,15 +347,12 @@ void *rt_malloc(rt_size_t size) RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0); -#ifdef RT_MEM_DEBUG - rt_kprintf("allocate memory at 0x%x, size: %d\n", + RT_DEBUG_LOG(RT_DEBUG_MEM,("allocate memory at 0x%x, size: %d\n", (rt_uint32_t)((rt_uint8_t*)mem + SIZEOF_STRUCT_MEM), - (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr))); -#endif + (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr)))); #ifdef RT_USING_HOOK - if (rt_malloc_hook != RT_NULL) - rt_malloc_hook((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM, size); + RT_OBJECT_HOOK_CALL2(rt_malloc_hook,(rt_uint8_t *)mem + SIZEOF_STRUCT_MEM,size); #endif /* return the memory data except mem struct */ return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM; @@ -361,13 +378,14 @@ void *rt_realloc(void *rmem, rt_size_t newsize) struct heap_mem *mem, *mem2; void* nmem; + RT_DEBUG_NOT_REENT + /* alignment size */ newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE); if (newsize > mem_size_aligned) { -#ifdef RT_MEM_DEBUG - rt_kprintf("realloc: out of memory\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_MEM,("realloc: out of memory\n")); + return RT_NULL; } @@ -449,6 +467,8 @@ void *rt_calloc(rt_size_t count, rt_size_t size) { void *p; + RT_DEBUG_NOT_REENT + /* allocate 'count' objects of size 'size' */ p = rt_malloc(count * size); @@ -468,31 +488,31 @@ void rt_free(void *rmem) { struct heap_mem *mem; + RT_DEBUG_NOT_REENT + if (rmem == RT_NULL) return; RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0); RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end); #ifdef RT_USING_HOOK - if (rt_free_hook != RT_NULL) rt_free_hook(rmem); + RT_OBJECT_HOOK_CALL2(rt_free_hook,rmem); #endif if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end) { -#ifdef RT_MEM_DEBUG - rt_kprintf("illegal memory\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_MEM,("illegal memory\n")); + return; } /* Get the corresponding struct heap_mem ... */ mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM); -#ifdef RT_MEM_DEBUG - rt_kprintf("release memory 0x%x, size: %d\n", + RT_DEBUG_LOG(RT_DEBUG_MEM,("release memory 0x%x, size: %d\n", (rt_uint32_t)rmem, - (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr))); -#endif + (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr)))); + /* protect the heap from concurrent access */ rt_sem_take(&heap_sem, RT_WAITING_FOREVER); diff --git a/src/mempool.c b/src/mempool.c index 5267a98fa1..614512fe0e 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -182,6 +182,8 @@ rt_mp_t rt_mp_create(const char* name, rt_size_t block_count, rt_size_t block_si struct rt_mempool* mp; register rt_base_t offset; + RT_DEBUG_NOT_REENT + /* allocate object */ mp = (struct rt_mempool*)rt_object_allocate(RT_Object_Class_MemPool, name); if (mp == RT_NULL) return RT_NULL; /* allocate object failed */ @@ -235,6 +237,8 @@ rt_err_t rt_mp_delete(rt_mp_t mp) struct rt_thread* thread; register rt_ubase_t temp; + RT_DEBUG_NOT_REENT + /* parameter check */ RT_ASSERT(mp != RT_NULL); @@ -321,6 +325,8 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) } else { + RT_DEBUG_NOT_REENT + /* get current thread */ thread = rt_thread_self(); @@ -363,7 +369,7 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) rt_hw_interrupt_enable(level); #ifdef RT_USING_HOOK - if (rt_mp_alloc_hook != RT_NULL) rt_mp_alloc_hook(mp, (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*))); + RT_OBJECT_HOOK_CALL2(rt_mp_alloc_hook,mp, (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*))); #endif return (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*)); @@ -387,7 +393,7 @@ void rt_mp_free (void *block) mp = (struct rt_mempool*) *block_ptr; #ifdef RT_USING_HOOK - if (rt_mp_free_hook != RT_NULL) rt_mp_free_hook(mp, block); + RT_OBJECT_HOOK_CALL2(rt_mp_free_hook,mp, block); #endif /* disable interrupt */ diff --git a/src/module.c b/src/module.c index a5af06c491..f15d856e12 100644 --- a/src/module.c +++ b/src/module.c @@ -14,13 +14,13 @@ * 2010-10-23 yi.qiu implement module memory allocator */ +#include #include #include #include "string.h" #include "kservice.h" -/* #define RT_MODULE_DEBUG */ #ifdef RT_USING_MODULE #include "module.h" @@ -137,9 +137,10 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3 case R_ARM_ABS32: *where += (Elf32_Addr)sym_val; -#ifdef RT_MODULE_DEBUG - rt_kprintf("R_ARM_ABS32: %x -> %x\n", where, *where); -#endif + + RT_DEBUG_LOG(RT_DEBUG_MODULE, + ("R_ARM_ABS32: %x -> %x\n", where, *where)); + break; case R_ARM_PC24: @@ -152,9 +153,9 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3 tmp = sym_val - (Elf32_Addr)where + (addend << 2); tmp >>= 2; *where = (*where & 0xff000000) | (tmp & 0x00ffffff); -#ifdef RT_MODULE_DEBUG - rt_kprintf("R_ARM_PC24: %x -> %x\n", where, *where); -#endif + + RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_PC24: %x -> %x\n", where, *where)); + break; case R_ARM_V4BX: @@ -164,15 +165,17 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3 case R_ARM_GLOB_DAT: case R_ARM_JUMP_SLOT: *where = (Elf32_Addr)sym_val; -#ifdef RT_MODULE_DEBUG - rt_kprintf("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val); -#endif + + RT_DEBUG_LOG(RT_DEBUG_MODULE, + ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val)); + break; case R_ARM_RELATIVE: *where += (Elf32_Addr)sym_val; -#ifdef RT_MODULE_DEBUG - rt_kprintf("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val); -#endif + + RT_DEBUG_LOG(RT_DEBUG_MODULE, + ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val)); + break; default: return -1; @@ -261,6 +264,8 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) rt_bool_t linked = RT_FALSE; rt_uint32_t index, module_size = 0; + RT_DEBUG_NOT_REENT + rt_kprintf("rt_module_load: %s ,", name); /* check ELF header */ @@ -348,17 +353,19 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) for (i = 0; i < nr_reloc; i ++) { Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)]; -#ifdef RT_MODULE_DEBUG - rt_kprintf("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx); -#endif + + 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) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL)) rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space + sym->st_value)); else if(!linked) { Elf32_Addr addr; -#ifdef RT_MODULE_DEBUG - rt_kprintf("unresolved relocate symbol: %s\n", strtab + sym->st_name); -#endif + + RT_DEBUG_LOG(RT_DEBUG_MODULE, + ("unresolved 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 == 0) @@ -477,6 +484,8 @@ rt_module_t rt_module_open(const char* filename) struct stat s; char *buffer, *offset_ptr;; + RT_DEBUG_NOT_REENT + /* check parameters */ RT_ASSERT(filename != RT_NULL); @@ -546,6 +555,8 @@ rt_err_t rt_module_unload(rt_module_t module) struct rt_object* object; struct rt_list_node *list; + RT_DEBUG_NOT_REENT + rt_kprintf("rt_module_unload: %s\n", module->parent.name); /* check parameter */ @@ -763,6 +774,8 @@ rt_module_t rt_module_find(const char* name) extern struct rt_object_information rt_object_container[]; + RT_DEBUG_NOT_REENT + /* enter critical */ rt_enter_critical(); @@ -794,6 +807,8 @@ static struct rt_mem_head *morepage(rt_size_t nu) struct rt_mem_head *up; struct rt_module_page *node; + RT_DEBUG_NOT_REENT + RT_ASSERT (nu != 0); /* allocate pages from system heap */ @@ -824,7 +839,9 @@ void *rt_module_malloc(rt_size_t size) struct rt_mem_head *b, *n; struct rt_mem_head **prev; rt_size_t nunits; - + + RT_DEBUG_NOT_REENT + nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1; RT_ASSERT(size != 0); @@ -892,7 +909,9 @@ void rt_module_free(rt_module_t module, void *addr) { struct rt_mem_head *b, *n; struct rt_mem_head **prev; - + + RT_DEBUG_NOT_REENT + RT_ASSERT(addr); RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0); @@ -942,6 +961,8 @@ void *rt_module_realloc(void *ptr, rt_size_t size) struct rt_mem_head *b, *p, *prev, *tmpp; rt_size_t nunits; + RT_DEBUG_NOT_REENT + if (!ptr) return rt_module_malloc(size); if (size == 0) { diff --git a/src/object.c b/src/object.c index 65551ca6a7..75c59e33cb 100644 --- a/src/object.c +++ b/src/object.c @@ -84,7 +84,15 @@ void (*rt_object_put_hook)(struct rt_object* object); */ void rt_object_attach_sethook(void (*hook)(struct rt_object* object)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_object_attach_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** @@ -95,7 +103,15 @@ void rt_object_attach_sethook(void (*hook)(struct rt_object* object)) */ void rt_object_detach_sethook(void (*hook)(struct rt_object* object)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_object_detach_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** @@ -113,7 +129,15 @@ void rt_object_detach_sethook(void (*hook)(struct rt_object* object)) */ void rt_object_trytake_sethook(void (*hook)(struct rt_object* object)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_object_trytake_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** @@ -132,7 +156,15 @@ void rt_object_trytake_sethook(void (*hook)(struct rt_object* object)) */ void rt_object_take_sethook(void (*hook)(struct rt_object* object)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_object_take_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** @@ -143,7 +175,15 @@ void rt_object_take_sethook(void (*hook)(struct rt_object* object)) */ void rt_object_put_sethook(void (*hook)(struct rt_object* object)) { + register rt_base_t temp; + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + rt_object_put_hook = hook; + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /*@}*/ @@ -199,10 +239,7 @@ void rt_object_init(struct rt_object* object, enum rt_object_class_type type, co } #ifdef RT_USING_HOOK - if (rt_object_attach_hook != RT_NULL) - { - rt_object_attach_hook(object); - } + RT_OBJECT_HOOK_CALL2(rt_object_attach_hook,object); #endif /* lock interrupt */ @@ -229,7 +266,7 @@ void rt_object_detach(rt_object_t object) RT_ASSERT(object != RT_NULL); #ifdef RT_USING_HOOK - if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object); + RT_OBJECT_HOOK_CALL2(rt_object_detach_hook,object); #endif /* lock interrupt */ @@ -257,6 +294,8 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name) register rt_base_t temp; struct rt_object_information* information; + RT_DEBUG_NOT_REENT + #ifdef RT_USING_MODULE /* get module object information, module object should be managed by kernel object container */ information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ? @@ -296,7 +335,7 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name) } #ifdef RT_USING_HOOK - if (rt_object_attach_hook != RT_NULL) rt_object_attach_hook(object); + RT_OBJECT_HOOK_CALL2(rt_object_attach_hook,object); #endif /* lock interrupt */ @@ -326,7 +365,7 @@ void rt_object_delete(rt_object_t object) RT_ASSERT(!(object->type & RT_Object_Class_Static)); #ifdef RT_USING_HOOK - if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object); + RT_OBJECT_HOOK_CALL2(rt_object_detach_hook,object); #endif /* lock interrupt */ diff --git a/src/scheduler.c b/src/scheduler.c index 9b9c3b42d5..af85e282ab 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -31,8 +31,6 @@ #include "kservice.h" -/* #define RT_SCHEDULER_DEBUG */ - static rt_int16_t rt_scheduler_lock_nest; extern volatile rt_uint8_t rt_interrupt_nest; @@ -134,9 +132,8 @@ void rt_system_scheduler_init(void) rt_scheduler_lock_nest = 0; -#ifdef RT_SCHEDULER_DEBUG - rt_kprintf("start scheduler: max priority 0x%02x\n", RT_THREAD_PRIORITY_MAX); -#endif + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, + ("start scheduler: max priority 0x%02x\n", RT_THREAD_PRIORITY_MAX)); for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) { @@ -277,14 +274,14 @@ void rt_schedule() #endif #ifdef RT_USING_HOOK - if (rt_scheduler_hook != RT_NULL) rt_scheduler_hook(from_thread, to_thread); + RT_OBJECT_HOOK_CALL2(rt_scheduler_hook,from_thread, to_thread); #endif /* switch to new thread */ -#ifdef RT_SCHEDULER_DEBUG - rt_kprintf("[%d]switch to priority#%d thread:%s\n", rt_interrupt_nest, - highest_ready_priority, to_thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, + ("[%d]switch to priority#%d thread:%s\n", rt_interrupt_nest, + highest_ready_priority, to_thread->name)); + #ifdef RT_USING_OVERFLOW_CHECK _rt_scheduler_stack_check(to_thread); #endif @@ -295,9 +292,8 @@ void rt_schedule() } else { -#ifdef RT_SCHEDULER_DEBUG - rt_kprintf("switch in interrupt\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,("switch in interrupt\n")); + rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp); } @@ -332,12 +328,13 @@ void rt_schedule_insert_thread(struct rt_thread* thread) &(thread->tlist)); /* set priority mask */ -#ifdef RT_SCHEDULER_DEBUG +#if RT_DEBUG_SCHEDULER #if RT_THREAD_PRIORITY_MAX <= 32 rt_kprintf("insert thread[%s], the priority: %d\n", thread->name, thread->current_priority); #else rt_kprintf("insert thread[%s], the priority: %d 0x%x %d\n", thread->name, thread->number, thread->number_mask, thread->high_mask); #endif + #endif #if RT_THREAD_PRIORITY_MAX > 32 @@ -365,13 +362,14 @@ void rt_schedule_remove_thread(struct rt_thread* thread) /* disable interrupt */ temp = rt_hw_interrupt_disable(); -#ifdef RT_SCHEDULER_DEBUG +#if RT_DEBUG_SCHEDULER #if RT_THREAD_PRIORITY_MAX <= 32 rt_kprintf("remove thread[%s], the priority: %d\n", thread->name, thread->current_priority); #else rt_kprintf("remove thread[%s], the priority: %d 0x%x %d\n", thread->name, thread->number, thread->number_mask, thread->high_mask); #endif + #endif /* remove thread from ready list */ diff --git a/src/slab.c b/src/slab.c index a39a59860f..b7ec301823 100644 --- a/src/slab.c +++ b/src/slab.c @@ -56,7 +56,6 @@ #include #include "kservice.h" -/* #define RT_SLAB_DEBUG */ #define RT_MEM_STATS #if defined (RT_USING_HEAP) && defined (RT_USING_SLAB) @@ -338,6 +337,8 @@ void rt_system_heap_init(void *begin_addr, void* end_addr) { rt_uint32_t limsize, npages; + RT_DEBUG_NOT_REENT + /* align begin and end addr to page */ heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE); heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE); @@ -354,10 +355,8 @@ void rt_system_heap_init(void *begin_addr, void* end_addr) /* initialize heap semaphore */ rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO); -#ifdef RT_SLAB_DEBUG - rt_kprintf("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n", heap_start, heap_end, - limsize, npages); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n", heap_start, heap_end, limsize, npages)); /* init pages */ rt_page_init((void*)heap_start, npages); @@ -372,18 +371,17 @@ void rt_system_heap_init(void *begin_addr, void* end_addr) zone_page_cnt = zone_size / RT_MM_PAGE_SIZE; -#ifdef RT_SLAB_DEBUG - rt_kprintf("zone size 0x%x, zone page count 0x%x\n", zone_size, zone_page_cnt); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("zone size 0x%x, zone page count 0x%x\n", zone_size, zone_page_cnt)); /* allocate memusage array */ limsize = npages * sizeof(struct memusage); limsize = RT_ALIGN(limsize, RT_MM_PAGE_SIZE); memusage = rt_page_alloc(limsize/RT_MM_PAGE_SIZE); -#ifdef RT_SLAB_DEBUG - rt_kprintf("memusage 0x%x, size 0x%x\n", (rt_uint32_t)memusage, limsize); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("memusage 0x%x, size 0x%x\n", (rt_uint32_t)memusage, limsize)); + } /* @@ -533,12 +531,11 @@ void *rt_malloc(rt_size_t size) kup->type = PAGE_TYPE_LARGE; kup->size = size >> RT_MM_PAGE_BITS; -#ifdef RT_SLAB_DEBUG - rt_kprintf("malloc a large memory 0x%x, page cnt %d, kup %d\n", + RT_DEBUG_LOG(RT_DEBUG_SLAB,("malloc a large memory 0x%x, page cnt %d, kup %d\n", size, size >> RT_MM_PAGE_BITS, - ((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS); -#endif + ((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS)); + /* lock heap */ rt_sem_take(&heap_sem, RT_WAITING_FOREVER); @@ -563,9 +560,8 @@ void *rt_malloc(rt_size_t size) zi = zoneindex(&size); RT_ASSERT(zi < NZONES); -#ifdef RT_SLAB_DEBUG - rt_kprintf("try to malloc 0x%x on zone: %d\n", size, zi); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("try to malloc 0x%x on zone: %d\n", size, zi)); if ((z = zone_array[zi]) != RT_NULL) { @@ -635,9 +631,7 @@ void *rt_malloc(rt_size_t size) /* lock heap */ rt_sem_take(&heap_sem, RT_WAITING_FOREVER); -#ifdef RT_SLAB_DEBUG - rt_kprintf("alloc a new zone: 0x%x\n", (rt_uint32_t)z); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB,("alloc a new zone: 0x%x\n", (rt_uint32_t)z)); /* set message usage */ for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++) @@ -688,7 +682,7 @@ done: rt_sem_release(&heap_sem); #ifdef RT_USING_HOOK - if (rt_malloc_hook != RT_NULL) rt_malloc_hook((char*)chunk, size); + RT_OBJECT_HOOK_CALL2(rt_malloc_hook,(char*)chunk, size); #endif return chunk; @@ -804,7 +798,7 @@ void rt_free(void *ptr) if (ptr == RT_NULL) return ; #ifdef RT_USING_HOOK - if (rt_free_hook != RT_NULL) rt_free_hook(ptr); + RT_OBJECT_HOOK_CALL2(rt_free_hook,ptr); #endif #ifdef RT_USING_MODULE @@ -816,13 +810,14 @@ void rt_free(void *ptr) #endif /* get memory usage */ -#ifdef RT_SLAB_DEBUG +#if RT_DEBUG_SLAB { rt_uint32_t addr = ((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK); - rt_kprintf("free a memory 0x%x and align to 0x%x, kup index %d\n", + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("free a memory 0x%x and align to 0x%x, kup index %d\n", (rt_uint32_t)ptr, (rt_uint32_t)addr, - ((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS); + ((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS)); } #endif @@ -843,9 +838,8 @@ void rt_free(void *ptr) #endif rt_sem_release(&heap_sem); -#ifdef RT_SLAB_DEBUG - rt_kprintf("free large memory block 0x%x, page count %d\n", (rt_uint32_t)ptr, size); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("free large memory block 0x%x, page count %d\n", (rt_uint32_t)ptr, size)); /* free this page */ rt_page_free(ptr, size); @@ -888,9 +882,8 @@ void rt_free(void *ptr) { slab_zone **pz; -#ifdef RT_SLAB_DEBUG - rt_kprintf("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex); -#endif + RT_DEBUG_LOG(RT_DEBUG_SLAB, + ("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex)); /* remove zone from zone array list */ for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next) ; diff --git a/src/thread.c b/src/thread.c index 61a5d9d023..3411ff1d3d 100644 --- a/src/thread.c +++ b/src/thread.c @@ -26,7 +26,6 @@ #include #include "kservice.h" -/*#define THREAD_DEBUG */ extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; extern struct rt_thread* rt_current_thread; @@ -206,10 +205,8 @@ rt_err_t rt_thread_startup (rt_thread_t thread) thread->number_mask = 1L << thread->current_priority; //1L means long int,fixed compile mistake with IAR EW M16C v3.401,fify 20100410 #endif -#ifdef THREAD_DEBUG - rt_kprintf("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority); -#endif - + RT_DEBUG_LOG(RT_DEBUG_THREAD,\ + ("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority)); /* change thread stat */ thread->stat = RT_THREAD_SUSPEND; /* then resume it */ @@ -536,15 +533,13 @@ rt_err_t rt_thread_suspend (rt_thread_t thread) /* thread check */ RT_ASSERT(thread != RT_NULL); -#ifdef THREAD_DEBUG - rt_kprintf("thread suspend: %s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name)); if (thread->stat != RT_THREAD_READY) { -#ifdef THREAD_DEBUG - rt_kprintf("thread suspend: thread disorder, %d\n", thread->stat); -#endif + RT_DEBUG_LOG(RT_DEBUG_THREAD,\ + ("thread suspend: thread disorder, %d\n", thread->stat)); + return -RT_ERROR; } @@ -576,15 +571,13 @@ rt_err_t rt_thread_resume (rt_thread_t thread) /* thread check */ RT_ASSERT(thread != RT_NULL); -#ifdef THREAD_DEBUG - rt_kprintf("thread resume: %s\n", thread->name); -#endif + RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name)); if (thread->stat != RT_THREAD_SUSPEND) { -#ifdef THREAD_DEBUG - rt_kprintf("thread resume: thread disorder, %d\n", thread->stat); -#endif + RT_DEBUG_LOG(RT_DEBUG_THREAD, \ + ("thread resume: thread disorder, %d\n", thread->stat)); + return -RT_ERROR; } diff --git a/src/timer.c b/src/timer.c index 21fe1f6466..1c7803b85d 100644 --- a/src/timer.c +++ b/src/timer.c @@ -24,8 +24,6 @@ #include "kservice.h" -/* #define RT_TIMER_DEBUG */ - /* hard timer list */ static rt_list_t rt_timer_list; @@ -213,7 +211,7 @@ rt_err_t rt_timer_start(rt_timer_t timer) if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) return -RT_ERROR; #ifdef RT_USING_HOOK - if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(timer->parent)); + RT_OBJECT_HOOK_CALL2(rt_object_take_hook,&(timer->parent)); #endif /* disable interrupt */ @@ -280,7 +278,7 @@ rt_err_t rt_timer_stop(rt_timer_t timer) if(!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR; #ifdef RT_USING_HOOK - if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(timer->parent)); + RT_OBJECT_HOOK_CALL2(rt_object_put_hook,&(timer->parent)); #endif /* disable interrupt */ @@ -350,9 +348,7 @@ void rt_timer_check(void) rt_tick_t current_tick; register rt_base_t level; -#ifdef RT_TIMER_DEBUG - rt_kprintf("timer check enter\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check enter\n")); current_tick = rt_tick_get(); @@ -369,21 +365,25 @@ void rt_timer_check(void) if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2) { #ifdef RT_USING_HOOK - if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t); + RT_OBJECT_HOOK_CALL2(rt_timer_timeout_hook,t); #endif /* remove timer from timer list firstly */ rt_list_remove(&(t->list)); + /* Timeout function called while interrupt is disabled, reentant function + is a must */ + RT_DEBUG_REENT_IN + /* call timeout function */ t->timeout_func(t->parameter); + RT_DEBUG_REENT_OUT + /* re-get tick */ current_tick = rt_tick_get(); -#ifdef RT_TIMER_DEBUG - rt_kprintf("current tick: %d\n", current_tick); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick)); if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) && (t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) @@ -409,9 +409,8 @@ void rt_timer_check(void) rt_soft_timer_tick_increase ( ); #endif -#ifdef RT_TIMER_DEBUG - rt_kprintf("timer check leave\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check leave\n")); + } #ifdef RT_USING_TIMER_SOFT @@ -443,9 +442,7 @@ void rt_soft_timer_check() rt_list_t *n; struct rt_timer *t; -#ifdef RT_TIMER_DEBUG - rt_kprintf("software timer check enter\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check enter\n")); current_tick = rt_tick_get(); @@ -459,7 +456,7 @@ void rt_soft_timer_check() if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2) { #ifdef RT_USING_HOOK - if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t); + RT_OBJECT_HOOK_CALL2(rt_timer_timeout_hook,t); #endif /* move node to the next */ n = n->next; @@ -473,9 +470,7 @@ void rt_soft_timer_check() /* re-get tick */ current_tick = rt_tick_get(); -#ifdef RT_TIMER_DEBUG - rt_kprintf("current tick: %d\n", current_tick); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick)); if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) && (t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) @@ -493,9 +488,8 @@ void rt_soft_timer_check() else break; /* not check anymore */ } -#ifdef RT_TIMER_DEBUG - rt_kprintf("software timer check leave\n"); -#endif + RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check leave\n")); + } /* system timer thread entry */ -- GitLab