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