scheduler.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <library/cpp/threading/future/legacy_future.h>
  3. #include <util/datetime/base.h>
  4. #include <util/generic/object_counter.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/vector.h>
  7. #include <library/cpp/deprecated/atomic/atomic.h>
  8. #include <util/system/condvar.h>
  9. #include <util/system/mutex.h>
  10. #include <util/system/thread.h>
  11. namespace NBus {
  12. namespace NPrivate {
  13. class IScheduleItem {
  14. public:
  15. inline IScheduleItem(TInstant scheduleTime) noexcept;
  16. virtual ~IScheduleItem() {
  17. }
  18. virtual void Do() = 0;
  19. inline TInstant GetScheduleTime() const noexcept;
  20. private:
  21. TInstant ScheduleTime;
  22. };
  23. using IScheduleItemAutoPtr = TAutoPtr<IScheduleItem>;
  24. class TScheduler {
  25. public:
  26. TScheduler();
  27. ~TScheduler();
  28. void Stop();
  29. void Schedule(TAutoPtr<IScheduleItem> i);
  30. size_t Size() const;
  31. private:
  32. void SchedulerThread();
  33. void FillNextItem();
  34. private:
  35. TVector<IScheduleItemAutoPtr> Items;
  36. IScheduleItemAutoPtr NextItem;
  37. typedef TMutex TLock;
  38. TLock Lock;
  39. TCondVar CondVar;
  40. TObjectCounter<TScheduler> ObjectCounter;
  41. bool StopThread;
  42. NThreading::TLegacyFuture<> Thread;
  43. };
  44. inline IScheduleItem::IScheduleItem(TInstant scheduleTime) noexcept
  45. : ScheduleTime(scheduleTime)
  46. {
  47. }
  48. inline TInstant IScheduleItem::GetScheduleTime() const noexcept {
  49. return ScheduleTime;
  50. }
  51. }
  52. }