user/lib/libc/src/threads/tls_static.c
/**
 * Allocates a thread local structure
 *
 * @return Base address of the TLS region (as allocated) or NULL if there is no TLS region
 */
Allocates a thread local structure
@return Base address of the TLS region (as allocated) or NULL if there is no TLS region
Allocates a thread local structure @return Base address of the TLS region ( as allocated ) or NULL if there is no TLS region
static void *AllocTls() {
    uthread_t *thrd = thrd_current();
    int err;
    void *base;
    const size_t tlsSize = (const size_t) &__tls_size;
    if(!tlsSize) return NULL;
    const size_t tlsAlignment = 0x8; 
    size_t allocAlign = max(tlsAlignment, alignof(tls_block_static_t));
    size_t allocSize = alignup(tlsSize, allocAlign) + sizeof(tls_block_static_t);
    err = posix_memalign(&base, allocAlign, allocSize);
    assert(!err);
    memset(base, 0, allocAlign);
    tls_block_static_t *info = (tls_block_static_t *) (((uint8_t *) base) + alignup(tlsSize, allocAlign));
    uint8_t *tls = ((uint8_t *) info) - alignup(tlsSize, tlsAlignment);
    memset(info, 0, sizeof *info);
    info->self = info;
    info->thread = thrd;
    const size_t tdataSize = (const size_t) &__tls_data_size;
    if(tdataSize) {
        void *from = (void *) &__tls_data_start;
        memcpy(tls, from, tdataSize);
    }
    SetCurrentTlsBase(info);
    thrd->tls.s.base = base;
    thrd->tls.s.length = allocSize;
    thrd->tls.s.tlsRegionLength = alignup(tlsSize, tlsAlignment);
    return base;
}
static void * AllocTls ( ) { uthread_t * thrd = thrd_current ( ) ; int err ; void * base ; const size_t tlsSize = ( const size_t ) & __tls_size ; if ( ! tlsSize ) return NULL ; const size_t tlsAlignment = 0x8 ; size_t allocAlign = max ( tlsAlignment , alignof ( tls_block_static_t ) ) ; size_t allocSize = alignup ( tlsSize , allocAlign ) + sizeof ( tls_block_static_t ) ; err = posix_memalign ( & base , allocAlign , allocSize ) ; assert ( ! err ) ; memset ( base , 0 , allocAlign ) ; tls_block_static_t * info = ( tls_block_static_t * ) ( ( ( uint8_t * ) base ) + alignup ( tlsSize , allocAlign ) ) ; uint8_t * tls = ( ( uint8_t * ) info ) - alignup ( tlsSize , tlsAlignment ) ; memset ( info , 0 , sizeof * info ) ; info -> self = info ; info -> thread = thrd ; const size_t tdataSize = ( const size_t ) & __tls_data_size ; if ( tdataSize ) { void * from = ( void * ) & __tls_data_start ; memcpy ( tls , from , tdataSize ) ; } SetCurrentTlsBase ( info ) ; thrd -> tls . s . base = base ; thrd -> tls . s . length = allocSize ; thrd -> tls . s . tlsRegionLength = alignup ( tlsSize , tlsAlignment ) ; return base ; }
Allocates a thread local structure
 @return Base address of the TLS region (as allocated) or NULL if there is no TLS region
Allocates a thread local structure @return Base address of the TLS region ( as allocated ) or NULL if there is no TLS region
// how much thread local storage do we need? // allocate the memory and ensure it's zeroed // TODO: read from ELF // set up pointers // copy the data // update the thread-local pointer