Make __ffs hidden
[glibc.git] / elf / tlsdeschtab.h
blob155f4fdd9bcef29eb9b51f8ee9d4bc4e0a761256
1 /* Hash table for TLS descriptors.
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef TLSDESCHTAB_H
21 # define TLSDESCHTAB_H 1
23 # ifdef SHARED
25 # include <inline-hashtab.h>
27 inline static int
28 hash_tlsdesc (void *p)
30 struct tlsdesc_dynamic_arg *td = p;
32 /* We know all entries are for the same module, so ti_offset is the
33 only distinguishing entry. */
34 return td->tlsinfo.ti_offset;
37 inline static int
38 eq_tlsdesc (void *p, void *q)
40 struct tlsdesc_dynamic_arg *tdp = p, *tdq = q;
42 return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset;
45 inline static int
46 map_generation (struct link_map *map)
48 size_t idx = map->l_tls_modid;
49 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
51 /* Find the place in the dtv slotinfo list. */
54 /* Does it fit in the array of this list element? */
55 if (idx < listp->len)
57 /* We should never get here for a module in static TLS, so
58 we can assume that, if the generation count is zero, we
59 still haven't determined the generation count for this
60 module. */
61 if (listp->slotinfo[idx].gen)
62 return listp->slotinfo[idx].gen;
63 else
64 break;
66 idx -= listp->len;
67 listp = listp->next;
69 while (listp != NULL);
71 /* If we get to this point, the module still hasn't been assigned an
72 entry in the dtv slotinfo data structures, and it will when we're
73 done with relocations. At that point, the module will get a
74 generation number that is one past the current generation, so
75 return exactly that. */
76 return GL(dl_tls_generation) + 1;
79 void *
80 internal_function
81 _dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset)
83 struct hashtab *ht;
84 void **entry;
85 struct tlsdesc_dynamic_arg *td, test;
87 /* FIXME: We could use a per-map lock here, but is it worth it? */
88 __rtld_lock_lock_recursive (GL(dl_load_lock));
90 ht = map->l_mach.tlsdesc_table;
91 if (! ht)
93 ht = htab_create ();
94 if (! ht)
96 __rtld_lock_unlock_recursive (GL(dl_load_lock));
97 return 0;
99 map->l_mach.tlsdesc_table = ht;
102 test.tlsinfo.ti_module = map->l_tls_modid;
103 test.tlsinfo.ti_offset = ti_offset;
104 entry = htab_find_slot (ht, &test, 1, hash_tlsdesc, eq_tlsdesc);
105 if (*entry)
107 td = *entry;
108 __rtld_lock_unlock_recursive (GL(dl_load_lock));
109 return td;
112 *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg));
113 /* This may be higher than the map's generation, but it doesn't
114 matter much. Worst case, we'll have one extra DTV update per
115 thread. */
116 td->gen_count = map_generation (map);
117 td->tlsinfo = test.tlsinfo;
119 __rtld_lock_unlock_recursive (GL(dl_load_lock));
120 return td;
123 # endif /* SHARED */
125 /* The idea of the following two functions is to stop multiple threads
126 from attempting to resolve the same TLS descriptor without busy
127 waiting. Ideally, we should be able to release the lock right
128 after changing td->entry, and then using say a condition variable
129 or a futex wake to wake up any waiting threads, but let's try to
130 avoid introducing such dependencies. */
132 static int
133 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc volatile *td, void *caller)
135 if (caller != td->entry)
136 return 1;
138 __rtld_lock_lock_recursive (GL(dl_load_lock));
139 if (caller != td->entry)
141 __rtld_lock_unlock_recursive (GL(dl_load_lock));
142 return 1;
145 td->entry = _dl_tlsdesc_resolve_hold;
147 return 0;
150 static void
151 _dl_tlsdesc_wake_up_held_fixups (void)
153 __rtld_lock_unlock_recursive (GL(dl_load_lock));
156 #endif