// // 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 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 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 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 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 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