|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <interface/STaskLoop-i.h> |
| 4 | +#include <helper/SSharedPtr.hpp> |
| 5 | +#include <unknown/obj-ref-impl.hpp> |
| 6 | +#include <helper/SFunctor.hpp> |
| 7 | +#include <core/SimpleWnd.h> |
| 8 | +#include <helper/SSemaphore.h> |
| 9 | +#include <souicoll.h> |
| 10 | +namespace SOUI |
| 11 | +{ |
| 12 | + class SOUI_EXP STaskHandler : public TObjRefImpl<ITaskLoop>, protected CSimpleWnd |
| 13 | + { |
| 14 | + public: |
| 15 | + /** |
| 16 | + * Constructor. |
| 17 | + */ |
| 18 | + STaskHandler(); |
| 19 | + |
| 20 | + /** |
| 21 | + * Destructor. |
| 22 | + */ |
| 23 | + virtual ~STaskHandler(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Start task mgr thread. |
| 27 | + */ |
| 28 | + void start(const char * pszName, Priority priority); |
| 29 | + |
| 30 | + /** |
| 31 | + * Stop task mgr synchronized. |
| 32 | + */ |
| 33 | + void stop(); |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Remove tasks for a sepcific object from task mgr pening task list |
| 38 | + * @param object the specific object wants pending functors to be removed |
| 39 | + */ |
| 40 | + void cancelTasksForObject(void *object); |
| 41 | + |
| 42 | + /** |
| 43 | + * Cancel tasks for a specific task ID list |
| 44 | + * @param taskList the task ID list to be canceled |
| 45 | + * @return the removed task list. |
| 46 | + */ |
| 47 | + bool cancelTask(long taskId); |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * get the total task number in the task mgr queue. |
| 52 | + * @return total task number in task mgr queue |
| 53 | + */ |
| 54 | + int getTaskCount() const; |
| 55 | + |
| 56 | + /** |
| 57 | + * get the run loop status. |
| 58 | + * @return the running status |
| 59 | + */ |
| 60 | + bool isRunning(); |
| 61 | + |
| 62 | + long postTask(const IRunnable *runnable, bool waitUntilDone, int priority); |
| 63 | + |
| 64 | + bool getName(char * pszBuf, int nBufLen); |
| 65 | + |
| 66 | + bool getRunningTaskInfo(char *buf, int bufLen); |
| 67 | + |
| 68 | + private: |
| 69 | + class TaskItem |
| 70 | + { |
| 71 | + public: |
| 72 | + TaskItem(IRunnable *runnable_, int nPriority_) |
| 73 | + : taskID(0) |
| 74 | + , runnable(runnable_) |
| 75 | + , nPriority(nPriority_) |
| 76 | + , semaphore(NULL) |
| 77 | + {} |
| 78 | + |
| 79 | + const char *getRunnableInfo() |
| 80 | + { |
| 81 | + return runnable->getClassInfo(); |
| 82 | + } |
| 83 | + |
| 84 | + long taskID; |
| 85 | + SSharedPtr<IRunnable> runnable; |
| 86 | + SSemaphore *semaphore; |
| 87 | + int nPriority; |
| 88 | + }; |
| 89 | + |
| 90 | + void OnTimer(UINT_PTR id); |
| 91 | + |
| 92 | + BEGIN_MSG_MAP_EX(STaskHandler) |
| 93 | + MSG_WM_TIMER(OnTimer) |
| 94 | + CHAIN_MSG_MAP(CSimpleWnd) |
| 95 | + END_MSG_MAP() |
| 96 | + |
| 97 | + mutable SCriticalSection m_taskListLock; |
| 98 | + SCriticalSection m_runningLock; |
| 99 | + SSemaphore m_itemsSem; |
| 100 | + SList<TaskItem> m_items; |
| 101 | + |
| 102 | + SCriticalSection m_runningInfoLock; |
| 103 | + bool m_hasRunningItem; |
| 104 | + TaskItem m_runningItem; |
| 105 | + long m_nextTaskID; |
| 106 | + bool m_isRunning; |
| 107 | + DWORD m_dwThreadID; |
| 108 | + }; |
| 109 | +} |
0 commit comments