2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "IMemoryPool.hpp"
6 #include "PoolManager.hpp"
8 #include "boost/assert.hpp"
9 #include "boost/polymorphic_cast.hpp"
16 PoolManager::PoolManager()
23 arm_compute::IMemoryPool *PoolManager::lock_pool()
25 BOOST_ASSERT_MSG(!(m_FreePools.empty() && m_OccupiedPools.empty()), "Haven't setup any pools");
28 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
30 BOOST_ASSERT_MSG(!m_FreePools.empty(), "Empty pool must exist as semaphore has been signalled");
31 m_OccupiedPools.splice(std::begin(m_OccupiedPools), m_FreePools, std::begin(m_FreePools));
33 return m_OccupiedPools.front().get();
36 void PoolManager::unlock_pool(arm_compute::IMemoryPool *pool)
38 BOOST_ASSERT_MSG(!(m_FreePools.empty() && m_OccupiedPools.empty()), "Haven't setup any pools!");
40 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
42 auto it = std::find_if(
43 std::begin(m_OccupiedPools),
44 std::end(m_OccupiedPools),
45 [pool](const std::unique_ptr<arm_compute::IMemoryPool> &poolIterator)
47 return poolIterator.get() == pool;
51 BOOST_ASSERT_MSG(it != std::end(m_OccupiedPools), "Pool to be unlocked couldn't be found");
52 m_FreePools.splice(std::begin(m_FreePools), m_OccupiedPools, it);
53 m_Semaphore->signal();
56 void PoolManager::register_pool(std::unique_ptr<arm_compute::IMemoryPool> pool)
58 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
59 BOOST_ASSERT_MSG(m_OccupiedPools.empty(), "All pools should be free in order to register a new one");
62 m_FreePools.push_front(std::move(pool));
65 m_Semaphore = std::make_unique<arm_compute::Semaphore>(m_FreePools.size());
68 size_t PoolManager::num_pools() const
70 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
72 return m_FreePools.size() + m_OccupiedPools.size();
75 void PoolManager::AllocatePools()
77 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
79 for (auto& pool : m_FreePools)
81 boost::polymorphic_downcast<IMemoryPool*>(pool.get())->AllocatePool();
84 for (auto& pool : m_OccupiedPools)
86 boost::polymorphic_downcast<IMemoryPool*>(pool.get())->AllocatePool();
90 void PoolManager::ReleasePools()
92 std::lock_guard<arm_compute::Mutex> lock(m_Mutex);
94 for (auto& pool : m_FreePools)
96 boost::polymorphic_downcast<IMemoryPool*>(pool.get())->ReleasePool();
99 for (auto& pool : m_OccupiedPools)
101 boost::polymorphic_downcast<IMemoryPool*>(pool.get())->ReleasePool();