You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scheduler.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2015 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_SCHEDULER_H
  5. #define STARWELS_SCHEDULER_H
  6. //
  7. // NOTE:
  8. // boost::thread / boost::chrono should be ported to std::thread / std::chrono
  9. // when we support C++11.
  10. //
  11. #include <boost/chrono/chrono.hpp>
  12. #include <boost/thread.hpp>
  13. #include <map>
  14. #include "sync.h"
  15. //
  16. // Simple class for background tasks that should be run
  17. // periodically or once "after a while"
  18. //
  19. // Usage:
  20. //
  21. // CScheduler* s = new CScheduler();
  22. // s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
  23. // s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
  24. // boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
  25. //
  26. // ... then at program shutdown, clean up the thread running serviceQueue:
  27. // t->interrupt();
  28. // t->join();
  29. // delete t;
  30. // delete s; // Must be done after thread is interrupted/joined.
  31. //
  32. class CScheduler
  33. {
  34. public:
  35. CScheduler();
  36. ~CScheduler();
  37. typedef std::function<void(void)> Function;
  38. // Call func at/after time t
  39. void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
  40. // Convenience method: call f once deltaSeconds from now
  41. void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
  42. // Another convenience method: call f approximately
  43. // every deltaSeconds forever, starting deltaSeconds from now.
  44. // To be more precise: every time f is finished, it
  45. // is rescheduled to run deltaSeconds later. If you
  46. // need more accurate scheduling, don't use this method.
  47. void scheduleEvery(Function f, int64_t deltaMilliSeconds);
  48. // To keep things as simple as possible, there is no unschedule.
  49. // Services the queue 'forever'. Should be run in a thread,
  50. // and interrupted using boost::interrupt_thread
  51. void serviceQueue();
  52. // Tell any threads running serviceQueue to stop as soon as they're
  53. // done servicing whatever task they're currently servicing (drain=false)
  54. // or when there is no work left to be done (drain=true)
  55. void stop(bool drain=false);
  56. // Returns number of tasks waiting to be serviced,
  57. // and first and last task times
  58. size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
  59. boost::chrono::system_clock::time_point &last) const;
  60. // Returns true if there are threads actively running in serviceQueue()
  61. bool AreThreadsServicingQueue() const;
  62. private:
  63. std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
  64. boost::condition_variable newTaskScheduled;
  65. mutable boost::mutex newTaskMutex;
  66. int nThreadsServicingQueue;
  67. bool stopRequested;
  68. bool stopWhenEmpty;
  69. bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
  70. };
  71. /**
  72. * Class used by CScheduler clients which may schedule multiple jobs
  73. * which are required to be run serially. Does not require such jobs
  74. * to be executed on the same thread, but no two jobs will be executed
  75. * at the same time.
  76. */
  77. class SingleThreadedSchedulerClient {
  78. private:
  79. CScheduler *m_pscheduler;
  80. CCriticalSection m_cs_callbacks_pending;
  81. std::list<std::function<void (void)>> m_callbacks_pending;
  82. bool m_are_callbacks_running = false;
  83. void MaybeScheduleProcessQueue();
  84. void ProcessQueue();
  85. public:
  86. SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
  87. void AddToProcessQueue(std::function<void (void)> func);
  88. // Processes all remaining queue members on the calling thread, blocking until queue is empty
  89. // Must be called after the CScheduler has no remaining processing threads!
  90. void EmptyQueue();
  91. };
  92. #endif