remote_procedure.hpp 4.07 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2
/*
 * (C) 2017 The University of Chicago
3
 *
Matthieu Dorier's avatar
Matthieu Dorier committed
4 5
 * See COPYRIGHT in top-level directory.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6 7 8 9 10 11 12
#ifndef __THALLIUM_REMOTE_PROCEDURE_HPP
#define __THALLIUM_REMOTE_PROCEDURE_HPP

#include <margo.h>

namespace thallium {

13
class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
14
class endpoint;
15
class provider_handle;
Matthieu Dorier's avatar
Matthieu Dorier committed
16
class callable_remote_procedure;
17 18 19
namespace detail {
    struct engine_impl;
}
Matthieu Dorier's avatar
Matthieu Dorier committed
20

Matthieu Dorier's avatar
Matthieu Dorier committed
21 22 23 24 25 26 27
/**
 * @brief remote_procedure objects are produced by
 * engine::define() when defining an RPC.
 * Using remote_procedure::on(endpoint) creates a
 * callable_remote_procedure that can be called with
 * some parameters to send an RPC.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
28
class remote_procedure {
Matthieu Dorier's avatar
Matthieu Dorier committed
29
    friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
30

31
  private:
32
    std::weak_ptr<detail::engine_impl> m_engine_impl;
33
    hg_id_t                            m_id = 0;
34
    bool                               m_ignore_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
35

Matthieu Dorier's avatar
Matthieu Dorier committed
36 37 38 39 40 41 42
    /**
     * @brief Constructor. Made private because remote_procedure
     * objects are created only by engine::define().
     *
     * @param e Engine object that created the remote_procedure.
     * @param id Mercury RPC id.
     */
43 44 45 46
    remote_procedure(std::weak_ptr<detail::engine_impl> e, hg_id_t id)
    : m_engine_impl(std::move(e))
    , m_id(id)
    , m_ignore_response(false) {}
Matthieu Dorier's avatar
Matthieu Dorier committed
47

48
  public:
49 50 51

    remote_procedure() = default;

Matthieu Dorier's avatar
Matthieu Dorier committed
52 53 54
    /**
     * @brief Copy-constructor is default.
     */
55
    remote_procedure(const remote_procedure& other) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
56 57 58 59

    /**
     * @brief Move-constructor is default.
     */
60
    remote_procedure(remote_procedure&& other) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
61 62 63 64

    /**
     * @brief Copy-assignment operator is default.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
65
    remote_procedure& operator=(const remote_procedure& other) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
66 67 68 69

    /**
     * @brief Move-assignment operator is default.
     */
70
    remote_procedure& operator=(remote_procedure&& other) = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
71 72 73 74

    /**
     * @brief Destructor is default.
     */
75
    ~remote_procedure() = default;
Matthieu Dorier's avatar
Matthieu Dorier committed
76

Matthieu Dorier's avatar
Matthieu Dorier committed
77 78 79 80 81 82 83 84
    /**
     * @brief Creates a callable_remote_procedure by associating the
     * remote_procedure with an endpoint.
     *
     * @param ep endpoint with which to associate the procedure.
     *
     * @return a callable_remote_procedure.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
85
    callable_remote_procedure on(const endpoint& ep) const;
86 87 88 89 90 91 92 93 94 95 96

    /**
     * @brief Creates a callable remote_procedure by associating the
     * remote_procedure with a particular provider_handle.
     *
     * @param ph provider_handle with which to associate the procedure.
     *
     * @return a callable_remote_procedure.
     */
    callable_remote_procedure on(const provider_handle& ph) const;

Matthieu Dorier's avatar
Matthieu Dorier committed
97 98 99 100 101
    /**
     * @brief Tell the remote_procedure that it should not expect responses.
     *
     * @return *this
     */
102 103
    remote_procedure& disable_response();

Matthieu Dorier's avatar
Matthieu Dorier committed
104 105 106 107 108
    /**
     * @brief Deregisters this RPC from the engine.
     */
    void deregister();

109 110
    [[deprecated("use disable_response() instead")]] inline remote_procedure&
    ignore_response() {
111 112
        return disable_response();
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
113 114
};

115
} // namespace thallium
Matthieu Dorier's avatar
Matthieu Dorier committed
116

117 118 119 120 121 122 123
#include <thallium/callable_remote_procedure.hpp>
#include <thallium/engine.hpp>
#include <thallium/provider_handle.hpp>

namespace thallium {

inline callable_remote_procedure remote_procedure::on(const endpoint& ep) const {
124 125
    if(m_id == 0)
        throw exception("remote_procedure object isn't initialized");
126 127 128 129 130
    return callable_remote_procedure(m_engine_impl, m_id, ep, m_ignore_response);
}

inline callable_remote_procedure
remote_procedure::on(const provider_handle& ph) const {
131 132
    if(m_id == 0)
        throw exception("remote_procedure object isn't initialized");
133 134 135 136 137 138
    return callable_remote_procedure(m_engine_impl, m_id, ph, m_ignore_response,
                                     ph.provider_id());
}

inline void remote_procedure::deregister() {
    auto engine_impl = m_engine_impl.lock();
139 140
    if(engine_impl)
        margo_deregister(engine_impl->m_mid, m_id);
141 142 143 144 145
}

inline remote_procedure& remote_procedure::disable_response() {
    m_ignore_response = true;
    auto engine_impl = m_engine_impl.lock();
146
    if(!engine_impl) throw exception("remote_procedure object isn't initialized");
147 148 149 150 151 152 153
    margo_registered_disable_response(engine_impl->m_mid, m_id, HG_TRUE);
    return *this;
}

} // namespace thallium


Matthieu Dorier's avatar
Matthieu Dorier committed
154
#endif