Skip to content
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

Conversation

LeonMatthesKDAB
Copy link
Contributor

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.

Copy link
Member

@MiKom MiKom left a 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.

src/kdbindings/connection_handle.h Outdated Show resolved Hide resolved
src/kdbindings/connection_evaluator.h Show resolved Hide resolved
MiKom
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 LeonMatthesKDAB merged commit aab1cfd into KDAB:main Jul 17, 2024
5 checks passed
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.

2 participants