LightClientAsyncWorker.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //#define TRACE
  16. #include "common.hpp"
  17. #include <thread>
  18. #include <mutex>
  19. #include <condition_variable>
  20. #include <array>
  21. #ifdef TRACE
  22. #include "Stopwatch.hpp"
  23. #include <iostream>
  24. #endif
  25. namespace RandomX {
  26. class Cache;
  27. using DatasetLine = std::array<uint64_t, CacheLineSize / sizeof(uint64_t)>;
  28. template<bool softAes>
  29. class LightClientAsyncWorker : public ILightClientAsyncWorker {
  30. public:
  31. LightClientAsyncWorker(const Cache*);
  32. void prepareBlock(addr_t) final;
  33. void prepareBlocks(void* out, uint32_t startBlock, uint32_t blockCount) final;
  34. const uint64_t* getBlock(addr_t) final;
  35. void getBlocks(void* out, uint32_t startBlock, uint32_t blockCount) final;
  36. void sync() final;
  37. private:
  38. void runWorker();
  39. std::condition_variable notifier;
  40. std::mutex mutex;
  41. alignas(16) DatasetLine currentLine;
  42. void* output;
  43. uint32_t startBlock, blockCount;
  44. bool hasWork;
  45. #ifdef TRACE
  46. Stopwatch sw;
  47. #endif
  48. std::thread workerThread;
  49. };
  50. }