Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 3 | * |
| 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 Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 28 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 30 | #include <list> |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | PoolManager::PoolManager() : _free_pools(), _occupied_pools(), _sem(), _mtx() |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | IMemoryPool *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 Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 43 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 44 | 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 | |
| 49 | void 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 Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 53 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | 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 Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 56 | 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 | |
| 61 | void PoolManager::register_pool(std::unique_ptr<IMemoryPool> pool) |
| 62 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 63 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 64 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 70 | _sem = std::make_unique<arm_compute::Semaphore>(_free_pools.size()); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 71 | } |
Georgios Pinitas | e5f3b7f | 2018-03-13 18:21:53 +0000 | [diff] [blame] | 72 | |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 73 | std::unique_ptr<IMemoryPool> PoolManager::release_pool() |
| 74 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 75 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to release one!"); |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (!_free_pools.empty()) |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 79 | { |
| 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 85 | _sem = std::make_unique<arm_compute::Semaphore>(_free_pools.size()); |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 86 | |
| 87 | return pool; |
| 88 | } |
| 89 | |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | void PoolManager::clear_pools() |
| 94 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 95 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 96 | 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 Pinitas | e5f3b7f | 2018-03-13 18:21:53 +0000 | [diff] [blame] | 103 | size_t PoolManager::num_pools() const |
| 104 | { |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 105 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | e5f3b7f | 2018-03-13 18:21:53 +0000 | [diff] [blame] | 106 | |
| 107 | return _free_pools.size() + _occupied_pools.size(); |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 108 | } |