Skip to content

Commit aff3366

Browse files
committed
Reduce use of std::call_once() / dispatch_once() further now that statics initialization is thread safe
https://bugs.webkit.org/show_bug.cgi?id=303070 Reviewed by Gerald Squelart. Reduce use of std::call_once() / dispatch_once() further now that statics initialization is thread safe everywhere except JSC and bmalloc. * Source/WebCore/Modules/webdatabase/Database.cpp: (WebCore::fullyQualifiedInfoTableName): * Source/WebCore/accessibility/AXObjectCache.cpp: (WebCore::AXObjectCache::isAppleInternalInstall): * Source/WebCore/page/NavigatorUAData.cpp: (WebCore::NavigatorUAData::brands const): * Source/WebCore/page/Quirks.cpp: (WebCore::Quirks::needsTikTokOverflowingContentQuirk const): * Source/WebCore/platform/audio/cocoa/AudioSampleDataSource.mm: (WebCore::AudioSampleDataSource::hostTime const): * Source/WebCore/platform/graphics/cocoa/WebMAudioUtilitiesCocoa.mm: (WebCore::registerVorbisDecoderIfNeeded): * Source/WebCore/platform/mac/PlatformPasteboardMac.mm: (WebCore::PlatformPasteboard::bufferForType const): * Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm: (WebCore::globaVideoCaptureSerialQueue): * Source/WebCore/platform/text/BidiContext.cpp: (WebCore::BidiContext::create): * Source/WebCore/rendering/mac/RenderThemeMac.mm: (WebCore::radioSizes): * Source/WebGPU/WebGPU/HardwareCapabilities.mm: (WebGPU::isWebGPUSwiftEnabled): * Source/WebKitLegacy/mac/Misc/WebIconDatabase.mm: (+[WebIconDatabase sharedIconDatabase]): (-[WebIconDatabase defaultIconWithSize:]): * Source/WebKitLegacy/mac/Storage/WebStorageManager.mm: (+[WebStorageManager _storageDirectoryPath]): * Source/WebKitLegacy/mac/WebView/WebHTMLView.mm: (-[WebHTMLView _toolTipOwnerForSendingMouseEvents]): * Source/WebKitLegacy/mac/WebView/WebVideoFullscreenController.mm: (allocWebAVPlayerViewInstance): Canonical link: https://commits.webkit.org/303523@main
1 parent 2acc7d2 commit aff3366

File tree

15 files changed

+51
-121
lines changed

15 files changed

+51
-121
lines changed

Source/WebCore/Modules/webdatabase/Database.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ const unsigned long long quotaIncreaseSize = 5 * 1024 * 1024;
9999

100100
static const String& fullyQualifiedInfoTableName()
101101
{
102-
static LazyNeverDestroyed<String> qualifiedName;
103-
static std::once_flag onceFlag;
104-
std::call_once(onceFlag, [] {
105-
qualifiedName.construct(MAKE_STATIC_STRING_IMPL("main.__WebKitDatabaseInfoTable__"));
106-
});
102+
static NeverDestroyed<String> qualifiedName { MAKE_STATIC_STRING_IMPL("main.__WebKitDatabaseInfoTable__") };
107103
return qualifiedName;
108104
}
109105

Source/WebCore/accessibility/AXObjectCache.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5965,12 +5965,7 @@ void AXObjectCache::onWidgetVisibilityChanged(RenderWidget& widget)
59655965
#if PLATFORM(MAC)
59665966
bool AXObjectCache::isAppleInternalInstall()
59675967
{
5968-
static bool isInternal = false;
5969-
static dispatch_once_t onceToken;
5970-
dispatch_once(&onceToken, ^{
5971-
isInternal = os_variant_allows_internal_security_policies("com.apple.Accessibility");
5972-
});
5973-
5968+
static bool isInternal = os_variant_allows_internal_security_policies("com.apple.Accessibility");
59745969
return isInternal;
59755970
}
59765971
#endif // PLATFORM(COCOA)

