annotate libgcc/emutls.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* TLS emulation.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #include "tconfig.h"
kono
parents:
diff changeset
27 #include "tsystem.h"
kono
parents:
diff changeset
28 #include "coretypes.h"
kono
parents:
diff changeset
29 #include "tm.h"
kono
parents:
diff changeset
30 #include "libgcc_tm.h"
kono
parents:
diff changeset
31 #include "gthr.h"
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 typedef unsigned int word __attribute__((mode(word)));
kono
parents:
diff changeset
34 typedef unsigned int pointer __attribute__((mode(pointer)));
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 struct __emutls_object
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 word size;
kono
parents:
diff changeset
39 word align;
kono
parents:
diff changeset
40 union {
kono
parents:
diff changeset
41 pointer offset;
kono
parents:
diff changeset
42 void *ptr;
kono
parents:
diff changeset
43 } loc;
kono
parents:
diff changeset
44 void *templ;
kono
parents:
diff changeset
45 };
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 struct __emutls_array
kono
parents:
diff changeset
48 {
kono
parents:
diff changeset
49 pointer size;
kono
parents:
diff changeset
50 void **data[];
kono
parents:
diff changeset
51 };
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 void *__emutls_get_address (struct __emutls_object *);
kono
parents:
diff changeset
54 void __emutls_register_common (struct __emutls_object *, word, word, void *);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 #ifdef __GTHREADS
kono
parents:
diff changeset
57 #ifdef __GTHREAD_MUTEX_INIT
kono
parents:
diff changeset
58 static __gthread_mutex_t emutls_mutex = __GTHREAD_MUTEX_INIT;
kono
parents:
diff changeset
59 #else
kono
parents:
diff changeset
60 static __gthread_mutex_t emutls_mutex;
kono
parents:
diff changeset
61 #endif
kono
parents:
diff changeset
62 static __gthread_key_t emutls_key;
kono
parents:
diff changeset
63 static pointer emutls_size;
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 static void
kono
parents:
diff changeset
66 emutls_destroy (void *ptr)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 struct __emutls_array *arr = ptr;
kono
parents:
diff changeset
69 pointer size = arr->size;
kono
parents:
diff changeset
70 pointer i;
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 for (i = 0; i < size; ++i)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 if (arr->data[i])
kono
parents:
diff changeset
75 free (arr->data[i][-1]);
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 free (ptr);
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 static void
kono
parents:
diff changeset
82 emutls_init (void)
kono
parents:
diff changeset
83 {
kono
parents:
diff changeset
84 #ifndef __GTHREAD_MUTEX_INIT
kono
parents:
diff changeset
85 __GTHREAD_MUTEX_INIT_FUNCTION (&emutls_mutex);
kono
parents:
diff changeset
86 #endif
kono
parents:
diff changeset
87 if (__gthread_key_create (&emutls_key, emutls_destroy) != 0)
kono
parents:
diff changeset
88 abort ();
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90 #endif
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 static void *
kono
parents:
diff changeset
93 emutls_alloc (struct __emutls_object *obj)
kono
parents:
diff changeset
94 {
kono
parents:
diff changeset
95 void *ptr;
kono
parents:
diff changeset
96 void *ret;
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* We could use here posix_memalign if available and adjust
kono
parents:
diff changeset
99 emutls_destroy accordingly. */
kono
parents:
diff changeset
100 if (obj->align <= sizeof (void *))
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 ptr = malloc (obj->size + sizeof (void *));
kono
parents:
diff changeset
103 if (ptr == NULL)
kono
parents:
diff changeset
104 abort ();
kono
parents:
diff changeset
105 ((void **) ptr)[0] = ptr;
kono
parents:
diff changeset
106 ret = ptr + sizeof (void *);
kono
parents:
diff changeset
107 }
kono
parents:
diff changeset
108 else
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 ptr = malloc (obj->size + sizeof (void *) + obj->align - 1);
kono
parents:
diff changeset
111 if (ptr == NULL)
kono
parents:
diff changeset
112 abort ();
kono
parents:
diff changeset
113 ret = (void *) (((pointer) (ptr + sizeof (void *) + obj->align - 1))
kono
parents:
diff changeset
114 & ~(pointer)(obj->align - 1));
kono
parents:
diff changeset
115 ((void **) ret)[-1] = ptr;
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 if (obj->templ)
kono
parents:
diff changeset
119 memcpy (ret, obj->templ, obj->size);
kono
parents:
diff changeset
120 else
kono
parents:
diff changeset
121 memset (ret, 0, obj->size);
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 return ret;
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 void *
kono
parents:
diff changeset
127 __emutls_get_address (struct __emutls_object *obj)
kono
parents:
diff changeset
128 {
kono
parents:
diff changeset
129 if (! __gthread_active_p ())
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 if (__builtin_expect (obj->loc.ptr == NULL, 0))
kono
parents:
diff changeset
132 obj->loc.ptr = emutls_alloc (obj);
kono
parents:
diff changeset
133 return obj->loc.ptr;
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 #ifndef __GTHREADS
kono
parents:
diff changeset
137 abort ();
kono
parents:
diff changeset
138 #else
kono
parents:
diff changeset
139 pointer offset = __atomic_load_n (&obj->loc.offset, __ATOMIC_ACQUIRE);
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 if (__builtin_expect (offset == 0, 0))
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
kono
parents:
diff changeset
144 __gthread_once (&once, emutls_init);
kono
parents:
diff changeset
145 __gthread_mutex_lock (&emutls_mutex);
kono
parents:
diff changeset
146 offset = obj->loc.offset;
kono
parents:
diff changeset
147 if (offset == 0)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 offset = ++emutls_size;
kono
parents:
diff changeset
150 __atomic_store_n (&obj->loc.offset, offset, __ATOMIC_RELEASE);
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152 __gthread_mutex_unlock (&emutls_mutex);
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 struct __emutls_array *arr = __gthread_getspecific (emutls_key);
kono
parents:
diff changeset
156 if (__builtin_expect (arr == NULL, 0))
kono
parents:
diff changeset
157 {
kono
parents:
diff changeset
158 pointer size = offset + 32;
kono
parents:
diff changeset
159 arr = calloc (size + 1, sizeof (void *));
kono
parents:
diff changeset
160 if (arr == NULL)
kono
parents:
diff changeset
161 abort ();
kono
parents:
diff changeset
162 arr->size = size;
kono
parents:
diff changeset
163 __gthread_setspecific (emutls_key, (void *) arr);
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165 else if (__builtin_expect (offset > arr->size, 0))
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 pointer orig_size = arr->size;
kono
parents:
diff changeset
168 pointer size = orig_size * 2;
kono
parents:
diff changeset
169 if (offset > size)
kono
parents:
diff changeset
170 size = offset + 32;
kono
parents:
diff changeset
171 arr = realloc (arr, (size + 1) * sizeof (void *));
kono
parents:
diff changeset
172 if (arr == NULL)
kono
parents:
diff changeset
173 abort ();
kono
parents:
diff changeset
174 arr->size = size;
kono
parents:
diff changeset
175 memset (arr->data + orig_size, 0,
kono
parents:
diff changeset
176 (size - orig_size) * sizeof (void *));
kono
parents:
diff changeset
177 __gthread_setspecific (emutls_key, (void *) arr);
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 void *ret = arr->data[offset - 1];
kono
parents:
diff changeset
181 if (__builtin_expect (ret == NULL, 0))
kono
parents:
diff changeset
182 {
kono
parents:
diff changeset
183 ret = emutls_alloc (obj);
kono
parents:
diff changeset
184 arr->data[offset - 1] = ret;
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186 return ret;
kono
parents:
diff changeset
187 #endif
kono
parents:
diff changeset
188 }
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 void
kono
parents:
diff changeset
191 __emutls_register_common (struct __emutls_object *obj,
kono
parents:
diff changeset
192 word size, word align, void *templ)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 if (obj->size < size)
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 obj->size = size;
kono
parents:
diff changeset
197 obj->templ = NULL;
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199 if (obj->align < align)
kono
parents:
diff changeset
200 obj->align = align;
kono
parents:
diff changeset
201 if (templ && size == obj->size)
kono
parents:
diff changeset
202 obj->templ = templ;
kono
parents:
diff changeset
203 }