Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/threadapi_pthreads.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 #define _DEFAULT_SOURCE
5
6 #include "azure_macro_utils/macro_utils.h"
7 #include "azure_c_shared_utility/threadapi.h"
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <errno.h>
12
13 #ifdef TI_RTOS
14 #include <ti/sysbios/knl/Task.h>
15 #else
16 #include <unistd.h>
17 #endif
18
19 #include <pthread.h>
20 #include <time.h>
21 #include "azure_c_shared_utility/xlogging.h"
22
23 MU_DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
24
25 typedef struct THREAD_INSTANCE_TAG
26 {
27 pthread_t Pthread_handle;
28 THREAD_START_FUNC ThreadStartFunc;
29 void* Arg;
30 } THREAD_INSTANCE;
31
32 static void* ThreadWrapper(void* threadInstanceArg)
33 {
34 THREAD_INSTANCE* threadInstance = (THREAD_INSTANCE*)threadInstanceArg;
35 int result = threadInstance->ThreadStartFunc(threadInstance->Arg);
36 return (void*)(intptr_t)result;
37 }
38
39 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
40 {
41 THREADAPI_RESULT result;
42
43 if ((threadHandle == NULL) ||
44 (func == NULL))
45 {
46 result = THREADAPI_INVALID_ARG;
47 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
48 }
49 else
50 {
51 THREAD_INSTANCE* threadInstance = malloc(sizeof(THREAD_INSTANCE));
52 if (threadInstance == NULL)
53 {
54 result = THREADAPI_NO_MEMORY;
55 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
56 }
57 else
58 {
59 threadInstance->ThreadStartFunc = func;
60 threadInstance->Arg = arg;
61 int createResult = pthread_create(&threadInstance->Pthread_handle, NULL, ThreadWrapper, threadInstance);
62 switch (createResult)
63 {
64 default:
65 free(threadInstance);
66
67 result = THREADAPI_ERROR;
68 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
69 break;
70
71 case 0:
72 *threadHandle = threadInstance;
73 result = THREADAPI_OK;
74 break;
75
76 case EAGAIN:
77 free(threadInstance);
78
79 result = THREADAPI_NO_MEMORY;
80 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
81 break;
82 }
83 }
84 }
85
86 return result;
87 }
88
89 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE threadHandle, int* res)
90 {
91 THREADAPI_RESULT result;
92
93 THREAD_INSTANCE* threadInstance = (THREAD_INSTANCE*)threadHandle;
94 if (threadInstance == NULL)
95 {
96 result = THREADAPI_INVALID_ARG;
97 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
98 }
99 else
100 {
101 void* threadResult;
102 if (pthread_join(threadInstance->Pthread_handle, &threadResult) != 0)
103 {
104 result = THREADAPI_ERROR;
105 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
106 }
107 else
108 {
109 if (res != NULL)
110 {
111 *res = (int)(intptr_t)threadResult;
112 }
113
114 result = THREADAPI_OK;
115 }
116
117 free(threadInstance);
118 }
119
120 return result;
121 }
122
123 void ThreadAPI_Exit(int res)
124 {
125 pthread_exit((void*)(intptr_t)res);
126 }
127
128 void ThreadAPI_Sleep(unsigned int milliseconds)
129 {
130 #ifdef TI_RTOS
131 Task_sleep(milliseconds);
132 #else
133 time_t seconds = milliseconds / 1000;
134 long nsRemainder = (milliseconds % 1000) * 1000000;
135 struct timespec timeToSleep = { seconds, nsRemainder };
136 (void)nanosleep(&timeToSleep, NULL);
137 #endif
138 }