Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/threadapi_win32.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include "windows.h"
5 #include "azure_macro_utils/macro_utils.h"
6 #include "azure_c_shared_utility/threadapi.h"
7 #include "azure_c_shared_utility/xlogging.h"
8
9 MU_DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
10
11 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
12 {
13 THREADAPI_RESULT result;
14 if ((threadHandle == NULL) ||
15 (func == NULL))
16 {
17 result = THREADAPI_INVALID_ARG;
18 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
19 }
20 else
21 {
22 *threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL);
23 if(*threadHandle == NULL)
24 {
25 result = (GetLastError() == ERROR_OUTOFMEMORY) ? THREADAPI_NO_MEMORY : THREADAPI_ERROR;
26
27 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
28 }
29 else
30 {
31 result = THREADAPI_OK;
32 }
33 }
34
35 return result;
36 }
37
38 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE threadHandle, int *res)
39 {
40 THREADAPI_RESULT result = THREADAPI_OK;
41
42 if (threadHandle == NULL)
43 {
44 result = THREADAPI_INVALID_ARG;
45 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
46 }
47 else
48 {
49 DWORD returnCode = WaitForSingleObject(threadHandle, INFINITE);
50
51 if( returnCode != WAIT_OBJECT_0)
52 {
53 result = THREADAPI_ERROR;
54 LogError("Error waiting for Single Object. Return Code: %d. Error Code: %d", returnCode, result);
55 }
56 else
57 {
58 if (res != NULL)
59 {
60 DWORD exit_code;
61 if (!GetExitCodeThread(threadHandle, &exit_code)) //If thread end is signaled we need to get the Thread Exit Code;
62 {
63 result = THREADAPI_ERROR;
64 LogError("Error Getting Exit Code. Error Code: %u.", (unsigned int)GetLastError());
65 }
66 else
67 {
68 *res = (int)exit_code;
69 }
70 }
71 }
72 CloseHandle(threadHandle);
73 }
74
75 return result;
76 }
77
78 void ThreadAPI_Exit(int res)
79 {
80 ExitThread(res);
81 }
82
83 void ThreadAPI_Sleep(unsigned int milliseconds)
84 {
85 Sleep(milliseconds);
86 }