libc: common: support for C11 thread-specific storage

Add C11 thread-specific storage (tss) support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2023-08-25 07:30:54 -04:00 committed by Stephanos Ioannidis
parent 70b03111eb
commit b9db7df628
4 changed files with 60 additions and 0 deletions

View file

@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_THRD
source/thrd/cnd.c
source/thrd/mtx.c
source/thrd/thrd.c
source/thrd/tss.c
)
# Prevent compiler from optimizing calloc into an infinite recursive call

View file

@ -14,6 +14,7 @@ extern "C" {
typedef int cnd_t;
typedef int mtx_t;
typedef int thrd_t;
typedef int tss_t;
#ifdef __cplusplus
}

View file

@ -63,6 +63,17 @@ int cnd_signal(cnd_t *cond);
int cnd_broadcast(cnd_t *cond);
void cnd_destroy(cnd_t *cond);
#ifndef thread_local
#define thread_local _Thread_local
#endif
typedef void (*tss_dtor_t)(void *val);
int tss_create(tss_t *key, tss_dtor_t destructor);
void *tss_get(tss_t key);
int tss_set(tss_t key, void *val);
void tss_delete(tss_t key);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2023, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <threads.h>
#include <zephyr/kernel.h>
#include <zephyr/posix/pthread.h>
int tss_create(tss_t *key, tss_dtor_t destructor)
{
switch (pthread_key_create(key, destructor)) {
case 0:
return thrd_success;
case EAGAIN:
return thrd_busy;
case ENOMEM:
return thrd_nomem;
default:
return thrd_error;
}
}
void *tss_get(tss_t key)
{
return pthread_getspecific(key);
}
int tss_set(tss_t key, void *val)
{
switch (pthread_setspecific(key, val)) {
case 0:
return thrd_success;
case ENOMEM:
return thrd_nomem;
default:
return thrd_error;
}
}
void tss_delete(tss_t key)
{
(void)pthread_key_delete(key);
}