Skip to content

Commit 3936a28

Browse files
committed
[examples] Add fiber examples
1 parent b22c89a commit 3936a28

File tree

7 files changed

+270
-1
lines changed

7 files changed

+270
-1
lines changed

.github/workflows/windows_hosted.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
- name: Hosted Examples
5757
shell: bash
5858
run: |
59-
(cd examples && python ../tools/scripts/examples_compile.py linux/assert linux/block_device linux/build_info linux/git linux/logger linux/printf)
59+
(cd examples && python ../tools/scripts/examples_compile.py linux/assert linux/block_device linux/build_info linux/git linux/logger linux/printf linux/etl linux/fiber)
6060
6161
- name: Hosted Unittests
6262
if: always()

examples/avr/fiber/main.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2020, Erik Henriksson
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/processing.hpp>
15+
16+
using namespace Board;
17+
using namespace std::chrono_literals;
18+
19+
constexpr uint32_t cycles = 100'000;
20+
volatile uint32_t f1counter = 0, f2counter = 0;
21+
uint32_t total_counter=0;
22+
23+
void
24+
fiber_function1()
25+
{
26+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
27+
while (++f1counter < cycles) { modm::fiber::yield(); total_counter++; }
28+
}
29+
30+
void
31+
fiber_function2(uint32_t cyc)
32+
{
33+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
34+
while (++f2counter < cyc) { modm::fiber::yield(); total_counter++; }
35+
}
36+
37+
struct Test
38+
{
39+
void
40+
fiber_function3()
41+
{
42+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
43+
while (++f3counter < cycles) { modm::fiber::yield(); total_counter++; }
44+
}
45+
46+
void
47+
fiber_function4(uint32_t cyc)
48+
{
49+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
50+
while (++f4counter < cyc) { modm::fiber::yield(); total_counter++; }
51+
}
52+
53+
volatile uint32_t f3counter{0};
54+
volatile uint32_t f4counter{0};
55+
} test;
56+
57+
modm::fiber::Stack<128> stack1;
58+
modm::fiber::Stack<128> stack2;
59+
modm::fiber::Stack<128> stack3;
60+
modm::fiber::Stack<128> stack4;
61+
modm::Fiber fiber1(stack1, fiber_function1);
62+
modm::Fiber fiber2(stack2, [](){ fiber_function2(cycles); });
63+
modm::Fiber fiber3(stack3, [](){ test.fiber_function3(); });
64+
modm::Fiber fiber4(stack4, [cyc=uint32_t(cycles)]() mutable { cyc++; test.fiber_function4(cyc); });
65+
66+
// ATmega2560@16MHz: 239996 yields in 2492668us, 96280 yields per second, 10386ns per yield
67+
int
68+
main()
69+
{
70+
Board::initialize();
71+
Board::LedD13::setOutput();
72+
MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl;
73+
MODM_LOG_INFO.flush();
74+
75+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
76+
modm::fiber::Scheduler::run();
77+
const auto diff = (modm::PreciseClock::now() - start);
78+
79+
MODM_LOG_INFO << "Benchmark done!" << modm::endl;
80+
MODM_LOG_INFO << "Executed " << total_counter << " yields in " << diff << modm::endl;
81+
MODM_LOG_INFO << uint32_t((total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count());
82+
MODM_LOG_INFO << " yields per second, ";
83+
MODM_LOG_INFO << uint32_t(std::chrono::nanoseconds(diff).count() / total_counter);
84+
MODM_LOG_INFO << "ns per yield" << modm::endl;
85+
MODM_LOG_INFO.flush();
86+
87+
while(1) ;
88+
return 0;
89+
}

examples/avr/fiber/project.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<library>
2+
<extends>modm:mega-2560-pro</extends>
3+
<!-- <extends>modm:arduino-nano</extends> -->
4+
<options>
5+
<option name="modm:build:build.path">../../../build/avr/fiber</option>
6+
<option name="modm:__fibers">yes</option>
7+
</options>
8+
<modules>
9+
<module>modm:build:scons</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:processing:fiber</module>
12+
</modules>
13+
</library>

examples/generic/fiber/main.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2020, Erik Henriksson
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/processing.hpp>
15+
16+
using namespace Board;
17+
using namespace std::chrono_literals;
18+
19+
constexpr uint32_t cycles = 1'000'000;
20+
volatile uint32_t f1counter = 0, f2counter = 0;
21+
uint32_t total_counter=0;
22+
23+
void
24+
fiber_function1()
25+
{
26+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
27+
while (++f1counter < cycles) { modm::fiber::yield(); total_counter++; }
28+
}
29+
30+
void
31+
fiber_function2(uint32_t cyc)
32+
{
33+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
34+
while (++f2counter < cyc) { modm::fiber::yield(); total_counter++; }
35+
}
36+
37+
struct Test
38+
{
39+
void
40+
fiber_function3()
41+
{
42+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
43+
while (++f3counter < cycles) { modm::fiber::yield(); total_counter++; }
44+
}
45+
46+
void
47+
fiber_function4(uint32_t cyc)
48+
{
49+
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
50+
while (++f4counter < cyc) { modm::fiber::yield(); total_counter++; }
51+
}
52+
53+
volatile uint32_t f3counter{0};
54+
volatile uint32_t f4counter{0};
55+
} test;
56+
57+
modm_faststack modm::fiber::Stack<2048> stack1;
58+
modm_faststack modm::fiber::Stack<2048> stack2;
59+
modm_faststack modm::fiber::Stack<2048> stack3;
60+
modm_faststack modm::fiber::Stack<2048> stack4;
61+
modm_fastdata modm::Fiber fiber1(stack1, fiber_function1);
62+
modm_fastdata modm::Fiber fiber2(stack2, [](){ fiber_function2(cycles); });
63+
modm_fastdata modm::Fiber fiber3(stack3, [](){ test.fiber_function3(); });
64+
modm_fastdata modm::Fiber fiber4(stack4, [cyc=uint32_t(0)]() mutable { cyc++; test.fiber_function4(cyc); });
65+
66+
// Blue pill (M3 72MHz): Executed 1000000 in 1098591us (910256.88 yields per second)
67+
// Feather M0 (M0+ 48MHz): Executed 1000000 in 1944692us (514220.25 yields per second)
68+
int
69+
main()
70+
{
71+
Board::initialize();
72+
MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl;
73+
MODM_LOG_INFO.flush();
74+
75+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
76+
modm::fiber::Scheduler::run();
77+
const auto diff = (modm::PreciseClock::now() - start);
78+
79+
MODM_LOG_INFO << "Benchmark done!" << modm::endl;
80+
MODM_LOG_INFO << "Executed " << total_counter << " yields in " << diff << modm::endl;
81+
MODM_LOG_INFO << ((total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count());
82+
MODM_LOG_INFO << " yields per second, ";
83+
MODM_LOG_INFO << (std::chrono::nanoseconds(diff).count() / total_counter);
84+
MODM_LOG_INFO << "ns per yield" << modm::endl;
85+
MODM_LOG_INFO.flush();
86+
87+
while(1) ;
88+
return 0;
89+
}

examples/generic/fiber/project.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<library>
2+
<extends>modm:nucleo-f429zi</extends>
3+
<!-- <extends>modm:nucleo-g071rb</extends> -->
4+
<options>
5+
<option name="modm:build:build.path">../../../build/generic/fiber</option>
6+
<option name="modm:__fibers">yes</option>
7+
</options>
8+
<modules>
9+
<module>modm:build:scons</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:processing:fiber</module>
12+
</modules>
13+
</library>

examples/linux/fiber/main.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2021, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/debug.hpp>
13+
#include <modm/processing.hpp>
14+
15+
void hello()
16+
{
17+
for(int ii=0; ii<10; ii++)
18+
{
19+
MODM_LOG_INFO << "Hello ";
20+
modm::fiber::yield();
21+
}
22+
}
23+
24+
struct Test
25+
{
26+
void world(const char *arg)
27+
{
28+
for(int ii=0; ii<10; ii++)
29+
{
30+
MODM_LOG_INFO << arg << modm::endl;
31+
modm::fiber::yield();
32+
}
33+
}
34+
} test;
35+
36+
modm::fiber::Stack<1024> stack1;
37+
modm::fiber::Stack<1024> stack2;
38+
modm::Fiber fiber1(stack1, hello);
39+
40+
int
41+
main(void)
42+
{
43+
const char *arg = "World";
44+
modm::Fiber fiber2(stack2, [=]() { test.world(arg); });
45+
46+
MODM_LOG_INFO << "Start" << modm::endl;
47+
modm::fiber::Scheduler::run();
48+
MODM_LOG_INFO << "End" << modm::endl;
49+
50+
return 0;
51+
}

examples/linux/fiber/project.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<!-- CI: run -->
3+
<options>
4+
<option name="modm:target">hosted-linux</option>
5+
<option name="modm:build:build.path">../../../build/linux/fiber</option>
6+
<option name="modm:__fibers">yes</option>
7+
</options>
8+
<modules>
9+
<module>modm:debug</module>
10+
<module>modm:platform:core</module>
11+
<module>modm:processing:fiber</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>

0 commit comments

Comments
 (0)