Changeset c8680be4 in mainline


Ignore:
Timestamp:
2025-03-09T20:10:46Z (11 hours ago)
Author:
GitHub <noreply@…>
Parents:
0ae9e18 (diff), 98743e2 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Matěj Volf <mat.volfik@…> (2025-03-09 20:10:46)
git-committer:
GitHub <noreply@…> (2025-03-09 20:10:46)
Message:

Merge 98743e252b3fa43cef96b1ea3617304534b2fcc8 into 0ae9e18465809a5520c51c76be2866b19c48bd0e

Location:
uspace/lib
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/libc.c

    r0ae9e18 rc8680be4  
    8181                main_fibril.tcb = tls_make_initial(__progsymbols.elfstart);
    8282        }
     83        main_fibril.is_freeable = false;
    8384
    8485        assert(main_fibril.tcb);
  • uspace/lib/c/generic/rtld/symbol.c

    r0ae9e18 rc8680be4  
    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 rc8680be4  
    3030includes += include_directories('include/posix', 'include')
    3131c_args += [ '-fno-builtin', '-D_XOPEN_SOURCE' ]
     32allow_shared = true
    3233
    3334# TODO
     
    6162        'test/stdlib.c',
    6263        'test/unistd.c',
     64        'test/pthread/keys.c',
    6365)
    6466
  • uspace/lib/posix/src/pthread/keys.c

    r0ae9e18 rc8680be4  
    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 fibril_local bool fibril_initialized = false;
     46static atomic_ushort next_key = 1; // skip the key 'zero'
     47
     48/*
     49 * For now, we just support maximum of 100 keys. This can be improved
     50 * in the future by implementing a dynamically growing array with
     51 * reallocations, but that will require more synchronization.
     52 */
     53#define PTHREAD_KEYS_MAX 100
     54
     55static fibril_local void *key_data[PTHREAD_KEYS_MAX];
    3956
    4057void *pthread_getspecific(pthread_key_t key)
    4158{
    42         not_implemented();
    43         return NULL;
     59        // initialization is done in setspecific -> if not initialized, nothing was set yet
     60        if (!fibril_initialized) {
     61                DPRINTF("pthread_getspecific(%d) = NULL (uninitialized)\n", key);
     62                return NULL;
     63        }
     64
     65        assert(key < PTHREAD_KEYS_MAX);
     66        assert(key < next_key);
     67        assert(key > 0);
     68
     69        DPRINTF("pthread_getspecific(%d) = %p\n", key, key_data[key]);
     70        return key_data[key];
    4471}
    4572
    4673int pthread_setspecific(pthread_key_t key, const void *data)
    4774{
    48         not_implemented();
    49         return ENOTSUP;
     75        DPRINTF("pthread_setspecific(%d, %p)\n", key, data);
     76        if (!fibril_initialized) {
     77                DPRINTF("initializing pthread keys\n");
     78                for (unsigned i = 0; i < PTHREAD_KEYS_MAX; i++) {
     79                        key_data[i] = NULL;
     80                }
     81                fibril_initialized = true;
     82        }
     83        assert(key < PTHREAD_KEYS_MAX);
     84        assert(key < next_key);
     85        assert(key > 0);
     86
     87        key_data[key] = (void *) data;
     88        return EOK;
    5089}
    5190
    5291int pthread_key_delete(pthread_key_t key)
    5392{
     93        // see https://github.com/HelenOS/helenos/pull/245#issuecomment-2706795848
    5494        not_implemented();
    55         return ENOTSUP;
     95        return EOK;
    5696}
    5797
    5898int pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
    5999{
    60         not_implemented();
    61         return ENOTSUP;
     100        unsigned short k = atomic_fetch_add(&next_key, 1);
     101        DPRINTF("pthread_key_create(%p, %p) = %d\n", key, destructor, k);
     102        if (k >= PTHREAD_KEYS_MAX) {
     103                atomic_store(&next_key, PTHREAD_KEYS_MAX + 1);
     104                return ELIMIT;
     105        }
     106        if (destructor != NULL) {
     107                static int __counter = 0;
     108                if (__counter == 0) {
     109                        fprintf(stderr, "pthread_key_create: destructors not supported\n");
     110                }
     111                __counter++;
     112        }
     113
     114        *key = k;
     115        return EOK;
    62116}
    63117
  • uspace/lib/posix/test/main.c

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