msgthrd.h 3.4 KB

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