Skip to content

Commit

Permalink
Thread update
Browse files Browse the repository at this point in the history
  • Loading branch information
RenardDev committed Feb 5, 2024
1 parent 32e997c commit 2230d42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
28 changes: 21 additions & 7 deletions Detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4857,7 +4857,6 @@ namespace Detours {

namespace Parallel {

/*
// ----------------------------------------------------------------
// Thread Data
// ----------------------------------------------------------------
Expand All @@ -4873,19 +4872,21 @@ namespace Detours {
DWORD WINAPI ThreadRoutine(PVOID lpThreadParameter) {
auto pTD = reinterpret_cast<PTHREAD_DATA>(lpThreadParameter);
if (!pTD) {
return EXIT_SUCCESS;
return EXIT_FAILURE;
}

auto pThread = static_cast<Thread*>(pTD->m_pParameter);
if (!pThread) {
return EXIT_SUCCESS;
delete pTD;
return EXIT_FAILURE;
}

auto pCallback = pThread->GetCallBack();
if (pCallback) {
pCallback(pThread->GetData());
}

delete pTD;
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -4940,16 +4941,16 @@ namespace Detours {
return false;
}

auto pTD = std::make_unique<THREAD_DATA>();
auto pTD = new THREAD_DATA;
if (!pTD) {
return false;
}

memset(pTD.get(), 0, sizeof(THREAD_DATA));
memset(pTD, 0, sizeof(THREAD_DATA));

pTD->m_pParameter = this;

m_hThread = CreateThread(nullptr, NULL, ThreadRoutine, pTD.get(), NULL, nullptr);
m_hThread = CreateThread(nullptr, NULL, ThreadRoutine, pTD, NULL, nullptr);
if (!m_hThread || (m_hThread == INVALID_HANDLE_VALUE)) {
return false;
}
Expand All @@ -4966,6 +4967,20 @@ namespace Detours {
return false;
}

DWORD unExitCode = EXIT_SUCCESS;
if (!GetExitCodeThread(m_hThread, &unExitCode)) {
CloseHandle(m_hThread);
m_hThread = nullptr;
return false;
}

CloseHandle(m_hThread);
m_hThread = nullptr;

if (unExitCode != EXIT_SUCCESS) {
return false;
}

return true;
}

Expand Down Expand Up @@ -5000,7 +5015,6 @@ namespace Detours {
void* Thread::GetData() const {
return m_pData;
}
*/

// ----------------------------------------------------------------
// Fiber Data
Expand Down
2 changes: 0 additions & 2 deletions Detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,6 @@ namespace Detours {

namespace Parallel {

/*
// ----------------------------------------------------------------
// Thread CallBack
// ----------------------------------------------------------------
Expand Down Expand Up @@ -2306,7 +2305,6 @@ namespace Detours {
void* m_pData;
HANDLE m_hThread;
};
*/

// ----------------------------------------------------------------
// Fiber CallBack
Expand Down
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,11 +1925,11 @@ int _tmain(int nArguments, PTCHAR* pArguments) {

_tprintf_s(_T("Parallel Example\n\n"));

//Detours::Parallel::Thread DetoursThread(OnThread);
Detours::Parallel::Thread DetoursThread(OnThread);
Detours::Parallel::Fiber DetoursFiber(OnFiber);

//_tprintf_s(_T("DetoursThread.Start() = %d\n"), DetoursThread.Start());
//_tprintf_s(_T("DetoursThread.Join() = %d\n"), DetoursThread.Join());
_tprintf_s(_T("DetoursThread.Start() = %d\n"), DetoursThread.Start());
_tprintf_s(_T("DetoursThread.Join() = %d\n"), DetoursThread.Join());

_tprintf_s(_T("DetoursFiber.Switch() = %d\n"), DetoursFiber.Switch());

Expand Down

0 comments on commit 2230d42

Please sign in to comment.