RIOT-OS/RIOT — pull request #506 by haukepetersen (2014-01-15 12:27:40+00:00)
Title: Thread-local memory

This PR is a proposal for an implementation to reserve a memory section of the stack memory for thread-local memory. This is especially useful for device drivers that run in their own thread, as there can be any amount of driver instances without the need of reserving memory inside the driver code in advance.

For example if you want to use multiple UARTs on a board, you can just create one thread for each device and each thread will use the same code. The usage could look like this:
```c
// somewhere in the uart code
typdef struct {...} uart_state_t; // struct containing the memory needed for the uart driver

// during initilization:
char mem1[STACKSIZE], mem2[STACKSIZE];
int uart1_pid = thread_create_with_local_mem(mem1, STACKSIZE, sizeof(uart_state_t), PRIO, 0, &uart_1_run, "UART 1");
int uart1_pid = thread_create_with_local_mem(mem2, STACKSIZE, sizeof(uart_state_t), PRIO, 0, &uart_2_run, "UART 1");

[...]

// for accessing the thread local memory inside the uart thread:
uart_state_t *my_state = (uart_state_t*) thread_get_local_mem(thread_getpid());
```

The advantages of this implementation over using malloc() are:
- it is independent from any clib implementation
- no dynamic memory allocation needed
- its platform independent

The only drawback is the use of sizeof(char*) bytes more in the tcb_t struct (e.g. 80byte when having 20 threads on a 32-bit machine...)

Does this work for you or does anyone have a better idea how to do this? I'm open for discussion!