cmCTestResourceAllocator.cxx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestResourceAllocator.h"
  4. #include <utility>
  5. #include <vector>
  6. #include "cmCTestResourceSpec.h"
  7. void cmCTestResourceAllocator::InitializeFromResourceSpec(
  8. const cmCTestResourceSpec& spec)
  9. {
  10. this->Resources.clear();
  11. for (auto const& it : spec.LocalSocket.Resources) {
  12. auto& res = this->Resources[it.first];
  13. for (auto const& specRes : it.second) {
  14. res[specRes.Id].Total = specRes.Capacity;
  15. res[specRes.Id].Locked = 0;
  16. }
  17. }
  18. }
  19. const std::map<std::string,
  20. std::map<std::string, cmCTestResourceAllocator::Resource>>&
  21. cmCTestResourceAllocator::GetResources() const
  22. {
  23. return this->Resources;
  24. }
  25. bool cmCTestResourceAllocator::AllocateResource(const std::string& name,
  26. const std::string& id,
  27. unsigned int slots)
  28. {
  29. auto it = this->Resources.find(name);
  30. if (it == this->Resources.end()) {
  31. return false;
  32. }
  33. auto resIt = it->second.find(id);
  34. if (resIt == it->second.end()) {
  35. return false;
  36. }
  37. if (resIt->second.Total < resIt->second.Locked + slots) {
  38. return false;
  39. }
  40. resIt->second.Locked += slots;
  41. return true;
  42. }
  43. bool cmCTestResourceAllocator::DeallocateResource(const std::string& name,
  44. const std::string& id,
  45. unsigned int slots)
  46. {
  47. auto it = this->Resources.find(name);
  48. if (it == this->Resources.end()) {
  49. return false;
  50. }
  51. auto resIt = it->second.find(id);
  52. if (resIt == it->second.end()) {
  53. return false;
  54. }
  55. if (resIt->second.Locked < slots) {
  56. return false;
  57. }
  58. resIt->second.Locked -= slots;
  59. return true;
  60. }
  61. bool cmCTestResourceAllocator::Resource::operator==(
  62. const Resource& other) const
  63. {
  64. return this->Total == other.Total && this->Locked == other.Locked;
  65. }
  66. bool cmCTestResourceAllocator::Resource::operator!=(
  67. const Resource& other) const
  68. {
  69. return !(*this == other);
  70. }