RT-Thread线程

线程是实现任务的载体,它是RT-Thread中最基本的调度单位,它描述了一个任务执行的运行环境,也描述了这个任务所处的优先等级。

RT-Thread中,线程由三部分组成:线程代码(入口函数),线程控制块,线程退栈。

1. 线程代码:无限循环结构、顺序执行结构

void thread_entry( void *parameter )
{
    while(1)
    {
        /*等待事件的发生*/
        ......
        /*处理事件*/
    }
}


void thread_entry( void *parameter )
{

     /*事件1处理*/
     /*事件2处理*/
      ......
     /*事件n处理*/
    
}

2. 线程控制块

/**
 * Thread structure
 */
struct rt_thread
{
    /* rt object */
    char        name[RT_NAME_MAX];                      /**< the name of thread */
    rt_uint8_t  type;                                   /**< type of object */
    rt_uint8_t  flags;                                  /**< thread's flags */

#ifdef RT_USING_MODULE
    void       *module_id;                              /**< id of application module */
#endif

    rt_list_t   list;                                   /**< the object list */
    rt_list_t   tlist;                                  /**< the thread list */

    /* stack point and entry */
    void       *sp;                                     /**< stack point */
    void       *entry;                                  /**< entry */
    void       *parameter;                              /**< parameter */
    void       *stack_addr;                             /**< stack address */
    rt_uint32_t stack_size;                             /**< stack size */

    /* error code */
    rt_err_t    error;                                  /**< error code */

    rt_uint8_t  stat;                                   /**< thread status */

#ifdef RT_USING_SMP
    rt_uint8_t  bind_cpu;                               /**< thread is bind to cpu */
    rt_uint8_t  oncpu;                                  /**< process on cpu` */

    rt_uint16_t scheduler_lock_nest;                    /**< scheduler lock count */
    rt_uint16_t cpus_lock_nest;                         /**< cpus lock count */
#endif /*RT_USING_SMP*/

    /* priority */
    rt_uint8_t  current_priority;                       /**< current priority */
    rt_uint8_t  init_priority;                          /**< initialized priority */
#if RT_THREAD_PRIORITY_MAX > 32
    rt_uint8_t  number;
    rt_uint8_t  high_mask;
#endif
    rt_uint32_t number_mask;

#if defined(RT_USING_EVENT)
    /* thread event */
    rt_uint32_t event_set;
    rt_uint8_t  event_info;
#endif

#if defined(RT_USING_SIGNALS)
    rt_sigset_t     sig_pending;                        /**< the pending signals */
    rt_sigset_t     sig_mask;                           /**< the mask bits of signal */

#ifndef RT_USING_SMP
    void            *sig_ret;                           /**< the return stack pointer from signal */
#endif
    rt_sighandler_t *sig_vectors;                       /**< vectors of signal handler */
    void            *si_list;                           /**< the signal infor list */
#endif

    rt_ubase_t  init_tick;                              /**< thread's initialized tick */
    rt_ubase_t  remaining_tick;                         /**< remaining tick */

    struct rt_timer thread_timer;                       /**< built-in thread timer */

    void (*cleanup)(struct rt_thread *tid);             /**< cleanup function when thread exit */

    /* light weight process if present */
#ifdef RT_USING_LWP
    void        *lwp;
#endif

    rt_uint32_t user_data;                             /**< private user data beyond this thread */
};
typedef struct rt_thread *rt_thread_t;

3. 线程栈

RT-Thread每个线程都有独立的栈空间,当进行线程切换时候,系统会将当前线程的上下文保存在线程栈中,当线程要恢复运行时,再从线程栈中读取上下文信息,恢复线程的运行。

线程上下文是指线程执行时的环境,具体来说就是各个变量和数据包括所有寄存器变量、堆栈信息、内存信息等。

线程栈在形式上是一段连续的内存空间,我们可以通过定义一个数组或者申请一段动态内存来作为线程的栈。

 

4. 创建线程(动态线程和静态线程)

//创建静态线程
/**
 * This function will initialize a thread, normally it's used to initialize a
 * static thread object.
 *
 * @param thread the static thread object
 * @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_start the start address of thread stack
 * @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 operation status, RT_EOK on OK, -RT_ERROR on error
 */
rt_err_t rt_thread_init(struct rt_thread *thread,
                        const char       *name,
                        void (*entry)(void *parameter),
                        void             *parameter,
                        void             *stack_start,
                        rt_uint32_t       stack_size,
                        rt_uint8_t        priority,
                        rt_uint32_t       tick)
//创建动态线程
/**
 * 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)
//例程

//静态线程
static char thread2_stack[1024];
static struct rt_thread thread2;

#define THREAD_STACK_SIZE 256
#define THREAD_PRIOTITY 25
#define THREAD_TIMESLICE  5

static void thread2_entry( void *param )
{
	rt_uint32_t count = 0;
	rt_kprintf( "thread2 in\n" );
	for( count=0; count<20; count++ )
	{
		rt_kprintf( "thread2 count:%d\n",count++ );
		rt_thread_mdelay(1000);
	}
	rt_kprintf( "thread2 exit\n" );
}


rt_err_t err = rt_thread_init( &thread2, "thread2", thread2_entry, RT_NULL, thread2_stack, sizeof(thread2_stack),THREAD_PRIOTITY-1, THREAD_TIMESLICE );
	if( err!= RT_EOK )
	{
		rt_kprintf( "rt thread init thread2 error\n" );
		return -1;	
	}
//例程

//动态线程

rt_thread_t tid1;
#define THREAD_STACK_SIZE 256
#define THREAD_PRIOTITY 25
#define THREAD_TIMESLICE  5


static void thread1_entry( void *param )
{
	rt_uint32_t count = 0;
	rt_kprintf( "thread1 in\n" );
	while(1)
	{
		rt_kprintf( "thread1 count:%d\n",count++ );
		rt_thread_mdelay(500);
	}
}

	tid1 = rt_thread_create( "thread1", thread1_entry, RT_NULL,  THREAD_STACK_SIZE, THREAD_PRIOTITY, THREAD_TIMESLICE );
	if( tid1==RT_NULL )
	{
		rt_kprintf( "rt thread create thread1 error\n" );
		return -1;
	}

5. 启动线程

调用此函数后,创建的线程会被加入到线程的就绪队列,执行调度。

rt_err_t rt_thread_startup(rt_thread_t thread)

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值