Source code of Windows XP (NT5)
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.

170 lines
3.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: U P T H R E A D . C P P
  7. //
  8. // Contents: Threading support code
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 29 Sep 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "upthread.h"
  18. #include "ncbase.h"
  19. CWorkItem::CWorkItem() : m_bDeleteOnComplete(FALSE)
  20. {
  21. }
  22. CWorkItem::~CWorkItem()
  23. {
  24. }
  25. HRESULT CWorkItem::HrStart(BOOL bDeleteOnComplete)
  26. {
  27. HRESULT hr = S_OK;
  28. m_bDeleteOnComplete = bDeleteOnComplete;
  29. if(!QueueUserWorkItem(&CWorkItem::DwThreadProc, this, GetFlags()))
  30. {
  31. hr = HrFromLastWin32Error();
  32. }
  33. TraceHr(ttidError, FAL, hr, FALSE, "CWorkItem::HrStart(%x)", this);
  34. return hr;
  35. }
  36. ULONG CWorkItem::GetFlags()
  37. {
  38. return WT_EXECUTEDEFAULT;
  39. }
  40. DWORD WINAPI CWorkItem::DwThreadProc(void * pvParam)
  41. {
  42. CWorkItem * pWorkItem = reinterpret_cast<CWorkItem*>(pvParam);
  43. DWORD dwRet = pWorkItem->DwRun();
  44. if(pWorkItem->m_bDeleteOnComplete)
  45. {
  46. delete pWorkItem;
  47. }
  48. return dwRet;
  49. }
  50. CThreadBase::CThreadBase() : m_bDeleteOnComplete(FALSE), m_hThread(NULL), m_dwThreadId(0)
  51. {
  52. }
  53. CThreadBase::~CThreadBase()
  54. {
  55. if(m_hThread)
  56. {
  57. CloseHandle(m_hThread);
  58. m_hThread = NULL;
  59. }
  60. }
  61. HRESULT CThreadBase::HrStart(BOOL bDeleteOnComplete, BOOL bCreateSuspended)
  62. {
  63. Assert(!m_hThread);
  64. HRESULT hr = S_OK;
  65. m_bDeleteOnComplete = bDeleteOnComplete;
  66. m_hThread = CreateThread(NULL, 0, &CThreadBase::DwThreadProc, this, bCreateSuspended ? CREATE_SUSPENDED : 0, &m_dwThreadId);
  67. if(!m_hThread)
  68. {
  69. hr = HrFromLastWin32Error();
  70. }
  71. TraceHr(ttidError, FAL, hr, FALSE, "CThreadBase::HrStart(%x)", this);
  72. return hr;
  73. }
  74. DWORD WINAPI CThreadBase::DwThreadProc(void * pvParam)
  75. {
  76. CThreadBase * pThread = reinterpret_cast<CThreadBase*>(pvParam);
  77. TraceTag(ttidUPnPBase, "CThreadBase(this=%x, id=%x) - thread started", pThread, pThread->m_dwThreadId);
  78. DWORD dwRet = pThread->DwRun();
  79. if(pThread->m_bDeleteOnComplete)
  80. {
  81. delete pThread;
  82. }
  83. return dwRet;
  84. }
  85. HRESULT CThreadBase::HrGetExitCodeThread(DWORD * pdwExitCode)
  86. {
  87. HRESULT hr = S_OK;
  88. if(!GetExitCodeThread(m_hThread, pdwExitCode))
  89. {
  90. hr = HrFromLastWin32Error();
  91. }
  92. TraceHr(ttidError, FAL, hr, FALSE, "CThreadBase::HrGetExitCodeThread");
  93. return hr;
  94. }
  95. HRESULT CThreadBase::HrResumeThread()
  96. {
  97. HRESULT hr = S_OK;
  98. if(-1 == ResumeThread(m_hThread))
  99. {
  100. hr = HrFromLastWin32Error();
  101. }
  102. TraceHr(ttidError, FAL, hr, FALSE, "CThreadBase::HrResumeThread");
  103. return hr;
  104. }
  105. HRESULT CThreadBase::HrSuspendThread()
  106. {
  107. HRESULT hr = S_OK;
  108. if(-1 == SuspendThread(m_hThread))
  109. {
  110. hr = HrFromLastWin32Error();
  111. }
  112. TraceHr(ttidError, FAL, hr, FALSE, "CThreadBase::HrSuspendThread");
  113. return hr;
  114. }
  115. HANDLE CThreadBase::GetThreadHandle()
  116. {
  117. return m_hThread;
  118. }
  119. DWORD CThreadBase::GetThreadId()
  120. {
  121. return m_dwThreadId;
  122. }
  123. HRESULT CThreadBase::HrWait(DWORD dwTimeoutInMillis, BOOL * pbTimedOut)
  124. {
  125. HRESULT hr = S_OK;
  126. DWORD dwRet = WaitForSingleObject(m_hThread, dwTimeoutInMillis);
  127. if(WAIT_TIMEOUT == dwRet)
  128. {
  129. hr = S_FALSE;
  130. TraceTag(ttidUPnPBase, "CThreadBase::HrWait(this=%x, id=%x) - timed out", this, m_dwThreadId);
  131. }
  132. else if(WAIT_FAILED == dwRet)
  133. {
  134. hr = HrFromLastWin32Error();
  135. }
  136. TraceHr(ttidError, FAL, hr, FALSE, "CThreadBase::HrWait");
  137. return hr;
  138. }