Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : // INTERNAL ONLY. A scoped switch of the calling thread to the "C" locale, for
28 : // number conversions. JSON and YAML always write the decimal point as '.', but
29 : // strtod() and "%g" follow LC_NUMERIC, so under a locale whose radix is ','
30 : // they stop at the '.' of "1.5" and write "0,25". ccol_c_locale_enter() moves
31 : // only the calling thread with uselocale(), and ccol_c_locale_leave() puts the
32 : // previous locale back; scopes nest.
33 : //
34 : // The state below has internal linkage, so each translation unit has its own
35 : // flag and its own object and nothing is exported. The header isn't
36 : // installed.
37 : #ifndef CCOL_CNUMLOCALE_H
38 : #define CCOL_CNUMLOCALE_H
39 :
40 : #include <common.h>
41 : #include <locale.h>
42 : // newlocale() and uselocale() live in <xlocale.h> on macOS.
43 : #if defined(__APPLE__)
44 : #include <xlocale.h>
45 : #endif
46 : #include <stdatomic.h>
47 : // C23 has bool, true and false as keywords.
48 : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L
49 : #include <stdbool.h>
50 : #endif
51 :
52 : // What one ccol_c_locale_enter() changed, for the matching leave.
53 : typedef struct {
54 : locale_t prev;
55 : bool switched;
56 : } ccol_c_locale_scope;
57 :
58 : // The "C" locale object, created once and lazily. The destructor clears the
59 : // pointer, so an enter after it finds no object and leaves the locale alone.
60 : static ccol__once_flag ccol__c_locale_once = CCOL__ONCE_INIT;
61 : static _Atomic(locale_t) ccol__c_locale_obj;
62 : // glibc and musl hand back a static "C" object that freelocale() doesn't free.
63 : // Elsewhere the object is allocated and a thread can still be inside a scope
64 : // at exit, so every scope counts itself and the destructor frees the object
65 : // only when nobody uses it; otherwise it keeps it reachable in
66 : // ccol__c_locale_kept. CCOL__C_LOCALE_COUNT_USERS_FOR_TESTS lets a Linux test
67 : // build the count too.
68 : #if !defined(__linux__) || defined(CCOL__C_LOCALE_COUNT_USERS_FOR_TESTS)
69 : #define CCOL__C_LOCALE_COUNTS_USERS 1
70 : static _Atomic size_t ccol__c_locale_users;
71 : static locale_t ccol__c_locale_kept;
72 : #endif
73 :
74 : // Creates the "C" locale object and publishes it in ccol__c_locale_obj (NULL
75 : // when the C library refuses). It runs once under ccol__c_locale_once, on the
76 : // first enter.
77 6 : static void ccol__c_locale_init(void) {
78 6 : atomic_store_explicit(&ccol__c_locale_obj,
79 : newlocale(LC_ALL_MASK, "C", (locale_t)0),
80 : memory_order_release);
81 6 : }
82 :
83 : // Frees the "C" locale object at unload or exit, so it doesn't stay allocated.
84 : // It clears ccol__c_locale_obj first and frees the object only when no scope
85 : // uses it; otherwise it keeps it in ccol__c_locale_kept. The clear comes before
86 : // the load of the users count, and an enter's increment before its load of the
87 : // pointer, all sequentially consistent, so either the enter sees no object or
88 : // the destructor sees the user.
89 11 : __attribute__((destructor)) static void ccol__c_locale_fini(void) {
90 : #ifdef CCOL__C_LOCALE_COUNTS_USERS
91 2 : locale_t loc = atomic_exchange(&ccol__c_locale_obj, (locale_t)0);
92 2 : if (!loc) return;
93 1 : if (atomic_load(&ccol__c_locale_users) == 0)
94 0 : freelocale(loc);
95 : else
96 1 : ccol__c_locale_kept = loc;
97 : #else
98 9 : locale_t loc = atomic_exchange_explicit(&ccol__c_locale_obj, (locale_t)0,
99 : memory_order_acq_rel);
100 9 : if (loc) freelocale(loc);
101 : #endif
102 9 : }
103 :
104 : // Moves the calling thread to the "C" locale, so the number conversions of
105 : // cjson and cyaml read and write '.' as the decimal point. Gives the scope to
106 : // pass to the matching ccol_c_locale_leave(). Creates the object on first use
107 : // and counts itself in ccol__c_locale_users where that count exists. When the
108 : // object doesn't exist it switches nothing and the conversions run under the
109 : // current locale.
110 36264 : static inline ccol_c_locale_scope ccol_c_locale_enter(void) {
111 36264 : ccol_c_locale_scope scope = {(locale_t)0, false};
112 36264 : ccol__call_once(ccol__c_locale_once, ccol__c_locale_init);
113 : #ifdef CCOL__C_LOCALE_COUNTS_USERS
114 2 : atomic_fetch_add(&ccol__c_locale_users, 1);
115 2 : locale_t loc = atomic_load(&ccol__c_locale_obj);
116 2 : if (!loc) {
117 1 : atomic_fetch_sub(&ccol__c_locale_users, 1);
118 1 : return scope;
119 : }
120 : #else
121 36262 : locale_t loc =
122 36262 : atomic_load_explicit(&ccol__c_locale_obj, memory_order_acquire);
123 36262 : if (!loc) return scope;
124 : #endif
125 36263 : scope.prev = uselocale(loc);
126 36263 : scope.switched = true;
127 36263 : return scope;
128 : }
129 :
130 : // Puts back the locale that the matching ccol_c_locale_enter() replaced, so the
131 : // caller's thread ends a conversion in the locale it started in. Drops the
132 : // scope's count in ccol__c_locale_users.
133 36264 : static inline void ccol_c_locale_leave(ccol_c_locale_scope scope) {
134 36264 : if (!scope.switched) return;
135 36262 : uselocale(scope.prev);
136 : #ifdef CCOL__C_LOCALE_COUNTS_USERS
137 1 : atomic_fetch_sub(&ccol__c_locale_users, 1);
138 : #endif
139 : }
140 :
141 : #endif
|