Part 1
요약·해설
Main과 idle thread의 생성 목적, priority, 종료·abort 규칙과 single-thread application의 main 활용법을 설명합니다.
init · main()
PM · yield
essential thread
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
CED7C024F291BE2155597EA1F2B94B9FD2B4FAD0EA26A0676A5F1ECE8524E437
.. _system_threads_v2:
System Threads
##############
.. contents::
:local:
:depth: 2
A :dfn:`system thread` is a thread that the kernel spawns automatically
during system initialization.
The kernel spawns the following system threads:
**Main thread**
This thread performs kernel initialization, then calls the application's
:c:func:`main` function (if one is defined).
By default, the main thread uses the highest configured preemptible thread
priority (i.e. 0). If the kernel is not configured to support preemptible
threads, the main thread uses the lowest configured cooperative thread
priority (i.e. -1).
The main thread is an essential thread while it is performing kernel
initialization or executing the application's :c:func:`main` function;
this means a fatal system error is raised if the thread aborts. If
:c:func:`main` is not defined, or if it executes and then does a normal
return, the main thread terminates normally and no error is raised.
**Idle thread**
This thread executes when there is no other work for the system to do.
If possible, the idle thread activates the board's power management support
to save power; otherwise, the idle thread simply performs a "do nothing"
loop. The idle thread remains in existence as long as the system is running
and never terminates.
The idle thread always uses the lowest configured thread priority.
If this makes it a cooperative thread, the idle thread repeatedly
yields the CPU to allow the application's other threads to run when
they need to.
The idle thread is an essential thread, which means a fatal system error
is raised if the thread aborts.
Additional system threads may also be spawned, depending on the kernel
and board configuration options specified by the application. For example,
enabling the system workqueue spawns a system thread
that services the work items submitted to it. (See :ref:`workqueues_v2`.)
Implementation
**************
Writing a main() function
=========================
An application-supplied :c:func:`main` function begins executing once
kernel initialization is complete. The kernel does not pass any arguments
to the function.
The following code outlines a trivial :c:func:`main` function.
The function used by a real application can be as complex as needed.
.. code-block:: c
int main(void)
{
/* initialize a semaphore */
...
/* register an ISR that gives the semaphore */
...
/* monitor the semaphore forever */
while (1) {
/* wait for the semaphore to be given by the ISR */
...
/* do whatever processing is now needed */
...
}
}
Suggested Uses
**************
Use the main thread to perform thread-based processing in an application
that only requires a single thread, rather than defining an additional
application-specific thread.
Part 3
한국어 전문 번역
System thread는 system initialization 도중 kernel이 자동으로 spawn하는 thread입니다.
Kernel이 만드는 system thread
Main thread
Main thread는 kernel initialization을 수행한 다음, application에 main 함수가 정의되어 있으면 이를 호출합니다.
기본적으로 main thread는 구성된 preemptible priority 중 가장 높은 priority, 즉 0을 사용합니다. Kernel이 preemptible thread를 지원하지 않도록 구성됐다면 구성된 cooperative priority 중 가장 낮은 priority, 즉 -1을 사용합니다.
Kernel initialization을 수행하거나 application main을 실행하는 동안 main thread는 essential thread입니다. 따라서 이때 abort되면 fatal system error가 발생합니다. main이 정의되지 않았거나 main이 정상 return하면 main thread는 오류 없이 정상 종료합니다.
Idle thread
Idle thread는 system에 다른 일이 없을 때 실행됩니다. 가능하면 board의 power management를 활성화해 전력을 절약하고, 그렇지 않으면 아무 일도 하지 않는 loop를 반복합니다. System이 실행되는 동안 계속 존재하며 절대 종료하지 않습니다.
Idle thread는 항상 구성된 가장 낮은 thread priority를 사용합니다. 이 priority가 cooperative class라면 application의 다른 thread가 필요할 때 실행될 수 있도록 CPU를 반복해서 yield합니다.
Idle thread도 essential thread이므로 abort되면 fatal system error가 발생합니다.
kernel initialization → application main() → 정상 return 시 종료
다른 work가 없을 때 PM 또는 do-nothing loop · 항상 존속
configuration에 따라 system workqueue 등 추가 thread 생성
Application이 지정한 kernel·board configuration option에 따라 system thread가 더 생길 수 있습니다. 예를 들어 system workqueue를 켜면 제출된 work item을 처리하는 system thread가 생성됩니다.
구현
main() 함수 작성
Application이 제공한 main 함수는 kernel initialization이 끝나면 실행됩니다. Kernel은 이 함수에 argument를 전달하지 않습니다.
다음 코드는 단순한 main 함수의 윤곽입니다. 실제 application에서는 필요한 만큼 복잡하게 작성할 수 있습니다.
int main(void)
{
/* initialize a semaphore */
...
/* register an ISR that gives the semaphore */
...
/* monitor the semaphore forever */
while (1) {
/* wait for the semaphore to be given by the ISR */
...
/* do whatever processing is now needed */
...
}
}권장 용도
Application에 thread 기반 처리가 하나만 필요하다면 별도의 application-specific thread를 정의하는 대신 main thread에서 처리합니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.