Truly independent web browser
LibUnicode: Make lazily filled caches safe for several threads
Andreas Kling5c977570a1ba
diff --git a/Libraries/LibUnicode/CharacterTypes.cpp b/Libraries/LibUnicode/CharacterTypes.cppindex b45c792e73c..cc7cb7027be 100644--- a/Libraries/LibUnicode/CharacterTypes.cpp+++ b/Libraries/LibUnicode/CharacterTypes.cpp@@ -6,9 +6,9 @@#include <AK/Array.h>#include <AK/CharacterTypes.h>+#include <AK/Diagnostics.h>#include <AK/Find.h>#include <AK/HashMap.h>-#include <AK/NeverDestroyed.h>#include <AK/NonnullOwnPtr.h>#include <AK/Traits.h>#include <LibUnicode/CharacterTypes.h>@@ -56,14 +56,26 @@ static constexpr GeneralCategory GENERAL_CATEGORY_LIMIT = U_CHAR_CATEGORY_COUNTstatic auto& category_sets_with_case_closure(){- static NeverDestroyed<HashMap<GeneralCategory, NonnullOwnPtr<icu::UnicodeSet>>> sets;- return *sets;+ using Sets = HashMap<GeneralCategory, NonnullOwnPtr<icu::UnicodeSet>>;+ // NB: Destroy each thread's ICU objects when that thread exits.+#ifdef AK_COMPILER_CLANG+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local Sets sets)+#else+ static thread_local Sets sets;+#endif+ return sets;}static auto& property_sets_with_case_closure(){- static NeverDestroyed<HashMap<Property, NonnullOwnPtr<icu::UnicodeSet>>> sets;- return *sets;+ using Sets = HashMap<Property, NonnullOwnPtr<icu::UnicodeSet>>;+ // NB: Destroy each thread's ICU objects when that thread exits.+#ifdef AK_COMPILER_CLANG+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local Sets sets)+#else+ static thread_local Sets sets;+#endif+ return sets;}Optional<GeneralCategory> general_category_from_string(StringView general_category)diff --git a/Libraries/LibUnicode/TimeZone.cpp b/Libraries/LibUnicode/TimeZone.cppindex e545922daf9..64c50d4b6d2 100644--- a/Libraries/LibUnicode/TimeZone.cpp+++ b/Libraries/LibUnicode/TimeZone.cpp@@ -5,6 +5,7 @@*/#include <AK/Array.h>+#include <AK/Mutex.h>#include <AK/NeverDestroyed.h>#include <AK/NonnullOwnPtr.h>#include <AK/QuickSort.h>@@ -18,10 +19,15 @@namespace Unicode {-static auto& cached_system_time_zone()+struct SystemTimeZoneCache {+ Mutex mutex;+ Optional<Utf16String> time_zone;+};++static SystemTimeZoneCache& system_time_zone_cache(){- static NeverDestroyed<Optional<Utf16String>> cached_system_time_zone;- return *cached_system_time_zone;+ static NeverDestroyed<SystemTimeZoneCache> cache;+ return *cache;}static Utf16String current_time_zone_impl(OwnPtr<icu::TimeZone> time_zone)@@ -55,12 +61,16 @@ static Utf16String current_default_time_zone()Utf16String current_time_zone(){- return cached_system_time_zone().ensure([] { return current_host_time_zone(); });+ auto& cache = system_time_zone_cache();+ MutexLocker locker { cache.mutex };+ return cache.time_zone.ensure([] { return current_host_time_zone(); });}void clear_system_time_zone_cache(){- cached_system_time_zone().clear();+ auto& cache = system_time_zone_cache();+ MutexLocker locker { cache.mutex };+ cache.time_zone.clear();}ErrorOr<void> set_current_time_zone(Utf16View time_zone)@@ -69,8 +79,10 @@ ErrorOr<void> set_current_time_zone(Utf16View time_zone)if (!time_zone_data.has_value())return Error::from_string_literal("Unable to find the provided time zone");+ auto& cache = system_time_zone_cache();+ MutexLocker locker { cache.mutex };icu::TimeZone::setDefault(time_zone_data->time_zone());- cached_system_time_zone() = current_default_time_zone();+ cache.time_zone = current_default_time_zone();return {};}