123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // Copyright @ 2015 Hangzhou Topzen Ltd.
- // Author: Tang (tang@hztopzen.com) @ 2015-07
- //
- #ifndef __TASK_H
- #define __TASK_H
- #include "Thread.h"
- #include "Semaphore.h"
- namespace tzc {
- struct Task {
- typedef TZ_INT RetType;
- virtual RetType ExecuteSync() = 0;
- // @delay is in millisecond
- virtual RetType ExecuteAsync(TZ_Uint32 delay = 0, TZ_BOOL waitRst = TRUE);
- };
- struct Task0 : public Task {
- typedef RetType (* FT_Handler) (void);
- Task0(FT_Handler handler)
- : m_handler(handler)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler();
- }
- private:
- FT_Handler m_handler;
- };
- template <class Arg>
- struct Task1 : public Task {
- typedef RetType (* FT_Handler) (Arg);
- Task1(FT_Handler handler, Arg arg)
- : m_handler(handler),
- m_arg(arg)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler(m_arg);
- }
- private:
- FT_Handler m_handler;
- Arg m_arg;
- };
- template <class Arg1, class Arg2>
- struct Task2 : public Task {
- typedef RetType (* FT_Handler) (Arg1, Arg2);
- Task2(FT_Handler handler, Arg1 arg1, Arg2 arg2)
- : m_handler(handler),
- m_arg1(arg1), m_arg2(arg2)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler(m_arg1, m_arg2);
- }
- private:
- FT_Handler m_handler;
- Arg1 m_arg1;
- Arg2 m_arg2;
- };
- template <class Arg1, class Arg2, class Arg3>
- struct Task3 : public Task {
- typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3);
- Task3(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3)
- : m_handler(handler),
- m_arg1(arg1), m_arg2(arg2), m_arg3(arg3)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler(m_arg1, m_arg2, m_arg3);
- }
- private:
- FT_Handler m_handler;
- Arg1 m_arg1;
- Arg2 m_arg2;
- Arg3 m_arg3;
- };
- template <class Arg1, class Arg2, class Arg3, class Arg4>
- struct Task4 : public Task {
- typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3, Arg4);
- Task4(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
- : m_handler(handler),
- m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler(m_arg1, m_arg2, m_arg3, m_arg4);
- }
- private:
- FT_Handler m_handler;
- Arg1 m_arg1;
- Arg2 m_arg2;
- Arg3 m_arg3;
- Arg4 m_arg4;
- };
- template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
- struct Task5 : public Task {
- typedef RetType (* FT_Handler) (Arg1, Arg2, Arg3, Arg4, Arg5);
- Task5(FT_Handler handler, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
- : m_handler(handler),
- m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5)
- {
- }
- RetType ExecuteSync()
- {
- return m_handler(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5);
- }
- private:
- FT_Handler m_handler;
- Arg1 m_arg1;
- Arg2 m_arg2;
- Arg3 m_arg3;
- Arg4 m_arg4;
- Arg5 m_arg5;
- };
- class DECLDLL TaskExecutor : public OSThread {
- public:
- TaskExecutor(Task * task, TZ_Uint32 delay);
- ~TaskExecutor();
- Task::RetType ExecuteTask(TZ_BOOL waitRst);
- protected:
- virtual void Entry();
- private:
- Task * m_task;
- Task::RetType m_result;
- TZ_Uint32 m_delay;
- Semaphore m_sema;
- };
- }; // namespace tzc
- #endif // end of __TASK_H
|