Changeset da54714 in mainline


Ignore:
Timestamp:
2025-03-11T10:50:28Z (24 hours ago)
Author:
GitHub <noreply@…>
Branches:
master
Parents:
0ae9e18
git-author:
Matěj Volf <git@…> (2025-03-11 10:50:28)
git-committer:
GitHub <noreply@…> (2025-03-11 10:50:28)
Message:

partially implement key-based pthread local storage (#245)

  • check for hash section presence in rtld
  • implement pthread thread-local storage keys without destructors and key recycling
Location:
uspace/lib
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/rtld/symbol.c

    r0ae9e18 rda54714  
    6565static elf_symbol_t *def_find_in_module(const char *name, module_t *m)
    6666{
     67        if (m->dyn.hash == NULL) {
     68                /* No hash table */
     69                return NULL;
     70        }
     71
    6772        elf_symbol_t *sym_table;
    6873        elf_symbol_t *s, *sym;
  • uspace/lib/posix/meson.build

    r0ae9e18 rda54714  
    6161        'test/stdlib.c',
    6262        'test/unistd.c',
     63        'test/pthread/keys.c',
    6364)
    6465
  • uspace/lib/posix/src/pthread/keys.c

    r0ae9e18 rda54714  
    3636#include <pthread.h>
    3737#include <errno.h>
     38#include <fibril.h>
     39#include <stdatomic.h>
    3840#include "../internal/common.h"
     41
     42#include <stdio.h>
     43#define DPRINTF(format, ...) ((void) 0);
     44
     45static atomic_ushort next_key = 1; // skip the key 'zero'
     46
     47/*
     48 * For now, we just support maximum of 100 keys. This can be improved
     49 * in the future by implementing a dynamically growing array with
     50 * reallocations, but that will require more synchronization.
     51 */
     52#define PTHREAD_KEYS_MAX 100
     53
     54static fibril_local void *key_data[PTHREAD_KEYS_MAX];
    3955
    4056void *pthread_getspecific(pthread_key_t key)
    4157{
    42         not_implemented();
    43         return NULL;
     58        assert(key < PTHREAD_KEYS_MAX);
     59        assert(key < next_key);
     60        assert(key > 0);
     61
     62        DPRINTF("pthread_getspecific(%d) = %p\n", key, key_data[key]);
     63        return key_data[key];
    4464}
    4565
    4666int pthread_setspecific(pthread_key_t key, const void *data)
    4767{
    48         not_implemented();
    49         return ENOTSUP;
     68        DPRINTF("pthread_setspecific(%d, %p)\n", key, data);
     69        assert(key < PTHREAD_KEYS_MAX);
     70        assert(key < next_key);
     71        assert(key > 0);
     72
     73        key_data[key] = (void *) data;
     74        return EOK;
    5075}
    5176
    5277int pthread_key_delete(pthread_key_t key)
    5378{
     79        /* see https://github.com/HelenOS/helenos/pull/245#issuecomment-2706795848 */
    5480        not_implemented();
    55         return ENOTSUP;
     81        return EOK;
    5682}
    5783
    5884int pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
    5985{
    60         not_implemented();
    61         return ENOTSUP;
     86        unsigned short k = atomic_fetch_add(&next_key, 1);
     87        DPRINTF("pthread_key_create(%p, %p) = %d\n", key, destructor, k);
     88        if (k >= PTHREAD_KEYS_MAX) {
     89                atomic_store(&next_key, PTHREAD_KEYS_MAX + 1);
     90                return ELIMIT;
     91        }
     92        if (destructor != NULL) {
     93                /* Inlined not_implemented() macro to add custom message */
     94                static int __not_implemented_counter = 0;
     95                if (__not_implemented_counter == 0) {
     96                        fprintf(stderr, "pthread_key_create: destructors not supported\n");
     97                }
     98                __not_implemented_counter++;
     99        }
     100
     101        *key = k;
     102        return EOK;
    62103}
    63104
  • uspace/lib/posix/test/main.c

    r0ae9e18 rda54714  
    3434PCUT_IMPORT(stdlib);
    3535PCUT_IMPORT(unistd);
     36PCUT_IMPORT(pthread_keys);
    3637
    3738PCUT_MAIN();
Note: See TracChangeset for help on using the changeset viewer.