Skip to content

Commit d59cbd8

Browse files
add platform utils examples (#97)
1 parent 991e509 commit d59cbd8

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CircularBuffer example
2+
3+
This example shows how to use `CircularBuffer` API's. Contents of data stream are pushed till buffer is full, and popped back till buffer is empty.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "platform/CircularBuffer.h"
8+
9+
#define BUF_SIZE 10
10+
11+
CircularBuffer<char, BUF_SIZE> buf;
12+
char data_stream[] = "DataToBeAddedToBuffer";
13+
14+
int main()
15+
{
16+
uint32_t bytes_written = 0;
17+
18+
while (!buf.full()) {
19+
buf.push(data_stream[bytes_written++]);
20+
}
21+
22+
printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n",
23+
(buf.full() ? "true" : "false"),
24+
(buf.empty() ? "true" : "false"));
25+
printf("Bytes written %ld \n", bytes_written);
26+
27+
// If buffer is full, contents will be over-written
28+
buf.push(data_stream[bytes_written++]);
29+
30+
char data = 0;
31+
printf("Buffer contents: ");
32+
while (!buf.empty()) {
33+
buf.pop(data);
34+
printf("%c", data);
35+
}
36+
printf("\n");
37+
38+
printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n",
39+
(buf.full() ? "true" : "false"),
40+
(buf.empty() ? "true" : "false"));
41+
42+
return 0;
43+
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CriticalSectionLock example
2+
3+
This example shows how to use `CriticalSection` API to avoid race condition when multiple threads update global shared counter.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
#define USE_CRITICAL_SECTION_LOCK 1 // Set 0 to see race condition
9+
// Note: Might require few runs to see race condition
10+
11+
#define THREAD_CNT 8
12+
13+
int32_t value = 100000;
14+
volatile int32_t counter = 0;
15+
16+
void increment(void)
17+
{
18+
for (int i = 0; i < value; i++) {
19+
#if (USE_CRITICAL_SECTION_LOCK == 1)
20+
CriticalSectionLock lock;
21+
#endif
22+
counter += 1;
23+
}
24+
}
25+
26+
int get_count(void)
27+
{
28+
if (counter == (value * THREAD_CNT)) {
29+
printf("No Race condition\n");
30+
} else {
31+
printf("Race condition\n");
32+
}
33+
return counter;
34+
}
35+
36+
int main()
37+
{
38+
Thread counter_thread[THREAD_CNT];
39+
40+
for (int i = 0; i < THREAD_CNT; i++) {
41+
counter_thread[i].start(callback(increment));
42+
}
43+
44+
// Wait for the threads to finish
45+
for (int i = 0; i < THREAD_CNT; i++) {
46+
counter_thread[i].join();
47+
}
48+
printf("Counter = %d\n", get_count());
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PlatformMutex example
2+
3+
This example shows how to use `PlatformMutex` to synchronize the execution of threads.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
PlatformMutex stdio_mutex;
9+
Thread t2;
10+
Thread t3;
11+
12+
void notify(const char *name, int state)
13+
{
14+
stdio_mutex.lock();
15+
printf("%s: %d\n\r", name, state);
16+
stdio_mutex.unlock();
17+
}
18+
19+
void test_thread(void const *args)
20+
{
21+
while (true) {
22+
notify((const char *)args, 0);
23+
ThisThread::sleep_for(1000);
24+
notify((const char *)args, 1);
25+
ThisThread::sleep_for(1000);
26+
}
27+
}
28+
29+
int main()
30+
{
31+
t2.start(callback(test_thread, (void *)"Th 2"));
32+
t3.start(callback(test_thread, (void *)"Th 3"));
33+
34+
test_thread((void *)"Th 1");
35+
}

APIs_Platform/Utils_ex_1/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Platform utils example
2+
3+
This example shows how to use memory statistics API and debug functions.

APIs_Platform/Utils_ex_1/main.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
#define MAX_THREAD_INFO 10
9+
10+
mbed_stats_heap_t heap_info;
11+
mbed_stats_stack_t stack_info[ MAX_THREAD_INFO ];
12+
int main()
13+
{
14+
debug("\nThis message is from debug function");
15+
debug_if(1, "\nThis message is from debug_if function");
16+
debug_if(0, "\nSOMETHING WRONG!!! This message from debug_if function shouldn't show on bash");
17+
18+
printf("\nMemoryStats:");
19+
mbed_stats_heap_get(&heap_info);
20+
printf("\n\tBytes allocated currently: %ld", heap_info.current_size);
21+
printf("\n\tMax bytes allocated at a given time: %ld", heap_info.max_size);
22+
printf("\n\tCumulative sum of bytes ever allocated: %ld", heap_info.total_size);
23+
printf("\n\tCurrent number of bytes allocated for the heap: %ld", heap_info.reserved_size);
24+
printf("\n\tCurrent number of allocations: %ld", heap_info.alloc_cnt);
25+
printf("\n\tNumber of failed allocations: %ld", heap_info.alloc_fail_cnt);
26+
27+
mbed_stats_stack_get(&stack_info[0]);
28+
printf("\nCumulative Stack Info:");
29+
printf("\n\tMaximum number of bytes used on the stack: %ld", stack_info[0].max_size);
30+
printf("\n\tCurrent number of bytes allocated for the stack: %ld", stack_info[0].reserved_size);
31+
printf("\n\tNumber of stacks stats accumulated in the structure: %ld", stack_info[0].stack_cnt);
32+
33+
mbed_stats_stack_get_each(stack_info, MAX_THREAD_INFO);
34+
printf("\nThread Stack Info:");
35+
for (int i = 0; i < MAX_THREAD_INFO; i++) {
36+
if (stack_info[i].thread_id != 0) {
37+
printf("\n\tThread: %d", i);
38+
printf("\n\t\tThread Id: 0x%08lX", stack_info[i].thread_id);
39+
printf("\n\t\tMaximum number of bytes used on the stack: %ld", stack_info[i].max_size);
40+
printf("\n\t\tCurrent number of bytes allocated for the stack: %ld", stack_info[i].reserved_size);
41+
printf("\n\t\tNumber of stacks stats accumulated in the structure: %ld", stack_info[i].stack_cnt);
42+
}
43+
}
44+
45+
printf("\nDone...\n\n");
46+
}
47+

0 commit comments

Comments
 (0)