Part 1
요약·해설
reschedule, sleep, no-wait, isr-ok, pre-kernel-ok, async, supervisor의 의미와 서로의 함의·호출 가능 조건을 다룹니다.
reschedule · sleep
ISR · Pre-kernel
no-wait · async
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
E6B873F962FDE14FFB384960EA230AC7C1E4F0715FEB6C31A3BEBD3A82C6BF3B
.. _api_terms:
API Terminology
###############
The following terms may be used as shorthand API tags to indicate the
allowed calling context (thread, ISR, pre-kernel), the effect of a call
on the current thread state, and other behavioral characteristics.
:ref:`api_term_reschedule`
if executing the function reaches a reschedule point
:ref:`api_term_sleep`
if executing the function can cause the invoking thread to sleep
:ref:`api_term_no-wait`
if a parameter to the function can prevent the invoking thread from
trying to sleep
:ref:`api_term_isr-ok`
if the function can be safely called and will have its specified
effect whether invoked from interrupt or thread context
:ref:`api_term_pre-kernel-ok`
if the function can be safely called before the kernel has been fully
initialized and will have its specified effect when invoked from that
context.
:ref:`api_term_async`
if the function may return before the operation it initializes is
complete (i.e. function return and operation completion are
asynchronous)
:ref:`api_term_supervisor`
if the calling thread must have supervisor privileges to execute the
function
Details on the behavioral impact of each attribute are in the following
sections.
.. _api_term_reschedule:
reschedule
==========
The reschedule attribute is used on a function that can reach a
:ref:`reschedule point <scheduling_v2>` within its execution.
Details
-------
The significance of this attribute is that when a rescheduling function
is invoked by a thread it is possible for that thread to be suspended as
a consequence of a higher-priority thread being made ready. Whether the
suspension actually occurs depends on the operation associated with the
reschedule point and the relative priorities of the invoking thread and
the head of the ready queue.
Note that in the case of timeslicing, or reschedule points executed from
interrupts, any thread may be suspended in any function.
Functions that are not **reschedule** may be invoked from either thread
or interrupt context.
Functions that are **reschedule** may be invoked from thread context.
Functions that are **reschedule** but not **sleep** may be invoked from
interrupt context.
.. _api_term_sleep:
sleep
=====
The sleep attribute is used on a function that can cause the invoking
thread to :ref:`sleep <scheduling_v2>`.
Explanation
-----------
This attribute is of relevance specifically when considering
applications that use only non-preemptible threads, because the kernel
will not replace a running cooperative-only thread at a reschedule point
unless that thread has explicitly invoked an operation that caused it to
sleep.
This attribute does not imply the function will sleep unconditionally,
but that the operation may require an invoking thread that would have to
suspend, wait, or invoke :c:func:`k_yield` before it can complete
its operation. This behavior may be mediated by **no-wait**.
Functions that are **sleep** are implicitly **reschedule**.
Functions that are **sleep** may be invoked from thread context.
Functions that are **sleep** may be invoked from interrupt and
pre-kernel contexts if and only if invoked in **no-wait** mode.
.. _api_term_no-wait:
no-wait
=======
The no-wait attribute is used on a function that is also **sleep** to
indicate that a parameter to the function can force an execution path
that will not cause the invoking thread to sleep.
Explanation
-----------
The paradigmatic case of a no-wait function is a function that takes a
timeout, to which :c:macro:`K_NO_WAIT` can be passed. The semantics of
this special timeout value are to execute the function's operation as
long as it can be completed immediately, and to return an error code
rather than sleep if it cannot.
It is use of the no-wait feature that allows functions like
:c:func:`k_sem_take` to be invoked from ISRs, since it is not
permitted to sleep in interrupt context.
A function with a no-wait path does not imply that taking that path
guarantees the function is synchronous.
Functions with this attribute may be invoked from interrupt and
pre-kernel contexts only when the parameter selects the no-wait path.
.. _api_term_isr-ok:
isr-ok
======
The isr-ok attribute is used on a function to indicate that it works
whether it is being invoked from interrupt or thread context.
Explanation
-----------
Any function that is not **sleep** is inherently **isr-ok**. Functions
that are **sleep** are **isr-ok** if the implementation ensures that the
documented behavior is implemented even if called from an interrupt
context. This may be achieved by having the implementation detect the
calling context and transfer the operation that would sleep to a thread,
or by documenting that when invoked from a non-thread context the
function will return a specific error (generally ``-EWOULDBLOCK``).
Note that a function that is **no-wait** is safe to call from interrupt
context only when the no-wait path is selected. **isr-ok** functions
need not provide a no-wait path.
.. _api_term_pre-kernel-ok:
pre-kernel-ok
=============
The pre-kernel-ok attribute is used on a function to indicate that it
works as documented even when invoked before the kernel main thread has
been started.
Explanation
-----------
This attribute is similar to **isr-ok** in function, but is intended for
use by any API that is expected to be called in :c:macro:`DEVICE_DEFINE()`
or :c:macro:`SYS_INIT()` calls that may be invoked with ``PRE_KERNEL_1``
or ``PRE_KERNEL_2`` initialization levels.
Generally a function that is **pre-kernel-ok** checks
:c:func:`k_is_pre_kernel` when determining whether it can fulfill its
required behavior. In many cases it would also check
:c:func:`k_is_in_isr` so it can be **isr-ok** as well.
.. _api_term_async:
async
=====
A function is **async** (i.e. asynchronous) if it may return before the
operation it initiates has completed. An asynchronous function will
generally provide a mechanism by which operation completion is reported,
e.g. a callback or event.
A function that is not asynchronous is synchronous, i.e. the operation
will always be complete when the function returns. As most functions
are synchronous this behavior does not have a distinct attribute to
identify it.
Explanation
-----------
Be aware that **async** is orthogonal to context-switching. Some APIs
may provide completion information through a callback, but may suspend
while waiting for the resource necessary to initiate the operation; an
example is :c:func:`spi_transceive_signal`.
If a function is both **no-wait** and **async** then selecting the
no-wait path only guarantees that the function will not sleep. It does
not affect whether the operation will be completed before the function
returns.
.. _api_term_supervisor:
supervisor
==========
The supervisor attribute is relevant only in user-mode applications, and
indicates that the function cannot be invoked from user mode.
Part 3
한국어 전문 번역
API 용어
다음 용어는 허용되는 호출 context(thread, ISR, pre-kernel), 호출이 현재 thread 상태에 주는 영향, 그 밖의 동작 특성을 나타내는 축약 API tag로 사용할 수 있습니다.
- reschedule
Function 실행 중 reschedule point에 도달할 수 있음을 나타냅니다.
- sleep
Function 실행으로 호출 thread가 sleep할 수 있음을 나타냅니다.
- no-wait
Function parameter가 호출 thread가 sleep을 시도하지 않게 만들 수 있음을 나타냅니다.
- isr-ok
Interrupt context와 thread context 어느 쪽에서 호출해도 안전하고 지정된 효과를 내는 function임을 나타냅니다.
- pre-kernel-ok
Kernel 초기화가 완전히 끝나기 전에도 안전하게 호출할 수 있고 그 context에서 지정된 효과를 내는 function임을 나타냅니다.
- async
Function이 시작한 operation이 완료되기 전에 반환할 수 있음을 나타냅니다. 즉 function return과 operation completion이 asynchronous입니다.
- supervisor
호출 thread가 function을 실행하려면 supervisor privilege가 필요함을 나타냅니다.
각 attribute가 동작에 미치는 자세한 영향은 다음 절에서 설명합니다.
reschedule
reschedule attribute는 실행 중 reschedule point에 도달할 수 있는 function에 사용합니다.
세부 내용
Thread가 rescheduling function을 호출하면 더 높은 priority의 thread가 ready 상태가 되는 결과로 호출 thread가 suspend될 수 있다는 의미입니다. 실제 suspend 여부는 reschedule point와 관련된 operation, 호출 thread의 relative priority, ready queue head의 priority에 따라 달라집니다.
Timeslicing 또는 interrupt에서 실행된 reschedule point의 경우에는 어떤 function을 실행 중이든 어느 thread라도 suspend될 수 있습니다.
reschedule이 아닌 function은 thread context 또는 interrupt context에서 호출할 수 있습니다.
reschedule function은 thread context에서 호출할 수 있습니다.
reschedule이지만 sleep은 아닌 function은 interrupt context에서도 호출할 수 있습니다.
sleep
sleep attribute는 호출 thread를 sleep 상태로 만들 수 있는 function에 사용합니다.
설명
이 attribute는 non-preemptible thread만 사용하는 application을 검토할 때 특히 중요합니다. 실행 중인 cooperative-only thread가 명시적으로 sleep을 일으키는 operation을 호출하지 않으면 kernel은 reschedule point에서 그 thread를 다른 thread로 교체하지 않기 때문입니다.
이 attribute는 function이 무조건 sleep한다는 뜻이 아닙니다. Operation을 완료하기 전에 호출 thread가 suspend·wait하거나 k_yield()를 호출해야 할 수 있다는 뜻입니다. 이 동작은 no-wait에 의해 조정될 수 있습니다.
sleep function은 암묵적으로 reschedule입니다.
sleep function은 thread context에서 호출할 수 있습니다.
sleep function은 no-wait mode로 호출할 때에만 interrupt와 pre-kernel context에서 호출할 수 있습니다.
no-wait
no-wait attribute는 sleep function의 parameter가 호출 thread를 sleep시키지 않는 execution path를 강제할 수 있음을 나타냅니다.
설명
대표적인 no-wait function은 timeout을 parameter로 받고 K_NO_WAIT를 전달할 수 있는 function입니다. 이 특별한 timeout은 operation을 즉시 완료할 수 있을 때만 실행하고, 불가능하면 sleep 대신 error code를 반환합니다.
k_sem_take() 같은 function을 ISR에서 호출할 수 있는 이유가 no-wait feature입니다. Interrupt context에서는 sleep이 허용되지 않습니다.
No-wait path가 있다고 해서 그 path를 선택한 function이 synchronous라는 보장은 없습니다.
이 attribute의 function은 parameter가 no-wait path를 선택할 때에만 interrupt와 pre-kernel context에서 호출할 수 있습니다.
isr-ok
isr-ok attribute는 interrupt context와 thread context 어느 쪽에서 호출해도 동작하는 function임을 나타냅니다.
설명
sleep이 아닌 모든 function은 본질적으로 isr-ok입니다. sleep function도 interrupt context에서 호출했을 때 문서화된 동작을 구현하도록 보장하면 isr-ok입니다. Implementation이 호출 context를 감지해 sleep할 operation을 thread로 넘기거나, non-thread context에서는 특정 error, 일반적으로 -EWOULDBLOCK을 반환한다고 문서화하는 방식으로 구현할 수 있습니다.
no-wait function은 no-wait path를 선택했을 때에만 interrupt context에서 안전합니다. isr-ok function이 반드시 no-wait path를 제공할 필요는 없습니다.
pre-kernel-ok
pre-kernel-ok attribute는 kernel main thread가 시작되기 전에 호출해도 문서화된 대로 동작하는 function임을 나타냅니다.
설명
isr-ok과 기능이 비슷하지만 DEVICE_DEFINE() 또는 SYS_INIT() call에서 호출될 것으로 예상되는 API에 사용합니다. 이런 call은 PRE_KERNEL_1 또는 PRE_KERNEL_2 initialization level에서 실행될 수 있습니다.
일반적으로 pre-kernel-ok function은 요구 동작을 수행할 수 있는지 결정할 때 k_is_pre_kernel()를 확인합니다. 많은 경우 k_is_in_isr()도 확인해 isr-ok이 되게 합니다.
async
async, 즉 asynchronous function은 시작한 operation이 완료되기 전에 반환할 수 있습니다. 일반적으로 callback이나 event처럼 operation completion을 보고하는 mechanism을 제공합니다.
Asynchronous가 아닌 function은 synchronous입니다. 즉 function이 반환할 때 operation이 항상 완료되어 있습니다. 대부분의 function이 synchronous이므로 이 동작을 식별하는 별도 attribute는 없습니다.
설명
async는 context switching과 독립적인 성질입니다. 일부 API는 callback으로 completion 정보를 제공하지만 operation 시작에 필요한 resource를 기다리며 suspend될 수 있습니다. spi_transceive_signal()이 그 예입니다.
Function이 no-wait이면서 async라면 no-wait path 선택은 function이 sleep하지 않는다는 것만 보장합니다. Function 반환 전에 operation이 완료되는지에는 영향을 주지 않습니다.
supervisor
supervisor attribute는 user-mode application에만 관련되며 function을 user mode에서 호출할 수 없음을 나타냅니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.