4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include <rte_malloc.h>
50 #include <rte_atomic_64.h>
52 #include "lthread_tls.h"
53 #include "lthread_queue.h"
54 #include "lthread_objcache.h"
55 #include "lthread_sched.h"
57 static struct rte_ring *key_pool;
58 static uint64_t key_pool_init;
60 /* needed to cause section start and end to be defined */
61 RTE_DEFINE_PER_LTHREAD(void *, dummy);
63 static struct lthread_key key_table[LTHREAD_MAX_KEYS];
65 void lthread_tls_ctor(void) __attribute__((constructor));
67 void lthread_tls_ctor(void)
74 * Initialize a pool of keys
75 * These are unique tokens that can be obtained by threads
76 * calling lthread_key_create()
78 void _lthread_key_pool_init(void)
80 static struct rte_ring *pool;
81 struct lthread_key *new_key;
82 char name[MAX_LTHREAD_NAME_SIZE];
84 bzero(key_table, sizeof(key_table));
86 /* only one lcore should do this */
87 if (rte_atomic64_cmpset(&key_pool_init, 0, 1)) {
90 MAX_LTHREAD_NAME_SIZE,
91 "lthread_key_pool_%d",
94 pool = rte_ring_create(name,
95 LTHREAD_MAX_KEYS, 0, 0);
100 for (i = 1; i < LTHREAD_MAX_KEYS; i++) {
101 new_key = &key_table[i];
102 rte_ring_mp_enqueue((struct rte_ring *)pool,
107 /* other lcores wait here till done */
108 while (key_pool == NULL) {
109 rte_compiler_barrier();
116 * this means getting a key from the the pool
118 int lthread_key_create(unsigned int *key, tls_destructor_func destructor)
121 return POSIX_ERRNO(EINVAL);
123 struct lthread_key *new_key;
125 if (rte_ring_mc_dequeue((struct rte_ring *)key_pool, (void **)&new_key)
127 new_key->destructor = destructor;
128 *key = (new_key - key_table);
132 return POSIX_ERRNO(EAGAIN);
139 int lthread_key_delete(unsigned int k)
141 struct lthread_key *key;
143 key = (struct lthread_key *) &key_table[k];
145 if (k > LTHREAD_MAX_KEYS)
146 return POSIX_ERRNO(EINVAL);
148 key->destructor = NULL;
149 rte_ring_mp_enqueue((struct rte_ring *)key_pool,
157 * Break association for all keys in use by this thread
158 * invoke the destructor if available.
159 * Since a destructor can create keys we could enter an infinite loop
160 * therefore we give up after LTHREAD_DESTRUCTOR_ITERATIONS
161 * the behavior is modelled on pthread
163 void _lthread_tls_destroy(struct lthread *lt)
169 for (i = 0; i < LTHREAD_DESTRUCTOR_ITERATIONS; i++) {
171 for (k = 1; k < LTHREAD_MAX_KEYS; k++) {
173 /* no keys in use ? */
174 nb_keys = lt->tls->nb_keys_inuse;
178 /* this key not in use ? */
179 if (lt->tls->data[k] == NULL)
182 /* remove this key */
183 data = lt->tls->data[k];
184 lt->tls->data[k] = NULL;
185 lt->tls->nb_keys_inuse = nb_keys-1;
187 /* invoke destructor */
188 if (key_table[k].destructor != NULL)
189 key_table[k].destructor(data);
195 * Return the pointer associated with a key
196 * If the key is no longer valid return NULL
199 *lthread_getspecific(unsigned int k)
202 if (k > LTHREAD_MAX_KEYS)
205 return THIS_LTHREAD->tls->data[k];
209 * Set a value against a key
210 * If the key is no longer valid return an error
213 int lthread_setspecific(unsigned int k, const void *data)
215 if (k > LTHREAD_MAX_KEYS)
216 return POSIX_ERRNO(EINVAL);
218 int n = THIS_LTHREAD->tls->nb_keys_inuse;
220 /* discard const qualifier */
221 char *p = (char *) (uintptr_t) data;
225 if (THIS_LTHREAD->tls->data[k] == NULL)
226 THIS_LTHREAD->tls->nb_keys_inuse = n+1;
229 THIS_LTHREAD->tls->data[k] = (void *) p;
234 * Allocate data for TLS cache
236 void _lthread_tls_alloc(struct lthread *lt)
238 struct lthread_tls *tls;
240 tls = _lthread_objcache_alloc((THIS_SCHED)->tls_cache);
242 RTE_ASSERT(tls != NULL);
244 tls->root_sched = (THIS_SCHED);
247 /* allocate data for TLS varaiables using RTE_PER_LTHREAD macros */
248 if (sizeof(void *) < (uint64_t)RTE_PER_LTHREAD_SECTION_SIZE) {
249 lt->per_lthread_data =
250 _lthread_objcache_alloc((THIS_SCHED)->per_lthread_cache);