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.

120 lines
3.4 KiB

  1. //------------------------------------------------------------------------------
  2. // File: MsgThrd.h
  3. //
  4. // Desc: DirectShow base classes - provides support for a worker thread
  5. // class to which one can asynchronously post messages.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. // Message class - really just a structure.
  10. //
  11. class CMsg {
  12. public:
  13. UINT uMsg;
  14. DWORD dwFlags;
  15. LPVOID lpParam;
  16. CAMEvent *pEvent;
  17. CMsg(UINT u, DWORD dw, LPVOID lp, CAMEvent *pEvnt)
  18. : uMsg(u), dwFlags(dw), lpParam(lp), pEvent(pEvnt) {}
  19. CMsg()
  20. : uMsg(0), dwFlags(0L), lpParam(NULL), pEvent(NULL) {}
  21. };
  22. // This is the actual thread class. It exports all the usual thread control
  23. // functions. The created thread is different from a normal WIN32 thread in
  24. // that it is prompted to perform particaular tasks by responding to messages
  25. // posted to its message queue.
  26. //
  27. class AM_NOVTABLE CMsgThread {
  28. private:
  29. static DWORD WINAPI DefaultThreadProc(LPVOID lpParam);
  30. DWORD m_ThreadId;
  31. HANDLE m_hThread;
  32. protected:
  33. // if you want to override GetThreadMsg to block on other things
  34. // as well as this queue, you need access to this
  35. CGenericList<CMsg> m_ThreadQueue;
  36. CCritSec m_Lock;
  37. HANDLE m_hSem;
  38. LONG m_lWaiting;
  39. public:
  40. CMsgThread()
  41. : m_ThreadId(0),
  42. m_hThread(NULL),
  43. m_lWaiting(0),
  44. m_hSem(NULL),
  45. // make a list with a cache of 5 items
  46. m_ThreadQueue(NAME("MsgThread list"), 5)
  47. {
  48. }
  49. ~CMsgThread();
  50. // override this if you want to block on other things as well
  51. // as the message loop
  52. void virtual GetThreadMsg(CMsg *msg);
  53. // override this if you want to do something on thread startup
  54. virtual void OnThreadInit() {
  55. };
  56. BOOL CreateThread();
  57. BOOL WaitForThreadExit(LPDWORD lpdwExitCode) {
  58. if (m_hThread != NULL) {
  59. WaitForSingleObject(m_hThread, INFINITE);
  60. return GetExitCodeThread(m_hThread, lpdwExitCode);
  61. }
  62. return FALSE;
  63. }
  64. DWORD ResumeThread() {
  65. return ::ResumeThread(m_hThread);
  66. }
  67. DWORD SuspendThread() {
  68. return ::SuspendThread(m_hThread);
  69. }
  70. int GetThreadPriority() {
  71. return ::GetThreadPriority(m_hThread);
  72. }
  73. BOOL SetThreadPriority(int nPriority) {
  74. return ::SetThreadPriority(m_hThread, nPriority);
  75. }
  76. HANDLE GetThreadHandle() {
  77. return m_hThread;
  78. }
  79. DWORD GetThreadId() {
  80. return m_ThreadId;
  81. }
  82. void PutThreadMsg(UINT uMsg, DWORD dwMsgFlags,
  83. LPVOID lpMsgParam, CAMEvent *pEvent = NULL) {
  84. CAutoLock lck(&m_Lock);
  85. CMsg* pMsg = new CMsg(uMsg, dwMsgFlags, lpMsgParam, pEvent);
  86. m_ThreadQueue.AddTail(pMsg);
  87. if (m_lWaiting != 0) {
  88. ReleaseSemaphore(m_hSem, m_lWaiting, 0);
  89. m_lWaiting = 0;
  90. }
  91. }
  92. // This is the function prototype of the function that the client
  93. // supplies. It is always called on the created thread, never on
  94. // the creator thread.
  95. //
  96. virtual LRESULT ThreadMessageProc(
  97. UINT uMsg, DWORD dwFlags, LPVOID lpParam, CAMEvent *pEvent) = 0;
  98. };