msgthrd.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, __inout_opt LPVOID lp, __in_opt 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(__inout 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(__out 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(__out 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. __in_opt LPVOID lpMsgParam, __in_opt 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, __inout_opt LPVOID lpParam, __in_opt CAMEvent *pEvent) = 0;
  98. };