AWS SDK for C++  1.9.108
AWS SDK for C++
ResourceManager.h
Go to the documentation of this file.
1 
5 #pragma once
6 
8 #include <mutex>
9 #include <condition_variable>
10 #include <atomic>
11 #include <cassert>
12 
13 namespace Aws
14 {
15  namespace Utils
16  {
25  template< typename RESOURCE_TYPE>
27  {
28  public:
29  ExclusiveOwnershipResourceManager() : m_shutdown(false) {}
30 
37  RESOURCE_TYPE Acquire()
38  {
39  std::unique_lock<std::mutex> locker(m_queueLock);
40  while(!m_shutdown.load() && m_resources.size() == 0)
41  {
42  m_semaphore.wait(locker, [&](){ return m_shutdown.load() || m_resources.size() > 0; });
43  }
44 
45  assert(!m_shutdown.load());
46 
47  RESOURCE_TYPE resource = m_resources.back();
48  m_resources.pop_back();
49 
50  return resource;
51  }
52 
60  {
61  std::lock_guard<std::mutex> locker(m_queueLock);
62  return m_resources.size() > 0 && !m_shutdown.load();
63  }
64 
70  void Release(RESOURCE_TYPE resource)
71  {
72  std::unique_lock<std::mutex> locker(m_queueLock);
73  m_resources.push_back(resource);
74  locker.unlock();
75  m_semaphore.notify_one();
76  }
77 
83  void PutResource(RESOURCE_TYPE resource)
84  {
85  m_resources.push_back(resource);
86  }
87 
98  {
100  std::unique_lock<std::mutex> locker(m_queueLock);
101  m_shutdown = true;
102 
103  //wait for all acquired resources to be released.
104  while (m_resources.size() < resourceCount)
105  {
106  m_semaphore.wait(locker, [&]() { return m_resources.size() == resourceCount; });
107  }
108 
109  resources = m_resources;
110  m_resources.clear();
111 
112  return resources;
113  }
114 
115  private:
116  Aws::Vector<RESOURCE_TYPE> m_resources;
117  std::mutex m_queueLock;
118  std::condition_variable m_semaphore;
119  std::atomic<bool> m_shutdown;
120  };
121  }
122 }
Aws::Utils::ExclusiveOwnershipResourceManager::Release
void Release(RESOURCE_TYPE resource)
Definition: ResourceManager.h:70
Aws::Vector
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17
Aws::Utils::ExclusiveOwnershipResourceManager::PutResource
void PutResource(RESOURCE_TYPE resource)
Definition: ResourceManager.h:83
AWSVector.h
Aws::Utils::ExclusiveOwnershipResourceManager::HasResourcesAvailable
bool HasResourcesAvailable()
Definition: ResourceManager.h:59
Aws::Utils::ExclusiveOwnershipResourceManager
Definition: ResourceManager.h:27
Aws::Utils::ExclusiveOwnershipResourceManager::ShutdownAndWait
Aws::Vector< RESOURCE_TYPE > ShutdownAndWait(size_t resourceCount)
Definition: ResourceManager.h:97
Aws
Definition: AccessManagementClient.h:15
Aws::Utils::ExclusiveOwnershipResourceManager::ExclusiveOwnershipResourceManager
ExclusiveOwnershipResourceManager()
Definition: ResourceManager.h:29
Aws::Utils::ExclusiveOwnershipResourceManager::Acquire
RESOURCE_TYPE Acquire()
Definition: ResourceManager.h:37