Skip to content

Commit 55d48bc

Browse files
authored
Merge pull request #13 from simdutf/dlemire/fix_static_init
Fixing static initialization.
2 parents 2b4b0d8 + 1d20d7f commit 55d48bc

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

src/is_utf8.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -826,17 +826,15 @@ template <typename T> class atomic_ptr {
826826
/**
827827
* The list of available implementations compiled into simdutf.
828828
*/
829-
extern IS_UTF8_DLLIMPORTEXPORT const internal::available_implementation_list
830-
available_implementations;
829+
extern IS_UTF8_DLLIMPORTEXPORT const internal::available_implementation_list& get_available_implementations();
831830
832831
/**
833832
* The active implementation.
834833
*
835834
* Automatically initialized on first use to the most advanced implementation
836835
* supported by this hardware.
837836
*/
838-
extern IS_UTF8_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>
839-
active_implementation;
837+
extern IS_UTF8_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>& get_active_implementation();
840838
841839
} // namespace is_utf8_internals
842840
@@ -4640,33 +4638,39 @@ detect_best_supported_implementation_on_first_use::set_best() const noexcept {
46404638

46414639
if (force_implementation_name) {
46424640
auto force_implementation =
4643-
available_implementations[force_implementation_name];
4641+
get_available_implementations()[force_implementation_name];
46444642
if (force_implementation) {
4645-
return active_implementation = force_implementation;
4643+
return get_active_implementation() = force_implementation;
46464644
} else {
46474645
// Note: abort() and stderr usage within the library is forbidden.
4648-
return active_implementation = &unsupported_singleton;
4646+
return get_active_implementation() = &unsupported_singleton;
46494647
}
46504648
}
4651-
return active_implementation =
4652-
available_implementations.detect_best_supported();
4649+
return get_active_implementation() =
4650+
get_available_implementations().detect_best_supported();
46534651
}
46544652

46554653
} // namespace internal
46564654

4657-
IS_UTF8_DLLIMPORTEXPORT const internal::available_implementation_list
4658-
available_implementations{};
4659-
IS_UTF8_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>
4660-
active_implementation{
4661-
&internal::detect_best_supported_implementation_on_first_use_singleton};
4655+
IS_UTF8_DLLIMPORTEXPORT const internal::available_implementation_list& get_available_implementations() {
4656+
static const internal::available_implementation_list available_implementations{};
4657+
return available_implementations;
4658+
}
4659+
4660+
IS_UTF8_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>& get_active_implementation() {
4661+
static const internal::detect_best_supported_implementation_on_first_use detect_best_supported_implementation_on_first_use_singleton;
4662+
static internal::atomic_ptr<const implementation> active_implementation{&detect_best_supported_implementation_on_first_use_singleton};
4663+
return active_implementation;
4664+
}
4665+
46624666

46634667
is_utf8_warn_unused bool validate_utf8(const char *buf, size_t len) noexcept {
4664-
return active_implementation->validate_utf8(buf, len);
4668+
return get_active_implementation()->validate_utf8(buf, len);
46654669
}
46664670

46674671
const implementation *builtin_implementation() {
46684672
static const implementation *builtin_impl =
4669-
available_implementations[IS_UTF8_STRINGIFY(
4673+
get_available_implementations()[IS_UTF8_STRINGIFY(
46704674
IS_UTF8_BUILTIN_IMPLEMENTATION)];
46714675
return builtin_impl;
46724676
}

0 commit comments

Comments
 (0)