Aguila

Dependents:   _test

Fork of mbed-rtos by mbed official

Revision:
112:53ace74b190c
Parent:
107:bdd541595fc5
Child:
115:11950e007d8a
--- a/rtos/Thread.cpp	Tue May 03 00:15:52 2016 +0100
+++ b/rtos/Thread.cpp	Thu May 05 20:45:13 2016 +0100
@@ -24,11 +24,17 @@
 #include "mbed_error.h"
 #include "rtos_idle.h"
 
+// rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
+#undef NULL  //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
+#include "rt_TypeDef.h"
+
+extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
+
 namespace rtos {
 
 Thread::Thread(void (*task)(void const *argument), void *argument,
         osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
-#ifdef CMSIS_OS_RTX
+#ifdef __MBED_CMSIS_RTOS_CM
     _thread_def.pthread = task;
     _thread_def.tpriority = priority;
     _thread_def.stacksize = stack_size;
@@ -71,8 +77,10 @@
 }
 
 Thread::State Thread::get_state() {
-#ifndef __MBED_CMSIS_RTOS_CA9
+#if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
+#ifdef CMSIS_OS_RTX
     return ((State)_thread_def.tcb.state);
+#endif
 #else
     uint8_t status;
     status = osThreadGetState(_tid);
@@ -82,37 +90,62 @@
 
 uint32_t Thread::stack_size() {
 #ifndef __MBED_CMSIS_RTOS_CA9
+#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
     return _thread_def.tcb.priv_stack;
 #else
+    P_TCB tcb = rt_tid2ptcb(_tid);
+    return tcb->priv_stack;
+#endif
+#else
     return 0;
 #endif
 }
 
 uint32_t Thread::free_stack() {
 #ifndef __MBED_CMSIS_RTOS_CA9
+#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
     uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
     return _thread_def.tcb.tsk_stack - bottom;
 #else
+    P_TCB tcb = rt_tid2ptcb(_tid);
+    uint32_t bottom = (uint32_t)tcb->stack;
+    return tcb->tsk_stack - bottom;
+#endif
+#else
     return 0;
 #endif
 }
 
 uint32_t Thread::used_stack() {
 #ifndef __MBED_CMSIS_RTOS_CA9
+#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
     uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
     return top - _thread_def.tcb.tsk_stack;
 #else
+    P_TCB tcb = rt_tid2ptcb(_tid);
+    uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
+    return top - tcb->tsk_stack;
+#endif
+#else
     return 0;
 #endif
 }
 
 uint32_t Thread::max_stack() {
 #ifndef __MBED_CMSIS_RTOS_CA9
+#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
     uint32_t high_mark = 0;
     while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
         high_mark++;
     return _thread_def.tcb.priv_stack - (high_mark * 4);
 #else
+    P_TCB tcb = rt_tid2ptcb(_tid);
+    uint32_t high_mark = 0;
+    while (tcb->stack[high_mark] == 0xE25A2EA5)
+        high_mark++;
+    return tcb->priv_stack - (high_mark * 4);
+#endif
+#else
     return 0;
 #endif
 }
@@ -139,9 +172,11 @@
 
 Thread::~Thread() {
     terminate();
+#ifdef __MBED_CMSIS_RTOS_CM
     if (_dynamic_stack) {
         delete[] (_thread_def.stack_pointer);
     }
+#endif
 }
 
 }