| 27 | } |
| 28 | |
| 29 | std::shared_ptr <CThread> CThreadManager::CreateCustomThread(int iThreadCode, LPTHREAD_START_ROUTINE pFunc, std::size_t ulFuncSize, LPVOID lpParam, DWORD dwMaxDelay, bool bIsTemporaryThread /* TODO: LPDWORD pdwErrorStep, bool bCreateSuspended = false */) |
| 30 | { |
| 31 | DEBUG_LOG(LL_SYS, "Custom thread(%u) create has been started!", iThreadCode); |
| 32 | |
| 33 | if (GetThreadFromThreadCode(iThreadCode)) |
| 34 | { |
| 35 | DEBUG_LOG(LL_CRI, "Already exist thread with code(%d)!", iThreadCode); |
| 36 | |
| 37 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_ALREADY_EXIST, g_winapiApiTable->GetLastError()); |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | auto dwThreadId = 0UL; |
| 42 | auto hThread = CThreadFunctions::CreateThread(iThreadCode, pFunc, lpParam, &dwThreadId); |
| 43 | if (!IS_VALID_HANDLE(hThread)) |
| 44 | { |
| 45 | DEBUG_LOG(LL_CRI, "Thread: %d can NOT created! Error: %u", iThreadCode, g_winapiApiTable->GetLastError()); |
| 46 | |
| 47 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_CREATE_FAIL, g_winapiApiTable->GetLastError()); |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | if (g_nmApp->AccessHelperInstance()->BlockAccess(hThread) == false) |
| 52 | { |
| 53 | DEBUG_LOG(LL_CRI, "Thread access rules(1) can NOT adjusted for: %d Error: %u", iThreadCode, g_winapiApiTable->GetLastError()); |
| 54 | |
| 55 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_ACCESS_1_FAIL, g_winapiApiTable->GetLastError()); |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | if (g_nmApp->AccessHelperInstance()->DenyThreadAccess(hThread) == false) |
| 60 | { |
| 61 | DEBUG_LOG(LL_CRI, "Thread access rules(2) can NOT adjusted for: %d Error: %u", iThreadCode, g_winapiApiTable->GetLastError()); |
| 62 | |
| 63 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_ACCESS_2_FAIL, g_winapiApiTable->GetLastError()); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | auto customThread = std::make_shared<CThread>(hThread, dwThreadId, iThreadCode); |
| 68 | if (!IS_VALID_SMART_PTR(customThread)) |
| 69 | { |
| 70 | DEBUG_LOG(LL_CRI, "Thread class can NOT created! Thread: %d Error: %u", iThreadCode, g_winapiApiTable->GetLastError()); |
| 71 | |
| 72 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_ALLOC_CLASS_FAIL, g_winapiApiTable->GetLastError()); |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | auto threadInfos = std::make_shared<SSelfThreads>(); |
| 77 | if (!IS_VALID_SMART_PTR(threadInfos)) |
| 78 | { |
| 79 | DEBUG_LOG(LL_CRI, "Thread data container can NOT created! Thread: %d Error: %u", iThreadCode, g_winapiApiTable->GetLastError()); |
| 80 | |
| 81 | g_nmApp->OnCloseRequest(EXIT_ERR_CUSTOM_THREAD_ALLOC_CONTAINER_FAIL, g_winapiApiTable->GetLastError()); |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | auto pExitCallbackHelper = std::make_shared<CThreadExitWatcher>(); |
| 86 | if (!IS_VALID_SMART_PTR(pExitCallbackHelper)) |
no test coverage detected