Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Wed Jun 17 10:00:10 2015 +0100
Parent:
83:5c72830f2d48
Child:
85:ef0a22cdf839
Commit message:
Synchronized with git revision 7c43e7db007aa7bcb56edfb3324266b1e69747e0

Full URL: https://github.com/mbedmicro/mbed/commit/7c43e7db007aa7bcb56edfb3324266b1e69747e0/

RTOS - Added stack checking methods to Thread

Changed in this revision

rtos/Thread.cpp Show annotated file Show diff for this revision Revisions of this file
rtos/Thread.h Show annotated file Show diff for this revision Revisions of this file
--- a/rtos/Thread.cpp	Tue Jun 16 07:45:29 2015 +0100
+++ b/rtos/Thread.cpp	Wed Jun 17 10:00:10 2015 +0100
@@ -41,6 +41,11 @@
             error("Error allocating the stack memory\n");
         _dynamic_stack = true;
     }
+    
+    //Fill the stack with a magic word for maximum usage checking
+    for (int i = 0; i < (stack_size / sizeof(uint32_t)); i++) {
+        _thread_def.stack_pointer[i] = 0xE25A2EA5;
+    }
 #endif
 #endif
     _tid = osThreadCreate(&_thread_def, argument);
@@ -76,6 +81,43 @@
 #endif
 }
 
+uint32_t Thread::stack_size() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    return _thread_def.tcb.priv_stack;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::free_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
+    return _thread_def.tcb.tsk_stack - bottom;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::used_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
+    return top - _thread_def.tcb.tsk_stack;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::max_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    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
+    return 0;
+#endif
+}
+
 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
     return osSignalWait(signals, millisec);
 }
--- a/rtos/Thread.h	Tue Jun 16 07:45:29 2015 +0100
+++ b/rtos/Thread.h	Wed Jun 17 10:00:10 2015 +0100
@@ -88,6 +88,26 @@
       @return  the State of this Thread
     */
     State get_state();
+    
+    /** Get the total stack memory size for this Thread
+      @return  the total stack memory size in bytes
+    */
+    uint32_t stack_size();
+    
+    /** Get the currently unused stack memory for this Thread
+      @return  the currently unused stack memory in bytes
+    */
+    uint32_t free_stack();
+    
+    /** Get the currently used stack memory for this Thread
+      @return  the currently used stack memory in bytes
+    */
+    uint32_t used_stack();
+    
+    /** Get the maximum stack memory usage to date for this Thread
+      @return  the maximum stack memory usage to date in bytes
+    */
+    uint32_t max_stack();
 
     /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
       @param   signals   wait until all specified signal flags set or 0 for any single signal flag.