Skip to content

Commit

Permalink
replaced AsyncRequest with Future class
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Sep 24, 2024
1 parent e00c867 commit 0814a2d
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 218 deletions.
3 changes: 1 addition & 2 deletions examples/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ int main(int argc, char** argv) {
alpha::ResourceHandle resource =
client.makeResourceHandle(g_address, g_provider_id);

int32_t result;
resource.computeSum(32, 54, &result);
int32_t result = resource.computeSum(32, 54).wait();

} catch(const alpha::Exception& ex) {
std::cerr << ex.what() << std::endl;
Expand Down
82 changes: 0 additions & 82 deletions include/alpha/AsyncRequest.hpp

This file was deleted.

84 changes: 84 additions & 0 deletions include/alpha/Future.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* (C) 2024 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __ALPHA_FUTURE_HPP
#define __ALPHA_FUTURE_HPP

#include <alpha/Exception.hpp>
#include <alpha/Result.hpp>
#include <thallium.hpp>
#include <memory>
#include <functional>

namespace alpha {

/**
* @brief Future objects are used to keep track of
* on-going asynchronous operations.
*/
template<typename T, typename Wrapper = T>
class Future {

public:

/**
* @brief Copy constructor.
*/
Future() = default;

/**
* @brief Copy constructor.
*/
Future(const Future& other) = default;

/**
* @brief Move constructor.
*/
Future(Future&& other) = default;

/**
* @brief Copy-assignment operator.
*/
Future& operator=(const Future& other) = default;

/**
* @brief Move-assignment operator.
*/
Future& operator=(Future&& other) = default;

/**
* @brief Destructor.
*/
~Future() = default;

/**
* @brief Wait for the request to complete.
*/
T wait() {
Result<Wrapper> result = m_resp.wait();
return std::move(result).valueOrThrow();
}

/**
* @brief Test if the request has completed, without blocking.
*/
bool completed() const {
return m_resp.received();
}

/**
* @brief Constructor.
*/
Future(thallium::async_response resp)
: m_resp(std::move(resp)) {}

private:

thallium::async_response m_resp;
};

}

#endif
12 changes: 4 additions & 8 deletions include/alpha/ResourceHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
#include <nlohmann/json.hpp>
#include <alpha/Client.hpp>
#include <alpha/Exception.hpp>
#include <alpha/AsyncRequest.hpp>
#include <alpha/Future.hpp>

namespace alpha {

namespace tl = thallium;

class Client;
class ResourceHandleImpl;

Expand Down Expand Up @@ -80,12 +78,10 @@ class ResourceHandle {
*
* @param[in] x first integer
* @param[in] y second integer
* @param[out] result result
* @param[out] req request for a non-blocking operation
*
* @return a Future<int32_t> that can be awaited to get the result.
*/
void computeSum(int32_t x, int32_t y,
int32_t* result = nullptr,
AsyncRequest* req = nullptr) const;
Future<int32_t> computeSum(int32_t x, int32_t y) const;

private:

Expand Down
40 changes: 35 additions & 5 deletions include/alpha/Result.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* (C) 2020 The University of Chicago
* (C) 2024 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __ALPHA_RESULT_HPP
#define __ALPHA_RESULT_HPP

#include <thallium/serialization/stl/string.hpp>
#include <alpha/Exception.hpp>
#include <string>

Expand All @@ -30,13 +31,42 @@ namespace alpha {
template<typename T>
class Result {

template<typename U>
friend class Result;

public:

Result() = default;
Result(Result&&) = default;
Result(const Result&) = default;
Result& operator=(Result&&) = default;
Result& operator=(const Result&) = default;

template<typename U>
Result(Result<U>&& other)
: m_success{other.m_success}
, m_error{std::move(other.m_error)}
, m_value{std::move(other.m_value)} {}

template<typename U>
Result(const Result<U>& other)
: m_success{other.m_success}
, m_error{other.m_error}
, m_value{other.m_value} {}

template<typename U>
Result& operator=(Result<U>&& other) {
if(this == reinterpret_cast<decltype(this)>(&other)) return *this;
m_success = other.m_success;
m_error = std::move(other.m_error);
m_value = std::move(other.m_value);
return *this;
}

template<typename U>
Result& operator=(const Result<U>& other) {
if(this == reinterpret_cast<decltype(this)>(&other)) return *this;
m_success = other.m_success;
m_error = other.m_error;
m_value = other.m_value;
return *this;
}

/**
* @brief Whether the request succeeded.
Expand Down
61 changes: 0 additions & 61 deletions src/AsyncRequest.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/AsyncRequestImpl.hpp

This file was deleted.

3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ set (server-src-files

set (client-src-files
Client.cpp
ResourceHandle.cpp
AsyncRequest.cpp)
ResourceHandle.cpp)

set (dummy-src-files
dummy/DummyBackend.cpp)
Expand Down
Loading

0 comments on commit 0814a2d

Please sign in to comment.