Leaked source code of windows server 2003
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.

204 lines
6.0 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. // CBackgroundThreadBase
  72. class CBackgroundThreadBase : public CWinThread
  73. {
  74. public:
  75. CBackgroundThreadBase();
  76. ~CBackgroundThreadBase();
  77. BOOL Start(HWND hWnd, CDSComponentData* pCD);
  78. virtual BOOL InitInstance();// MFC override
  79. virtual int ExitInstance();
  80. virtual int Run() { return -1;} // // MFC override, need to override
  81. protected:
  82. BOOL PostMessageToWnd(UINT msg, WPARAM wParam, LPARAM lParam);
  83. HWND GetHiddenWnd() { ASSERT(m_hWnd!= NULL); return m_hWnd;}
  84. CDSComponentData* GetCD() { ASSERT(m_pCD); return m_pCD;}
  85. virtual void PostExitNotification() {}
  86. private:
  87. HWND m_hWnd; // hidden window handle
  88. CDSComponentData* m_pCD;
  89. };
  90. ////////////////////////////////////////////////////////////////////
  91. // CBackgroundThreadInfo
  92. enum ThreadState { notStarted=0, running, busy, shuttingDown, terminated };
  93. struct CBackgroundThreadInfo
  94. {
  95. CBackgroundThreadInfo();
  96. UINT m_nThreadID; // thread ID if the thread
  97. HANDLE m_hThreadHandle; // thread handle of the thread
  98. ThreadState m_state;
  99. CBackgroundThreadBase* m_pThreadObj; // pointer to the thread object
  100. };
  101. ////////////////////////////////////////////////////////////////////
  102. // CDispatcherThread
  103. class CDispatcherThread : public CBackgroundThreadBase
  104. {
  105. public:
  106. CDispatcherThread();
  107. ~CDispatcherThread();
  108. virtual int Run();
  109. protected:
  110. virtual void PostExitNotification();
  111. private:
  112. UINT GetThreadEntryFromPool();
  113. void ReturnThreadToPool(UINT nThreadID);
  114. BOOL BroadcastShutDownAllThreads();
  115. BOOL MarkThreadAsTerminated(UINT nThreadID);
  116. void WaitForAllWorkerThreadsToExit();
  117. UINT _GetEntryFromArray();
  118. UINT m_nArrCount;
  119. CBackgroundThreadInfo* m_pThreadInfoArr;
  120. };
  121. ////////////////////////////////////////////////////////////////////
  122. // CWorkerThread
  123. class CWorkerThread : public CBackgroundThreadBase
  124. {
  125. public:
  126. CWorkerThread(UINT nParentThreadID);
  127. ~CWorkerThread();
  128. virtual int Run();
  129. void AddToQueryResult(CUINode* pUINode);
  130. void SendCurrentQueryResult();
  131. BOOL MustQuit() { return m_bQuit; }
  132. protected:
  133. virtual void PostExitNotification();
  134. private:
  135. UINT m_nParentThreadID;
  136. BOOL m_bQuit;
  137. CThreadQueryResult* m_pCurrentQueryResult;
  138. WPARAM m_currWParamCookie;
  139. const int m_nMaxQueueLength;
  140. };
  141. //////////////////////////////////////////////////////////////////////
  142. #endif // __DSTHREAD_H__