forked

Revision:
167:e84263d55307
Parent:
152:9a67f0b066fc
--- a/platform/mbed_stats.c	Thu Jun 08 15:02:37 2017 +0100
+++ b/platform/mbed_stats.c	Wed Jun 21 17:46:44 2017 +0100
@@ -1,8 +1,10 @@
 #include "mbed_stats.h"
 #include <string.h>
+#include <stdlib.h>
+#include "mbed_assert.h"
 
 #if MBED_CONF_RTOS_PRESENT
-#include "cmsis_os.h"
+#include "cmsis_os2.h"
 #endif
 
 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
@@ -12,24 +14,25 @@
     memset(stats, 0, sizeof(mbed_stats_stack_t));
 
 #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
-    osThreadEnumId enumid = _osThreadsEnumStart();
-    osThreadId threadid;
+    uint32_t thread_n = osThreadGetCount();
+    unsigned i;
+    osThreadId_t *threads;
 
-    while ((threadid = _osThreadEnumNext(enumid))) {
-        osEvent e;
+    threads = malloc(sizeof(osThreadId_t) * thread_n);
+    MBED_ASSERT(threads != NULL);
+
+    osKernelLock();
+    thread_n = osThreadEnumerate(threads, thread_n);
 
-        e = _osThreadGetInfo(threadid, osThreadInfoStackMax);
-        if (e.status == osOK) {
-           stats->max_size += (uint32_t)e.value.p;
-        }
+    for(i = 0; i < thread_n; i++) {
+        uint32_t stack_size = osThreadGetStackSize(threads[i]);
+        stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
+        stats->reserved_size += stack_size;
+        stats->stack_cnt++;
+    }
+    osKernelUnlock();
 
-        e = _osThreadGetInfo(threadid, osThreadInfoStackSize);
-        if (e.status == osOK) {
-           stats->reserved_size += (uint32_t)e.value.p;
-        }
-
-        stats->stack_cnt += 1;
-    }
+    free(threads);
 #endif
 }
 
@@ -39,26 +42,24 @@
     size_t i = 0;
 
 #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
-    osThreadEnumId enumid = _osThreadsEnumStart();
-    osThreadId threadid;
+    osThreadId_t *threads;
 
-    while ((threadid = _osThreadEnumNext(enumid)) && i < count) {
-        osEvent e;
+    threads = malloc(sizeof(osThreadId_t) * count);
+    MBED_ASSERT(threads != NULL);
+
+    osKernelLock();
+    count = osThreadEnumerate(threads, count);
 
-        e = _osThreadGetInfo(threadid, osThreadInfoStackMax);
-        if (e.status == osOK) {
-           stats[i].max_size = (uint32_t)e.value.p;
-        }
+    for(i = 0; i < count; i++) {
+        uint32_t stack_size = osThreadGetStackSize(threads[i]);
+        stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
+        stats[i].reserved_size = stack_size;
+        stats[i].thread_id = (uint32_t)threads[i];
+        stats[i].stack_cnt = 1;
+    }
+    osKernelUnlock();
 
-        e = _osThreadGetInfo(threadid, osThreadInfoStackSize);
-        if (e.status == osOK) {
-           stats[i].reserved_size = (uint32_t)e.value.p;
-        }
-
-        stats[i].thread_id = (uint32_t)threadid;
-        stats[i].stack_cnt = 1;
-        i += 1;
-    }
+    free(threads);
 #endif
 
     return i;