Changeset d50f3e5 in mainline for uspace/lib/posix/src/pthread/keys.c


Ignore:
Timestamp:
2025-03-09T20:06:24Z (16 hours ago)
Author:
Matěj Volf <git@…>
Children:
53e652d
Parents:
af28af6
git-author:
Matěj Volf <git@…> (2025-03-09 20:05:51)
git-committer:
Matěj Volf <git@…> (2025-03-09 20:06:24)
Message:

decrease scrope of pthread keys support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/src/pthread/keys.c

    raf28af6 rd50f3e5  
    4040#include "../internal/common.h"
    4141
    42 #define DPRINTF(format, ...) ((void) 0)
     42#include <stdio.h>
     43#define DPRINTF(format, ...) printf("pthread_keys: " format, ##__VA_ARGS__)
    4344
    4445static fibril_local bool fibril_initialized = false;
     
    5152 */
    5253#define PTHREAD_KEYS_MAX 100
    53 static void (*destructors[PTHREAD_KEYS_MAX])(void *);
    5454
    5555static fibril_local void *key_data[PTHREAD_KEYS_MAX];
     
    5858{
    5959        // initialization is done in setspecific -> if not initialized, nothing was set yet
    60         if (!fibril_initialized)
     60        if (!fibril_initialized) {
     61                DPRINTF("pthread_getspecific(%d) = NULL (uninitialized)\n", key);
    6162                return NULL;
     63        }
    6264
    6365        assert(key < PTHREAD_KEYS_MAX);
     
    6567        assert(key > 0);
    6668
     69        DPRINTF("pthread_getspecific(%d) = %p\n", key, key_data[key]);
    6770        return key_data[key];
    68 }
    69 
    70 static void pthread_key_on_fibril_exit(void)
    71 {
    72         if (!fibril_initialized)
    73                 return;
    74 
    75         for (unsigned i = 0; i < PTHREAD_KEYS_MAX; i++) {
    76                 /*
    77                  * Note that this doesn't cause a race with pthread_key_create:
    78                  * if key `i` has not been assigned yet (it could be just being
    79                  * created), key_data[i] has never been assigned, therefore it
    80                  * is NULL, and the destructor is not checked at all.
    81                  */
    82                 if (key_data[i] != NULL && destructors[i] != NULL)
    83                         destructors[i](key_data[i]);
    84         }
    8571}
    8672
    8773int pthread_setspecific(pthread_key_t key, const void *data)
    8874{
     75        DPRINTF("pthread_setspecific(%d, %p)\n", key, data);
    8976        if (!fibril_initialized) {
    9077                DPRINTF("initializing pthread keys\n");
    91                 errno_t res = fibril_add_exit_hook(pthread_key_on_fibril_exit);
    92                 if (res != EOK)
    93                         return res;
    94 
    9578                for (unsigned i = 0; i < PTHREAD_KEYS_MAX; i++) {
    9679                        key_data[i] = NULL;
     
    10891int pthread_key_delete(pthread_key_t key)
    10992{
    110         /*
    111          * FIXME: this can cause a data race if another fibrill concurrently
    112          * runs on_fibril_exit. The obvious solution is to add a rwlock on
    113          * the destructors array, which will be needed anyway if we want to
    114          * support unlimited number of keys.
    115          */
    116         destructors[key] = NULL;
    117         key_data[key] = NULL;
    118 
    119         // TODO: the key could also be reused
     93        // see https://github.com/HelenOS/helenos/pull/245#issuecomment-2706795848
     94        not_implemented();
    12095        return EOK;
    12196}
     
    12499{
    125100        unsigned short k = atomic_fetch_add(&next_key, 1);
     101        DPRINTF("pthread_key_create(%p, %p) = %d\n", key, destructor, k);
    126102        if (k >= PTHREAD_KEYS_MAX) {
    127103                atomic_store(&next_key, PTHREAD_KEYS_MAX + 1);
    128104                return ELIMIT;
    129105        }
     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        }
    130113
    131         destructors[k] = destructor;
    132114        *key = k;
    133115        return EOK;
Note: See TracChangeset for help on using the changeset viewer.