OSDN Git Service

* config/vxlib.c: Extract tls related routines into ...
[pf3gnuchains/gcc-fork.git] / gcc / config / vxlib-tls.c
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2    Contributed by Zack Weinberg <zack@codesourcery.com>
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  */
20
21 /* As a special exception, if you link this library with other files,
22    some of which are compiled with GCC, to produce an executable,
23    this library does not by itself cause the resulting executable
24    to be covered by the GNU General Public License.
25    This exception does not however invalidate any other reasons why
26    the executable file might be covered by the GNU General Public License.  */
27
28 /* Threads compatibility routines for libgcc2 for VxWorks.
29    These are out-of-line routines called from gthr-vxworks.h. 
30
31    This file provides the TLS related support routines, calling specific
32    VxWorks kernel entry points for this purpose.  The base VxWorks 5.x kernels
33    don't feature these entry points, and we provide gthr_supp_vxw_5x.c as an
34    option to fill this gap.  Asking users to rebuild a kernel is not to be
35    taken lightly, still, so we have isolated these routines from the rest of
36    vxlib to ensure that the kernel dependencies are only dragged when really
37    necessary.  */
38
39 #include "tconfig.h"
40 #include "tsystem.h"
41 #include "gthr.h"
42
43 #if defined(__GTHREADS)
44 #include <vxWorks.h>
45 #ifndef __RTP__
46 #include <vxLib.h>
47 #endif
48 #include <taskLib.h>
49 #ifndef __RTP__
50 #include <taskHookLib.h>
51 #else
52 # include <errno.h>
53 #endif
54
55 /* Thread-local storage.
56
57    We reserve a field in the TCB to point to a dynamically allocated
58    array which is used to store TLS values.  A TLS key is simply an
59    offset in this array.  The exact location of the TCB field is not
60    known to this code nor to vxlib.c -- all access to it indirects
61    through the routines __gthread_get_tls_data and
62    __gthread_set_tls_data, which are provided by the VxWorks kernel.
63
64    There is also a global array which records which keys are valid and
65    which have destructors.
66
67    A task delete hook is installed to execute key destructors.  The
68    routines __gthread_enter_tls_dtor_context and
69    __gthread_leave_tls_dtor_context, which are also provided by the
70    kernel, ensure that it is safe to call free() on memory allocated
71    by the task being deleted.  (This is a no-op on VxWorks 5, but
72    a major undertaking on AE.)
73
74    The task delete hook is only installed when at least one thread
75    has TLS data.  This is a necessary precaution, to allow this module
76    to be unloaded - a module with a hook can not be removed.
77
78    Since this interface is used to allocate only a small number of
79    keys, the table size is small and static, which simplifies the
80    code quite a bit.  Revisit this if and when it becomes necessary.  */
81
82 #define MAX_KEYS 4
83
84 /* This is the structure pointed to by the pointer returned
85    by __gthread_get_tls_data.  */
86 struct tls_data
87 {
88   int *owner;
89   void *values[MAX_KEYS];
90   unsigned int generation[MAX_KEYS];
91 };
92
93 /* To make sure we only delete TLS data associated with this object,
94    include a pointer to a local variable in the TLS data object.  */
95 static int self_owner;
96
97 /* The number of threads for this module which have active TLS data.
98    This is protected by tls_lock.  */
99 static int active_tls_threads;
100
101 /* kernel provided routines */
102 extern void *__gthread_get_tls_data (void);
103 extern void __gthread_set_tls_data (void *data);
104
105 extern void __gthread_enter_tls_dtor_context (void);
106 extern void __gthread_leave_tls_dtor_context (void);
107
108
109 /* This is a global structure which records all of the active keys.
110
111    A key is potentially valid (i.e. has been handed out by
112    __gthread_key_create) iff its generation count in this structure is
113    even.  In that case, the matching entry in the dtors array is a
114    routine to be called when a thread terminates with a valid,
115    non-NULL specific value for that key.
116
117    A key is actually valid in a thread T iff the generation count
118    stored in this structure is equal to the generation count stored in
119    T's specific-value structure.  */
120
121 typedef void (*tls_dtor) (void *);
122
123 struct tls_keys
124 {
125   tls_dtor dtor[MAX_KEYS];
126   unsigned int generation[MAX_KEYS];
127 };
128
129 #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
130
131 /* Note: if MAX_KEYS is increased, this initializer must be updated
132    to match.  All the generation counts begin at 1, which means no
133    key is valid.  */
134 static struct tls_keys tls_keys =
135 {
136   { 0, 0, 0, 0 },
137   { 1, 1, 1, 1 }
138 };
139
140 /* This lock protects the tls_keys structure.  */
141 static __gthread_mutex_t tls_lock;
142
143 static __gthread_once_t tls_init_guard = __GTHREAD_ONCE_INIT;
144
145 /* Internal routines.  */
146
147 /* The task TCB has just been deleted.  Call the destructor
148    function for each TLS key that has both a destructor and
149    a non-NULL specific value in this thread.
150
151    This routine does not need to take tls_lock; the generation
152    count protects us from calling a stale destructor.  It does
153    need to read tls_keys.dtor[key] atomically.  */
154
155 static void
156 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED)
157 {
158   struct tls_data *data = __gthread_get_tls_data ();
159   __gthread_key_t key;
160
161   if (data && data->owner == &self_owner)
162     {
163       __gthread_enter_tls_dtor_context ();
164       for (key = 0; key < MAX_KEYS; key++)
165         {
166           if (data->generation[key] == tls_keys.generation[key])
167             {
168               tls_dtor dtor = tls_keys.dtor[key];
169
170               if (dtor)
171                 dtor (data->values[key]);
172             }
173         }
174       free (data);
175
176       /* We can't handle an error here, so just leave the thread
177          marked as loaded if one occurs.  */
178       if (__gthread_mutex_lock (&tls_lock) != ERROR)
179         {
180           active_tls_threads--;
181           if (active_tls_threads == 0)
182             taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
183           __gthread_mutex_unlock (&tls_lock);
184         }
185
186       __gthread_set_tls_data (0);
187       __gthread_leave_tls_dtor_context ();
188     }
189
190
191 /* Initialize global data used by the TLS system.  */
192 static void
193 tls_init (void)
194 {
195   __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
196 }
197
198 static void tls_destructor (void) __attribute__ ((destructor));
199 static void
200 tls_destructor (void)
201 {
202 #ifdef __RTP__
203   /* All threads but this one should have exited by now.  */
204   tls_delete_hook (NULL);
205 #else
206   /* Unregister the hook forcibly.  The counter of active threads may
207      be incorrect, because constructors (like the C++ library's) and
208      destructors (like this one) run in the context of the shell rather
209      than in a task spawned from this module.  */
210   taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
211 #endif
212
213   if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
214     semDelete (tls_lock);
215 }
216
217 /* External interface */
218
219 /* Store in KEYP a value which can be passed to __gthread_setspecific/
220    __gthread_getspecific to store and retrieve a value which is
221    specific to each calling thread.  If DTOR is not NULL, it will be
222    called when a thread terminates with a non-NULL specific value for
223    this key, with the value as its sole argument.  */
224
225 int
226 __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
227 {
228   __gthread_key_t key;
229
230   __gthread_once (&tls_init_guard, tls_init);
231
232   if (__gthread_mutex_lock (&tls_lock) == ERROR)
233     return errno;
234
235   for (key = 0; key < MAX_KEYS; key++)
236     if (!KEY_VALID_P (key))
237       goto found_slot;
238
239   /* no room */
240   __gthread_mutex_unlock (&tls_lock);
241   return EAGAIN;
242
243  found_slot:
244   tls_keys.generation[key]++;  /* making it even */
245   tls_keys.dtor[key] = dtor;
246   *keyp = key;
247   __gthread_mutex_unlock (&tls_lock);
248   return 0;
249 }
250
251 /* Invalidate KEY; it can no longer be used as an argument to
252    setspecific/getspecific.  Note that this does NOT call destructor
253    functions for any live values for this key.  */
254 int
255 __gthread_key_delete (__gthread_key_t key)
256 {
257   if (key >= MAX_KEYS)
258     return EINVAL;
259
260   __gthread_once (&tls_init_guard, tls_init);
261
262   if (__gthread_mutex_lock (&tls_lock) == ERROR)
263     return errno;
264
265   if (!KEY_VALID_P (key))
266     {
267       __gthread_mutex_unlock (&tls_lock);
268       return EINVAL;
269     }
270
271   tls_keys.generation[key]++;  /* making it odd */
272   tls_keys.dtor[key] = 0;
273
274   __gthread_mutex_unlock (&tls_lock);
275   return 0;
276 }
277
278 /* Retrieve the thread-specific value for KEY.  If it has never been
279    set in this thread, or KEY is invalid, returns NULL.
280
281    It does not matter if this function races with key_create or
282    key_delete; the worst that can happen is you get a value other than
283    the one that a serialized implementation would have provided.  */
284
285 void *
286 __gthread_getspecific (__gthread_key_t key)
287 {
288   struct tls_data *data;
289
290   if (key >= MAX_KEYS)
291     return 0;
292
293   data = __gthread_get_tls_data ();
294
295   if (!data)
296     return 0;
297
298   if (data->generation[key] != tls_keys.generation[key])
299     return 0;
300
301   return data->values[key];
302 }
303
304 /* Set the thread-specific value for KEY.  If KEY is invalid, or
305    memory allocation fails, returns -1, otherwise 0.
306
307    The generation count protects this function against races with
308    key_create/key_delete; the worst thing that can happen is that a
309    value is successfully stored into a dead generation (and then
310    immediately becomes invalid).  However, we do have to make sure
311    to read tls_keys.generation[key] atomically.  */
312
313 int
314 __gthread_setspecific (__gthread_key_t key, void *value)
315 {
316   struct tls_data *data;
317   unsigned int generation;
318
319   if (key >= MAX_KEYS)
320     return EINVAL;
321
322   data = __gthread_get_tls_data ();
323   if (!data)
324     {
325       if (__gthread_mutex_lock (&tls_lock) == ERROR)
326         return ENOMEM;
327       if (active_tls_threads == 0)
328         taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
329       active_tls_threads++;
330       __gthread_mutex_unlock (&tls_lock);
331
332       data = malloc (sizeof (struct tls_data));
333       if (!data)
334         return ENOMEM;
335
336       memset (data, 0, sizeof (struct tls_data));
337       data->owner = &self_owner;
338       __gthread_set_tls_data (data);
339     }
340
341   generation = tls_keys.generation[key];
342
343   if (generation & 1)
344     return EINVAL;
345
346   data->generation[key] = generation;
347   data->values[key] = value;
348
349   return 0;
350 }
351 #endif /* __GTHREADS */