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

Update exception_list.hpp #6570

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 71 additions & 37 deletions libs/core/errors/include/hpx/errors/exception_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,81 +26,115 @@ namespace hpx {

/// The class exception_list is a container of exception_ptr objects
/// parallel algorithms may use to communicate uncaught exceptions
/// encountered during parallel execution to the caller of the algorithm
/// encountered during parallel execution to the caller of the algorithm.
///
/// The type exception_list::const_iterator fulfills the requirements of
/// a forward iterator.
///
class HPX_CORE_EXPORT exception_list : public hpx::exception
{
private:
/// \cond NOINTERNAL

// TODO: Does this need to be hpx::spinlock?
// typedef hpx::spinlock mutex_type;
// TODO: Add correct initialization of hpx::util::detail spinlock.
using mutex_type = hpx::util::detail::spinlock;

using exception_list_type = std::list<std::exception_ptr>;

exception_list_type exceptions_;
mutable mutex_type mtx_;

void add_no_lock(std::exception_ptr const& e);
/// \endcond
// Adds an exception to the list without acquiring a lock
void add_no_lock(std::exception_ptr const& e)
{
exceptions_.push_back(e);
}

public:
/// bidirectional iterator
using iterator = exception_list_type::const_iterator;

/// \cond NOINTERNAL
// \throws nothing
~exception_list() noexcept override = default;

exception_list();
explicit exception_list(std::exception_ptr const& e);
explicit exception_list(exception_list_type&& l);
exception_list() = default;

explicit exception_list(std::exception_ptr const& e)
{
add(e);
}

explicit exception_list(exception_list_type&& l)
{
std::lock_guard<mutex_type> lock(mtx_);
exceptions_ = std::move(l);
}

exception_list(exception_list const& l) : hpx::exception(l)
{
std::lock_guard<mutex_type> lock(l.mtx_);
exceptions_ = l.exceptions_;
}

exception_list(exception_list&& l) noexcept : hpx::exception(std::move(l))
{
std::lock_guard<mutex_type> l_lock(l.mtx_);
exceptions_ = std::move(l.exceptions_);
}

exception_list(exception_list const& l);
exception_list(exception_list&& l) noexcept;
exception_list& operator=(exception_list const& l)
{
if (this != &l) {
std::lock_guard<mutex_type> this_lock(mtx_);
std::lock_guard<mutex_type> l_lock(l.mtx_);
exceptions_ = l.exceptions_;
}
return *this;
}

exception_list& operator=(exception_list const& l);
exception_list& operator=(exception_list&& l) noexcept;
exception_list& operator=(exception_list&& l) noexcept
{
if (this != &l) {
std::lock_guard<mutex_type> this_lock(mtx_);
std::lock_guard<mutex_type> l_lock(l.mtx_);
exceptions_ = std::move(l.exceptions_);
}
return *this;
}

///
void add(std::exception_ptr const& e);
/// \endcond
// Adds an exception to the list in a thread-safe manner
void add(std::exception_ptr const& e)
{
std::lock_guard<mutex_type> lock(mtx_);
add_no_lock(e);
}

/// The number of exception_ptr objects contained within the
/// exception_list.
///
/// \note Complexity: Constant time.
/// Returns the number of exception_ptr objects in the exception_list.
/// Complexity: Constant time.
[[nodiscard]] std::size_t size() const noexcept
{
std::lock_guard<mutex_type> l(mtx_);
std::lock_guard<mutex_type> lock(mtx_);
return exceptions_.size();
}

/// An iterator referring to the first exception_ptr object contained
/// within the exception_list.
/// Returns an iterator referring to the first exception_ptr object.
[[nodiscard]] exception_list_type::const_iterator begin() const noexcept
{
std::lock_guard<mutex_type> l(mtx_);
std::lock_guard<mutex_type> lock(mtx_);
return exceptions_.begin();
}

/// An iterator which is the past-the-end value for the exception_list.
/// Returns a past-the-end iterator for the exception_list.
[[nodiscard]] exception_list_type::const_iterator end() const noexcept
{
std::lock_guard<mutex_type> l(mtx_);
std::lock_guard<mutex_type> lock(mtx_);
return exceptions_.end();
}

/// \cond NOINTERNAL
[[nodiscard]] std::error_code get_error_code() const;
[[nodiscard]] std::error_code get_error_code() const
{
return std::error_code(); // Placeholder implementation
}

[[nodiscard]] std::string get_message() const;
/// \endcond
[[nodiscard]] std::string get_message() const
{
return "Exception occurred"; // Placeholder implementation
}
};
} // namespace hpx
} // namespace hpx

#include <hpx/config/warnings_suffix.hpp>
Loading