]>
Commit | Line | Data |
---|---|---|
30c14c31 RM |
1 | /* uselocale -- fetch and set the current per-thread locale |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
20 | #include <locale.h> | |
21 | #include "localeinfo.h" | |
22 | ||
30c14c31 RM |
23 | /* Switch the current thread's locale to DATASET. |
24 | If DATASET is null, instead just return the current setting. | |
25 | The special value LC_GLOBAL_LOCALE is the initial setting | |
26 | for all threads, and means the thread uses the global | |
27 | setting controlled by `setlocale'. */ | |
28 | locale_t | |
29 | __uselocale (locale_t newloc) | |
30 | { | |
31 | if (newloc == NULL) | |
32 | { | |
33 | locale_t loc = __libc_tsd_get (LOCALE); | |
34 | return loc == &_nl_global_locale ? LC_GLOBAL_LOCALE : loc; | |
35 | } | |
1a0d874e | 36 | else |
30c14c31 | 37 | { |
1a0d874e RM |
38 | const locale_t locobj |
39 | = newloc == LC_GLOBAL_LOCALE ? &_nl_global_locale : newloc; | |
40 | __libc_tsd_set (LOCALE, locobj); | |
41 | ||
42 | #ifdef NL_CURRENT_INDIRECT | |
43 | /* Now we must update all the per-category thread-local variables to | |
44 | point into the new current locale for this thread. The magic | |
45 | symbols _nl_current_LC_FOO_used are defined to meaningless values | |
46 | if _nl_current_LC_FOO was linked in. By using weak references to | |
47 | both symbols and testing the address of _nl_current_LC_FOO_used, | |
48 | we can avoid accessing the _nl_current_LC_FOO thread-local | |
49 | variable at all when no code referring to it was linked in. We | |
50 | need the special bogus symbol because while TLS symbols can be | |
51 | weak, there is no reasonable way to test for the default-zero | |
52 | value as with a heap symbol (taking the address would just use | |
53 | some bogus offset from our thread pointer). */ | |
54 | ||
55 | # define DEFINE_CATEGORY(category, category_name, items, a) \ | |
56 | { \ | |
57 | extern char _nl_current_##category##_used; \ | |
58 | weak_extern (_nl_current_##category##_used) \ | |
59 | weak_extern (_nl_current_##category) \ | |
60 | if (&_nl_current_##category##_used != 0) \ | |
61 | _nl_current_##category = &locobj->__locales[category]; \ | |
62 | } | |
63 | # include "categories.def" | |
64 | # undef DEFINE_CATEGORY | |
65 | #endif | |
30c14c31 | 66 | } |
1a0d874e | 67 | |
30c14c31 RM |
68 | return newloc; |
69 | } | |
70 | weak_alias (__uselocale, uselocale) |