kernel/services/other/thread_local_storage.html · kernel/services/other/thread_local_storage.rst 공식 원문 ↗

Zephyr 3.7.0 · Kernel · Other Services

Thread Local Storage

Thread stack마다 variable copy를 만들고 user thread별 errno 접근을 system call 없이 제공합니다.

Official pathkernel/services/other/thread_local_storage.html
Source filekernel/services/other/thread_local_storage.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Architecture·toolchain gate, CONFIG_THREAD_LOCAL_STORAGE와 CONFIG_ERRNO_IN_TLS, extern·static __thread 선언을 설명합니다.

Scope

one copy per thread

Storage

thread stack

Syntax

__thread

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 485995329F5C676F28D810FE3C6D97FB4A8A8D0D94CE81399F380153E45456FD
.. _thread_local_storage:

Thread Local Storage (TLS)
##########################

Thread Local Storage (TLS) allows variables to be allocated on a per-thread
basis. These variables are stored in the thread stack which means every
thread has its own copy of these variables.

Zephyr currently requires toolchain support for TLS.


Configuration
*************

To enable thread local storage in Zephyr, :kconfig:option:`CONFIG_THREAD_LOCAL_STORAGE`
needs to be enabled. Note that this option may not be available if
the architecture or the SoC does not have the hidden option
:kconfig:option:`CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE` enabled, which means
the architecture or the SoC does not have the necessary code to support
thread local storage and/or the toolchain does not support TLS.

:kconfig:option:`CONFIG_ERRNO_IN_TLS` can be enabled together with
:kconfig:option:`CONFIG_ERRNO` to let the variable ``errno`` be a thread local
variable. This allows user threads to access the value of ``errno`` without
making a system call.


Declaring and Using Thread Local Variables
******************************************

The keyword ``__thread`` can be used to declare thread local variables.

For example, to declare a thread local variable in header files:

.. code-block:: c

   extern __thread int i;

And to declare the actual variable in source files:

.. code-block:: c

   __thread int i;

Keyword ``static`` can also be used to limit the variable within a source file:

.. code-block:: c

   static __thread int j;

Using the thread local variable is the same as using other variable, for example:

.. code-block:: c

   void testing(void) {
       i = 10;
   }

Part 3

한국어 전문 번역

Thread Local Storage(TLS)는 variable을 thread마다 별도로 할당합니다. Variable은 thread stack에 저장되므로 각 thread가 자신만의 copy를 가집니다.

Zephyr TLS는 현재 toolchain 지원이 필요합니다.

설정

CONFIG_THREAD_LOCAL_STORAGE를 켜야 합니다. Architecture 또는 SoC가 hidden option CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE를 제공하지 않으면 필요한 architecture/SoC code나 toolchain TLS 지원이 없다는 뜻이므로 option을 선택할 수 없습니다.

CONFIG_ERRNO와 함께 CONFIG_ERRNO_IN_TLS를 켜면 errno를 thread-local variable로 만듭니다. User thread는 system call 없이 자신의 errno 값을 읽을 수 있습니다.

Thread-local variable 선언과 사용

__thread keyword로 thread-local variable을 선언합니다. Header file에서는 extern declaration을 둡니다.

extern __thread int i;

Source file에서는 실제 variable을 정의합니다.

__thread int i;

static을 함께 써서 source file 내부로 범위를 제한할 수도 있습니다.

static __thread int j;

Thread-local variable을 사용하는 문법은 일반 variable과 같습니다. 다음 assignment는 실행 중인 thread의 i copy만 바꿉니다.

void testing(void) {
    i = 10;
}

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.