OSDN Git Service

2008-05-15 Diego Novillo <dnovillo@google.com>
[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;
159   __gthread_key_t key;
160
161 #ifdef __RTP__
162   data = __gthread_get_tls_data ();
163 #else
164   /* In kernel mode, we can be called in the context of the thread
165      doing the killing, so must use the TCB to determine the data of
166      the thread being killed.  */
167   data = __gthread_get_tsd_data (tcb);
168 #endif
169   
170   if (data && data->owner == &self_owner)
171     {
172       __gthread_enter_tls_dtor_context ();
173       for (key = 0; key < MAX_KEYS; key++)
174         {
175           if (data->generation[key] == tls_keys.generation[key])
176             {
177               tls_dtor dtor = tls_keys.dtor[key];
178
179               if (dtor)
180                 dtor (data->values[key]);
181             }
182         }
183       free (data);
184
185       /* We can't handle an error here, so just leave the thread
186          marked as loaded if one occurs.  */
187       if (__gthread_mutex_lock (&tls_lock) != ERROR)
188         {
189           active_tls_threads--;
190           if (active_tls_threads == 0)
191             taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
192           __gthread_mutex_unlock (&tls_lock);
193         }
194 #ifdef __RTP__
195       __gthread_set_tls_data (0);
196 #else
197       __gthread_set_tsd_data (tcb, 0);
198 #endif
199       __gthread_leave_tls_dtor_context ();
200     }
201
202
203 /* Initialize global data used by the TLS system.  */
204 static void
205 tls_init (void)
206 {
207   __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
208 }
209
210 static void tls_destructor (void) __attribute__ ((destructor));
211 static void
212 tls_destructor (void)
213 {
214 #ifdef __RTP__
215   /* All threads but this one should have exited by now.  */
216   tls_delete_hook (NULL);
217 #else
218   /* Unregister the hook forcibly.  The counter of active threads may
219      be incorrect, because constructors (like the C++ library's) and
220      destructors (like this one) run in the context of the shell rather
221      than in a task spawned from this module.  */
222   taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
223 #endif
224
225   if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
226     semDelete (tls_lock);
227 }
228
229 /* External interface */
230
231 /* Store in KEYP a value which can be passed to __gthread_setspecific/
232    __gthread_getspecific to store and retrieve a value which is
233    specific to each calling thread.  If DTOR is not NULL, it will be
234    called when a thread terminates with a non-NULL specific value for
235    this key, with the value as its sole argument.  */
236
237 int
238 __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
239 {
240   __gthread_key_t key;
241
242   __gthread_once (&tls_init_guard, tls_init);
243
244   if (__gthread_mutex_lock (&tls_lock) == ERROR)
245     return errno;
246
247   for (key = 0; key < MAX_KEYS; key++)
248     if (!KEY_VALID_P (key))
249       goto found_slot;
250
251   /* no room */
252   __gthread_mutex_unlock (&tls_lock);
253   return EAGAIN;
254
255  found_slot:
256   tls_keys.generation[key]++;  /* making it even */
257   tls_keys.dtor[key] = dtor;
258   *keyp = key;
259   __gthread_mutex_unlock (&tls_lock);
260   return 0;
261 }
262
263 /* Invalidate KEY; it can no longer be used as an argument to
264    setspecific/getspecific.  Note that this does NOT call destructor
265    functions for any live values for this key.  */
266 int
267 __gthread_key_delete (__gthread_key_t key)
268 {
269   if (key >= MAX_KEYS)
270     return EINVAL;
271
272   __gthread_once (&tls_init_guard, tls_init);
273
274   if (__gthread_mutex_lock (&tls_lock) == ERROR)
275     return errno;
276
277   if (!KEY_VALID_P (key))
278     {
279       __gthread_mutex_unlock (&tls_lock);
280       return EINVAL;
281     }
282
283   tls_keys.generation[key]++;  /* making it odd */
284   tls_keys.dtor[key] = 0;
285
286   __gthread_mutex_unlock (&tls_lock);
287   return 0;
288 }
289
290 /* Retrieve the thread-specific value for KEY.  If it has never been
291    set in this thread, or KEY is invalid, returns NULL.
292
293    It does not matter if this function races with key_create or
294    key_delete; the worst that can happen is you get a value other than
295    the one that a serialized implementation would have provided.  */
296
297 void *
298 __gthread_getspecific (__gthread_key_t key)
299 {
300   struct tls_data *data;
301
302   if (key >= MAX_KEYS)
303     return 0;
304
305   data = __gthread_get_tls_data ();
306
307   if (!data)
308     return 0;
309
310   if (data->generation[key] != tls_keys.generation[key])
311     return 0;
312
313   return data->values[key];
314 }
315
316 /* Set the thread-specific value for KEY.  If KEY is invalid, or
317    memory allocation fails, returns -1, otherwise 0.
318
319    The generation count protects this function against races with
320    key_create/key_delete; the worst thing that can happen is that a
321    value is successfully stored into a dead generation (and then
322    immediately becomes invalid).  However, we do have to make sure
323    to read tls_keys.generation[key] atomically.  */
324
325 int
326 __gthread_setspecific (__gthread_key_t key, void *value)
327 {
328   struct tls_data *data;
329   unsigned int generation;
330
331   if (key >= MAX_KEYS)
332     return EINVAL;
333
334   data = __gthread_get_tls_data ();
335   if (!data)
336     {
337       if (__gthread_mutex_lock (&tls_lock) == ERROR)
338         return ENOMEM;
339       if (active_tls_threads == 0)
340         taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
341       active_tls_threads++;
342       __gthread_mutex_unlock (&tls_lock);
343
344       data = malloc (sizeof (struct tls_data));
345       if (!data)
346         return ENOMEM;
347
348       memset (data, 0, sizeof (struct tls_data));
349       data->owner = &self_owner;
350       __gthread_set_tls_data (data);
351     }
352
353   generation = tls_keys.generation[key];
354
355   if (generation & 1)
356     return EINVAL;
357
358   data->generation[key] = generation;
359   data->values[key] = value;
360
361   return 0;
362 }
363 #endif /* __GTHREADS */