1 //------------------------------------------------------------------------------
4 // Desc: DirectShow base classes - provides support for a worker thread
5 // class to which one can asynchronously post messages.
7 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
8 //------------------------------------------------------------------------------
11 // Message class - really just a structure.
20 CMsg(UINT u
, DWORD dw
, LPVOID lp
, CAMEvent
*pEvnt
)
21 : uMsg(u
), dwFlags(dw
), lpParam(lp
), pEvent(pEvnt
) {}
24 : uMsg(0), dwFlags(0L), lpParam(NULL
), pEvent(NULL
) {}
27 // This is the actual thread class. It exports all the usual thread control
28 // functions. The created thread is different from a normal WIN32 thread in
29 // that it is prompted to perform particaular tasks by responding to messages
30 // posted to its message queue.
32 class AM_NOVTABLE CMsgThread
{
34 static DWORD WINAPI
DefaultThreadProc(LPVOID lpParam
);
40 // if you want to override GetThreadMsg to block on other things
41 // as well as this queue, you need access to this
42 CGenericList
<CMsg
> m_ThreadQueue
;
53 // make a list with a cache of 5 items
54 m_ThreadQueue(NAME("MsgThread list"), 5)
59 // override this if you want to block on other things as well
60 // as the message loop
61 void virtual GetThreadMsg(CMsg
*msg
);
63 // override this if you want to do something on thread startup
64 virtual void OnThreadInit() {
69 BOOL
WaitForThreadExit(LPDWORD lpdwExitCode
) {
70 if (m_hThread
!= NULL
) {
71 WaitForSingleObject(m_hThread
, INFINITE
);
72 return GetExitCodeThread(m_hThread
, lpdwExitCode
);
77 DWORD
ResumeThread() {
78 return ::ResumeThread(m_hThread
);
81 DWORD
SuspendThread() {
82 return ::SuspendThread(m_hThread
);
85 int GetThreadPriority() {
86 return ::GetThreadPriority(m_hThread
);
89 BOOL
SetThreadPriority(int nPriority
) {
90 return ::SetThreadPriority(m_hThread
, nPriority
);
93 HANDLE
GetThreadHandle() {
102 void PutThreadMsg(UINT uMsg
, DWORD dwMsgFlags
,
103 LPVOID lpMsgParam
, CAMEvent
*pEvent
= NULL
) {
104 CAutoLock
lck(&m_Lock
);
105 CMsg
* pMsg
= new CMsg(uMsg
, dwMsgFlags
, lpMsgParam
, pEvent
);
106 m_ThreadQueue
.AddTail(pMsg
);
107 if (m_lWaiting
!= 0) {
108 ReleaseSemaphore(m_hSem
, m_lWaiting
, 0);
113 // This is the function prototype of the function that the client
114 // supplies. It is always called on the created thread, never on
115 // the creator thread.
117 virtual LRESULT
ThreadMessageProc(
118 UINT uMsg
, DWORD dwFlags
, LPVOID lpParam
, CAMEvent
*pEvent
) = 0;