Skip to content

[WTF] Add ThreadSafeLazyUniquePtr#64619

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
Constellation:eng/WTF-Add-ThreadSafeLazyUniquePtr
May 11, 2026
Merged

[WTF] Add ThreadSafeLazyUniquePtr#64619
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
Constellation:eng/WTF-Add-ThreadSafeLazyUniquePtr

Conversation

@Constellation
Copy link
Copy Markdown
Member

@Constellation Constellation commented May 9, 2026

1661ae2

[WTF] Add ThreadSafeLazyUniquePtr
https://bugs.webkit.org/show_bug.cgi?id=314487
rdar://176696158

Reviewed by Keith Miller.

Point of this class is adding handy class which uses the pattern,

    if (oldValue)
        return oldValue;

    newValue = factory();
    if (oldValue = Strong-CAS-with-release(oldValue, nullptr, newValue)) {
        destroy newValue;
        return oldValue;
    }
    return newValue;

This removes necessity of locking to publish a new value to the other
thread. We speculatively create a new value and attempt to publish it,
if it failed, we use the already published one. But otherwise, we
install a newly created one. This is ownership wrapped version of
Atomics.h's ensurePointer.

This is something similar to standard C++'s
`std::atomic<std::shared_ptr<T>>`. But it does not offer
`std::atomic<std::unique_ptr<T>>`, so this is why this class exists.

We use this class for IPIntSharedBytecode in RTT. We also remove
storeStoreFence() since now we use "release" to publish a value via strong-CAS.

* Source/JavaScriptCore/wasm/WasmTypeDefinition.cpp:
(JSC::Wasm::RTT::ensureArgumINTBytecode const):
(JSC::Wasm::RTT::ensureUINTBytecode const):
(JSC::Wasm::RTT::ensureCallBytecode const):
(JSC::Wasm::RTT::ensureTailCallBytecode const):
* Source/JavaScriptCore/wasm/WasmTypeDefinition.h:
* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/ThreadSafeLazyUniquePtr.h: Added.

Canonical link: https://commits.webkit.org/313019@main

bf3fa09

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
✅ 🧪 style ✅ 🛠 ios 🛠 mac ✅ 🛠 wpe ⏳ 🛠 win ⏳ 🛠 ios-apple
✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug 🧪 wpe-wk2 ⏳ 🧪 win-tests ⏳ 🛠 mac-apple
✅ 🧪 webkitperl 🧪 ios-wk2 🧪 api-mac 🧪 api-wpe ⏳ 🛠 vision-apple
⏳ 🧪 ios-wk2-wpt ⏳ 🧪 api-mac-debug 🛠 gtk3-libwebrtc
loading 🛠 🧪 jsc ⏳ 🧪 api-ios 🧪 mac-wk1 ✅ 🛠 gtk
loading 🛠 🧪 jsc-debug-arm64 🛠 ios-safer-cpp 🧪 mac-wk2 🧪 gtk-wk2
✅ 🛠 vision ⏳ 🧪 mac-AS-debug-wk2 🧪 api-gtk
✅ 🛠 vision-sim 🧪 mac-wk2-stress ✅ 🛠 playstation
✅ 🛠 🧪 unsafe-merge 🧪 vision-wk2 🧪 mac-intel-wk2 ✅ 🛠 jsc-armv7
✅ 🛠 tv 🛠 mac-safer-cpp 🧪 jsc-armv7-tests
✅ 🛠 tv-sim
✅ 🛠 watch
🛠 watch-sim

@Constellation Constellation requested a review from a team as a code owner May 9, 2026 19:07
@Constellation Constellation self-assigned this May 9, 2026
@Constellation Constellation force-pushed the eng/WTF-Add-ThreadSafeLazyUniquePtr branch from f31a772 to a0282ad Compare May 9, 2026 19:08
@Constellation Constellation force-pushed the eng/WTF-Add-ThreadSafeLazyUniquePtr branch from a0282ad to 1c1a141 Compare May 9, 2026 19:12
Copy link
Copy Markdown
Contributor

@kmiller68 kmiller68 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me

Comment on lines +52 to +53
template<typename Func>
T& ensure(NOESCAPE const Func& func) const
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Might be worth a comment along the lines of "In general there is no need to fence in func the compareExchangeStrong will ensure any stores to initialize the value will happen before the new pointer becomes visible."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

@Constellation Constellation force-pushed the eng/WTF-Add-ThreadSafeLazyUniquePtr branch from 1c1a141 to bf3fa09 Compare May 11, 2026 18:27
@Constellation Constellation added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label May 11, 2026
https://bugs.webkit.org/show_bug.cgi?id=314487
rdar://176696158

Reviewed by Keith Miller.

Point of this class is adding handy class which uses the pattern,

    if (oldValue)
        return oldValue;

    newValue = factory();
    if (oldValue = Strong-CAS-with-release(oldValue, nullptr, newValue)) {
        destroy newValue;
        return oldValue;
    }
    return newValue;

This removes necessity of locking to publish a new value to the other
thread. We speculatively create a new value and attempt to publish it,
if it failed, we use the already published one. But otherwise, we
install a newly created one. This is ownership wrapped version of
Atomics.h's ensurePointer.

This is something similar to standard C++'s
`std::atomic<std::shared_ptr<T>>`. But it does not offer
`std::atomic<std::unique_ptr<T>>`, so this is why this class exists.

We use this class for IPIntSharedBytecode in RTT. We also remove
storeStoreFence() since now we use "release" to publish a value via strong-CAS.

* Source/JavaScriptCore/wasm/WasmTypeDefinition.cpp:
(JSC::Wasm::RTT::ensureArgumINTBytecode const):
(JSC::Wasm::RTT::ensureUINTBytecode const):
(JSC::Wasm::RTT::ensureCallBytecode const):
(JSC::Wasm::RTT::ensureTailCallBytecode const):
* Source/JavaScriptCore/wasm/WasmTypeDefinition.h:
* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/ThreadSafeLazyUniquePtr.h: Added.

Canonical link: https://commits.webkit.org/313019@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/WTF-Add-ThreadSafeLazyUniquePtr branch from bf3fa09 to 1661ae2 Compare May 11, 2026 18:35
@webkit-commit-queue
Copy link
Copy Markdown
Collaborator

Committed 313019@main (1661ae2): https://commits.webkit.org/313019@main

Reviewed commits have been landed. Closing PR #64619 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 1661ae2 into WebKit:main May 11, 2026
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants