Skip to content

Commit

Permalink
Update exception_list.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
charan-003 authored Nov 10, 2024
1 parent 9e03e6f commit 5f31ce5
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions libs/core/errors/include/hpx/errors/exception_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,125 +26,128 @@ 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:
using mutex_type = hpx::spinlock; // Use hpx::spinlock for thread safety
/// \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_; // Holds the list of exceptions
mutable mutex_type mtx_; // Mutex for thread-safe access
exception_list_type exceptions_;
mutable mutex_type mtx_;

// Internal function to add an exception without locking
void add_no_lock(std::exception_ptr const& e)
{
exceptions_.push_back(e);
}
/// \endcond

public:
/// Bidirectional iterator type for the exception list
/// bidirectional iterator
using iterator = exception_list_type::const_iterator;

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

// Default constructor
exception_list() = default;

// Constructor that adds an initial exception
explicit exception_list(std::exception_ptr const& e)
{
add(e);
}

// Move constructor that transfers ownership of exceptions
explicit exception_list(exception_list_type&& l)
{
std::lock_guard<mutex_type> lock(mtx_);
exceptions_ = std::move(l);
}

// Copy constructor with thread-safe access
exception_list(exception_list const& l)
exception_list(exception_list const& l) : hpx::exception(l)
{
std::lock_guard<mutex_type> lock(l.mtx_);
exceptions_ = l.exceptions_;
}

// Move constructor with thread-safe access
exception_list(exception_list&& l) noexcept
exception_list(exception_list&& l) noexcept : hpx::exception(std::move(l))
{
std::lock_guard<mutex_type> lock(l.mtx_);
std::lock_guard<mutex_type> l_lock(l.mtx_);
exceptions_ = std::move(l.exceptions_);
}

// Copy assignment operator with thread-safe access
exception_list& operator=(exception_list const& l)
{
if (this != &l) { // Avoid self-assignment
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;
}

// Move assignment operator with thread-safe access
exception_list& operator=(exception_list&& l) noexcept
{
if (this != &l) { // Avoid self-assignment
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;
}

// 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_);
std::lock_guard<mutex_type> l(mtx_);
add_no_lock(e);
}
/// \endcond

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

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

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

// Placeholder for retrieving an error code
/// \cond NOINTERNAL
[[nodiscard]] std::error_code get_error_code() const
{
return std::error_code(); // Placeholder implementation
return std::error_code(); // placeholder implementation
}

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

#include <hpx/config/warnings_suffix.hpp>

0 comments on commit 5f31ce5

Please sign in to comment.