summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author陈豪 <[email protected]>2014-09-20 01:19:42 +0800
committer陈豪 <[email protected]>2014-09-20 01:19:42 +0800
commit62af08370bef02ab49c8207414ed709d5c78381f (patch)
treec314a985c77e89b20be5e0d9637cb5c70e6c5220 /src
parent1f05b87e5a7b84e170ee287998ecb0b1f814445d (diff)
parentbd6b39cace67fcefccb105e0e6f7609973bbb7ce (diff)
Merge pull request #2 from RT-Thread/master
sync
Diffstat (limited to 'src')
-rw-r--r--src/device.c4
-rw-r--r--src/idle.c32
-rw-r--r--src/kservice.c2
-rw-r--r--src/mem.c6
-rw-r--r--src/mempool.c97
-rw-r--r--src/module.h2
6 files changed, 76 insertions, 67 deletions
diff --git a/src/device.c b/src/device.c
index 835a517ce3..957c9fc774 100644
--- a/src/device.c
+++ b/src/device.c
@@ -362,8 +362,8 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
RTM_EXPORT(rt_device_control);
/**
- * This function will set the indication callback function when device receives
- * data.
+ * This function will set the reception indication callback function. This callback function
+ * is invoked when this device receives data.
*
* @param dev the pointer of device driver structure
* @param rx_ind the indication callback function
diff --git a/src/idle.c b/src/idle.c
index 7bca6c28cb..867af87612 100644
--- a/src/idle.c
+++ b/src/idle.c
@@ -44,16 +44,12 @@ static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
extern rt_list_t rt_thread_defunct;
#ifdef RT_USING_HOOK
-/**
- * @addtogroup Hook
- */
-
-/*@{*/
-
static void (*rt_thread_idle_hook)();
/**
- * This function will set a hook function to idle thread loop.
+ * @ingroup Hook
+ * This function sets a hook function to idle thread loop. When the system performs
+ * idle loop, this hook function should be invoked.
*
* @param hook the specified hook function
*
@@ -63,10 +59,22 @@ void rt_thread_idle_sethook(void (*hook)(void))
{
rt_thread_idle_hook = hook;
}
-
-/*@}*/
#endif
+/* Return whether there is defunctional thread to be deleted. */
+rt_inline int _has_defunct_thread(void)
+{
+ /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
+ * So the compiler has a good reason that the rt_thread_defunct list does
+ * not change within rt_thread_idle_excute thus optimize the "while" loop
+ * into a "if".
+ *
+ * So add the volatile qualifier here. */
+ const volatile rt_list_t *l = (const volatile rt_list_t*)&rt_thread_defunct;
+
+ return l->next != l;
+}
+
/**
* @ingroup Thread
*
@@ -76,7 +84,7 @@ void rt_thread_idle_excute(void)
{
/* Loop until there is no dead thread. So one call to rt_thread_idle_excute
* will do all the cleanups. */
- while (!rt_list_isempty(&rt_thread_defunct))
+ while (_has_defunct_thread())
{
rt_base_t lock;
rt_thread_t thread;
@@ -89,7 +97,7 @@ void rt_thread_idle_excute(void)
lock = rt_hw_interrupt_disable();
/* re-check whether list is empty */
- if (!rt_list_isempty(&rt_thread_defunct))
+ if (_has_defunct_thread())
{
/* get defunct thread */
thread = rt_list_entry(rt_thread_defunct.next,
@@ -180,7 +188,7 @@ static void rt_thread_idle_entry(void *parameter)
}
/**
- * @ingroup SymstemInit
+ * @ingroup SystemInit
*
* This function will initialize idle thread, then start it.
*
diff --git a/src/kservice.c b/src/kservice.c
index cb0064f38b..12878e67d1 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -1073,7 +1073,7 @@ rt_device_t rt_console_set_device(const char *name)
/* set new console device */
_console_device = new;
- rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR);
+ rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
}
return old;
diff --git a/src/mem.c b/src/mem.c
index e8aae72489..004de01d94 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -176,10 +176,10 @@ static void plug_holes(struct heap_mem *mem)
/**
* @ingroup SystemInit
*
- * This function will init system heap
+ * This function will initialize system heap memory.
*
- * @param begin_addr the beginning address of system page
- * @param end_addr the end address of system page
+ * @param begin_addr the beginning address of system heap memory.
+ * @param end_addr the end address of system heap memory.
*/
void rt_system_heap_init(void *begin_addr, void *end_addr)
{
diff --git a/src/mempool.c b/src/mempool.c
index 05904bfbc7..b6d83597bf 100644
--- a/src/mempool.c
+++ b/src/mempool.c
@@ -323,23 +323,15 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
rt_uint8_t *block_ptr;
register rt_base_t level;
struct rt_thread *thread;
+ rt_uint32_t before_sleep = 0;
+
+ /* get current thread */
+ thread = rt_thread_self();
/* disable interrupt */
level = rt_hw_interrupt_disable();
- if (mp->block_free_count)
- {
- /* memory block is available. decrease the free block counter */
- mp->block_free_count --;
-
- /* get block from block list */
- block_ptr = mp->block_list;
- mp->block_list = *(rt_uint8_t **)block_ptr;
-
- /* point to memory pool */
- *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
- }
- else
+ while (mp->block_free_count == 0)
{
/* memory block is unavailable. */
if (time == 0)
@@ -347,55 +339,64 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
/* enable interrupt */
rt_hw_interrupt_enable(level);
+ rt_set_errno(-RT_ETIMEOUT);
+
return RT_NULL;
}
- else
- {
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- /* get current thread */
- thread = rt_thread_self();
-
- thread->error = RT_EOK;
- /* need suspend thread */
- rt_thread_suspend(thread);
- rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
- mp->suspend_thread_count ++;
+ RT_DEBUG_NOT_IN_INTERRUPT;
- if (time > 0)
- {
- /* init thread timer and start it */
- rt_timer_control(&(thread->thread_timer),
- RT_TIMER_CTRL_SET_TIME,
- &time);
- rt_timer_start(&(thread->thread_timer));
- }
+ thread->error = RT_EOK;
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
+ /* need suspend thread */
+ rt_thread_suspend(thread);
+ rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
+ mp->suspend_thread_count++;
- /* do a schedule */
- rt_schedule();
+ if (time > 0)
+ {
+ /* get the start tick of timer */
+ before_sleep = rt_tick_get();
- if (thread->error != RT_EOK)
- return RT_NULL;
+ /* init thread timer and start it */
+ rt_timer_control(&(thread->thread_timer),
+ RT_TIMER_CTRL_SET_TIME,
+ &time);
+ rt_timer_start(&(thread->thread_timer));
+ }
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
- /* decrease free block */
- mp->block_free_count --;
+ /* do a schedule */
+ rt_schedule();
- /* get block from block list */
- block_ptr = mp->block_list;
- mp->block_list = *(rt_uint8_t **)block_ptr;
+ if (thread->error != RT_EOK)
+ return RT_NULL;
- /* point to memory pool */
- *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
+ if (time > 0)
+ {
+ time -= rt_tick_get() - before_sleep;
+ if (time < 0)
+ time = 0;
}
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
}
+ /* memory block is available. decrease the free block counter */
+ mp->block_free_count--;
+
+ /* get block from block list */
+ block_ptr = mp->block_list;
+ RT_ASSERT(block_ptr != RT_NULL);
+
+ /* Setup the next free node. */
+ mp->block_list = *(rt_uint8_t **)block_ptr;
+
+ /* point to memory pool */
+ *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
+
/* enable interrupt */
rt_hw_interrupt_enable(level);
diff --git a/src/module.h b/src/module.h
index d2d4c4fcc8..3c45c96d02 100644
--- a/src/module.h
+++ b/src/module.h
@@ -239,7 +239,7 @@ typedef struct
#define SHT_DYNAMIC 6 /* dynamic section */
#define SHT_NOTE 7 /* note section */
#define SHT_NOBITS 8 /* no space section */
-#define SHT_REL 9 /* relation section without addends */
+#define SHT_REL 9 /* relocation section without addends */
#define SHT_SHLIB 10 /* reserved - purpose unknown */
#define SHT_DYNSYM 11 /* dynamic symbol table section */
#define SHT_NUM 12 /* number of section types */