Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
threadapi_rtx_mbed.cpp
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 #include <stdlib.h> 00005 #ifdef _CRTDBG_MAP_ALLOC 00006 #include <crtdbg.h> 00007 #endif 00008 00009 #include "azure_c_shared_utility/threadapi.h" 00010 #include "azure_c_shared_utility/xlogging.h" 00011 #include "rtos.h" 00012 00013 DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES); 00014 00015 #define MAX_THREADS 4 00016 #define STACK_SIZE 0x4000 00017 00018 typedef struct _thread 00019 { 00020 Thread* thrd; 00021 osThreadId id; 00022 Queue<int, 1> result; 00023 } mbedThread; 00024 static mbedThread threads[MAX_THREADS] = { 0 }; 00025 00026 typedef struct _create_param 00027 { 00028 THREAD_START_FUNC func; 00029 const void* arg; 00030 mbedThread *p_thread; 00031 } create_param; 00032 00033 static void thread_wrapper(const void* createParamArg) 00034 { 00035 const create_param* p = (const create_param*)createParamArg; 00036 p->p_thread->id = Thread::gettid(); 00037 (*(p->func))((void*)p->arg); 00038 free((void*)p); 00039 } 00040 00041 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg) 00042 { 00043 THREADAPI_RESULT result; 00044 if ((threadHandle == NULL) || 00045 (func == NULL)) 00046 { 00047 result = THREADAPI_INVALID_ARG; 00048 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result)); 00049 } 00050 else 00051 { 00052 size_t slot; 00053 for (slot = 0; slot < MAX_THREADS; slot++) 00054 { 00055 if (threads[slot].id == NULL) 00056 break; 00057 } 00058 00059 if (slot < MAX_THREADS) 00060 { 00061 create_param* param = (create_param*)malloc(sizeof(create_param)); 00062 if (param != NULL) 00063 { 00064 param->func = func; 00065 param->arg = arg; 00066 param->p_thread = threads + slot; 00067 threads[slot].thrd = new Thread(thread_wrapper, param, osPriorityNormal, STACK_SIZE); 00068 *threadHandle = (THREAD_HANDLE)(threads + slot); 00069 result = THREADAPI_OK; 00070 } 00071 else 00072 { 00073 result = THREADAPI_NO_MEMORY; 00074 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result)); 00075 } 00076 } 00077 else 00078 { 00079 result = THREADAPI_NO_MEMORY; 00080 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result)); 00081 } 00082 } 00083 00084 return result; 00085 } 00086 00087 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE thr, int *res) 00088 { 00089 THREADAPI_RESULT result = THREADAPI_OK; 00090 mbedThread* p = (mbedThread*)thr; 00091 if (p) 00092 { 00093 osEvent evt = p->result.get(); 00094 if (evt.status == osEventMessage) { 00095 Thread* t = p->thrd; 00096 if (res) 00097 { 00098 *res = (int)evt.value.p; 00099 } 00100 (void)t->terminate(); 00101 } 00102 else 00103 { 00104 result = THREADAPI_ERROR; 00105 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result)); 00106 } 00107 } 00108 else 00109 { 00110 result = THREADAPI_INVALID_ARG; 00111 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result)); 00112 } 00113 return result; 00114 } 00115 00116 void ThreadAPI_Exit(int res) 00117 { 00118 mbedThread* p; 00119 for (p = threads; p < &threads[MAX_THREADS]; p++) 00120 { 00121 if (p->id == Thread::gettid()) 00122 { 00123 p->result.put((int*)res); 00124 break; 00125 } 00126 } 00127 } 00128 00129 void ThreadAPI_Sleep(unsigned int millisec) 00130 { 00131 // 00132 // The timer on mbed seems to wrap around 65 seconds. Hmmm. 00133 // So we will do our waits in increments of 30 seconds. 00134 // 00135 const int thirtySeconds = 30000; 00136 int numberOfThirtySecondWaits = millisec / thirtySeconds; 00137 int remainderOfThirtySeconds = millisec % thirtySeconds; 00138 int i; 00139 for (i = 1; i <= numberOfThirtySecondWaits; i++) 00140 { 00141 Thread::wait(thirtySeconds); 00142 } 00143 Thread::wait(remainderOfThirtySeconds); 00144 }
Generated on Tue Jul 12 2022 12:43:24 by
