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:
parent
70b03111eb
commit
b9db7df628
|
@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_THRD
|
||||||
source/thrd/cnd.c
|
source/thrd/cnd.c
|
||||||
source/thrd/mtx.c
|
source/thrd/mtx.c
|
||||||
source/thrd/thrd.c
|
source/thrd/thrd.c
|
||||||
|
source/thrd/tss.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prevent compiler from optimizing calloc into an infinite recursive call
|
# Prevent compiler from optimizing calloc into an infinite recursive call
|
||||||
|
|
|
@ -14,6 +14,7 @@ extern "C" {
|
||||||
typedef int cnd_t;
|
typedef int cnd_t;
|
||||||
typedef int mtx_t;
|
typedef int mtx_t;
|
||||||
typedef int thrd_t;
|
typedef int thrd_t;
|
||||||
|
typedef int tss_t;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,6 +63,17 @@ int cnd_signal(cnd_t *cond);
|
||||||
int cnd_broadcast(cnd_t *cond);
|
int cnd_broadcast(cnd_t *cond);
|
||||||
void cnd_destroy(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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
47
lib/libc/common/source/thrd/tss.c
Normal file
47
lib/libc/common/source/thrd/tss.c
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue