Skip to content

Commit

Permalink
Update exception_list.hpp
Browse files Browse the repository at this point in the history
This exception_list class in the hpx namespace is a thread-safe container for handling multiple exceptions encountered during parallel algorithm execution. It allows storing std::exception_ptr objects, provides thread-safe access via a spinlock, and includes methods to add exceptions, get the count (size()), and iterate over the stored exceptions (begin()/end()). The class also includes placeholders for retrieving error codes and messages. This is useful for collecting and managing exceptions across parallel threads.
  • Loading branch information
charan-003 authored Nov 9, 2024
1 parent 5356c52 commit f0032e1
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions libs/core/errors/include/hpx/errors/exception_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ namespace hpx {
exception_list_type exceptions_;
mutable mutex_type mtx_;

void add_no_lock(std::exception_ptr const& e);
void add_no_lock(std::exception_ptr const& e)
{
exceptions_.push_back(e);
}
/// \endcond

public:
Expand All @@ -61,13 +64,37 @@ namespace hpx {
explicit exception_list(exception_list_type&& l);

exception_list(exception_list const& l);
exception_list(exception_list&& l) noexcept;

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

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&& 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);
void add(std::exception_ptr const& e)
{
std::lock_guard<mutex_type> l(mtx_);
add_no_lock(e);
}
/// \endcond

/// The number of exception_ptr objects contained within the
Expand Down Expand Up @@ -96,9 +123,15 @@ namespace hpx {
}

/// \cond NOINTERNAL
[[nodiscard]] std::error_code get_error_code() const;

[[nodiscard]] std::string get_message() const;
[[nodiscard]] std::error_code get_error_code() const
{
return std::error_code(); // placeholder implementation
}

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

0 comments on commit f0032e1

Please sign in to comment.