factory.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <functional>
  4. class IThreadFactory {
  5. public:
  6. class IThreadAble {
  7. public:
  8. inline IThreadAble() noexcept = default;
  9. virtual ~IThreadAble() = default;
  10. inline void Execute() {
  11. DoExecute();
  12. }
  13. private:
  14. virtual void DoExecute() = 0;
  15. };
  16. class IThread {
  17. friend class IThreadFactory;
  18. public:
  19. inline IThread() noexcept = default;
  20. virtual ~IThread() = default;
  21. inline void Join() noexcept {
  22. DoJoin();
  23. }
  24. private:
  25. inline void Run(IThreadAble* func) {
  26. DoRun(func);
  27. }
  28. private:
  29. // it's actually DoStart
  30. virtual void DoRun(IThreadAble* func) = 0;
  31. virtual void DoJoin() noexcept = 0;
  32. };
  33. inline IThreadFactory() noexcept = default;
  34. virtual ~IThreadFactory() = default;
  35. // XXX: rename to Start
  36. inline THolder<IThread> Run(IThreadAble* func) {
  37. THolder<IThread> ret(DoCreate());
  38. ret->Run(func);
  39. return ret;
  40. }
  41. THolder<IThread> Run(const std::function<void()>& func);
  42. private:
  43. virtual IThread* DoCreate() = 0;
  44. };
  45. IThreadFactory* SystemThreadFactory();
  46. void SetSystemThreadFactory(IThreadFactory* pool);