Implementing local thread storage in software

We are moving the embedded application from Windows CE to another system. The current processor is STM32F4. Our current code makes heavy use of TLS. The new prototype uses KEIL CMSIS RTOS, which has very low functionality.

In http://www.keil.com/support/man/docs/armcc/armcc_chr1359124216560.htm it states that the thread local storage is maintained 5.04. Now we are using 5.04. The problem is that when linking our program to the definition of a variable, the __thread int a;linker cannot find __aeabi_read_tpwhat makes sense to me.

My question is: is it possible to implement __aeabi_read_tp, and it will work, or is there more to it?

If this is simply not possible for us: is there a way to implement TLS only in software? Don't talk about performance there yet.

EDIT I ​​tried to implement __aeabi_read_tpby looking at the old freeBSD source and other sources. Although the function is mainly implemented in the assembly, I found a version in C that boils down to the following:

extern "C"
{
    extern osThreadId svcThreadGetId(void);
    void *__aeabi_read_tp()
    {
        return (void*)svcThreadGetId();
    }
}

This basically gives me the ID (void *) of my current executable thread. If I understand correctly, this is what we want. Could this work?

+4
source share
3 answers

CMIS RTOS ( ), , - , - , , , .

, , .

EDIT: TLS pthreads:

#include <pthread.h>

#define MAX_PARALLEL_THREADS 10

static pthread_t threads[MAX_PARALLEL_THREADS];
static struct tls_data tls_data[MAX_PARALLEL_THREADS];
static int tls_data_free_index = 0;

static void *worker_thread(void *arg) {
    static struct tls_data *data = (struct tls_data *) arg;

    /* Code omitted. */
}

static int spawn_thread() {
    if (tls_data_free_index >= MAX_PARALLEL_THREADS) {
        // Consider increasing MAX_PARALLEL_THREADS
        return -1;
    }

    /* Prepare thread data - code omitted. */

    pthread_create(& threads[tls_data_free_index], NULL, worker_thread, & tls_data[tls_data_free_index]);
}
+3

- std::map<threadID, T>. , .

- , .

+1

, , , , .

, __thread thread_local ELF ( ARM AEABI):

https://www.akkadia.org/drepper/tls.pdf

:

  • .tbss / .tdata , , .
  • (TCB) d ynamic t hread-local v t24 > ), . , . ( __aeabi_read_tp())
  • .tdata memset .tbss .
  • , __aeabi_read_tp() , .

, , " ", , , , .

TLS, , , , :

http://www.fsfla.org/~lxoliva/writeups/TLS/RFC-TLSDESC-x86.txt

. , .tbss .tdata, . , , , , . , ​​ TLS. , .

, , . , , API TLS .

0
source

All Articles