RT-Thread - Thread

RT-Thread 2018-11-19 2.6k

File: rtdef.h

結構#

492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* 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
  • 一些基本資料,如名字等
505
506
rt_list_t   list;                                   /**< the object list */
rt_list_t tlist; /**< the thread list */
  • 兩條鏈:thread list、object list
507
508
509
510
511
512
/* 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 */
  • stack 相關的
513
514
515
516
/* error code */
rt_err_t error; /**< error code */

rt_uint8_t stat; /**< thread status */
  • 狀態,下面會列出來所有可能
517
518
519
520
521
522
523
524
    /* 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;
  • 與權限相關的
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#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 */

void *sig_ret; /**< the return stack pointer from signal */
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 */
  • event、sig、tick
544
545
546
547
548
549
550
551
552
553
    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;
  • cleanup 函數及 user_data

狀態#

  • 一共有 6 種狀態
471
472
473
474
475
476
477
#define RT_THREAD_INIT                  0x00                /**< Initialized status */
#define RT_THREAD_READY 0x01 /**< Ready status */
#define RT_THREAD_SUSPEND 0x02 /**< Suspend status */
#define RT_THREAD_RUNNING 0x03 /**< Running status */
#define RT_THREAD_BLOCK RT_THREAD_SUSPEND /**< Blocked status */
#define RT_THREAD_CLOSE 0x04 /**< Closed status */
#define RT_THREAD_STAT_MASK 0x0f

File: thread.h

  • 下圖為官方文本的 thread 流向圖,接著一個一個的看下去

初始化、建立 thread#

靜態記憶體管理#

功能 回傳值
初始化 thread RT-EOK
*thread *name *entry *parameter
thread 本體 名字 要執行的副程式 副程式參數
*stack_start stack_size priority tick
thread 堆疊起點 thread 堆疊大小 優先級 可執行的 tick 數
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* 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)
{
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(stack_start != RT_NULL);

/* init thread object */
rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);

return _rt_thread_init(thread,
name,
entry,
parameter,
stack_start,
stack_size,
priority,
tick);
}
RTM_EXPORT(rt_thread_init);
  • 透過 _rt_thread_init 完成初始化

功能 回傳值
初始化 thread RT_EOK
*thread *name *entry *parameter
thread 本體 名字 要執行的副程式 副程式參數
*stack_start stack_size priority tick
thread 堆疊起點 thread 堆疊大小 優先級 可執行的 tick 數
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
static 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)
{
/* init thread list */
rt_list_init(&(thread->tlist));

thread->entry = (void *)entry;
thread->parameter = parameter;

/* stack init */
thread->stack_addr = stack_start;
thread->stack_size = stack_size;
  • 首先將傳入的資料填入結構
136
137
138
139
140
/* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size);
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(void *)((char *)thread->stack_addr + thread->stack_size - 4),
(void *)rt_thread_exit);
  • 接著設定堆疊,使用 rt_hw_stack_init 來完成(根據不同 cpu 有不同的方式,rt_hw_stack_init 在 /libcpu 中針對不同的 cpu 有不同的函式宣告)
141
142
143
144
145
146
147
148
149
150
    /* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
thread->init_priority = priority;
thread->current_priority = priority;

thread->number_mask = 0;
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = 0;
thread->high_mask = 0;
#endif
  • 設定 priority 及 mask
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    /* tick init */
thread->init_tick = tick;
thread->remaining_tick = tick;

/* error and flags */
thread->error = RT_EOK;
thread->stat = RT_THREAD_INIT;

/* initialize cleanup function and user data */
thread->cleanup = 0;
thread->user_data = 0;

/* init thread timer */
rt_timer_init(&(thread->thread_timer),
thread->name,
rt_thread_timeout,
thread,
0,
RT_TIMER_FLAG_ONE_SHOT);

/* initialize signal */
#ifdef RT_USING_SIGNALS
thread->sig_mask = 0x00;
thread->sig_pending = 0x00;

thread->sig_ret = RT_NULL;
thread->sig_vectors = RT_NULL;
thread->si_list = RT_NULL;
#endif

#ifdef RT_USING_LWP
thread->lwp = RT_NULL;
#endif

RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));

return RT_EOK;
}
  • 最後依序完成 tick、sig、hook 等的初始化

動態記憶體管理#

