Example #1
0
/**
 * This function will create a thread object and allocate thread object memory
 * and stack.
 *
 * @param name the name of thread, which shall be unique
 * @param entry the entry function of thread
 * @param parameter the parameter of thread enter function
 * @param stack_size the size of thread stack
 * @param priority the priority of thread
 * @param tick the time slice if there are same priority thread
 *
 * @return the created thread object
 *
 */
rt_thread_t rt_thread_create(const char *name,
	void (*entry)(void *parameter), void *parameter,
	rt_uint32_t stack_size,
	rt_uint8_t priority,
	rt_uint32_t tick)
{
	struct rt_thread *thread;
	void *stack_start;

	thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread, name);
	if (thread == RT_NULL) return RT_NULL;

	stack_start = (void *)rt_malloc(stack_size);
	if (stack_start == RT_NULL)
	{
		/* allocate stack failure */
		rt_object_delete((rt_object_t)thread);
		return RT_NULL; 
	}

	_rt_thread_init(thread, name, entry, parameter,
		stack_start, stack_size,
		priority, tick);

	return thread;
}
Example #2
0
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();
#endif

#ifdef RT_USING_HEAP
		/* check the defunct thread list */
		if (!rt_list_isempty(&rt_thread_defunct))
		{
			rt_base_t lock;

			struct rt_thread* thread = rt_list_entry(rt_thread_defunct.next, struct rt_thread, tlist);

			/* disable interrupt */
			lock = rt_hw_interrupt_disable();

			rt_list_remove(&(thread->tlist));

			/* enable interrupt */
			rt_hw_interrupt_enable(lock);

			/* release thread's stack */
			rt_free(thread->stack_addr);

			/* delete thread object */
			rt_object_delete((rt_object_t)thread);
		}
#endif
	}
Example #3
0
/**
 * This function will create a mempool object and allocate the memory pool from
 * heap.
 *
 * @param name the name of memory pool
 * @param block_count the count of blocks in memory pool
 * @param block_size the size for each block
 *
 * @return the created mempool object
 */
rt_mp_t rt_mp_create(const char *name,
                     rt_size_t   block_count,
                     rt_size_t   block_size)
{
    rt_uint8_t *block_ptr;
    struct rt_mempool *mp;
    register rt_base_t offset;

    RT_DEBUG_NOT_IN_INTERRUPT;

    /* allocate object */
    mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
    /* allocate object failed */
    if (mp == RT_NULL)
        return RT_NULL;

    /* initialize memory pool */
    block_size     = RT_ALIGN(block_size, RT_ALIGN_SIZE);
    mp->block_size = block_size;
    mp->size       = (block_size + sizeof(rt_uint8_t *)) * block_count;

    /* allocate memory */
    mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
                                  block_count);
    if (mp->start_address == RT_NULL)
    {
        /* no memory, delete memory pool object */
        rt_object_delete(&(mp->parent));

        return RT_NULL;
    }

    mp->block_total_count = block_count;
    mp->block_free_count  = mp->block_total_count;

    /* initialize suspended thread list */
    rt_list_init(&(mp->suspend_thread));
    mp->suspend_thread_count = 0;

    /* initialize free block list */
    block_ptr = (rt_uint8_t *)mp->start_address;
    for (offset = 0; offset < mp->block_total_count; offset ++)
    {
        *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
            = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
    }

    *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
        = RT_NULL;

    mp->block_list = block_ptr;

    return mp;
}
Example #4
0
/**
 * This function will delete a mutex object and release the memory
 *
 * @param mutex the mutex object
 *
 * @return the error code
 *
 * @see rt_mutex_detach
 */
rt_err_t rt_mutex_delete (rt_mutex_t mutex)
{
	RT_ASSERT(mutex != RT_NULL);

	/* wakeup all suspend threads */
	rt_ipc_object_resume_all(&(mutex->parent));

	/* delete semaphore object */
	rt_object_delete(&(mutex->parent.parent));

	return RT_EOK;
}
Example #5
0
/**
 * This function will delete a semaphore object and release the memory
 *
 * @param sem the semaphore object
 *
 * @return the error code
 *
 * @see rt_sem_detach
 */
