Optimize x86-64 pthread_cond_wait.
[glibc.git] / elf / tlsdeschtab.h
blobc3cbc3fb7954edce8c08d933d0ae6dbcb88e4adf
1 /* Hash table for TLS descriptors.
2 Copyright (C) 2005, 2008 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef TLSDESCHTAB_H
22 # define TLSDESCHTAB_H 1
24 # ifdef SHARED
26 # include <inline-hashtab.h>
28 inline static int
29 hash_tlsdesc (void *p)
31 struct tlsdesc_dynamic_arg *td = p;
33 /* We know all entries are for the same module, so ti_offset is the
34 only distinguishing entry. */
35 return td->tlsinfo.ti_offset;
38 inline static int
39 eq_tlsdesc (void *p, void *q)
41 struct tlsdesc_dynamic_arg *tdp = p, *tdq = q;
43 return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset;
46 inline static int
47 map_generation (struct link_map *map)
49 size_t idx = map->l_tls_modid;
50 struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
52 /* Find the place in the dtv slotinfo list. */
55 /* Does it fit in the array of this list element? */
56 if (idx < listp->len)
58 /* We should never get here for a module in static TLS, so
59 we can assume that, if the generation count is zero, we
60 still haven't determined the generation count for this
61 module. */
62 if (listp->slotinfo[idx].gen)
63 return listp->slotinfo[idx].gen;
64 else
65 break;
67 idx -= listp->len;
68 listp = listp->next;
70 while (listp != NULL);
72 /* If we get to this point, the module still hasn't been assigned an
73 entry in the dtv slotinfo data structures, and it will when we're
74 done with relocations. At that point, the module will get a
75 generation number that is one past the current generation, so
76 return exactly that. */
77 return GL(dl_tls_generation) + 1;
80 void *
81 internal_function
82 _dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset)
84 struct hashtab *ht;
85 void **entry;
86 struct tlsdesc_dynamic_arg *td, test;
88 /* FIXME: We could use a per-map lock here, but is it worth it? */
89 __rtld_lock_lock_recursive (GL(dl_load_lock));
91 ht = map->l_mach.tlsdesc_table;
92 if (! ht)
94 ht = htab_create ();
95 if (! ht)
97 __rtld_lock_unlock_recursive (GL(dl_load_lock));
98 return 0;
100 map->l_mach.tlsdesc_table = ht;
103 test.tlsinfo.ti_module = map->l_tls_modid;
104 test.tlsinfo.ti_offset = ti_offset;
105 entry = htab_find_slot (ht, &test, 1, hash_tlsdesc, eq_tlsdesc);
106 if (*entry)
108 td = *entry;
109 __rtld_lock_unlock_recursive (GL(dl_load_lock));
110 return td;
113 *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg));
114 /* This may be higher than the map's generation, but it doesn't
115 matter much. Worst case, we'll have one extra DTV update per
116 thread. */
117 td->gen_count = map_generation (map);
118 td->tlsinfo = test.tlsinfo;
120 __rtld_lock_unlock_recursive (GL(dl_load_lock));
121 return td;
124 # endif /* SHARED */
126 /* The idea of the following two functions is to stop multiple threads
127 from attempting to resolve the same TLS descriptor without busy
128 waiting. Ideally, we should be able to release the lock right
129 after changing td->entry, and then using say a condition variable
130 or a futex wake to wake up any waiting threads, but let's try to
131 avoid introducing such dependencies. */
133 inline static int
134 _dl_tlsdesc_resolve_early_return_p (struct tlsdesc volatile *td, void *caller)
136 if (caller != td->entry)
137 return 1;
139 __rtld_lock_lock_recursive (GL(dl_load_lock));
140 if (caller != td->entry)
142 __rtld_lock_unlock_recursive (GL(dl_load_lock));
143 return 1;
146 td->entry = _dl_tlsdesc_resolve_hold;
148 return 0;
151 inline static void
152 _dl_tlsdesc_wake_up_held_fixups (void)
154 __rtld_lock_unlock_recursive (GL(dl_load_lock));
157 #endif