Skip to content

Commit c6f9c93

Browse files
lemirechenhao94
andauthored
Use static variables to enforce initialization order. (#1773) (#1777)
Fixes #1771 Co-authored-by: Hao Chen <chenhao.yalier@gmail.com>
1 parent 3a93e45 commit c6f9c93

26 files changed

Lines changed: 300 additions & 207 deletions

benchmark/benchfeatures.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ struct option_struct {
9797
verbose = true;
9898
break;
9999
case 'a': {
100-
auto impl = simdjson::available_implementations[optarg];
100+
auto impl = simdjson::get_available_implementations()[optarg];
101101
if(impl && impl->supported_by_runtime_system()) {
102-
simdjson::active_implementation = impl;
102+
simdjson::get_active_implementation() = impl;
103103
} else {
104104
std::cerr << "implementation " << optarg << " not found or not supported " << std::endl;
105105
}

benchmark/dom/parse.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void print_usage(ostream& out) {
6666
out << "-H - Make the buffers hot (reduce page allocation and related OS tasks during parsing) [default]" << endl;
6767
out << "-a IMPL - Use the given parser implementation. By default, detects the most advanced" << endl;
6868
out << " implementation supported on the host machine." << endl;
69-
for (auto impl : simdjson::available_implementations) {
69+
for (auto impl : simdjson::get_available_implementations()) {
7070
if(impl->supported_by_runtime_system()) {
7171
out << "-a " << std::left << std::setw(9) << impl->name() << " - Use the " << impl->description() << " parser implementation." << endl;
7272
}
@@ -116,18 +116,18 @@ struct option_struct {
116116
verbose = true;
117117
break;
118118
case 'a': {
119-
const implementation *impl = simdjson::available_implementations[optarg];
119+
const implementation *impl = simdjson::get_available_implementations()[optarg];
120120
if ((!impl) || (!impl->supported_by_runtime_system())) {
121121
std::string exit_message = string("Unsupported option value -a ") + optarg + ": expected -a with one of ";
122-
for (auto imple : simdjson::available_implementations) {
122+
for (auto imple : simdjson::get_available_implementations()) {
123123
if(imple->supported_by_runtime_system()) {
124124
exit_message += imple->name();
125125
exit_message += " ";
126126
}
127127
}
128128
exit_usage(exit_message);
129129
}
130-
simdjson::active_implementation = impl;
130+
simdjson::get_active_implementation() = impl;
131131
break;
132132
}
133133
case 'C':
@@ -175,7 +175,7 @@ int main(int argc, char *argv[]) {
175175
option_struct options(argc, argv);
176176
if (options.verbose) {
177177
verbose_stream = &cout;
178-
verbose() << "Implementation: " << simdjson::active_implementation->name() << endl;
178+
verbose() << "Implementation: " << simdjson::get_active_implementation()->name() << endl;
179179
}
180180

181181
// Start collecting events. We put this early so if it prints an error message, it's the

benchmark/json_benchmark/run_json_benchmark.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ void maybe_display_implementation() {
1010
static bool displayed_implementation = false;
1111
if(!displayed_implementation) {
1212
displayed_implementation = true;
13-
std::cout << "simdjson::dom implementation: " << simdjson::active_implementation->name() << std::endl;
14-
std::cout << "simdjson::ondemand implementation (stage 1): " << simdjson::active_implementation->name() << std::endl;
13+
std::cout << "simdjson::dom implementation: " << simdjson::get_active_implementation()->name() << std::endl;
14+
std::cout << "simdjson::ondemand implementation (stage 1): " << simdjson::get_active_implementation()->name() << std::endl;
1515
std::cout << "simdjson::ondemand implementation (stage 2): " << simdjson::builtin_implementation()->name() << std::endl;
1616
}
1717
}

doc/implementation-selection.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ You can check what implementation is running with `active_implementation`:
5151

5252
```c++
5353
cout << "simdjson v" << SIMDJSON_STRINGIFY(SIMDJSON_VERSION) << endl;
54-
cout << "Detected the best implementation for your machine: " << simdjson::active_implementation->name();
55-
cout << "(" << simdjson::active_implementation->description() << ")" << endl;
54+
cout << "Detected the best implementation for your machine: " << simdjson::get_active_implementation()->name();
55+
cout << "(" << simdjson::get_active_implementation()->description() << ")" << endl;
5656
```
5757

5858
Implementation detection will happen in this case when you first call `name()`.
@@ -63,18 +63,18 @@ Querying Available Implementations
6363
You can list all available implementations, regardless of which one was selected:
6464

6565
```c++
66-
for (auto implementation : simdjson::available_implementations) {
66+
for (auto implementation : simdjson::get_available_implementations()) {
6767
cout << implementation->name() << ": " << implementation->description() << endl;
6868
}
6969
```
7070

7171
And look them up by name:
7272

7373
```c++
74-
cout << simdjson::available_implementations["fallback"]->description() << endl;
74+
cout << simdjson::get_available_implementations()["fallback"]->description() << endl;
7575
```
7676
Though the fallback implementation should always be available, others might be missing. When
77-
an implementation is not available, the bracket call `simdjson::available_implementations[name]`
77+
an implementation is not available, the bracket call `simdjson::get_available_implementations()[name]`
7878
will return the null pointer.
7979

8080
The available implementations have been compiled but may not necessarily be run safely on your system
@@ -90,18 +90,18 @@ can select the CPU architecture yourself:
9090

9191
```c++
9292
// Use the fallback implementation, even though my machine is fast enough for anything
93-
simdjson::active_implementation = simdjson::available_implementations["fallback"];
93+
simdjson::get_active_implementation() = simdjson::get_available_implementations()["fallback"];
9494
```
9595

9696
You are responsible for ensuring that the requirements of the selected implementation match your current system.
97-
Furthermore, you should check that the implementation is available before setting it to `simdjson::active_implementation`
97+
Furthermore, you should check that the implementation is available before setting it to `simdjson::get_active_implementation()`
9898
by comparing it with the null pointer.
9999

100100
```c++
101-
auto my_implementation = simdjson::available_implementations["haswell"];
101+
auto my_implementation = simdjson::get_available_implementations()["haswell"];
102102
if(! my_implementation) { exit(1); }
103103
if(! my_implementation->supported_by_runtime_system()) { exit(1); }
104-
simdjson::active_implementation = my_implementation;
104+
simdjson::get_active_implementation() = my_implementation;
105105
```
106106
107107
Checking that an Implementation can Run on your System
@@ -110,7 +110,7 @@ Checking that an Implementation can Run on your System
110110
You should call `supported_by_runtime_system()` to compare the processor's features with the need of the implementation.
111111
112112
```c++
113-
for (auto implementation : simdjson::available_implementations) {
113+
for (auto implementation : simdjson::get_available_implementations()) {
114114
if(implementation->supported_by_runtime_system()) {
115115
cout << implementation->name() << ": " << implementation->description() << endl;
116116
}

fuzz/fuzz_implementations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
9393
std::size_t nerrors=0;
9494
for(std::size_t i=0; i<Nimplementations; ++i) {
9595
auto& e=implementations[i];
96-
simdjson::active_implementation=e.impl;
96+
simdjson::get_active_implementation()=e.impl;
9797
e.error=e.parser.parse(Data,Size).get(e.element);
9898
if(e.error) {
9999
++nerrors;

fuzz/supported_implementations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
std::vector<const simdjson::implementation*>
1818
get_runtime_supported_implementations() {
1919
std::vector<const simdjson::implementation*> ret;
20-
for(auto& e: simdjson::available_implementations) {
20+
for(auto& e: simdjson::get_available_implementations()) {
2121
if(e->supported_by_runtime_system()) {
2222
ret.emplace_back(e);
2323
}

include/simdjson/dom/parser-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ inline error_code parser::allocate(size_t capacity, size_t max_depth) noexcept {
176176
if (implementation) {
177177
err = implementation->allocate(capacity, max_depth);
178178
} else {
179-
err = simdjson::active_implementation->create_dom_parser_implementation(capacity, max_depth, implementation);
179+
err = simdjson::get_active_implementation()->create_dom_parser_implementation(capacity, max_depth, implementation);
180180
}
181181
if (err) { return err; }
182182
return SUCCESS;

include/simdjson/generic/ondemand/parser-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ simdjson_warn_unused simdjson_really_inline error_code parser::allocate(size_t n
2121
SIMDJSON_TRY( implementation->set_capacity(new_capacity) );
2222
SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) );
2323
} else {
24-
SIMDJSON_TRY( simdjson::active_implementation->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) );
24+
SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) );
2525
}
2626
_capacity = new_capacity;
2727
_max_depth = new_max_depth;

include/simdjson/implementation.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class implementation {
5656
/**
5757
* The name of this implementation.
5858
*
59-
* const implementation *impl = simdjson::active_implementation;
59+
* const implementation *impl = simdjson::get_active_implementation();
6060
* cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl;
6161
*
6262
* @return the name of the implementation, e.g. "haswell", "westmere", "arm64"
@@ -66,7 +66,7 @@ class implementation {
6666
/**
6767
* The description of this implementation.
6868
*
69-
* const implementation *impl = simdjson::active_implementation;
69+
* const implementation *impl = simdjson::get_active_implementation();
7070
* cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl;
7171
*
7272
* @return the name of the implementation, e.g. "haswell", "westmere", "arm64"
@@ -95,7 +95,7 @@ class implementation {
9595
/**
9696
* @private For internal implementation use
9797
*
98-
* const implementation *impl = simdjson::active_implementation;
98+
* const implementation *impl = simdjson::get_active_implementation();
9999
* cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl;
100100
*
101101
* @param capacity The largest document that will be passed to the parser.
@@ -189,10 +189,10 @@ class available_implementation_list {
189189
*
190190
* Case sensitive.
191191
*
192-
* const implementation *impl = simdjson::available_implementations["westmere"];
192+
* const implementation *impl = simdjson::get_available_implementations()["westmere"];
193193
* if (!impl) { exit(1); }
194194
* if (!imp->supported_by_runtime_system()) { exit(1); }
195-
* simdjson::active_implementation = impl;
195+
* simdjson::get_active_implementation() = impl;
196196
*
197197
* @param name the implementation to find, e.g. "westmere", "haswell", "arm64"
198198
* @return the implementation, or nullptr if the parse failed.
@@ -210,7 +210,7 @@ class available_implementation_list {
210210
* This is used to initialize the implementation on startup.
211211
*
212212
* const implementation *impl = simdjson::available_implementation::detect_best_supported();
213-
* simdjson::active_implementation = impl;
213+
* simdjson::get_active_implementation() = impl;
214214
*
215215
* @return the most advanced supported implementation for the current host, or an
216216
* implementation that returns UNSUPPORTED_ARCHITECTURE if there is no supported
@@ -242,14 +242,14 @@ class atomic_ptr {
242242
/**
243243
* The list of available implementations compiled into simdjson.
244244
*/
245-
extern SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list available_implementations;
245+
extern SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list& get_available_implementations();
246246

247247
/**
248248
* The active implementation.
249249
*
250250
* Automatically initialized on first use to the most advanced implementation supported by this hardware.
251251
*/
252-
extern SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr<const implementation> active_implementation;
252+
extern SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>& get_active_implementation();
253253

254254
} // namespace simdjson
255255

0 commit comments

Comments
 (0)