VSM C++ SDK
Vehicle Specific Modules SDK
timer_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef TIMER_PROCESSOR_H_
12 #define TIMER_PROCESSOR_H_
13 
14 #include <thread>
15 #include <map>
16 
18 #include <ugcs/vsm/singleton.h>
19 
20 namespace ugcs {
21 namespace vsm {
22 
26 public:
28  template <typename... Args>
29  static Ptr
30  Get_instance(Args &&... args)
31  {
32  return singleton.Get_instance(std::forward<Args>(args)...);
33  }
34 
36 
43 
45  class Timer: public std::enable_shared_from_this<Timer> {
47  public:
49  Timer(const Timer_processor::Ptr &processor,
50  std::chrono::milliseconds interval);
51 
53  void
54  Cancel();
55 
59  bool
60  Is_running() const;
61 
63  std::chrono::steady_clock::time_point
64  Get_fire_time() const
65  {
66  return fire_time;
67  }
68 
69  private:
70  friend class Timer_processor;
71 
73  Timer_processor::Ptr processor;
75  bool is_running = true;
77  std::chrono::milliseconds interval;
79  std::chrono::steady_clock::time_point fire_time;
81  Request::Ptr request;
83  mutable std::mutex mutex;
85  std::list<Ptr> attached_timers;
86 
88  void
89  Set_request(Request::Ptr &request);
90 
92  void
93  Fire();
94 
100  void
101  Destroy(bool cancel = false);
102 
104  void
105  Attach(Timer::Ptr &timer);
106 
108  void
109  Attach(std::list<Ptr> &&timers);
110 
112  void
113  Detach(Timer::Ptr &timer);
114 
116  Ptr
117  Get_attached();
118  };
119 
132  Timer::Ptr
133  Create_timer(std::chrono::milliseconds interval, const Handler &handler,
134  Request_container::Ptr container);
135 
137  void
138  Cancel_timer(Timer::Ptr timer);
139 
140 private:
142  typedef decltype(std::chrono::milliseconds().count()) Tick_type;
143 
145  std::thread thread;
147  std::map<Tick_type, Timer::Ptr> tree;
149  std::mutex tree_lock;
151  static Singleton<Timer_processor> singleton;
152 
154  virtual void
155  On_enable() override;
156 
158  virtual void
159  On_disable() override;
160 
162  virtual void
163  On_wait_and_process() override;
164 
166  void
167  Timer_process_handler(Timer::Ptr timer);
168 
170  void
171  Timer_handler(Timer::Ptr timer, Handler handler,
172  Request_container::Ptr container);
173 
175  void
176  Insert_timer(Timer::Ptr &timer);
177 
179  static Tick_type
180  Get_ticks(const std::chrono::steady_clock::time_point &time);
181 
183  void
184  Create_request(Timer::Ptr &timer, const Handler &handler,
185  Request_container::Ptr &container);
186 };
187 
188 } /* namespace vsm */
189 } /* namespace ugcs */
190 
191 #endif /* TIMER_PROCESSOR_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Singleton class definition.
void Cancel_timer(Timer::Ptr timer)
Cancel the specified timer in case it is running.
Timer processor manages all timers in the VSM.
Definition: timer_processor.h:24
std::shared_ptr< Timer_processor > Ptr
Pointer type.
Definition: timer_processor.h:25
std::chrono::steady_clock::time_point Get_fire_time() const
Get time of next timer firing.
Definition: timer_processor.h:64
STL namespace.
Timer::Ptr Create_timer(std::chrono::milliseconds interval, const Handler &handler, Request_container::Ptr container)
Create and schedule the timer instance.
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Request execution context.
Request execution context.
Definition: request_context.h:24
Callback_proxy< bool > Handler
Timer handler.
Definition: timer_processor.h:42
Helper class for proxying callback invocation.
Definition: callback.h:691
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: timer_processor.h:30
Represents timer instance.
Definition: timer_processor.h:45
std::shared_ptr< Request_container > Ptr
Pointer type.
Definition: request_container.h:32
Generic container for queued requests.
Definition: request_container.h:31
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
void Cancel()
Cancel running timer.
bool Is_running() const
Check if the timer still is running - i.e.
Helper class for implementing singletons.
Definition: singleton.h:69