Task.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2015-07
  4. //
  5. #ifndef __TASK_H
  6. #define __TASK_H
  7. #include "Thread.h"
  8. #include "Semaphore.h"
  9. namespace tzc {
  10. struct Task {
  11. typedef TZ_INT RetType;
  12. virtual RetType ExecuteSync() = 0;
  13. // @delay is in millisecond
  14. virtual RetType ExecuteAsync(TZ_Uint32 delay = 0, TZ_BOOL waitRst = TRUE);
  15. };
  16. struct Task0 : public Task {
  17. typedef RetType (* FT_Handler) (void);
  18. Task0(FT_Handler handler)
  19. : m_handler(handler)
  20. {
  21. }
  22. RetType ExecuteSync()
  23. {
  24. return m_handler();
  25. }
  26. private:
  27. FT_Handler m_handler;
  28. };
  29. template <class Arg>
  30. struct Task1 : public Task {
  31. typedef RetType (* FT_Handler) (Arg);
  32. Task1(FT_Handler handler, Arg arg)
  33. : m_handler(handler),
  34. m_arg(arg)
  35. {
  36. }
  37. RetType ExecuteSync()
  38. {
  39. return m_handler(m_arg);
  40. }
  41. private:
  42. FT_Handler m_handler;
  43. Arg m_arg;
  44. };
  45. template <class Arg1, class Arg2>
  46. struct Task2 : public Task {
  47. typedef RetType (* FT_Handler) (Arg1, Arg2);
  48. Task2(FT_Handler handler, Arg1 arg1, Arg2 arg2)
  49. : m_handler(handler),
  50. m_arg1(arg1), m_arg2(arg2)
  51. {
  52. }
  53. RetType ExecuteSync()
  54. {
  55. return m_handler(m_arg1, m_arg2);
  56. }
  57. private:
  58. FT_Handler m_handler;
  59. Arg1 m_arg1;
  60. Arg2 m_arg2;
  61. };
  62. template <class Arg1, class Arg2, class Arg3>
  63. struct Task3 : public Task {
  64. typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3);
  65. Task3(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3)
  66. : m_handler(handler),
  67. m_arg1(arg1), m_arg2(arg2), m_arg3(arg3)
  68. {
  69. }
  70. RetType ExecuteSync()
  71. {
  72. return m_handler(m_arg1, m_arg2, m_arg3);
  73. }
  74. private:
  75. FT_Handler m_handler;
  76. Arg1 m_arg1;
  77. Arg2 m_arg2;
  78. Arg3 m_arg3;
  79. };
  80. template <class Arg1, class Arg2, class Arg3, class Arg4>
  81. struct Task4 : public Task {
  82. typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3, Arg4);
  83. Task4(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
  84. : m_handler(handler),
  85. m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4)
  86. {
  87. }
  88. RetType ExecuteSync()
  89. {
  90. return m_handler(m_arg1, m_arg2, m_arg3, m_arg4);
  91. }
  92. private:
  93. FT_Handler m_handler;
  94. Arg1 m_arg1;
  95. Arg2 m_arg2;
  96. Arg3 m_arg3;
  97. Arg4 m_arg4;
  98. };
  99. template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
  100. struct Task5 : public Task {
  101. typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3, Arg4, Arg5);
  102. Task5(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
  103. : m_handler(handler),
  104. m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5)
  105. {
  106. }
  107. RetType ExecuteSync()
  108. {
  109. return m_handler(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5);
  110. }
  111. private:
  112. FT_Handler m_handler;
  113. Arg1 m_arg1;
  114. Arg2 m_arg2;
  115. Arg3 m_arg3;
  116. Arg4 m_arg4;
  117. Arg5 m_arg5;
  118. };
  119. class DECLDLL TaskExecutor : public OSThread {
  120. public:
  121. TaskExecutor(Task * task, TZ_Uint32 delay);
  122. ~TaskExecutor();
  123. Task::RetType ExecuteTask(TZ_BOOL waitRst);
  124. protected:
  125. virtual void Entry();
  126. private:
  127. Task * m_task;
  128. Task::RetType m_result;
  129. TZ_Uint32 m_delay;
  130. Semaphore m_sema;
  131. };
  132. }; // namespace tzc
  133. #endif // end of __TASK_H