6#include <azure/core/context.hpp>
7#include <azure/core/diagnostics/logger.hpp>
8#include <azure/core/internal/diagnostics/log.hpp>
10#include <condition_variable>
16namespace Azure {
namespace Core {
namespace Amqp {
namespace Common {
namespace _internal {
24 template <
typename... T>
class AsyncOperationQueue final {
26 AsyncOperationQueue() =
default;
27 ~AsyncOperationQueue() =
default;
29 AsyncOperationQueue(
const AsyncOperationQueue&) =
delete;
30 AsyncOperationQueue& operator=(
const AsyncOperationQueue&) =
delete;
32 AsyncOperationQueue(AsyncOperationQueue&&) =
default;
33 AsyncOperationQueue& operator=(AsyncOperationQueue&&) =
default;
35 void CompleteOperation(T... operationParameters)
37 std::unique_lock<std::mutex> lock(m_operationComplete);
38 m_operationQueue.push_back(std::make_unique<std::tuple<T...>>(
39 std::make_tuple<T...>(std::forward<T>(operationParameters)...)));
41 m_operationCondition.notify_one();
44 template <
class... Poller>
45 std::unique_ptr<std::tuple<T...>> WaitForPolledResult(
46 Context
const& context,
52 std::unique_lock<std::mutex> lock(m_operationComplete);
53 if (!m_operationQueue.empty())
55 std::unique_ptr<std::tuple<T...>> rv;
56 rv = std::move(m_operationQueue.front());
57 m_operationQueue.pop_front();
60 if (context.IsCancelled())
65 std::this_thread::yield();
84 template <
class... Poller>
85 std::unique_ptr<std::tuple<T...>> WaitForResult(Context
const& context, Poller&... pollers)
91 std::unique_lock<std::mutex> lock(m_operationComplete);
93 if (!m_operationQueue.empty())
95 std::unique_ptr<std::tuple<T...>> rv;
96 rv = std::move(m_operationQueue.front());
97 m_operationQueue.pop_front();
104 m_operationCondition.wait_for(
105 lock, std::chrono::milliseconds(100), [
this, &context]() ->
bool {
107 if (context.IsCancelled())
111 return !m_operationQueue.empty();
114 if (context.IsCancelled())
131 std::unique_ptr<std::tuple<T...>> TryWaitForResult()
134 std::unique_lock<std::mutex> lock(m_operationComplete);
136 if (!m_operationQueue.empty())
138 std::unique_ptr<std::tuple<T...>> rv;
139 rv = std::move(m_operationQueue.front());
140 m_operationQueue.pop_front();
151 std::unique_lock<std::mutex> lock(m_operationComplete);
152 m_operationQueue.clear();
156 std::mutex m_operationComplete;
157 std::condition_variable m_operationCondition;
158 std::list<std::unique_ptr<std::tuple<T...>>> m_operationQueue;
162 template <
class PT,
class... Ts>
void Poll(PT& first, Ts&... rest)