thread_init_early

lk/kernel/thread.c:
thread_init_early:

/**  * @brief  Initialize threading system  *  * This function is called once, from kmain()  */ void thread_init_early(void) {     int i;

    DEBUG_ASSERT(arch_curr_cpu_num() == 0);

    /* initialize the run queues */     for (i=0; i < NUM_PRIORITIES; i++)         list_initialize(&run_queue[i]);

    /* initialize the thread list */     list_initialize(&thread_list);

    /* create a thread to cover the current running state */     thread_t *t = idle_thread(0);     init_thread_struct(t, "bootstrap");

    /* half construct this thread, since we're already running */     t->priority = HIGHEST_PRIORITY;     t->state = THREAD_RUNNING;     t->flags = THREAD_FLAG_DETACHED;     thread_set_curr_cpu(t, 0);     thread_set_pinned_cpu(t, 0);     wait_queue_init(&t->retcode_wait_queue);     list_add_head(&thread_list, &t->thread_list_node);     set_current_thread(t); }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
rt_thread_init() 是 RT-Thread 实时操作系统中用于初始化线程控制块(Thread Control Block,TCB)的函数。它的原型定义如下: ```c void rt_thread_init(struct rt_thread *thread, const char *name, void (*entry)(void *parameter), void *parameter, rt_uint8_t *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick); ``` 该函数的参数说明如下: - thread:线程控制块指针; - name:线程名称; - entry:线程执行函数; - parameter:传递给线程执行函数的参数; - stack_start:线程栈起始地址; - stack_size:线程栈大小; - priority:线程优先级; - tick:线程时间片长度。 在使用 rt_thread_init() 函数之前,需要先申请一块内存作为线程控制块,并将申请到的内存地址赋值给线程控制块指针 thread。线程控制块结构体定义如下: ```c struct rt_thread { /* 线程控制块成员 */ }; ``` rt_thread_init() 函数主要是将上述参数设置到线程控制块中,然后将线程控制块加入线程就绪队列等待调度。在 RT-Thread 中,线程的优先级越高,就越容易被调度器选中执行。线程的时间片长度是指该线程执行的时间片大小,该时间片用完后,线程会被调度器重新放回线程就绪队列等待下一次调度。 下面是一个使用 rt_thread_init() 函数初始化线程控制块的例子: ```c #include <rtthread.h> /* 线程1执行函数 */ static void thread1_entry(void *parameter) { while (1) { /* 在此处添加线程1的处理逻辑 */ } } /* 线程2执行函数 */ static void thread2_entry(void *parameter) { while (1) { /* 在此处添加线程2的处理逻辑 */ } } int main(void) { rt_thread_t thread1, thread2; /* 申请线程控制块内存 */ thread1 = rt_malloc(sizeof(struct rt_thread)); thread2 = rt_malloc(sizeof(struct rt_thread)); /* 初始化线程1 */ rt_thread_init(thread1, "thread1", thread1_entry, RT_NULL, stack1, sizeof(stack1), 20, 10); /* 初始化线程2 */ rt_thread_init(thread2, "thread2", thread2_entry, RT_NULL, stack2, sizeof(stack2), 30, 20); /* 启动线程1 */ rt_thread_startup(thread1); /* 启动线程2 */ rt_thread_startup(thread2); /* 进入 RT-Thread 线程调度器,开始执行线程 */ rt_thread_run(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值