azure-core-amqp
Loading...
Searching...
No Matches
global_state.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include <azure/core/azure_assert.hpp>
7
8#include <atomic>
9#include <list>
10#include <memory>
11#include <mutex>
12#include <thread>
13
14namespace Azure { namespace Core { namespace Amqp { namespace Common { namespace _detail {
15
26 class Pollable {
27 public:
28 Pollable() = default;
29 Pollable(const Pollable&) = delete;
30 Pollable& operator=(const Pollable&) = delete;
31 Pollable(Pollable&&) = delete;
32 Pollable& operator=(Pollable&&) = delete;
33
34 virtual void Poll() = 0;
35 virtual ~Pollable() = default;
36 };
37 class GlobalStateHolder final {
38 GlobalStateHolder();
39 ~GlobalStateHolder();
40
41 std::list<std::shared_ptr<Pollable>> m_pollables;
42 std::mutex m_pollablesMutex;
43 std::thread m_pollingThread;
44 std::atomic<bool> m_activelyPolling;
45 bool m_stopped{false};
46
47 public:
48 static GlobalStateHolder* GlobalStateInstance();
49
50 GlobalStateHolder(GlobalStateHolder const&) = delete;
51 GlobalStateHolder& operator=(GlobalStateHolder const&) = delete;
52
53 GlobalStateHolder(GlobalStateHolder&&) = delete;
54 GlobalStateHolder& operator=(GlobalStateHolder&&) = delete;
55
56 void AddPollable(std::shared_ptr<Pollable> pollable);
57
58 void RemovePollable(std::shared_ptr<Pollable> pollable);
59
60 void AssertIdle()
61 {
62 std::lock_guard<std::mutex> lock(m_pollablesMutex);
63 AZURE_ASSERT(m_pollables.empty());
64 if (!m_pollables.empty())
65 {
66 Azure::Core::_internal::AzureNoReturnPath("Global state is not idle.");
67 }
68 }
69 };
70}}}}} // namespace Azure::Core::Amqp::Common::_detail