功能 回傳值
建立 thread thread
*name *entry *parameter
名字 要執行的副程式 副程式參數
*stack_start stack_size priority tick
thread 堆疊起點 thread 堆疊大小 優先級 可執行的 tick 數
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* 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_KERNEL_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;
}
RTM_EXPORT(rt_thread_create);
  • 首先 allocate 一塊給 thread,一塊給堆疊
  • 再呼叫 _rt_thread_init 完成初始化

啟動 thread#

功能 回傳值 thread
啟動 thread RT_EOK 欲啟動的 thread
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* This function will start a thread and put it to system ready queue
*
* @param thread the thread to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_startup(rt_thread_t thread)
{
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

/* set current priority to init priority */
thread->current_priority = thread->init_priority;
  • 設定 priority
268
    /* calculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 5bit */
thread->number_mask = 1L << thread->number;
thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1L << thread->current_priority;
#endif
  • 這些參數是用來計算權限的,scheudler 會用到
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    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 */
rt_thread_resume(thread);
if (rt_thread_self() != RT_NULL)
{
/* do a scheduling */
rt_schedule();
}

return RT_EOK;
}
RTM_EXPORT(rt_thread_startup);
  • 最後將 stat 設為 RT_THREAD_SUSPEND,再透過 rt_thread_resume 來完成啟動
  • 啟動完成後呼叫 rt_scheduler() 來執行一次調度

暫停、復原 thread#

功能 回傳值 *thread
暫停 thread RT-EOK 欲暫停的 thread
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/**
* This function will suspend the specified thread.
*
* @param thread the thread to be suspended
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*
* @note if suspend self thread, after this function call, the
* rt_schedule() must be invoked.
*/
rt_err_t rt_thread_suspend(rt_thread_t thread)
{
register rt_base_t temp;

/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));

if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
thread->stat));

return -RT_ERROR;
}

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

/* change thread stat */
thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
rt_schedule_remove_thread(thread);

/* stop thread timer anyway */
rt_timer_stop(&(thread->thread_timer));

/* enable interrupt */
rt_hw_interrupt_enable(temp);

RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
return RT_EOK;
}
RTM_EXPORT(rt_thread_suspend);
  • 首先將狀態修改為 RT_THREAD_SUSPEND,接著將 thread 從 tlist 移除,結束 timer

Code: rt_thread_delay

功能 回傳值 tick
延遲 thread RT-EOK 欲延遲的時間
519
520
521
522
523
524
525
526
527
528
529
530
/**
* This function will let current thread delay for some ticks.
*
* @param tick the delay ticks
*
* @return RT_EOK
*/
rt_err_t rt_thread_delay(rt_tick_t tick)
{
return rt_thread_sleep(tick);
}
RTM_EXPORT(rt_thread_delay);
  • 透過 rt_thread_sleep 實作

功能 回傳值
使 thread 休眠 RT-EOK
tick
欲睡眠的時間
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* This function will let current thread sleep for some ticks.
*
* @param tick the sleep ticks
*
* @return RT_EOK
*/
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
register rt_base_t temp;
struct rt_thread *thread;

/* disable interrupt */
temp = rt_hw_interrupt_disable();
/* set to current thread */
thread = rt_current_thread;
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

/* suspend thread */
rt_thread_suspend(thread);

/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
rt_timer_start(&(thread->thread_timer));

/* enable interrupt */
rt_hw_interrupt_enable(temp);

rt_schedule();

/* clear error number of this thread to RT_EOK */
if (thread->error == -RT_ETIMEOUT)
thread->error = RT_EOK;

return RT_EOK;
}

復原 thread#

功能 回傳值 *thread
復原 thread RT-EOK 欲復原的 thread
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/**
* This function will resume a thread and put it to system ready queue.
*
* @param thread the thread to be resumed
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_resume(rt_thread_t thread)
{
register rt_base_t temp;

/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));

if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
thread->stat));

return -RT_ERROR;
}

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

/* remove from suspend list */
rt_list_remove(&(thread->tlist));

rt_timer_stop(&thread->thread_timer);

/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* insert to schedule ready list */
rt_schedule_insert_thread(thread);

RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
return RT_EOK;
}
RTM_EXPORT(rt_thread_resume);
  • 首先從 suspend list 移除,停止 timer,掛回去 ready list
  • rt_schedule_insert_thread 會將狀態修改成 RT_THREAD_READY

離開、刪除 thread#

