Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Resource.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
| 9 | using namespace frc; |
| 10 | |
| 11 | wpi::mutex Resource::m_createMutex; |
| 12 | |
| 13 | void Resource::CreateResourceObject(std::unique_ptr<Resource>& r, |
| 14 | uint32_t elements) { |
| 15 | std::scoped_lock lock(m_createMutex); |
| 16 | if (!r) { |
| 17 | r = std::make_unique<Resource>(elements); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | Resource::Resource(uint32_t elements) { |
| 22 | m_isAllocated = std::vector<bool>(elements, false); |
| 23 | } |
| 24 | |
| 25 | uint32_t Resource::Allocate(const std::string& resourceDesc) { |
| 26 | std::scoped_lock lock(m_allocateMutex); |
| 27 | for (uint32_t i = 0; i < m_isAllocated.size(); i++) { |
| 28 | if (!m_isAllocated[i]) { |
| 29 | m_isAllocated[i] = true; |
| 30 | return i; |
| 31 | } |
| 32 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | throw FRC_MakeError(err::NoAvailableResources, "{}", resourceDesc); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | uint32_t Resource::Allocate(uint32_t index, const std::string& resourceDesc) { |
| 37 | std::scoped_lock lock(m_allocateMutex); |
| 38 | if (index >= m_isAllocated.size()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 39 | throw FRC_MakeError(err::ChannelIndexOutOfRange, "{}", resourceDesc); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | } |
| 41 | if (m_isAllocated[index]) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 42 | throw FRC_MakeError(err::ResourceAlreadyAllocated, "{}", resourceDesc); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
| 44 | m_isAllocated[index] = true; |
| 45 | return index; |
| 46 | } |
| 47 | |
| 48 | void Resource::Free(uint32_t index) { |
| 49 | std::unique_lock lock(m_allocateMutex); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 50 | if (index == std::numeric_limits<uint32_t>::max()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | return; |
| 52 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 53 | if (index >= m_isAllocated.size()) { |
| 54 | throw FRC_MakeError(err::NotAllocated, "index {}", index); |
| 55 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | if (!m_isAllocated[index]) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 57 | throw FRC_MakeError(err::NotAllocated, "index {}", index); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | } |
| 59 | m_isAllocated[index] = false; |
| 60 | } |