mutex.hpp 4.5 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (c) 2017 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

#ifndef __THALLIUM_MUTEX_HPP
#define __THALLIUM_MUTEX_HPP

#include <abt.h>
11 12
#include <thallium/exception.hpp>
#include <thallium/abt_errors.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
13 14 15

namespace thallium {

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * Exception class thrown by the mutex class.
 */
class mutex_exception : public exception {

    public:

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

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

#define TL_MUTEX_ASSERT(__call) {\
    int __ret = __call; \
    if(__ret != ABT_SUCCESS) {\
        throw TL_MUTEX_EXCEPTION(__call, __ret);\
    }\
}
Matthieu Dorier's avatar
Matthieu Dorier committed
38 39 40 41 42 43 44

/**
 * @brief The mutex class is an equivalent of std::mutex
 * but build around Argobot's mutex.
 */
class mutex {

45 46 47
    ABT_mutex m_mutex;

    public:
Matthieu Dorier's avatar
Matthieu Dorier committed
48 49 50 51 52 53 54 55 56 57 58

    /**
     * @brief Type of the underlying native handle.
     */
    typedef ABT_mutex native_handle_type;

    /**
     * @brief Constructor.
     *
     * @param recursive whether the mutex is recursive or not.
     */
59
    explicit mutex(bool recursive = false) {
60
        ABT_mutex_attr attr;
61 62
        TL_MUTEX_ASSERT(ABT_mutex_attr_create(&attr));
        if(recursive) {
63 64
            TL_MUTEX_ASSERT(ABT_mutex_attr_set_recursive(attr, ABT_TRUE));
        }
65
        TL_MUTEX_ASSERT(ABT_mutex_create_with_attr(attr, &m_mutex));
66
        TL_MUTEX_ASSERT(ABT_mutex_attr_free(&attr));
67
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
68 69 70 71

    /**
     * @brief Copy constructor is deleted.
     */
72
    mutex(const mutex& other) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
73 74 75 76 77 78 79 80 81

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

    /**
     * @brief Move assignment operator is deleted.
     */
82
    mutex& operator=(mutex&& other) {
83 84 85
        if(m_mutex != ABT_MUTEX_NULL) {
            TL_MUTEX_ASSERT(ABT_mutex_free(&m_mutex));
        }
86 87 88 89
        m_mutex = other.m_mutex;
        other.m_mutex = ABT_MUTEX_NULL;
        return *this;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
90 91 92 93

    /**
     * @brief Move constructor.
     */
94 95 96 97
    mutex(mutex&& other) {
        m_mutex = other.m_mutex;
        other.m_mutex = ABT_MUTEX_NULL;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
98 99 100 101

    /**
     * @brief Destructor.
     */
102 103 104 105
    virtual ~mutex() noexcept {
        if(m_mutex != ABT_MUTEX_NULL)
            ABT_mutex_free(&m_mutex);
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
106 107 108 109

    /**
     * @brief Lock the mutex.
     */
110
    void lock() {
111
        TL_MUTEX_ASSERT(ABT_mutex_lock(m_mutex));
112
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
113 114 115 116

    /**
     * @brief Lock the mutex in low priority.
     */
117 118 119
    void lock_low() {
        TL_MUTEX_ASSERT(ABT_mutex_lock_low(m_mutex));
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
120 121 122 123

    /**
     * @brief Lock the mutex without context switch.
     */
124 125 126
    void spin_lock() {
        TL_MUTEX_ASSERT(ABT_mutex_spinlock(m_mutex));
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
127 128 129 130 131 132

    /**
     * @brief Try locking the mutex without blocking.
     *
     * @return true if it managed to lock the mutex.
     */
133
    bool try_lock() {
134 135 136 137 138 139 140 141
        int ret = ABT_mutex_trylock(m_mutex);
        if(ABT_SUCCESS == ret) {
            return true;
        } else if(ABT_ERR_MUTEX_LOCKED == ret) {
            return false;
        } else {
           TL_MUTEX_EXCEPTION(ABT_mutex_trylock(m_mutex), ret); 
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
142
        return false;
143
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
144 145 146 147

    /**
     * @brief Unlock the mutex.
     */
148 149 150
    void unlock() {
        TL_MUTEX_ASSERT(ABT_mutex_unlock(m_mutex));
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
151 152 153 154

    /**
     * @brief Hand over the mutex within the ES.
     */
155 156 157
    void unlock_se() {
        TL_MUTEX_ASSERT(ABT_mutex_unlock_se(m_mutex));
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    
    /**
     * @brief Get the underlying native handle.
     *
     * @return the underlying native handle.
     */
    ABT_mutex native_handle() const noexcept {
        return m_mutex;
    }
};

/**
 * @brief Child class of mutex to represent a recursive mutex.
 */
class recursive_mutex : public mutex {

    public:

    /**
     * @brief Constructor.
     */
    recursive_mutex()
    : mutex(true) {}

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

    /**
     * @brief Move constructor.
     */
    recursive_mutex(recursive_mutex&& other)
        : mutex(std::move(other)) {}

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

    /**
199
     * @brief Move assignment operator.
Matthieu Dorier's avatar
Matthieu Dorier committed
200
     */
201
    recursive_mutex& operator=(recursive_mutex&& other) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
202 203 204 205
};

}

206 207 208
#undef TL_MUTEX_EXCEPTION
#undef TL_MUTEX_ASSERT

Matthieu Dorier's avatar
Matthieu Dorier committed
209
#endif /* end of include guard */