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.

171 lines
3.6 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////
  2. // RegExThread.cpp : Implementation of CRegExThread
  3. // Copyright (c) Microsoft Corporation 1999.
  4. #include "stdafx.h"
  5. #include "RegExThread.h"
  6. namespace BDATuningModel {
  7. // --- CBaseThread ----------------------
  8. // when the thread starts, it calls this function. We unwrap the 'this'
  9. //pointer and call ThreadProc.
  10. DWORD WINAPI
  11. CBaseThread::InitialThreadProc(LPVOID pv)
  12. {
  13. CBaseThread * pThread = (CBaseThread *) pv;
  14. HRESULT hrCoInit = CBaseThread::CoInitializeHelper(pThread->m_dwCoInitFlags);
  15. if(FAILED(hrCoInit)) {
  16. return hrCoInit;
  17. }
  18. HRESULT hr = pThread->ThreadProc();
  19. if(SUCCEEDED(hrCoInit)) {
  20. CoUninitialize();
  21. }
  22. return hr;
  23. }
  24. BOOL
  25. CBaseThread::Create()
  26. {
  27. DWORD threadid;
  28. CAutoLock lock(&m_AccessLock);
  29. if (ThreadExists()) {
  30. return FALSE;
  31. }
  32. m_hThread = CreateThread(
  33. NULL,
  34. 0,
  35. CBaseThread::InitialThreadProc,
  36. this,
  37. 0,
  38. &threadid);
  39. if (!m_hThread) {
  40. return FALSE;
  41. }
  42. return TRUE;
  43. }
  44. DWORD
  45. CBaseThread::CallWorker(DWORD dwParam)
  46. {
  47. // lock access to the worker thread for scope of this object
  48. CAutoLock lock(&m_AccessLock);
  49. if (!ThreadExists()) {
  50. return (DWORD) E_FAIL;
  51. }
  52. // set the parameter
  53. m_dwParam = dwParam;
  54. m_dwReturnVal = 0;
  55. // signal the worker thread
  56. m_EventSend.Set();
  57. // wait for the completion to be signalled or the thread to terminate
  58. HANDLE h[2];
  59. h[0] = m_EventComplete;
  60. h[1] = m_hThread;
  61. DWORD rc = WaitForMultipleObjects(2, h, 0, INFINITE);
  62. // done - this is the thread's return value
  63. return m_dwReturnVal;
  64. }
  65. #if 0
  66. // Wait for a request from the client
  67. DWORD
  68. CBaseThread::GetRequest()
  69. {
  70. m_EventSend.Wait();
  71. return m_dwParam;
  72. }
  73. #endif
  74. // is there a request?
  75. BOOL
  76. CBaseThread::CheckRequest(DWORD * pParam)
  77. {
  78. if (!m_EventSend.Check()) {
  79. return FALSE;
  80. } else {
  81. if (pParam) {
  82. *pParam = m_dwParam;
  83. }
  84. return TRUE;
  85. }
  86. }
  87. // reply to the request
  88. void
  89. CBaseThread::Reply(DWORD dw)
  90. {
  91. m_dwReturnVal = dw;
  92. // The request is now complete so CheckRequest should fail from
  93. // now on
  94. //
  95. // This event should be reset BEFORE we signal the client or
  96. // the client may Set it before we reset it and we'll then
  97. // reset it (!)
  98. m_EventSend.Reset();
  99. // Tell the client we're finished
  100. m_EventComplete.Set();
  101. }
  102. HRESULT CBaseThread::CoInitializeHelper(DWORD dwCoInitFlags)
  103. {
  104. // call CoInitializeEx and tell OLE not to create a window (this
  105. // thread probably won't dispatch messages and will hang on
  106. // broadcast msgs o/w).
  107. //
  108. // If CoInitEx is not available, threads that don't call CoCreate
  109. // aren't affected. Threads that do will have to handle the
  110. // failure. Perhaps we should fall back to CoInitialize and risk
  111. // hanging?
  112. //
  113. // older versions of ole32.dll don't have CoInitializeEx
  114. HRESULT hr = E_FAIL;
  115. HINSTANCE hOle = GetModuleHandle(TEXT("ole32.dll"));
  116. if(hOle)
  117. {
  118. typedef HRESULT (STDAPICALLTYPE *PCoInitializeEx)(
  119. LPVOID pvReserved, DWORD dwCoInit);
  120. PCoInitializeEx pCoInitializeEx =
  121. (PCoInitializeEx)(GetProcAddress(hOle, "CoInitializeEx"));
  122. if(pCoInitializeEx)
  123. {
  124. hr = (*pCoInitializeEx)(NULL, dwCoInitFlags);
  125. }
  126. }
  127. return hr;
  128. }
  129. // end of private copy of dshow stuff
  130. };
  131. // end of file - RegExThread.cpp