threadapi_rtx_mbed.cpp 3.6 KB

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