Action.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __ACTION_H
  6. #define __ACTION_H
  7. #include "BaseTypes.h"
  8. #include "BaseHeaders.h"
  9. #include "Thread.h"
  10. #include "Logger.h"
  11. namespace tzc {
  12. typedef TZ_Uint64 ActionTime;
  13. typedef TZ_Uint64 ActionTick;
  14. typedef TZ_Int32 (* ActionCallback) (void * pData);
  15. class DECLDLL Action {
  16. public:
  17. Action(const std::string & sName = "Action");
  18. ~Action();
  19. enum __ACTION_STATE {
  20. STATE_INVALID,
  21. STATE_WAITING, // waiting to be scheduled
  22. STATE_SCHEDULED, // active, will execute soon
  23. STATE_NOTSCHEDULED, // initialized state, nothing to do
  24. STATE_POLL_WAITING, // has been polled, next will be active
  25. STATE_TOBEDELETED,
  26. STATE_UNKNOWN
  27. };
  28. typedef enum __ACTION_STATE State;
  29. void Execute();
  30. void Reset();
  31. void Wait4Detachable();
  32. void SetDetachable(bool flag);
  33. bool GetDetachable() const;
  34. void SetProcessor(ActionCallback callback, void * pData);
  35. void SetState(const State eState);
  36. State GetState() const;
  37. void SetSchedTick(const ActionTick iTick);
  38. ActionTick GetSchedTick() const;
  39. void SetDelayTicks(const ActionTick iTicks);
  40. ActionTick GetDelayTicks() const;
  41. const std::string & Name() const;
  42. private:
  43. volatile State m_eState;
  44. volatile bool m_bDetachable;
  45. ActionTick m_tickSched; // when to be scheduled
  46. ActionTick m_tickDelay; // when to be waked up
  47. void * m_pData;
  48. ActionCallback m_callback;
  49. std::string m_sName;
  50. };
  51. //
  52. // inlines
  53. //
  54. inline void Action::Execute()
  55. {
  56. m_callback(m_pData);
  57. }
  58. inline void Action::SetDetachable(bool flag)
  59. {
  60. m_bDetachable = flag;
  61. }
  62. inline bool Action::GetDetachable() const
  63. {
  64. return m_bDetachable;
  65. }
  66. inline void Action::SetProcessor(ActionCallback callback, void * pData)
  67. {
  68. m_callback = callback;
  69. m_pData = pData;
  70. }
  71. inline void Action::SetState(const State eState)
  72. {
  73. m_eState = eState;
  74. }
  75. inline Action::State Action::GetState() const
  76. {
  77. return m_eState;
  78. }
  79. inline void Action::SetSchedTick(const ActionTick iTick)
  80. {
  81. m_tickSched = iTick;
  82. }
  83. inline ActionTick Action::GetSchedTick() const
  84. {
  85. return m_tickSched;
  86. }
  87. inline void Action::SetDelayTicks(const ActionTick iTicks)
  88. {
  89. m_tickDelay = iTicks;
  90. }
  91. inline ActionTick Action::GetDelayTicks() const
  92. {
  93. return m_tickDelay;
  94. }
  95. inline const std::string & Action::Name() const
  96. {
  97. return m_sName;
  98. }
  99. } // namespace tzc
  100. #include "Locks.h"
  101. namespace tzc {
  102. class DECLDLL ActionMgr : public OSThread {
  103. #ifdef ACTION_SINGLETON
  104. private:
  105. ActionMgr();
  106. virtual ~ActionMgr();
  107. #else
  108. public:
  109. ActionMgr(TZ_Int32 iResolution = 250, TZ_Uint32 iMaxLoops = 1);
  110. virtual ~ActionMgr();
  111. #endif
  112. public:
  113. #ifdef ACTION_SINGLETON
  114. static ActionMgr * Instance();
  115. static void DestroyInstance();
  116. #endif
  117. void Initialize();
  118. void Poll();
  119. void Schedule(Action * pAction);
  120. void ScheduleHead(Action * pAction);
  121. void DeleteAction(Action * pAction);
  122. void NewAction(Action * pAction, ActionTime timeout);
  123. TZ_Int32 ModActionTime(Action * pAction, ActionTime newTimeout);
  124. void SetResolution(const TZ_Int32 iResolution);
  125. TZ_Int32 GetResolution() const;
  126. void SetMaxLoops(const TZ_Uint32 iMax);
  127. TZ_Uint32 GetMaxLoops() const;
  128. void SetCompInterval(const TZ_Uint32 interval);
  129. TZ_Uint32 GetCompInterval() const;
  130. protected:
  131. virtual void Entry();
  132. private:
  133. ActionTick actionTime2Ticks(const ActionTime atime);
  134. ActionTick actionTickDiff(const Action * pAction,
  135. const ActionTick currentTick);
  136. void checkAction(const ActionTick currentTick);
  137. TZ_BOOL isTickAfter(const ActionTick tick1, const ActionTick tick2);
  138. TZ_Int32 findAndEraseAction(std::list<Action *> & rActionList,
  139. const Action * pAction);
  140. void scheduleImp(Action * pAction, ActionTime time2Sched,
  141. const TZ_BOOL bHead = FALSE);
  142. private:
  143. std::list<Action *> m_listPend;
  144. std::list<Action *> m_listPoll;
  145. std::list<Action *> m_listActive;
  146. volatile TZ_BOOL m_bInProgress;
  147. volatile TZ_Uint32 m_iSchedLoops;
  148. ActionTick m_atPreTicks;
  149. ActionTick m_atTicks;
  150. TZ_Int32 m_iResolution; // in milliseconds
  151. TZ_Uint32 m_iMaxLoops;
  152. TZ_Uint32 m_iCompInterval; // resolution compensate interval
  153. Mutex m_mtxLock;
  154. #ifdef ACTION_SINGLETON
  155. private:
  156. static Mutex _mutex;
  157. static ActionMgr * _instance;
  158. #endif
  159. };
  160. //
  161. // inlines
  162. //
  163. inline void ActionMgr::SetResolution(const TZ_Int32 iResolution)
  164. {
  165. m_iResolution = iResolution;
  166. }
  167. inline TZ_Int32 ActionMgr::GetResolution() const
  168. {
  169. return m_iResolution;
  170. }
  171. inline void ActionMgr::SetMaxLoops(const TZ_Uint32 iMax)
  172. {
  173. m_iMaxLoops = iMax;
  174. }
  175. inline TZ_Uint32 ActionMgr::GetMaxLoops() const
  176. {
  177. return m_iMaxLoops;
  178. }
  179. inline void ActionMgr::SetCompInterval(const TZ_Uint32 interval)
  180. {
  181. m_iCompInterval = interval;
  182. }
  183. inline TZ_Uint32 ActionMgr::GetCompInterval() const
  184. {
  185. return m_iCompInterval;
  186. }
  187. inline ActionTick ActionMgr::actionTime2Ticks(const ActionTime atime)
  188. {
  189. return (atime / m_iResolution);
  190. }
  191. // check whether tick1 > tick2 or not
  192. inline TZ_BOOL ActionMgr::isTickAfter(const ActionTick tick1, const ActionTick tick2)
  193. {
  194. return !((tick1 - tick2) & (1ULL << 63));
  195. }
  196. #ifdef ACTION_SINGLETON
  197. #define ACTION_RESOLUTION ActionMgr::Instance()->GetResolution()
  198. #define ACTION_MAXLOOPS ActionMgr::Instance()->GetMaxLoops()
  199. #define ACTION_EVENTLOOP() ActionMgr::Instance()->Poll()
  200. #endif
  201. } // namespace tzc
  202. #endif /* ----- #ifndef __ACTION_H ----- */