47505f2d42a0d1d556438f1a3c219082634e72b9
[deb_dpdk.git] / examples / performance-thread / common / lthread_tls.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <limits.h>
39 #include <inttypes.h>
40 #include <unistd.h>
41 #include <pthread.h>
42 #include <fcntl.h>
43 #include <sys/time.h>
44 #include <sys/mman.h>
45 #include <sched.h>
46
47 #include <rte_malloc.h>
48 #include <rte_log.h>
49 #include <rte_ring.h>
50 #include <rte_atomic_64.h>
51
52 #include "lthread_tls.h"
53 #include "lthread_queue.h"
54 #include "lthread_objcache.h"
55 #include "lthread_sched.h"
56
57 static struct rte_ring *key_pool;
58 static uint64_t key_pool_init;
59
60 /* needed to cause section start and end to be defined */
61 RTE_DEFINE_PER_LTHREAD(void *, dummy);
62
63 static struct lthread_key key_table[LTHREAD_MAX_KEYS];
64
65 void lthread_tls_ctor(void) __attribute__((constructor));
66
67 void lthread_tls_ctor(void)
68 {
69         key_pool = NULL;
70         key_pool_init = 0;
71 }
72
73 /*
74  * Initialize a pool of keys
75  * These are unique tokens that can be obtained by threads
76  * calling lthread_key_create()
77  */
78 void _lthread_key_pool_init(void)
79 {
80         static struct rte_ring *pool;
81         struct lthread_key *new_key;
82         char name[MAX_LTHREAD_NAME_SIZE];
83
84         bzero(key_table, sizeof(key_table));
85
86         /* only one lcore should do this */
87         if (rte_atomic64_cmpset(&key_pool_init, 0, 1)) {
88
89                 snprintf(name,
90                         MAX_LTHREAD_NAME_SIZE,
91                         "lthread_key_pool_%d",
92                         getpid());
93
94                 pool = rte_ring_create(name,
95                                         LTHREAD_MAX_KEYS, 0, 0);
96                 RTE_ASSERT(pool);
97
98                 int i;
99
100                 for (i = 1; i < LTHREAD_MAX_KEYS; i++) {
101                         new_key = &key_table[i];
102                         rte_ring_mp_enqueue((struct rte_ring *)pool,
103                                                 (void *)new_key);
104                 }
105                 key_pool = pool;
106         }
107         /* other lcores wait here till done */
108         while (key_pool == NULL) {
109                 rte_compiler_barrier();
110                 sched_yield();
111         };
112 }
113
114 /*
115  * Create a key
116  * this means getting a key from the the pool
117  */
118 int lthread_key_create(unsigned int *key, tls_destructor_func destructor)
119 {
120         if (key == NULL)
121                 return POSIX_ERRNO(EINVAL);
122
123         struct lthread_key *new_key;
124
125         if (rte_ring_mc_dequeue((struct rte_ring *)key_pool, (void **)&new_key)
126             == 0) {
127                 new_key->destructor = destructor;
128                 *key = (new_key - key_table);
129
130                 return 0;
131         }
132         return POSIX_ERRNO(EAGAIN);
133 }
134
135
136 /*
137  * Delete a key
138  */
139 int lthread_key_delete(unsigned int k)
140 {
141         struct lthread_key *key;
142
143         key = (struct lthread_key *) &key_table[k];
144
145         if (k > LTHREAD_MAX_KEYS)
146                 return POSIX_ERRNO(EINVAL);
147
148         key->destructor = NULL;
149         rte_ring_mp_enqueue((struct rte_ring *)key_pool,
150                                         (void *)key);
151         return 0;
152 }
153
154
155
156 /*
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
162  */
163 void _lthread_tls_destroy(struct lthread *lt)
164 {
165         int i, k;
166         int nb_keys;
167         void *data;
168
169         for (i = 0; i < LTHREAD_DESTRUCTOR_ITERATIONS; i++) {
170
171                 for (k = 1; k < LTHREAD_MAX_KEYS; k++) {
172
173                         /* no keys in use ? */
174                         nb_keys = lt->tls->nb_keys_inuse;
175                         if (nb_keys == 0)
176                                 return;
177
178                         /* this key not in use ? */
179                         if (lt->tls->data[k] == NULL)
180                                 continue;
181
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;
186
187                         /* invoke destructor */
188                         if (key_table[k].destructor != NULL)
189                                 key_table[k].destructor(data);
190                 }
191         }
192 }
193
194 /*
195  * Return the pointer associated with a key
196  * If the key is no longer valid return NULL
197  */
198 void
199 *lthread_getspecific(unsigned int k)
200 {
201
202         if (k > LTHREAD_MAX_KEYS)
203                 return NULL;
204
205         return THIS_LTHREAD->tls->data[k];
206 }
207
208 /*
209  * Set a value against a key
210  * If the key is no longer valid return an error
211  * when storing value
212  */
213 int lthread_setspecific(unsigned int k, const void *data)
214 {
215         if (k > LTHREAD_MAX_KEYS)
216                 return POSIX_ERRNO(EINVAL);
217
218         int n = THIS_LTHREAD->tls->nb_keys_inuse;
219
220         /* discard const qualifier */
221         char *p = (char *) (uintptr_t) data;
222
223
224         if (data != NULL) {
225                 if (THIS_LTHREAD->tls->data[k] == NULL)
226                         THIS_LTHREAD->tls->nb_keys_inuse = n+1;
227         }
228
229         THIS_LTHREAD->tls->data[k] = (void *) p;
230         return 0;
231 }
232
233 /*
234  * Allocate data for TLS cache
235 */
236 void _lthread_tls_alloc(struct lthread *lt)
237 {
238         struct lthread_tls *tls;
239
240         tls = _lthread_objcache_alloc((THIS_SCHED)->tls_cache);
241
242         RTE_ASSERT(tls != NULL);
243
244         tls->root_sched = (THIS_SCHED);
245         lt->tls = tls;
246
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);
251         }
252 }