2016-03-11 02:31:22 +01:00
|
|
|
/****************************************************************************
|
2021-06-29 10:01:02 +02:00
|
|
|
* libs/libc/tls/tls_alloc.c
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
2020-05-05 17:52:15 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
2020-05-05 17:52:15 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
2020-05-05 17:52:15 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-05-08 15:23:26 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2016-03-11 02:31:22 +01:00
|
|
|
#include <assert.h>
|
2020-05-08 15:23:26 +02:00
|
|
|
#include <debug.h>
|
2016-03-11 02:31:22 +01:00
|
|
|
|
2021-05-14 04:03:23 +02:00
|
|
|
#include <nuttx/spinlock.h>
|
2016-03-11 02:31:22 +01:00
|
|
|
#include <nuttx/tls.h>
|
2021-06-29 10:01:02 +02:00
|
|
|
#include <nuttx/sched.h>
|
2021-06-09 08:53:23 +02:00
|
|
|
|
2020-05-07 17:46:47 +02:00
|
|
|
#if CONFIG_TLS_NELEM > 0
|
2016-03-11 02:31:22 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-08 15:23:26 +02:00
|
|
|
* Name: tls_alloc
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-08 15:23:26 +02:00
|
|
|
* Allocate a group-unique TLS data index
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-05-08 15:23:26 +02:00
|
|
|
* None
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-05-08 15:23:26 +02:00
|
|
|
* A TLS index that is unique for use within this task group.
|
2021-06-29 10:01:02 +02:00
|
|
|
* If unsuccessful, an errno value will be returned and set to errno.
|
2016-03-11 02:31:22 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-29 10:01:02 +02:00
|
|
|
int tls_alloc(CODE void (*dtor)(FAR void *))
|
2016-03-11 02:31:22 +01:00
|
|
|
{
|
2021-06-29 10:01:02 +02:00
|
|
|
FAR struct task_info_s *info = task_get_info();
|
2020-05-08 15:23:26 +02:00
|
|
|
int candidate;
|
2021-06-29 10:01:02 +02:00
|
|
|
int ret;
|
2020-05-08 15:23:26 +02:00
|
|
|
|
2021-06-29 10:01:02 +02:00
|
|
|
DEBUGASSERT(info);
|
2016-03-11 02:31:22 +01:00
|
|
|
|
2020-05-08 15:23:26 +02:00
|
|
|
/* Search for an unused index. This is done in a critical section here to
|
|
|
|
* avoid concurrent modification of the group TLS index set.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 10:01:02 +02:00
|
|
|
ret = _SEM_WAIT(&info->ta_sem);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = _SEM_ERRVAL(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -EAGAIN;
|
|
|
|
|
2020-05-08 15:23:26 +02:00
|
|
|
for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
|
2016-03-11 02:31:22 +01:00
|
|
|
{
|
2020-05-08 15:23:26 +02:00
|
|
|
/* Is this candidate index available? */
|
|
|
|
|
|
|
|
tls_ndxset_t mask = (1 << candidate);
|
2021-06-29 10:01:02 +02:00
|
|
|
if ((info->ta_tlsset & mask) == 0)
|
2020-05-08 15:23:26 +02:00
|
|
|
{
|
|
|
|
/* Yes.. allocate the index and break out of the loop */
|
2016-03-11 02:31:22 +01:00
|
|
|
|
2021-06-29 10:01:02 +02:00
|
|
|
info->ta_tlsset |= mask;
|
|
|
|
info->ta_tlsdtor[candidate] = dtor;
|
|
|
|
ret = candidate;
|
2020-05-08 15:23:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:01:02 +02:00
|
|
|
_SEM_POST(&info->ta_sem);
|
2016-03-11 02:31:22 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:46:47 +02:00
|
|
|
#endif /* CONFIG_TLS_NELEM > 0 */
|