rt-thread/components/libc/pthreads/pthread_tls.c

102 lines
2.0 KiB
C
Raw Normal View History

2013-06-26 23:18:30 +08:00
/*
2018-10-14 19:28:18 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2013-06-26 23:18:30 +08:00
*
2018-10-14 19:28:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2013-06-26 23:18:30 +08:00
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
2013-01-08 22:40:58 +08:00
#include <pthread.h>
#include "pthread_internal.h"
_pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
void pthread_key_system_init()
{
2013-06-26 23:18:30 +08:00
rt_memset(&_thread_keys[0], 0, sizeof(_thread_keys));
2013-01-08 22:40:58 +08:00
}
void *pthread_getspecific(pthread_key_t key)
{
2013-06-26 23:18:30 +08:00
struct _pthread_data* ptd;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
ptd = _pthread_get_data(rt_thread_self());
RT_ASSERT(ptd != NULL);
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
if (ptd->tls == NULL)
return NULL;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
if ((key < PTHREAD_KEY_MAX) && (_thread_keys[key].is_used))
return ptd->tls[key];
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return NULL;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_getspecific);
int pthread_setspecific(pthread_key_t key, const void *value)
{
2013-06-26 23:18:30 +08:00
struct _pthread_data* ptd;
ptd = _pthread_get_data(rt_thread_self());
RT_ASSERT(ptd != NULL);
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
/* check tls area */
if (ptd->tls == NULL)
{
ptd->tls = (void**)rt_malloc(sizeof(void*) * PTHREAD_KEY_MAX);
}
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
if ((key < PTHREAD_KEY_MAX) && _thread_keys[key].is_used)
{
ptd->tls[key] = (void *)value;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
}
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return EINVAL;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_setspecific);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
{
2013-06-26 23:18:30 +08:00
rt_uint32_t index;
rt_enter_critical();
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
{
if (_thread_keys[index].is_used == 0)
{
_thread_keys[index].is_used = 1;
_thread_keys[index].destructor = destructor;
*key = index;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
rt_exit_critical();
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
}
}
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
rt_exit_critical();
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return EAGAIN;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_key_create);
int pthread_key_delete(pthread_key_t key)
{
2013-06-26 23:18:30 +08:00
if (key >= PTHREAD_KEY_MAX)
return EINVAL;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
rt_enter_critical();
_thread_keys[key].is_used = 0;
_thread_keys[key].destructor = 0;
rt_exit_critical();
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_key_delete);