Skip to content

Commit

Permalink
Cocoa WorkQueue::dispatchWithQOS() and dispatch() work item lifetime …
Browse files Browse the repository at this point in the history
…semantics do not match

https://bugs.webkit.org/show_bug.cgi?id=260852
rdar://114618174

Reviewed by Jean-Yves Avenard and Chris Dumez.

dispatch() users expect the work function to be destructed in the
queue

Add the same semantic to dispatchWithQOS() to avoid confusion in the
code and to have uniform interface.

* Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp:
(WTF::WorkQueueBase::dispatchWithQOS):
* Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/267776@main
  • Loading branch information
kkinnunen-apple committed Sep 8, 2023
1 parent dc0f5e2 commit 56fa22f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ void WorkQueueBase::dispatch(Function<void()>&& function)

void WorkQueueBase::dispatchWithQOS(Function<void()>&& function, QOS qos)
{
dispatch_block_t blockWithQOS = dispatch_block_create_with_qos_class(DISPATCH_BLOCK_ENFORCE_QOS_CLASS, Thread::dispatchQOSClass(qos), 0, makeBlockPtr([function = WTFMove(function)] {
dispatch_block_t blockWithQOS = dispatch_block_create_with_qos_class(DISPATCH_BLOCK_ENFORCE_QOS_CLASS, Thread::dispatchQOSClass(qos), 0, makeBlockPtr([function = WTFMove(function)] () mutable {
function();
function = { };
}).get());
dispatch_async(m_dispatchQueue.get(), blockWithQOS);
#if !__has_feature(objc_arc)
Expand Down
7 changes: 6 additions & 1 deletion Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,14 @@ TEST(WTF_WorkQueue, DestroyDispatchedOnDispatchQueue)
for (size_t j = 0; j < queueCount; ++j)
queue[j]->dispatch([instance = std::make_unique<DestructionWorkQueueTester>(counter, *queue[j])] { }); // NOLINT
}
// dispatchQOS() behaves the same as dispatch().
for (size_t i = 0; i < iterationCount; ++i) {
for (size_t j = 0; j < queueCount; ++j)
queue[j]->dispatchWithQOS([instance = std::make_unique<DestructionWorkQueueTester>(counter, *queue[j])] { }, Thread::QOS::UserInteractive); // NOLINT
}
for (size_t j = 0; j < queueCount; ++j)
queue[j]->dispatchSync([] { });
EXPECT_EQ(queueCount * iterationCount, counter);
EXPECT_EQ(2u * queueCount * iterationCount, counter);
}

namespace {
Expand Down

0 comments on commit 56fa22f

Please sign in to comment.