-
Notifications
You must be signed in to change notification settings - Fork 154
add DeferCall to replace QMetaObject::invokeMethod #48101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (C) 2025 Fastly, Inc. | ||
| * | ||
| * This file is part of Pushpin. | ||
| * | ||
| * $FANOUT_BEGIN_LICENSE:APACHE2$ | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * $FANOUT_END_LICENSE$ | ||
| */ | ||
|
|
||
| #include "defercall.h" | ||
| #include <assert.h> | ||
|
|
||
| static thread_local DeferCall *g_instance = nullptr; | ||
|
|
||
| DeferCall::DeferCall() = default; | ||
|
|
||
| DeferCall::~DeferCall() = default; | ||
|
|
||
| void DeferCall::defer(std::function<void ()> handler) | ||
| { | ||
| Call c; | ||
| c.handler = handler; | ||
|
|
||
| deferredCalls_.push_back(c); | ||
|
|
||
| QMetaObject::invokeMethod(this, "callNext", Qt::QueuedConnection); | ||
| } | ||
|
|
||
| DeferCall *DeferCall::global() | ||
| { | ||
| if(!g_instance) | ||
| g_instance = new DeferCall; | ||
|
|
||
| return g_instance; | ||
| } | ||
|
|
||
| void DeferCall::cleanup() | ||
| { | ||
| delete g_instance; | ||
| g_instance = nullptr; | ||
| } | ||
|
|
||
| void DeferCall::callNext() | ||
| { | ||
| // there can't be more invokeMethod resolutions than queued calls | ||
| assert(!deferredCalls_.empty()); | ||
|
|
||
| Call c = deferredCalls_.front(); | ||
| deferredCalls_.pop_front(); | ||
|
|
||
| c.handler(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (C) 2025 Fastly, Inc. | ||
| * | ||
| * This file is part of Pushpin. | ||
| * | ||
| * $FANOUT_BEGIN_LICENSE:APACHE2$ | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * $FANOUT_END_LICENSE$ | ||
| */ | ||
|
|
||
| #ifndef DEFERCALL_H | ||
| #define DEFERCALL_H | ||
|
|
||
| #include <QObject> | ||
|
|
||
| // queues calls to be run after returning to the event loop | ||
| class DeferCall : public QObject | ||
| { | ||
| Q_OBJECT | ||
|
|
||
| public: | ||
| DeferCall(); | ||
| ~DeferCall(); | ||
|
|
||
| // queue handler to be called after returning to the event loop. if | ||
| // handler contains references, they must outlive DeferCall. the | ||
| // recommended usage is for each object needing to perform deferred calls | ||
| // to keep a DeferCall as a member variable, and only refer to the | ||
| // object's own data in the handler. that way, any references are | ||
| // guaranteed to live long enough. | ||
| void defer(std::function<void ()> handler); | ||
|
|
||
| static DeferCall *global(); | ||
| static void cleanup(); | ||
|
|
||
| template <typename T> | ||
| static void deleteLater(T *p) | ||
| { | ||
| global()->defer([=]() { delete p; }); | ||
| } | ||
|
|
||
| private slots: | ||
| void callNext(); | ||
|
|
||
| private: | ||
| class Call | ||
| { | ||
| public: | ||
| std::function<void ()> handler; | ||
| }; | ||
|
|
||
| std::list<Call> deferredCalls_; | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will execute a list of deferred calls in First-In-First-Out order, which seems to make sense to me. But
deferin go executes in reverse order. I can't recall the specifics of any other languages right now, but I wonder if there's an expectation for one or other that might cause problems in the future, especially when using thedeleteLaterfeature with complex data structures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point regarding
deleteLater. Will double-check Qt's behavior before landing this, since the goal is for it to work similarly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed the deletions are in-order.
There is one exception: if there are nested event loops, deferred deletions only occur within the event loop that they were originally queued. So processing of deletions within an event loop is FIFO, but processing of the loops themselves is LIFO. In any case, we don't use nested event loops.