Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: dsthread.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef __DSTHREAD_H__
  11. #define __DSTHREAD_H__
  12. ////////////////////////////////////////////////////////////////////
  13. // thread messages
  14. // dispatcher thread posts to worker thread to run query
  15. #define DISPATCH_THREAD_RUN_MSG (WM_USER + 100)
  16. // worker thread posts to dispatcher thread once done with the query
  17. #define DISPATCH_THREAD_DONE_MSG (WM_USER + 101)
  18. // worker thread posts to dispatcher thread to ack startup
  19. #define WORKER_THREAD_START_MSG (WM_USER + 102)
  20. // message posted to threads to ask for shutdown
  21. #define THREAD_SHUTDOWN_MSG (WM_USER + 103)
  22. // message posted to threads to ack shutdown
  23. #define THREAD_SHUTDOWN_ACK_MSG (WM_USER + 104)
  24. void WaitForThreadShutdown(HANDLE* hThreadArray, DWORD dwCount);
  25. ////////////////////////////////////////////////////////////////////
  26. // forward declarations
  27. class CDSComponentData;
  28. ////////////////////////////////////////////////////////////////////
  29. // CHiddenWnd
  30. class CHiddenWnd : public CWindowImpl<CHiddenWnd>
  31. {
  32. public:
  33. DECLARE_WND_CLASS(L"DSAHiddenWindow")
  34. static const UINT s_ThreadStartNotificationMessage;
  35. static const UINT s_ThreadTooMuchDataNotificationMessage;
  36. static const UINT s_ThreadHaveDataNotificationMessage;
  37. static const UINT s_ThreadDoneNotificationMessage;
  38. static const UINT s_SheetCloseNotificationMessage;
  39. static const UINT s_SheetCreateNotificationMessage;
  40. static const UINT s_RefreshAllNotificationMessage;
  41. static const UINT s_ThreadShutDownNotificationMessage;
  42. CHiddenWnd(CDSComponentData* pCD)
  43. {
  44. ASSERT(pCD != NULL);
  45. m_pCD = pCD;
  46. }
  47. BOOL Create();
  48. // message handlers
  49. LRESULT OnThreadStartNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  50. LRESULT OnThreadTooMuchDataNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  51. LRESULT OnThreadHaveDataNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  52. LRESULT OnThreadDoneNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  53. LRESULT OnSheetCloseNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  54. LRESULT OnSheetCreateNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  55. LRESULT OnRefreshAllNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  56. LRESULT OnThreadShutDownNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  57. BEGIN_MSG_MAP(CHiddenWnd)
  58. MESSAGE_HANDLER( CHiddenWnd::s_ThreadStartNotificationMessage, OnThreadStartNotification )
  59. MESSAGE_HANDLER( CHiddenWnd::s_ThreadTooMuchDataNotificationMessage, OnThreadTooMuchDataNotification )
  60. MESSAGE_HANDLER( CHiddenWnd::s_ThreadHaveDataNotificationMessage, OnThreadHaveDataNotification )
  61. MESSAGE_HANDLER( CHiddenWnd::s_ThreadDoneNotificationMessage, OnThreadDoneNotification )
  62. MESSAGE_HANDLER( CHiddenWnd::s_SheetCloseNotificationMessage, OnSheetCloseNotification )
  63. MESSAGE_HANDLER( CHiddenWnd::s_SheetCreateNotificationMessage, OnSheetCreateNotification )
  64. MESSAGE_HANDLER( CHiddenWnd::s_RefreshAllNotificationMessage, OnRefreshAllNotification )
  65. MESSAGE_HANDLER( CHiddenWnd::s_ThreadShutDownNotificationMessage, OnThreadShutDownNotification )
  66. END_MSG_MAP()
  67. private:
  68. CDSComponentData* m_pCD;
  69. };
  70. ////////////////////////////////////////////////////////////////////
  71. // CBackgroundThreadInfo
  72. enum ThreadState { notStarted=0, running, busy, shuttingDown, terminated };
  73. struct CBackgroundThreadInfo
  74. {
  75. CBackgroundThreadInfo()
  76. {
  77. m_nThreadID = 0;
  78. m_hThreadHandle = 0;
  79. m_state = notStarted;
  80. }
  81. UINT m_nThreadID; // thread ID if the thread
  82. HANDLE m_hThreadHandle; // thread handle of the thread
  83. ThreadState m_state;
  84. };
  85. ////////////////////////////////////////////////////////////////////
  86. // CBackgroundThreadBase
  87. class CBackgroundThreadBase : public CWinThread
  88. {
  89. public:
  90. CBackgroundThreadBase();
  91. ~CBackgroundThreadBase();
  92. BOOL Start(HWND hWnd, CDSComponentData* pCD);
  93. virtual BOOL InitInstance();// MFC override
  94. virtual int ExitInstance();
  95. virtual int Run() { return -1;} // // MFC override, need to override
  96. protected:
  97. BOOL PostMessageToWnd(UINT msg, WPARAM wParam, LPARAM lParam);
  98. HWND GetHiddenWnd() { ASSERT(m_hWnd!= NULL); return m_hWnd;}
  99. CDSComponentData* GetCD() { ASSERT(m_pCD); return m_pCD;}
  100. virtual void PostExitNotification() {}
  101. private:
  102. HWND m_hWnd; // hidden window handle
  103. CDSComponentData* m_pCD;
  104. };
  105. ////////////////////////////////////////////////////////////////////
  106. // CDispatcherThread
  107. class CDispatcherThread : public CBackgroundThreadBase
  108. {
  109. public:
  110. CDispatcherThread();
  111. ~CDispatcherThread();
  112. virtual int Run();
  113. protected:
  114. virtual void PostExitNotification();
  115. private:
  116. UINT GetThreadEntryFromPool();
  117. void ReturnThreadToPool(UINT nThreadID);
  118. BOOL BroadcastShutDownAllThreads();
  119. BOOL MarkThreadAsTerminated(UINT nThreadID);
  120. void WaitForAllWorkerThreadsToExit();
  121. UINT _GetEntryFromArray();
  122. UINT m_nArrCount;
  123. CBackgroundThreadInfo* m_pThreadInfoArr;
  124. };
  125. ////////////////////////////////////////////////////////////////////
  126. // CWorkerThread
  127. class CWorkerThread : public CBackgroundThreadBase
  128. {
  129. public:
  130. CWorkerThread(UINT nParentThreadID);
  131. ~CWorkerThread();
  132. virtual int Run();
  133. void AddToQueryResult(CUINode* pUINode);
  134. void SendCurrentQueryResult();
  135. BOOL MustQuit() { return m_bQuit; }
  136. protected:
  137. virtual void PostExitNotification();
  138. private:
  139. UINT m_nParentThreadID;
  140. BOOL m_bQuit;
  141. CThreadQueryResult* m_pCurrentQueryResult;
  142. WPARAM m_currWParamCookie;
  143. const int m_nMaxQueueLength;
  144. };
  145. //////////////////////////////////////////////////////////////////////
  146. #endif // __DSTHREAD_H__