resolv: fix rotate option
[glibc.git] / elf / tlsdeschtab.h
blobd7e7955db2c67298f93b237986acc9e3b7ae9190
1 /* Hash table for TLS descriptors.
2 Copyright (C) 2005-2015 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 __rtld_lock_unlock_recursive (GL(dl_load_lock));
108 return 0;
111 if (*entry)
113 td = *entry;
114 __rtld_lock_unlock_recursive (GL(dl_load_lock));
115 return td;
118 *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg));
119 /* This may be higher than the map's generation, but it doesn't
120 matter much. Worst case, we'll have one extra DTV update per
121 thread. */
122 td->gen_count = map_generation (map);
123 td->tlsinfo = test.tlsinfo;
125 __rtld_lock_unlock_recursive (GL(dl_load_lock));
126 return td;
129 # endif /* SHARED */
131 /* The idea of the following two functions is to stop multiple threads
132 from attempting to resolve the same TLS descriptor without busy
133 waiting. Ideally, we should be able to release the lock right
134 after changing td->entry, and then using say a condition variable
135 or a futex wake to wake up any waiting threads, but let's try to
136 avoid introducing such dependencies. */
138 static int
139 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc volatile *td, void *caller)
141 if (caller != td->entry)
142 return 1;
144 __rtld_lock_lock_recursive (GL(dl_load_lock));
145 if (caller != td->entry)
147 __rtld_lock_unlock_recursive (GL(dl_load_lock));
148 return 1;
151 td->entry = _dl_tlsdesc_resolve_hold;
153 return 0;
156 static void
157 _dl_tlsdesc_wake_up_held_fixups (void)
159 __rtld_lock_unlock_recursive (GL(dl_load_lock));
162 #endif