功能 回傳值
暫停 thread void
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
void rt_thread_exit(void)
{
struct rt_thread *thread;
register rt_base_t level;

/* get current thread */
thread = rt_current_thread;

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

/* remove from schedule */
rt_schedule_remove_thread(thread);
/* change stat */
thread->stat = RT_THREAD_CLOSE;

/* remove it from timer list */
rt_timer_detach(&thread->thread_timer);
  • 首先從 ready list 中移除,修改狀態為 RT_THREAD_CLOSE,從 timer list 中移除
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
thread->cleanup == RT_NULL)
{
rt_object_detach((rt_object_t)thread);
}
else
{
/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
}

/* enable interrupt */
rt_hw_interrupt_enable(level);

/* switch to next task */
rt_schedule();
}

  • 如果為系統的 thread,且 cleanup 函式沒有被定義時,呼叫 rt_object_detach 來完成移除的動作
  • 否則,將 thread 插入至 rt_thread_defunct,此鏈上面的 thread 會由 idle 清除。

刪除 thread#

動態記憶體管理#

功能 回傳值 *thread
刪除 thread RT-EOK 欲刪除的 thread
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* This function will delete a thread. The thread object will be removed from
* thread queue and deleted from system object management in the idle thread.
*
* @param thread the thread to be deleted
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_delete(rt_thread_t thread)
{
rt_base_t lock;

/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);

if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
{
/* remove from schedule */
rt_schedule_remove_thread(thread);
}
  • 如果此 thread 已經啟動過了,將此 thread 從 ready list 移除
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
    /* release thread timer */
rt_timer_detach(&(thread->thread_timer));

/* change stat */
thread->stat = RT_THREAD_CLOSE;

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

/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));

/* enable interrupt */
rt_hw_interrupt_enable(lock);

return RT_EOK;
}
RTM_EXPORT(rt_thread_delete);
  • 接著將 timer 還回去,修改狀態為 RT_THREAD_CLOSE,插入至 rt_thread_defunct

靜態記憶體管理#

功能 回傳值
刪除 thread RT-EOK
*thread
欲刪除的 thread
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* This function will detach a thread. The thread object will be removed from
* thread queue and detached/deleted from system object management.
*
* @param thread the thread to be deleted
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_detach(rt_thread_t thread)
{
rt_base_t lock;

/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));

if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
{
/* remove from schedule */
rt_schedule_remove_thread(thread);
}

/* release thread timer */
rt_timer_detach(&(thread->thread_timer));

/* change stat */
thread->stat = RT_THREAD_CLOSE;

/* detach object */
rt_object_detach((rt_object_t)thread);

if (thread->cleanup != RT_NULL)
{
/* disable interrupt */
lock = rt_hw_interrupt_disable();

/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));

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

return RT_EOK;
}
RTM_EXPORT(rt_thread_detach);
  • 與 delete 不同的差在第32行

控制 thread#

功能 回傳值
控制 thread RT-EOK
*thread cmd *argv
欲控制的 thread 需執行的動作 伴隨動作的參數
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/**
* This function will control thread behaviors according to control command.
*
* @param thread the specified thread to be controlled
* @param cmd the control command, which includes
* RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
* RT_THREAD_CTRL_STARTUP for starting a thread;
* RT_THREAD_CTRL_CLOSE for delete a thread.
* @param arg the argument of control command
*
* @return RT_EOK
*/
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
{
register rt_base_t temp;

/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

switch (cmd)
{
case RT_THREAD_CTRL_CHANGE_PRIORITY:
/* disable interrupt */
temp = rt_hw_interrupt_disable();

/* for ready thread, change queue */
if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
{
/* remove thread from schedule queue first */
rt_schedule_remove_thread(thread);

/* change thread priority */
thread->current_priority = *(rt_uint8_t *)arg;

/* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 5bit */
thread->number_mask = 1 << thread->number;
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
#endif

/* insert thread to schedule queue again */
rt_schedule_insert_thread(thread);
}
else
{
thread->current_priority = *(rt_uint8_t *)arg;

/* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 5bit */
thread->number_mask = 1 << thread->number;
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
#endif
}

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

case RT_THREAD_CTRL_STARTUP:
return rt_thread_startup(thread);

#ifdef RT_USING_HEAP
case RT_THREAD_CTRL_CLOSE:
return rt_thread_delete(thread);
#endif

default:
break;
}

return RT_EOK;
}
RTM_EXPORT(rt_thread_control);