Debian Codegraph @

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

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 <stdlib.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 #include "rtos.h"
9
10 MU_DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
11
12 #define MAX_THREADS 4
13 #define STACK_SIZE 0x4000
14
15 typedef struct _thread
16 {
17 Thread* thrd;
18 osThreadId id;
19 Queue<int, 1> result;
20 } mbedThread;
21 static mbedThread threads[MAX_THREADS] = { 0 };
22
23 typedef struct _create_param
24 {
25 THREAD_START_FUNC func;
26 const void* arg;
27 mbedThread *p_thread;
28 } create_param;
29
30 static void thread_wrapper(const void* createParamArg)
31 {
32 const create_param* p = (const create_param*)createParamArg;
33 p->p_thread->id = Thread::gettid();
34 (*(p->func))((void*)p->arg);
35 free((void*)p);
36 }
37
38 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
39 {
40 THREADAPI_RESULT result;
41 if ((threadHandle == NULL) ||
42 (func == NULL))
43 {
44 result = THREADAPI_INVALID_ARG;
45 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
46 }
47 else
48 {
49 size_t slot;
50 for (slot = 0; slot < MAX_THREADS; slot++)
51 {
52 if (threads[slot].id == NULL)
53 break;
54 }
55
56 if (slot < MAX_THREADS)
57 {
58 create_param* param = (create_param*)malloc(sizeof(create_param));
59 if (param != NULL)
60 {
61 param->func = func;
62 param->arg = arg;
63 param->p_thread = threads + slot;
64 threads[slot].thrd = new Thread(thread_wrapper, param, osPriorityNormal, STACK_SIZE);
65 *threadHandle = (THREAD_HANDLE)(threads + slot);
66 result = THREADAPI_OK;
67 }
68 else
69 {
70 result = THREADAPI_NO_MEMORY;
71 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
72 }
73 }
74 else
75 {
76 result = THREADAPI_NO_MEMORY;
77 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
78 }
79 }
80
81 return result;
82 }
83
84 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE thr, int *res)
85 {
86 THREADAPI_RESULT result = THREADAPI_OK;
87 mbedThread* p = (mbedThread*)thr;
88 if (p)
89 {
90 osEvent evt = p->result.get();
91 if (evt.status == osEventMessage) {
92 Thread* t = p->thrd;
93 if (res)
94 {
95 *res = (int)evt.value.p;
96 }
97 (void)t->terminate();
98 }
99 else
100 {
101 result = THREADAPI_ERROR;
102 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
103 }
104 }
105 else
106 {
107 result = THREADAPI_INVALID_ARG;
108 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(THREADAPI_RESULT, result));
109 }
110 return result;
111 }
112
113 void ThreadAPI_Exit(int res)
114 {
115 mbedThread* p;
116 for (p = threads; p < &threads[MAX_THREADS]; p++)
117 {
118 if (p->id == Thread::gettid())
119 {
120 p->result.put((int*)res);
121 break;
122 }
123 }
124 }
125
126 void ThreadAPI_Sleep(unsigned int millisec)
127 {
128 //
129 // The timer on mbed seems to wrap around 65 seconds. Hmmm.
130 // So we will do our waits in increments of 30 seconds.
131 //
132 const int thirtySeconds = 30000;
133 int numberOfThirtySecondWaits = millisec / thirtySeconds;
134 int remainderOfThirtySeconds = millisec % thirtySeconds;
135 int i;
136 for (i = 1; i <= numberOfThirtySecondWaits; i++)
137 {
138 Thread::wait(thirtySeconds);
139 }
140 Thread::wait(remainderOfThirtySeconds);
141 }