eventual.hpp 5.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * Copyright (c) 2017 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */
#ifndef __THALLIUM_EVENTUAL_HPP
#define __THALLIUM_EVENTUAL_HPP

#include <type_traits>
#include <abt.h>
#include <thallium/exception.hpp>

namespace thallium {

/**
 * Exception class thrown by the eventual class.
 */
class eventual_exception : public exception {

    public:

        template<typename ... Args>
            eventual_exception(Args&&... args)
            : exception(std::forward<Args>(args)...) {}
};

#define TL_EVENTUAL_EXCEPTION(__fun,__ret) \
    eventual_exception(#__fun," returned ", abt_error_get_name(__ret),\
            " (", abt_error_get_description(__ret),") in ",__FILE__,":",__LINE__);

#define TL_EVENTUAL_ASSERT(__call) {\
    int __ret = __call; \
    if(__ret != ABT_SUCCESS) {\
        throw TL_EVENTUAL_EXCEPTION(__call, __ret);\
    }\
}

/**
 * @brief The eventual class wraps an ABT_eventual object.
 * It is a template class, with the template type T being
 * the object stored by the eventual. T must be default-constructible
 * and assignable.
 */
template<typename T>
class eventual {

47 48

    public:
49 50 51 52

    /**
     * @brief Type of value stored by the eventual.
     */
53
    using value_type = typename std::remove_reference<typename std::remove_cv<T>::type>::type;
54 55 56 57 58 59 60
    /**
     * @brief Native handle type.
     */
    using native_handle_type = ABT_eventual;

    private:

61
    ABT_eventual m_eventual;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    value_type   m_value;

    public:

    /**
     * @brief Get the underlying native handle.
     *
     * @return The underlying native handle.
     */
    ABT_eventual native_handle() const noexcept {
        return m_eventual;
    }

    /**
     * @brief Constryctor.
     */
    eventual() {
        TL_EVENTUAL_ASSERT(ABT_eventual_create(0, &m_eventual));
    }

    /**
     * @brief Destructor.
     */
    ~eventual() {
        ABT_eventual_free(&m_eventual);
    }

    /**
     * @brief Copy constructor is deleted.
     */
    eventual(const eventual& other)            = delete;

    /**
     * @brief Copy assignment operator is deleted.
     */
    eventual& operator=(const eventual& other) = delete;

    /**
     * @brief Move assignment operator. If the left and right
     * operands are different, this function will assign the
     * right operand's resource to the left. This invalidates
     * the right operand.
     */
    eventual& operator=(eventual&& other) {
        if(this == other.m_eventual) return *this;
        if(m_eventual != ABT_EVENTUAL_NULL) {
            TL_EVENTUAL_ASSERT(ABT_eventual_free(&m_eventual));
        }
        m_eventual = other.m_eventual;
        other.m_eventual = ABT_EVENTUAL_NULL;
        return *this;
    }

    /**
     * @brief Move constructor.
     */
    eventual(eventual&& other)
    : m_eventual(other.m_eventual) {
            other.m_eventual = ABT_EVENTUAL_NULL;
    }

    /**
     * @brief Set the eventual's value (by copy).
     *
     * @param val Value to give the eventual.
     */
    void set_value(const T& val) {
        m_value = val;
        TL_EVENTUAL_ASSERT(ABT_eventual_set(m_eventual, nullptr, 0));
    }

    /**
     * @brief Set the eventual's value (by moving).
     *
     * @param val Value to give the eventual.
     */
    void set_value(T&& val) {
        m_value = std::move(val);
        TL_EVENTUAL_ASSERT(ABT_eventual_set(m_eventual, nullptr, 0));
    }

    /**
     * @brief Wait on the eventual.
     *
     * @return The value stored in the eventual.
     */
    value_type wait() {
        TL_EVENTUAL_ASSERT(ABT_eventual_wait(m_eventual, nullptr));
        return m_value;
    }

    /**
     * @brief Reset the eventual.
     */
    void reset() {
        TL_EVENTUAL_ASSERT(ABT_eventual_reset(m_eventual));
    }
};

/**
 * @brief Specialization of eventual class for T=void
 */
template<>
class eventual<void> {
166 167

    public:
168 169 170 171 172 173 174 175

    /**
     * @brief Native handle type.
     */
    using native_handle_type = ABT_eventual;

    private:

176
    ABT_eventual m_eventual;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    public:

    /**
     * @brief Returns the underlying native handle.
     *
     * @return The underlying native handle.
     */
    ABT_eventual native_handle() const noexcept {
        return m_eventual;
    }

    /**
     * @brief Constructor.
     */
    eventual() {
        TL_EVENTUAL_ASSERT(ABT_eventual_create(0, &m_eventual));
    }

    /**
     * @brief Destructor.
     */
    ~eventual() {
        ABT_eventual_free(&m_eventual);
    }

    /**
     * @brief Copy constructor is deleted.
     */
    eventual(const eventual& other)            = delete;

    /**
     * @brief Copy assignment operator is deleted.
     */
    eventual& operator=(const eventual& other) = delete;

    /**
     * @brief Move assignment operator. If the left and right
     * operands are different, this function will assign the
     * right operand's resource to the left. This invalidates
     * the right operand.
     */
    eventual& operator=(eventual&& other) { 
        if(this == other.m_eventual) return *this;
        if(m_eventual != ABT_EVENTUAL_NULL) {
            TL_EVENTUAL_ASSERT(ABT_eventual_free(&m_eventual));
        }
        m_eventual = other.m_eventual;
        other.m_eventual = ABT_EVENTUAL_NULL;
        return *this;
    }

    /**
     * @brief Move constructor.
     */
    eventual(eventual&& other)
    : m_eventual(other.m_eventual) {
            other.m_eventual = ABT_EVENTUAL_NULL;
    }

    /**
     * @brief Set the eventual.
     */
    void set_value() {
        TL_EVENTUAL_ASSERT(ABT_eventual_set(m_eventual, nullptr, 0));
    }

    /**
     * @brief Wait on the eventual.
     */
    void wait() {
        TL_EVENTUAL_ASSERT(ABT_eventual_wait(m_eventual, nullptr));
    }

    /**
     * @brief Reset the eventual.
     */
    void reset() {
        TL_EVENTUAL_ASSERT(ABT_eventual_reset(m_eventual));
    }
};

}

#undef TL_EVENTUAL_EXCEPTION
#undef TL_EVENTUAL_ASSERT

#endif /* end of include guard */