Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/Resource.h" |
| 9 | |
| 10 | #include "frc/ErrorBase.h" |
| 11 | #include "frc/WPIErrors.h" |
| 12 | |
| 13 | using namespace frc; |
| 14 | |
| 15 | wpi::mutex Resource::m_createMutex; |
| 16 | |
| 17 | void Resource::CreateResourceObject(std::unique_ptr<Resource>& r, |
| 18 | uint32_t elements) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 19 | std::scoped_lock lock(m_createMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 20 | if (!r) { |
| 21 | r = std::make_unique<Resource>(elements); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | Resource::Resource(uint32_t elements) { |
| 26 | m_isAllocated = std::vector<bool>(elements, false); |
| 27 | } |
| 28 | |
| 29 | uint32_t Resource::Allocate(const std::string& resourceDesc) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 30 | std::scoped_lock lock(m_allocateMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 31 | for (uint32_t i = 0; i < m_isAllocated.size(); i++) { |
| 32 | if (!m_isAllocated[i]) { |
| 33 | m_isAllocated[i] = true; |
| 34 | return i; |
| 35 | } |
| 36 | } |
| 37 | wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc); |
| 38 | return std::numeric_limits<uint32_t>::max(); |
| 39 | } |
| 40 | |
| 41 | uint32_t Resource::Allocate(uint32_t index, const std::string& resourceDesc) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 42 | std::scoped_lock lock(m_allocateMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 43 | if (index >= m_isAllocated.size()) { |
| 44 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); |
| 45 | return std::numeric_limits<uint32_t>::max(); |
| 46 | } |
| 47 | if (m_isAllocated[index]) { |
| 48 | wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc); |
| 49 | return std::numeric_limits<uint32_t>::max(); |
| 50 | } |
| 51 | m_isAllocated[index] = true; |
| 52 | return index; |
| 53 | } |
| 54 | |
| 55 | void Resource::Free(uint32_t index) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 56 | std::unique_lock lock(m_allocateMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 57 | if (index == std::numeric_limits<uint32_t>::max()) return; |
| 58 | if (index >= m_isAllocated.size()) { |
| 59 | wpi_setWPIError(NotAllocated); |
| 60 | return; |
| 61 | } |
| 62 | if (!m_isAllocated[index]) { |
| 63 | wpi_setWPIError(NotAllocated); |
| 64 | return; |
| 65 | } |
| 66 | m_isAllocated[index] = false; |
| 67 | } |