BuilderList.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/utilities/BuilderList.h>
  9. namespace AssetProcessor
  10. {
  11. void BuilderList::AddBuilder(AZStd::shared_ptr<Builder> builder, BuilderPurpose purpose)
  12. {
  13. if (purpose == BuilderPurpose::CreateJobs)
  14. {
  15. if (m_createJobsBuilder && m_createJobsBuilder->IsValid())
  16. {
  17. AZ_Error(
  18. "BuilderList", false, "AddBuilder called with CreateJobs builder (%s) but a CreateJobs builder (%s) already exists and is valid",
  19. builder->UuidString().c_str(), m_createJobsBuilder->UuidString().c_str());
  20. return;
  21. }
  22. m_createJobsBuilder = AZStd::move(builder);
  23. }
  24. else
  25. {
  26. m_builders.emplace(builder->GetUuid(), builder);
  27. }
  28. }
  29. AZStd::shared_ptr<Builder> BuilderList::Find(AZ::Uuid uuid)
  30. {
  31. if (m_createJobsBuilder && m_createJobsBuilder->GetUuid() == uuid)
  32. {
  33. return m_createJobsBuilder;
  34. }
  35. auto itr = m_builders.find(uuid);
  36. return itr != m_builders.end() ? itr->second : nullptr;
  37. }
  38. BuilderRef BuilderList::GetFirst(BuilderPurpose purpose)
  39. {
  40. if (purpose == BuilderPurpose::CreateJobs)
  41. {
  42. if (m_createJobsBuilder)
  43. {
  44. if (!m_createJobsBuilder->m_busy)
  45. {
  46. m_createJobsBuilder->PumpCommunicator();
  47. if (m_createJobsBuilder->IsValid())
  48. {
  49. return BuilderRef(m_createJobsBuilder);
  50. }
  51. m_createJobsBuilder = nullptr;
  52. }
  53. else
  54. {
  55. AZ_Warning(
  56. "BuilderList",
  57. false,
  58. "CreateJobs builder requested but existing builder is already busy. There should not be multiple parallel "
  59. "requests for CreateJobs builders");
  60. }
  61. }
  62. return {};
  63. }
  64. for (auto itr = m_builders.begin(); itr != m_builders.end();)
  65. {
  66. auto& builder = itr->second;
  67. if (!builder->m_busy)
  68. {
  69. builder->PumpCommunicator();
  70. if (builder->IsValid())
  71. {
  72. return BuilderRef(builder);
  73. }
  74. itr = m_builders.erase(itr);
  75. }
  76. else
  77. {
  78. ++itr;
  79. }
  80. }
  81. return {};
  82. }
  83. AZStd::string BuilderList::RemoveByConnectionId(AZ::u32 connId)
  84. {
  85. AZStd::string uuidString;
  86. // Note that below the connectionId will be set to 0.
  87. // The builder might not be destroyed immediately if another thread is currently holding a reference.
  88. // If the builder is currently in use, this will signal to the waiting thread to not expect a reply
  89. // and fail the current job request.
  90. if (m_createJobsBuilder && m_createJobsBuilder->GetConnectionId() == connId)
  91. {
  92. uuidString = m_createJobsBuilder->UuidString();
  93. m_createJobsBuilder->m_connectionId = 0;
  94. m_createJobsBuilder = nullptr;
  95. return uuidString;
  96. }
  97. else
  98. {
  99. for (auto itr = m_builders.begin(); itr != m_builders.end(); ++itr)
  100. {
  101. auto& builder = itr->second;
  102. if (builder->GetConnectionId() == connId)
  103. {
  104. uuidString = builder->UuidString();
  105. builder->m_connectionId = 0;
  106. m_builders.erase(itr);
  107. return uuidString;
  108. }
  109. }
  110. }
  111. return {};
  112. }
  113. void BuilderList::RemoveByUuid(AZ::Uuid uuid)
  114. {
  115. if (m_createJobsBuilder && m_createJobsBuilder->GetUuid() == uuid)
  116. {
  117. m_createJobsBuilder = nullptr;
  118. }
  119. else
  120. {
  121. m_builders.erase(uuid);
  122. }
  123. }
  124. void BuilderList::PumpIdleBuilders()
  125. {
  126. if (m_createJobsBuilder && !m_createJobsBuilder->m_busy)
  127. {
  128. m_createJobsBuilder->PumpCommunicator();
  129. }
  130. for (auto pair : m_builders)
  131. {
  132. auto builder = pair.second;
  133. if (!builder->m_busy)
  134. {
  135. builder->PumpCommunicator();
  136. }
  137. }
  138. }
  139. }