-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add noexcept specifiers where appropriate #91
Merged
LeonMatthesKDAB
merged 2 commits into
KDAB:main
from
LeonMatthesKDAB:experimental-noexcept-disconnect
Jul 17, 2024
Merged
Add noexcept specifiers where appropriate #91
LeonMatthesKDAB
merged 2 commits into
KDAB:main
from
LeonMatthesKDAB:experimental-noexcept-disconnect
Jul 17, 2024
Conversation
This file contains 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
LeonMatthesKDAB
force-pushed
the
experimental-noexcept-disconnect
branch
from
July 15, 2024 09:49
ec48bb7
to
4015198
Compare
MiKom
requested changes
Jul 15, 2024
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.
Awesome job. Some small suggestions.
MiKom
previously approved these changes
Jul 15, 2024
We can mark the following as `noexcept`: - ~ScopedConnection - ~Signal - ConnectionHandle::disconnect - several move constructors There are some caveats though: When disconnecting, we need to lock the mutex in the ConnectionEvaluator and allocate in the GenerationalIndexArray, which can both throw exceptions. However, we currently do not have any out of memory guarantees in KDBindings, so terminating in that case should be a sufficient way of handling this case. The same goes for when locking a mutex isn't possible. This likely indicates some kind of OS issue from which we can't really recover, so terminating is appropriate. We could improve the situation by avoiding allocations in the GenerationalIndexArray when erasing slots. This would be possible by using an inline linked-list inside the array to store free indices. Which would save memory and would no longer require us to allocate memory when erasing entries. However, it's also not trivial to implement, so leave this as a TODO. Error checking in ConnectionEvaluator ------------------------------------- This patch also adds some basic exception handling to the ConnectionEvaluator. It is now somewhat protected from slots disconnecting themselves while they are evaluated and slots throwing exceptions. However, this error-handling is rather basic and will simply **not** dequeue any slots while the evaluator is already evaluating and will dequeue all slots if any slot throws an exception. We could improve this situation by using a queue and popping elements 1-by-1 when evaluating without actually iterating over it. That way we could erase from the queue while its being evaluated. And when a slot throws, calling evaluate again could simply continue with the next slot, instead of skipping all queued slots. However, we should ideally implement this queue as a ring-buffer to avoid excessive allocations by std::list or std::dequeu. But this isn't part of the STL, so would require us to roll our own ring-buffer implementation. For now, this error handling isn't publicly documented and throwing within a slot or dequeuing while evaluating remains *undefined*, so that we can improve the situation in the future.
We currently don't really deal with this correctly. Until we do so, at least mention that it's UB.
LeonMatthesKDAB
force-pushed
the
experimental-noexcept-disconnect
branch
from
July 15, 2024 14:57
727236b
to
db9891b
Compare
MiKom
approved these changes
Jul 16, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We can mark the following as
noexcept
:There are some caveats though:
When disconnecting, we need to lock the mutex in the ConnectionEvaluator and allocate in the GenerationalIndexArray, which can both throw exceptions.
However, we currently do not have any out of memory guarantees in KDBindings, so terminating in that case should be a sufficient way of handling this case.
The same goes for when locking a mutex isn't possible. This likely indicates some kind of OS issue from which we can't really recover, so terminating is appropriate.
We could improve the situation by avoiding allocations in the GenerationalIndexArray when erasing slots.
This would be possible by using an inline linked-list inside the array to store free indices.
Which would save memory and would no longer require us to allocate memory when erasing entries.
However, it's also not trivial to implement, so leave this as a TODO.
Error checking in ConnectionEvaluator
This patch also adds some basic exception handling to the ConnectionEvaluator.
It is now somewhat protected from slots disconnecting themselves while they are evaluated and slots throwing exceptions.
However, this error-handling is rather basic and will simply not dequeue any slots while the evaluator is already evaluating and will dequeue all slots if any slot throws an exception. We could improve this situation by using a queue and popping elements 1-by-1 when evaluating without actually iterating over it. That way we could erase from the queue while its being evaluated. And when a slot throws, calling evaluate again could simply continue with the next slot, instead of skipping all queued slots.
However, we should ideally implement this queue as a ring-buffer to avoid excessive allocations by std::list or std::dequeu. But this isn't part of the STL, so would require us to roll our own ring-buffer implementation.
For now, this error handling isn't publicly documented and throwing within a slot or dequeuing while evaluating remains undefined, so that we can improve the situation in the future.