Skip to content

Commit

Permalink
update async_simple 6be48e7b3edde61a8a4e7ca432d25a8d9840153c
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Mar 16, 2024
1 parent ac54ad0 commit daa7ceb
Show file tree
Hide file tree
Showing 44 changed files with 3,445 additions and 2,661 deletions.
73 changes: 0 additions & 73 deletions include/ylt/thirdparty/async_simple/CMakeLists.txt

This file was deleted.

75 changes: 37 additions & 38 deletions include/ylt/thirdparty/async_simple/Collect.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
* Copyright (c) 2022, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,14 +17,14 @@
#define ASYNC_SIMPLE_COLLECT_H

#include <exception>
#include <iostream>
#include <iterator>
#include <vector>

#include "async_simple/Common.h"
#include "async_simple/Future.h"
#include "async_simple/Try.h"

#include <iostream>

namespace async_simple {

// collectAll - collect all the values for a range of futures.
Expand All @@ -50,48 +50,47 @@ template <std::input_iterator Iterator>
inline Future<std::vector<
Try<typename std::iterator_traits<Iterator>::value_type::value_type>>>
collectAll(Iterator begin, Iterator end) {
using T = typename std::iterator_traits<Iterator>::value_type::value_type;
size_t n = std::distance(begin, end);
using T = typename std::iterator_traits<Iterator>::value_type::value_type;
size_t n = std::distance(begin, end);

bool allReady = true;
for (auto iter = begin; iter != end; ++iter) {
if (!iter->hasResult()) {
allReady = false;
break;
}
}
if (allReady) {
std::vector<Try<T>> results;
results.reserve(n);
bool allReady = true;
for (auto iter = begin; iter != end; ++iter) {
results.push_back(std::move(iter->result()));
if (!iter->hasResult()) {
allReady = false;
break;
}
}
if (allReady) {
std::vector<Try<T>> results;
results.reserve(n);
for (auto iter = begin; iter != end; ++iter) {
results.push_back(std::move(iter->result()));
}
return Future<std::vector<Try<T>>>(std::move(results));
}
return Future<std::vector<Try<T>>>(std::move(results));
}

Promise<std::vector<Try<T>>> promise;
auto future = promise.getFuture();
Promise<std::vector<Try<T>>> promise;
auto future = promise.getFuture();

struct Context {
Context(size_t n, Promise<std::vector<Try<T>>> p_)
: results(n), p(std::move(p_)) {}
~Context() { p.setValue(std::move(results)); }
std::vector<Try<T>> results;
Promise<std::vector<Try<T>>> p;
};
struct Context {
Context(size_t n, Promise<std::vector<Try<T>>> p_)
: results(n), p(std::move(p_)) {}
~Context() { p.setValue(std::move(results)); }
std::vector<Try<T>> results;
Promise<std::vector<Try<T>>> p;
};

auto ctx = std::make_shared<Context>(n, std::move(promise));
for (size_t i = 0; i < n; ++i, ++begin) {
if (begin->hasResult()) {
ctx->results[i] = std::move(begin->result());
}
else {
begin->setContinuation([ctx, i](Try<T>&& t) mutable {
ctx->results[i] = std::move(t);
});
auto ctx = std::make_shared<Context>(n, std::move(promise));
for (size_t i = 0; i < n; ++i, ++begin) {
if (begin->hasResult()) {
ctx->results[i] = std::move(begin->result());
} else {
begin->setContinuation([ctx, i](Try<T>&& t) mutable {
ctx->results[i] = std::move(t);
});
}
}
}
return future;
return future;
}

} // namespace async_simple
Expand Down
22 changes: 18 additions & 4 deletions include/ylt/thirdparty/async_simple/Common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
* Copyright (c) 2022, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,6 +44,20 @@
#endif // __SANITIZE_ADDRESS__
#endif // __GNUC__

#if defined(__alibaba_clang__) && \
__has_cpp_attribute(ACC::coro_only_destroy_when_complete)
#define CORO_ONLY_DESTROY_WHEN_DONE [[ACC::coro_only_destroy_when_complete]]
#else
#define CORO_ONLY_DESTROY_WHEN_DONE
#endif

#if defined(__alibaba_clang__) && \
__has_cpp_attribute(ACC::elideable_after_await)
#define ELIDEABLE_AFTER_AWAIT [[ACC::elideable_after_await]]
#else
#define ELIDEABLE_AFTER_AWAIT
#endif

namespace async_simple {
// Different from assert, logicAssert is meaningful in
// release mode. logicAssert should be used in case that
Expand All @@ -52,9 +66,9 @@ namespace async_simple {
// a bug in the library. If logicAssert fails, it means
// there is a bug in the user code.
inline void logicAssert(bool x, const char* errorMsg) {
if (x)
AS_LIKELY { return; }
throw std::logic_error(errorMsg);
if (x)
AS_LIKELY { return; }
throw std::logic_error(errorMsg);
}

} // namespace async_simple
Expand Down
Loading

0 comments on commit daa7ceb

Please sign in to comment.