blob: 7fb9bd8000d49f413c6b8102f3844ed2d9a5b83b [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/PoolManager.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/runtime/IMemoryPool.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010028
Matthew Bentham758b5ba2020-03-05 23:37:48 +000029#include <algorithm>
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030#include <list>
31
32using namespace arm_compute;
33
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034PoolManager::PoolManager() : _free_pools(), _occupied_pools(), _sem(), _mtx()
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010035{
36}
37
38IMemoryPool *PoolManager::lock_pool()
39{
40 ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty() && _occupied_pools.empty(), "Haven't setup any pools!");
41
42 _sem->wait();
Georgios Pinitas879d1312019-09-30 13:25:53 +010043 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010044 ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty(), "Empty pool must exist as semaphore has been signalled");
45 _occupied_pools.splice(std::begin(_occupied_pools), _free_pools, std::begin(_free_pools));
46 return _occupied_pools.front().get();
47}
48
49void PoolManager::unlock_pool(IMemoryPool *pool)
50{
51 ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty() && _occupied_pools.empty(), "Haven't setup any pools!");
52
Georgios Pinitas879d1312019-09-30 13:25:53 +010053 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 auto it = std::find_if(std::begin(_occupied_pools), std::end(_occupied_pools),
55 [pool](const std::unique_ptr<IMemoryPool> &pool_it) { return pool_it.get() == pool; });
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010056 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_occupied_pools), "Pool to be unlocked couldn't be found!");
57 _free_pools.splice(std::begin(_free_pools), _occupied_pools, it);
58 _sem->signal();
59}
60
61void PoolManager::register_pool(std::unique_ptr<IMemoryPool> pool)
62{
Georgios Pinitas879d1312019-09-30 13:25:53 +010063 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010064 ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to register a new one!");
65
66 // Set pool
67 _free_pools.push_front(std::move(pool));
68
69 // Update semaphore
Georgios Pinitas40f51a62020-11-21 03:04:18 +000070 _sem = std::make_unique<arm_compute::Semaphore>(_free_pools.size());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010071}
Georgios Pinitase5f3b7f2018-03-13 18:21:53 +000072
Georgios Pinitas9da19e92018-10-11 15:33:11 +010073std::unique_ptr<IMemoryPool> PoolManager::release_pool()
74{
Georgios Pinitas879d1312019-09-30 13:25:53 +010075 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Georgios Pinitas9da19e92018-10-11 15:33:11 +010076 ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to release one!");
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (!_free_pools.empty())
Georgios Pinitas9da19e92018-10-11 15:33:11 +010079 {
80 std::unique_ptr<IMemoryPool> pool = std::move(_free_pools.front());
81 ARM_COMPUTE_ERROR_ON(_free_pools.front() != nullptr);
82 _free_pools.pop_front();
83
84 // Update semaphore
Georgios Pinitas40f51a62020-11-21 03:04:18 +000085 _sem = std::make_unique<arm_compute::Semaphore>(_free_pools.size());
Georgios Pinitas9da19e92018-10-11 15:33:11 +010086
87 return pool;
88 }
89
90 return nullptr;
91}
92
93void PoolManager::clear_pools()
94{
Georgios Pinitas879d1312019-09-30 13:25:53 +010095 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Georgios Pinitas9da19e92018-10-11 15:33:11 +010096 ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to clear the PoolManager!");
97 _free_pools.clear();
98
99 // Update semaphore
100 _sem = nullptr;
101}
102
Georgios Pinitase5f3b7f2018-03-13 18:21:53 +0000103size_t PoolManager::num_pools() const
104{
Georgios Pinitas879d1312019-09-30 13:25:53 +0100105 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
Georgios Pinitase5f3b7f2018-03-13 18:21:53 +0000106
107 return _free_pools.size() + _occupied_pools.size();
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000108}