Source/WebCore/page/NavigatorUAData.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ const Vector<NavigatorUABrandVersion>& NavigatorUAData::brands() const
9595
if (overrideFromUserAgentString)
9696
return NavigatorUAData::m_brands;
9797

98-
static LazyNeverDestroyed<Vector<NavigatorUABrandVersion>> brandVersion;
99-
static std::once_flag onceKey;
100-
std::call_once(onceKey, [] {
98+
static NeverDestroyed<Vector<NavigatorUABrandVersion>> brandVersion = [] {
10199
Vector<NavigatorUABrandVersion> temp = {
102100
NavigatorUABrandVersion {
103101
.brand = "AppleWebKit"_s,
@@ -109,8 +107,8 @@ const Vector<NavigatorUABrandVersion>& NavigatorUAData::brands() const
109107

110108
auto rng = std::default_random_engine { };
111109
std::ranges::shuffle(temp, rng);
112-
brandVersion.construct(temp);
113-
});
110+
return temp;
111+
}();
114112

115113
return brandVersion;
116114
}

Source/WebCore/page/Quirks.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,23 +2301,9 @@ std::optional<Quirks::TikTokOverflowingContentQuirkType> Quirks::needsTikTokOver
23012301
if (!element.elementData() || !element.hasClass())
23022302
return { };
23032303

2304-
static LazyNeverDestroyed<AtomString> contentContainerSubstring;
2305-
static std::once_flag contentContainerSubstringOnceKey;
2306-
std::call_once(contentContainerSubstringOnceKey, [&] {
2307-
contentContainerSubstring.construct("DivContentContainer"_s);
2308-
});
2309-
2310-
static LazyNeverDestroyed<AtomString> videoContainerSubstring;
2311-
static std::once_flag videoContainerSubstringOnceKey;
2312-
std::call_once(videoContainerSubstringOnceKey, [&] {
2313-
videoContainerSubstring.construct("DivVideoContainer"_s);
2314-
});
2315-
2316-
static LazyNeverDestroyed<AtomString> browserModeContainerSubstring;
2317-
static std::once_flag browserModeContainerSubstringOnceKey;
2318-
std::call_once(browserModeContainerSubstringOnceKey, [&] {
2319-
browserModeContainerSubstring.construct("DivBrowserModeContainer"_s);
2320-
});
2304+
static NeverDestroyed<AtomString> contentContainerSubstring { "DivContentContainer"_s };
2305+
static NeverDestroyed<AtomString> videoContainerSubstring { "DivVideoContainer"_s };
2306+
static NeverDestroyed<AtomString> browserModeContainerSubstring { "DivBrowserModeContainer"_s };
23212307

23222308
auto parentElementClassNamesContainsBrowserModeContainerSubstring = [&] {
23232309
RefPtr parentElement = element.parentElement();

Source/WebCore/platform/audio/cocoa/AudioSampleDataSource.mm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@
109109
MediaTime AudioSampleDataSource::hostTime() const
110110
{
111111
// Based on listing #2 from Apple Technical Q&A QA1398, modified to be thread-safe.
112-
static double frequency;
113-
static mach_timebase_info_data_t timebaseInfo;
114-
static std::once_flag initializeTimerOnceFlag;
115-
std::call_once(initializeTimerOnceFlag, [] {
112+
static double frequency = [] {
113+
static mach_timebase_info_data_t timebaseInfo;
116114
kern_return_t kr = mach_timebase_info(&timebaseInfo);
117-
frequency = 1e-9 * static_cast<double>(timebaseInfo.numer) / static_cast<double>(timebaseInfo.denom);
115+
double frequency = 1e-9 * static_cast<double>(timebaseInfo.numer) / static_cast<double>(timebaseInfo.denom);
118116
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
119117
ASSERT(timebaseInfo.denom);
120-
});
118+
return frequency;
119+
}();
121120

122121
return MediaTime::createWithDouble(mach_absolute_time() * frequency);
123122
}

Source/WebCore/platform/graphics/cocoa/WebMAudioUtilitiesCocoa.mm

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,7 @@ bool isVorbisDecoderAvailable()
516516
bool registerVorbisDecoderIfNeeded()
517517
{
518518
#if ENABLE(VORBIS)
519-
static bool available;
520-
521-
static dispatch_once_t onceToken;
522-
dispatch_once(&onceToken, ^{
523-
available = registerDecoderFactory("ACVorbisDecoderFactory"_s, kAudioFormatVorbis);
524-
});
525-
519+
static bool available = registerDecoderFactory("ACVorbisDecoderFactory"_s, kAudioFormatVorbis);
526520
return available;
527521
#else
528522
return false;

Source/WebCore/platform/mac/PlatformPasteboardMac.mm

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,12 @@ static bool canWriteAllPasteboardTypes(const Vector<String>& types)
9898
if (pasteboardType == String(legacyTIFFPasteboardTypeSingleton())) {
9999
data = [m_pasteboard _dataWithoutConversionForType:pasteboardType.createNSString().get() securityScoped:NO];
100100
if (!data) {
101-
static NeverDestroyed<RetainPtr<NSArray>> sourceTypes;
102-
static std::once_flag onceFlag;
103-
std::call_once(onceFlag, [] {
104-
auto originalSourceTypes = adoptCF(CGImageSourceCopyTypeIdentifiers());
105-
if (originalSourceTypes) {
106-
sourceTypes.get() = [(__bridge NSArray *)originalSourceTypes.get() arrayByExcludingObjectsInArray:@[UTTypePDF.identifier]];
107-
} else
108-
sourceTypes.get() = nil;
109-
});
101+
static NeverDestroyed<RetainPtr<NSArray>> sourceTypes = [] -> NSArray * {
102+
RetainPtr originalSourceTypes = adoptCF(CGImageSourceCopyTypeIdentifiers());
103+
if (originalSourceTypes)
104+
return [(__bridge NSArray *)originalSourceTypes.get() arrayByExcludingObjectsInArray:@[UTTypePDF.identifier]];
105+
return nil;
106+
}();
110107

111108
for (NSString *sourceType in sourceTypes.get().get()) {
112109
data = [m_pasteboard _dataWithoutConversionForType:sourceType securityScoped:NO];

Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#import <objc/runtime.h>
5050
#import <pal/avfoundation/MediaTimeAVFoundation.h>
5151
#import <pal/spi/cocoa/AVFoundationSPI.h>
52+
#import <wtf/NeverDestroyed.h>
5253
#import <wtf/Scope.h>
5354
#import <wtf/WorkQueue.h>
5455
#import <wtf/cocoa/VectorCocoa.h>
@@ -114,12 +115,8 @@ static CMVideoDimensions toCMVideoDimensions(const IntSize& size)
114115

115116
static dispatch_queue_t globaVideoCaptureSerialQueue()
116117
{
117-
static dispatch_queue_t globalQueue;
118-
static dispatch_once_t onceToken;
119-
dispatch_once(&onceToken, ^{
120-
globalQueue = dispatch_queue_create_with_target("WebCoreAVVideoCaptureSource video capture queue", DISPATCH_QUEUE_SERIAL, globalDispatchQueueSingleton(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
121-
});
122-
return globalQueue;
118+
static NeverDestroyed<OSObjectPtr<dispatch_queue_t>> globalQueue = adoptOSObject(dispatch_queue_create_with_target("WebCoreAVVideoCaptureSource video capture queue", DISPATCH_QUEUE_SERIAL, globalDispatchQueueSingleton(DISPATCH_QUEUE_PRIORITY_HIGH, 0)));
119+
return globalQueue.get().get();
123120
}
124121

125122
static FillLightMode toFillLightMode(AVCaptureTorchMode mode)

Source/WebCore/platform/text/BidiContext.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,20 @@ Ref<BidiContext> BidiContext::create(unsigned char level, UCharDirection directi
5959
ASSERT(level <= 1);
6060
if (!level) {
6161
if (!override) {
62-
static NeverDestroyed<RefPtr<BidiContext>> ltrContext;
63-
static std::once_flag ltrContextOnceFlag;
64-
std::call_once(ltrContextOnceFlag, [&]() {
65-
ltrContext.get() = createUncached(0, U_LEFT_TO_RIGHT, false, FromStyleOrDOM, 0);
66-
});
62+
static NeverDestroyed<RefPtr<BidiContext>> ltrContext = createUncached(0, U_LEFT_TO_RIGHT, false, FromStyleOrDOM, 0);
6763
return *ltrContext.get();
6864
}
6965

70-
static NeverDestroyed<RefPtr<BidiContext>> ltrOverrideContext;
71-
static std::once_flag ltrOverrideContextOnceFlag;
72-
std::call_once(ltrOverrideContextOnceFlag, [&]() {
73-
ltrOverrideContext.get() = createUncached(0, U_LEFT_TO_RIGHT, true, FromStyleOrDOM, 0);
74-
});
66+
static NeverDestroyed<RefPtr<BidiContext>> ltrOverrideContext = createUncached(0, U_LEFT_TO_RIGHT, true, FromStyleOrDOM, 0);
7567
return *ltrOverrideContext.get();
7668
}
7769

7870
if (!override) {
79-
static NeverDestroyed<RefPtr<BidiContext>> rtlContext;
80-
static std::once_flag rtlContextOnceFlag;
81-
std::call_once(rtlContextOnceFlag, [&]() {
82-
rtlContext.get() = createUncached(1, U_RIGHT_TO_LEFT, false, FromStyleOrDOM, 0);
83-
});
71+
static NeverDestroyed<RefPtr<BidiContext>> rtlContext = createUncached(1, U_RIGHT_TO_LEFT, false, FromStyleOrDOM, 0);
8472
return *rtlContext.get();
8573
}
8674

87-
static NeverDestroyed<RefPtr<BidiContext>> rtlOverrideContext;
88-
static std::once_flag rtlOverrideContextOnceFlag;
89-
std::call_once(rtlOverrideContextOnceFlag, [&]() {
90-
rtlOverrideContext.get() = createUncached(1, U_RIGHT_TO_LEFT, true, FromStyleOrDOM, 0);
91-
});
75+
static NeverDestroyed<RefPtr<BidiContext>> rtlOverrideContext = createUncached(1, U_RIGHT_TO_LEFT, true, FromStyleOrDOM, 0);
9276
return *rtlOverrideContext.get();
9377
}
9478

Source/WebCore/rendering/mac/RenderThemeMac.mm

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -930,15 +930,11 @@ static NSControlSize controlSizeForFont(const FontCascade& font)
930930

931931
static const std::span<const IntSize, 4> radioSizes()
932932
{
933-
static std::array<IntSize, 4> sizes;
934-
static dispatch_once_t onceToken;
935-
dispatch_once(&onceToken, ^{
936-
if (supportsLargeFormControls()) {
937-
sizes = { { IntSize(14, 14), IntSize(12, 12), IntSize(10, 10), IntSize(16, 16) } };
938-
return;
939-
}
940-
sizes = { { IntSize(16, 16), IntSize(12, 12), IntSize(10, 10), IntSize(0, 0) } };
941-
});
933+
static std::array<IntSize, 4> sizes = [] {
934+
if (supportsLargeFormControls())
935+
return std::array<IntSize, 4> { { IntSize(14, 14), IntSize(12, 12), IntSize(10, 10), IntSize(16, 16) } };
936+
return std::array<IntSize, 4> { { IntSize(16, 16), IntSize(12, 12), IntSize(10, 10), IntSize(0, 0) } };
937+
}();
942938
return sizes;
943939
}
944940

0 commit comments

Comments
 (0)