rt_err_t rt_sem_delete (rt_sem_t sem)
{
	RT_ASSERT(sem != RT_NULL);

	/* wakeup all suspend threads */
	rt_ipc_object_resume_all(&(sem->parent));

	/* delete semaphore object */
	rt_object_delete(&(sem->parent.parent));

	return RT_EOK;
}
Example #6
0
/**
 * This function will delete a memory pool and release the object memory.
 *
 * @param mp the memory pool object
 *
 * @return RT_EOK
 */
rt_err_t rt_mp_delete(rt_mp_t mp)
{
	struct rt_thread *thread;
	register rt_ubase_t temp;

	RT_DEBUG_NOT_IN_INTERRUPT;

	/* parameter check */
	RT_ASSERT(mp != RT_NULL);

	/* wake up all suspended threads */
	while (!rt_list_isempty(&(mp->suspend_thread)))
	{
		/* disable interrupt */
		temp = rt_hw_interrupt_disable();

		/* get next suspend thread */
		thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
		/* set error code to RT_ERROR */
		thread->error = -RT_ERROR;

		/*
		 * resume thread
		 * In rt_thread_resume function, it will remove current thread from suspend
		 * list
		 */
		rt_thread_resume(thread);

		/* decrease suspended thread count */
		mp->suspend_thread_count --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}

#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
	/* the mp object belongs to an application module */
	if (mp->parent.flag & RT_OBJECT_FLAG_MODULE) 
		rt_module_free(mp->parent.module_id, mp->start_address);
	else
#endif

	/* release allocated room */
	rt_free(mp->start_address);

	/* detach object */
	rt_object_delete(&(mp->parent));

	return RT_EOK;
}
Example #7
0
/**
 * This function will delete a mutex object and release the memory
 *
 * @param mutex the mutex object
 *
 * @return the error code
 *
 * @see rt_mutex_detach
 */
rt_err_t rt_mutex_delete(rt_mutex_t mutex)
{
	RT_DEBUG_NOT_IN_INTERRUPT;

	RT_ASSERT(mutex != RT_NULL);

	/* wakeup all suspend threads */
	rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));

	/* delete semaphore object */
	rt_object_delete(&(mutex->parent.parent));

	return RT_EOK;
}
Example #8
0
/**
 * This function will delete a timer and release timer memory
 *
 * @param timer the timer to be deleted
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_delete(rt_timer_t timer)
{
    register rt_base_t level;

    /* timer check */
    RT_ASSERT(timer != RT_NULL);

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    _rt_timer_remove(timer);

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    rt_object_delete((rt_object_t)timer);

    return -RT_EOK;
}
Example #9
0
/**
 * This function will delete a memory pool and release the object memory.
 *
 * @param mp the memory pool object
 *
 * @return the operation status, RT_EOK on OK; -RT_ERROR on error
 *
 */
rt_err_t rt_mp_delete(rt_mp_t mp)
{
	struct rt_thread* thread;
	register rt_ubase_t temp;

	/* parameter check */
	RT_ASSERT(mp != RT_NULL);

	/* wakeup all suspended threads */
	while (!rt_list_isempty(&(mp->suspend_thread)))
	{
		/* disable interrupt */
		temp = rt_hw_interrupt_disable();

		/* get next suspend thread */
		thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
		/* set error code to RT_ERROR */
		thread->error = -RT_ERROR;

		/*
		 * resume thread
		 * In rt_thread_resume function, it will remove current thread from suspend
		 * list
		 */
		rt_thread_resume(thread);

		/* decrease suspended thread count */
		mp->suspend_thread_count --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}

	/* release allocated room */
	rt_free(mp->start_address);

	/* detach object */
	rt_object_delete(&(mp->parent));

	return RT_EOK;
}