LightClientAsyncWorker.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (c) 2019 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "LightClientAsyncWorker.hpp"
  16. #include "dataset.hpp"
  17. #include "Cache.hpp"
  18. namespace RandomX {
  19. template<bool softAes>
  20. LightClientAsyncWorker<softAes>::LightClientAsyncWorker(const Cache* c) : ILightClientAsyncWorker(c), output(nullptr), hasWork(false), workerThread(&LightClientAsyncWorker::runWorker, this) {
  21. }
  22. template<bool softAes>
  23. void LightClientAsyncWorker<softAes>::prepareBlock(addr_t addr) {
  24. {
  25. std::lock_guard<std::mutex> lk(mutex);
  26. startBlock = addr / CacheLineSize;
  27. blockCount = 1;
  28. output = currentLine.data();
  29. hasWork = true;
  30. }
  31. notifier.notify_all();
  32. }
  33. template<bool softAes>
  34. const uint64_t* LightClientAsyncWorker<softAes>::getBlock(addr_t addr) {
  35. uint32_t currentBlock = addr / CacheLineSize;
  36. if (currentBlock != startBlock || output != currentLine.data()) {
  37. initBlock<softAes>(cache->getCache(), (uint8_t*)currentLine.data(), currentBlock, cache->getKeys());
  38. }
  39. else {
  40. sync();
  41. }
  42. return currentLine.data();
  43. }
  44. template<bool softAes>
  45. void LightClientAsyncWorker<softAes>::prepareBlocks(void* out, uint32_t startBlock, uint32_t blockCount) {
  46. {
  47. std::lock_guard<std::mutex> lk(mutex);
  48. startBlock = startBlock;
  49. blockCount = blockCount;
  50. output = out;
  51. hasWork = true;
  52. }
  53. notifier.notify_all();
  54. }
  55. template<bool softAes>
  56. void LightClientAsyncWorker<softAes>::getBlocks(void* out, uint32_t startBlock, uint32_t blockCount) {
  57. for (uint32_t i = 0; i < blockCount; ++i) {
  58. initBlock<softAes>(cache->getCache(), (uint8_t*)out + CacheLineSize * i, startBlock + i, cache->getKeys());
  59. }
  60. }
  61. template<bool softAes>
  62. void LightClientAsyncWorker<softAes>::sync() {
  63. std::unique_lock<std::mutex> lk(mutex);
  64. notifier.wait(lk, [this] { return !hasWork; });
  65. }
  66. template<bool softAes>
  67. void LightClientAsyncWorker<softAes>::runWorker() {
  68. for (;;) {
  69. std::unique_lock<std::mutex> lk(mutex);
  70. notifier.wait(lk, [this] { return hasWork; });
  71. getBlocks(output, startBlock, blockCount);
  72. hasWork = false;
  73. lk.unlock();
  74. notifier.notify_all();
  75. }
  76. }
  77. template class LightClientAsyncWorker<true>;
  78. template class LightClientAsyncWorker<false>;
  79. }