Supports storage (variables and stack space) for every creatd thread and the main thread. The main thread is initialized with <initmain_threadtls> all other with <init_threadtls>.
ThreadLocalStorage | Supports storage (variables and stack space) for every creatd thread and the main thread. |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file ThreadLocalStorage. |
C-kern/ | Implementation file ThreadLocalStorage impl. |
Types | |
struct thread_tls_t | Export thread_tls_t into global namespace. |
Functions | |
test | |
unittest_platform_task_thread_tls | Test thread_tls_t functionality. |
thread_tls_t | Holds thread local memory. |
lifetime | |
thread_tls_FREE | Static initializer. |
init_threadtls | Allocates a memory block big enoug to hold all thread local storage data. |
free_threadtls | Changes protection of memory to normal and frees it. |
initmain_threadtls | Same as init_threadtls but calls no other functions of C-kern system. |
freemain_threadtls | Same as free_threadtls but calls no other functions of C-kern system. |
query | |
current_threadtls | Returns thread_tls_t of the current thread. |
context_threadtls | Returns pointer to threadcontext_t stored in thread local storage. |
thread_threadtls | Returns pointer to thread_t stored in thread local storage. |
size_threadtls | Returns the size of the allocated memory block. |
signalstack_threadtls | Returns in stackmem the signalstack from tls. |
threadstack_threadtls | Returns in stackmem the thread stack from tls. |
generic | |
genericcast_threadtls | Casts pointer to an object into pointer to thread_tls_t. |
inline implementation | |
Macros | |
context_threadtls | Implements thread_tls_t.context_threadtls. |
current_threadtls | Implements thread_tls_t.current_threadtls. |
genericcast_threadtls | Implements thread_tls_t.genericcast_threadtls. |
thread_threadtls | Implements thread_tls_t.thread_threadtls. |
size_threadtls | Implements thread_tls_t.size_threadtls. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2013 Jörg Seebohn
Header file ThreadLocalStorage.
Implementation file ThreadLocalStorage impl.
typedef struct thread_tls_t thread_tls_t
Export thread_tls_t into global namespace.
test | |
unittest_platform_task_thread_tls | Test thread_tls_t functionality. |
int unittest_platform_task_thread_tls( void )
Test thread_tls_t functionality.
struct thread_tls_t
Holds thread local memory. The memory comprises the variables thread_t and threadcontext_t, the signal stack and thread stack and 3 protection pages in between.
lifetime | |
thread_tls_FREE | Static initializer. |
init_threadtls | Allocates a memory block big enoug to hold all thread local storage data. |
free_threadtls | Changes protection of memory to normal and frees it. |
initmain_threadtls | Same as init_threadtls but calls no other functions of C-kern system. |
freemain_threadtls | Same as free_threadtls but calls no other functions of C-kern system. |
query | |
current_threadtls | Returns thread_tls_t of the current thread. |
context_threadtls | Returns pointer to threadcontext_t stored in thread local storage. |
thread_threadtls | Returns pointer to thread_t stored in thread local storage. |
size_threadtls | Returns the size of the allocated memory block. |
signalstack_threadtls | Returns in stackmem the signalstack from tls. |
threadstack_threadtls | Returns in stackmem the thread stack from tls. |
generic | |
genericcast_threadtls | Casts pointer to an object into pointer to thread_tls_t. |
int init_threadtls( /*out*/thread_tls_t * tls )
Allocates a memory block big enoug to hold all thread local storage data. The access rights of parts of the memory block is changed to protect the stack from overflowing. The allocated memory block is aligned to its own size. The thread local variables (threadcontext_t and thread_t) are initialized with their INIT_STATIC resp. INIT_FREEABLE values.
int initmain_threadtls( /*out*/thread_tls_t * tls, /*out*/struct memblock_t * threadstack, /*out*/struct memblock_t * signalstack )
Same as init_threadtls but calls no other functions of C-kern system. Called from platform_t.init_platform. Especially no logging is done and no calls to pagesize_vm and <initaligned_vmpage> are made.
int freemain_threadtls( thread_tls_t * tls )
Same as free_threadtls but calls no other functions of C-kern system. Especially no logging is done and no calls to pagesize_vm and <free_vmpage> are made.
thread_tls_t current_threadtls( void * local_var )
Returns thread_tls_t of the current thread. The parameter local_var must point to a local variable on the current stack. The function <sys_context_thread> is identical with context_threadtls(¤t_threadtls(&err)). If you change this function change sys_context_thread (and self_thread) also.
struct threadcontext_t * context_threadtls( const thread_tls_t * tls )
Returns pointer to threadcontext_t stored in thread local storage. The function <sys_context_thread> is identical with context_threadtls(¤t_threadtls()). If you change this function change sys_context_thread also.
struct thread_t * thread_threadtls( const thread_tls_t * tls )
Returns pointer to thread_t stored in thread local storage. The function self_thread is identical with thread_threadtls(¤t_threadtls()). If you change this function change self_thread also.
void signalstack_threadtls( const thread_tls_t * tls, /*out*/struct memblock_t * stackmem )
Returns in stackmem the signalstack from tls. If tls is in a freed state stackmem is set to <memblock_FREE>. The signal stack is used in case of a signal (exceptions). For example if the thread stack overflows SIGSEGV signal is thrown. To handle this case the system must have an extra signal stack cause signal handling needs stack space.
thread_tls_t * genericcast_threadtls( void * obj, IDNAME nameprefix )
Casts pointer to an object into pointer to thread_tls_t. The object must have a single member with name nameprefix##addr of the same type as thread_tls_t.
#define context_threadtls( tls ) ((threadcontext_t*) ( (tls)->addr ))
Implements thread_tls_t.context_threadtls.
#define current_threadtls( local_var ) (thread_tls_t) { (uint8_t*) ( (uintptr_t)(local_var) & ~(uintptr_t) (size_threadtls()-1) ) }
Implements thread_tls_t.current_threadtls.
#define genericcast_threadtls( obj, nameprefix ) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( sizeof(_obj->nameprefix##addr) == sizeof(((thread_tls_t*)0)->addr) && (typeof(_obj->nameprefix##addr))10 == (uint8_t*)10 && 0 == offsetof(thread_tls_t, addr), "obj is compatible") ; (thread_tls_t *)(&_obj->nameprefix##addr) ; }))
Implements thread_tls_t.genericcast_threadtls.
#define thread_threadtls( tls ) ((thread_t*) ( (tls)->addr + sizeof(threadcontext_t) ))
Implements thread_tls_t.thread_threadtls.
#define size_threadtls( ) (sys_size_threadtls())
Implements thread_tls_t.size_threadtls.
Export thread_tls_t into global namespace.
typedef struct thread_tls_t thread_tls_t
Holds thread local memory.
struct thread_tls_t
Test thread_tls_t functionality.
int unittest_platform_task_thread_tls( void )
Static initializer.
#define thread_tls_FREE { 0 }
Allocates a memory block big enoug to hold all thread local storage data.
int init_threadtls( /*out*/thread_tls_t * tls )
Changes protection of memory to normal and frees it.
int free_threadtls( thread_tls_t * tls )
Same as init_threadtls but calls no other functions of C-kern system.
int initmain_threadtls( /*out*/thread_tls_t * tls, /*out*/struct memblock_t * threadstack, /*out*/struct memblock_t * signalstack )
Same as free_threadtls but calls no other functions of C-kern system.
int freemain_threadtls( thread_tls_t * tls )
Returns thread_tls_t of the current thread.
thread_tls_t current_threadtls( void * local_var )
Returns pointer to threadcontext_t stored in thread local storage.
struct threadcontext_t * context_threadtls( const thread_tls_t * tls )
Returns pointer to thread_t stored in thread local storage.
struct thread_t * thread_threadtls( const thread_tls_t * tls )
Returns the size of the allocated memory block.
size_t size_threadtls( void )
Returns in stackmem the signalstack from tls.
void signalstack_threadtls( const thread_tls_t * tls, /*out*/struct memblock_t * stackmem )
Returns in stackmem the thread stack from tls.
void threadstack_threadtls( const thread_tls_t * tls, /*out*/struct memblock_t * stackmem )
Casts pointer to an object into pointer to thread_tls_t.
thread_tls_t * genericcast_threadtls( void * obj, IDNAME nameprefix )
Implements thread_tls_t.context_threadtls.
#define context_threadtls( tls ) ((threadcontext_t*) ( (tls)->addr ))
Implements thread_tls_t.current_threadtls.
#define current_threadtls( local_var ) (thread_tls_t) { (uint8_t*) ( (uintptr_t)(local_var) & ~(uintptr_t) (size_threadtls()-1) ) }
Implements thread_tls_t.genericcast_threadtls.
#define genericcast_threadtls( obj, nameprefix ) ( __extension__ ({ typeof(obj) _obj = (obj) ; static_assert( sizeof(_obj->nameprefix##addr) == sizeof(((thread_tls_t*)0)->addr) && (typeof(_obj->nameprefix##addr))10 == (uint8_t*)10 && 0 == offsetof(thread_tls_t, addr), "obj is compatible") ; (thread_tls_t *)(&_obj->nameprefix##addr) ; }))
Implements thread_tls_t.thread_threadtls.
#define thread_threadtls( tls ) ((thread_t*) ( (tls)->addr + sizeof(threadcontext_t) ))
Implements thread_tls_t.size_threadtls.
#define size_threadtls( ) (sys_size_threadtls())
Initialize system context and calls main_thread.
int init_platform( mainthread_f main_thread, void * user )
Returns the virtual memory page size supported by the underlying system.
uint32_t pagesize_vm( void )
Implements thread_t.self_thread.
#define self_thread( ) (sys_thread_threadtls())