Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fiber] Add more concurrency classes #1026

Merged
merged 11 commits into from
May 20, 2024
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: |
export HOMEBREW_NO_INSTALL_CLEANUP=1 # saves time
brew update
brew unlink gcc
# brew unlink gcc
brew install doxygen boost gcc@12 avr-gcc@12 arm-gcc-bin@12 cmake || true
brew link --force avr-gcc@12
# brew upgrade boost gcc git || true
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
url = https://github.com/modm-ext/cmsis-dsp-partial.git
[submodule "ext/nlohmann/json"]
path = ext/nlohmann/json
url = git@github.com:modm-ext/json-partial.git
url = https://github.com/modm-ext/json-partial.git
8 changes: 4 additions & 4 deletions examples/avr/fiber/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ void
fiber_function1()
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f1counter < cycles) { modm::fiber::yield(); total_counter++; }
while (++f1counter < cycles) { modm::this_fiber::yield(); total_counter++; }
}

void
fiber_function2(uint32_t cyc)
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f2counter < cyc) { modm::fiber::yield(); total_counter++; }
while (++f2counter < cyc) { modm::this_fiber::yield(); total_counter++; }
}

struct Test
Expand All @@ -40,14 +40,14 @@ struct Test
fiber_function3()
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f3counter < cycles) { modm::fiber::yield(); total_counter++; }
while (++f3counter < cycles) { modm::this_fiber::yield(); total_counter++; }
}

void
fiber_function4(uint32_t cyc)
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f4counter < cyc) { modm::fiber::yield(); total_counter++; }
while (++f4counter < cyc) { modm::this_fiber::yield(); total_counter++; }
}

volatile uint32_t f3counter{0};
Expand Down
16 changes: 8 additions & 8 deletions examples/generic/fiber/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ void
fiber_function1()
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f1counter < cycles) { modm::fiber::yield(); total_counter++; }
while (++f1counter < cycles) { modm::this_fiber::yield(); total_counter++; }
}

void
fiber_function2(uint32_t cyc)
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f2counter < cyc) { modm::fiber::yield(); total_counter++; }
while (++f2counter < cyc) { modm::this_fiber::yield(); total_counter++; }
}

struct Test
Expand All @@ -42,23 +42,23 @@ struct Test
fiber_function3()
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f3counter < cycles) { modm::fiber::yield(); total_counter++; }
while (++f3counter < cycles) { modm::this_fiber::yield(); total_counter++; }
}

void
fiber_function4(uint32_t cyc)
{
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
while (++f4counter < cyc) { modm::fiber::yield(); total_counter++; }
while (++f4counter < cyc) { modm::this_fiber::yield(); total_counter++; }
}

volatile uint32_t f3counter{0};
volatile uint32_t f4counter{0};
} test;

// Single purpose fibers to time the yield
modm_faststack modm::Fiber<> fiber_y1([](){ modm::fiber::yield(); counter.stop(); });
modm_faststack modm::Fiber<> fiber_y2([](){ counter.start(); modm::fiber::yield(); });
modm_faststack modm::Fiber<> fiber_y1([](){ modm::this_fiber::yield(); counter.stop(); });
modm_faststack modm::Fiber<> fiber_y2([](){ counter.start(); modm::this_fiber::yield(); });

modm_faststack modm::Fiber<> fiber1(fiber_function1, modm::fiber::Start::Later);
modm_faststack modm::Fiber<> fiber2([](){ fiber_function2(cycles); }, modm::fiber::Start::Later);
Expand All @@ -71,12 +71,12 @@ extern modm::Fiber<> fiber_pong;
extern modm::Fiber<> fiber_ping;
modm_faststack modm::Fiber<> fiber_ping([](){
MODM_LOG_INFO << "ping = " << fiber_ping.stack_usage() << modm::endl;
modm::fiber::sleep(1s);
modm::this_fiber::sleep_for(1s);
fiber_pong.start();
}, modm::fiber::Start::Later);
modm_faststack modm::Fiber<> fiber_pong([](){
MODM_LOG_INFO << "pong = " << fiber_pong.stack_usage() << modm::endl;
modm::fiber::sleep(1s);
modm::this_fiber::sleep_for(1s);
fiber_ping.start();
}, modm::fiber::Start::Later);

Expand Down
4 changes: 2 additions & 2 deletions examples/linux/fiber/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void hello()
for(int ii=0; ii<10; ii++)
{
MODM_LOG_INFO << "Hello ";
modm::fiber::yield();
modm::this_fiber::yield();
}
}

Expand All @@ -28,7 +28,7 @@ struct Test
for(int ii=0; ii<10; ii++)
{
MODM_LOG_INFO << arg << modm::endl;
modm::fiber::yield();
modm::this_fiber::yield();
}
}
} test;
Expand Down
2 changes: 1 addition & 1 deletion examples/nucleo_f429zi/spi_flash_fatfs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ modm_faststack modm::Fiber<> blinkyFiber([]()
while(true)
{
Board::Leds::toggle();
modm::fiber::sleep(200ms);
modm::this_fiber::sleep_for(200ms);
}
});

Expand Down
4 changes: 2 additions & 2 deletions examples/rp_pico/fiber/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fiber_function1(CoreData& d)
{
while (++d.f1counter < cycles)
{
modm::fiber::yield();
modm::this_fiber::yield();
d.total_counter++;
}
}
Expand All @@ -52,7 +52,7 @@ fiber_function2(CoreData& d)
{
while (++d.f2counter < cycles)
{
modm::fiber::yield();
modm::this_fiber::yield();
d.total_counter++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/stm32f3_discovery/rotation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ modm_faststack modm::Fiber<> fiber_gyro([]()
}

// repeat every 5 ms
modm::fiber::sleep(5ms);
modm::this_fiber::sleep_for(5ms);
}
});

Expand All @@ -78,7 +78,7 @@ modm_faststack modm::Fiber<> fiber_blinky([]()
while (true)
{
Board::LedSouth::toggle();
modm::fiber::sleep(1s);
modm::this_fiber::sleep_for(1s);
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f469_discovery/touchscreen/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ modm_faststack modm::Fiber<> fiber_blinky([]()
while(true)
{
Board::LedGreen::toggle();
modm::fiber::sleep(20ms);
modm::this_fiber::sleep_for(20ms);
}
});

Expand Down
4 changes: 4 additions & 0 deletions ext/gcc/assert.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
void
__throw_bad_any_cast()
{ __modm_stdcpp_failure("bad_any_cast"); }

void
__throw_system_error(int errc __attribute__((unused)))
{ __modm_stdcpp_failure("system_error"); }
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
7 changes: 7 additions & 0 deletions ext/gcc/cxxabi.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void __cxa_deleted_virtual()

%% if with_threadsafe_statics
#include <atomic>
%% if with_fibers
#include <modm/processing/fiber.hpp>
%% endif
%% if is_avr
%#
// Even thought the actual guard size is uint64_t on AVR, we only need to access
Expand Down Expand Up @@ -71,6 +74,10 @@ __cxa_guard_acquire(guard_type *guard)
// We got called from inside an interrupt, but we cannot yield back
modm_assert(not is_in_irq, "stat.rec",
"Recursive initialization of a function static!", guard);
%% if with_fibers
// we're not in an interrupt, try to yield back to the initializing fiber
modm::this_fiber::yield();
%% endif
}
value = UNINITIALIZED;
}
Expand Down
1 change: 1 addition & 0 deletions ext/gcc/module_c++.lb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def build(env):
"with_threadsafe_statics": with_threadsafe_statics,
"with_memory_traits": env.has_module(":architecture:memory"),
"with_heap": env.has_module(":platform:heap"),
"with_fibers": env.has_module(":processing:fiber"),
"is_avr": is_avr,
"is_cortex_m": is_cortex_m,
}
Expand Down
4 changes: 4 additions & 0 deletions src/modm/architecture/detect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,19 @@
# define MODM_CPU_ARM 1
# define MODM_ALIGNMENT 4
# if defined __ARM_ARCH_6SM__ || defined __ARM_ARCH_6M__
# define MODM_CPU_CORTEX_M 1
# define MODM_CPU_CORTEX_M0 1
# define MODM_CPU_STRING "ARM Cortex-M0"
# elif defined __ARM_ARCH_7M__
# define MODM_CPU_CORTEX_M 1
# define MODM_CPU_CORTEX_M3 1
# define MODM_CPU_STRING "ARM Cortex-M3"
# elif defined __ARM_ARCH_7EM__
# define MODM_CPU_CORTEX_M 1
# define MODM_CPU_CORTEX_M4 1
# define MODM_CPU_STRING "ARM Cortex-M4"
# elif defined __ARM_ARCH_8M_MAIN__
# define MODM_CPU_CORTEX_M 1
# define MODM_CPU_CORTEX_M33 1
# define MODM_CPU_STRING "ARM Cortex-M33"
# elif defined __ARM_ARCH_ISA_A64
Expand Down
2 changes: 1 addition & 1 deletion src/modm/driver/inertial/bmi088_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void
Bmi088<Transport>::timerWait()
{
while (timer_.isArmed()) {
modm::fiber::yield();
modm::this_fiber::yield();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/modm/driver/inertial/bmi088_transport_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Bmi088SpiTransport<SpiMaster, AccCs, GyroCs>::readRegisters(uint8_t startReg,
}

while (!this->acquireMaster()) {
modm::fiber::yield();
modm::this_fiber::yield();
}
Cs::reset();

Expand Down Expand Up @@ -95,7 +95,7 @@ bool
Bmi088SpiTransport<SpiMaster, AccCs, GyroCs>::writeRegister(uint8_t reg, uint8_t data)
{
while (!this->acquireMaster()) {
modm::fiber::yield();
modm::this_fiber::yield();
}
Cs::reset();

Expand Down
2 changes: 1 addition & 1 deletion src/modm/platform/spi/at90_tiny_mega/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)
// start transfer by copying data into register
SPDR{{ id }} = data;

do modm::fiber::yield();
do modm::this_fiber::yield();
while (!(SPSR{{ id }} & (1 << SPIF{{ id }})));

return SPDR{{ id }};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ modm::platform::UartSpiMaster{{id}}::transfer(uint8_t data)
%% if use_fiber
// wait for transmit register empty
while (!((UCSR{{id}}A & (1 << UDRE{{id}}))))
modm::fiber::yield();
modm::this_fiber::yield();

%% if not extended
if(dataOrder == DataOrder::MsbFirst) {
Expand All @@ -72,7 +72,7 @@ modm::platform::UartSpiMaster{{id}}::transfer(uint8_t data)
UDR{{id}} = data;

// wait for receive register not empty
do modm::fiber::yield();
do modm::this_fiber::yield();
while (!((UCSR{{id}}A & (1 << RXC{{id}}))));

data = UDR{{id}};
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/spi/rp/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)
{
%% if use_fiber
// wait for previous transfer to finish
while (txFifoFull()) modm::fiber::yield();
while (txFifoFull()) modm::this_fiber::yield();

// start transfer by copying data into register
write(data);

while (rxFifoEmpty()) modm::fiber::yield();
while (rxFifoEmpty()) modm::this_fiber::yield();

return read();
%% else
Expand Down
6 changes: 3 additions & 3 deletions src/modm/platform/spi/rp/spi_master_dma_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transfer(
startTransfer(tx, rx, length);

while (Dma::TxChannel::isBusy() or (rx and Dma::RxChannel::isBusy()))
modm::fiber::yield();
modm::this_fiber::yield();

while (!txFifoEmpty() or (rx and !rxFifoEmpty()) or isBusy())
modm::fiber::yield();
modm::this_fiber::yield();

if (!rx) {
// Drain RX FIFO, then wait for shifting to finish (which
// may be *after* TX FIFO drains), then drain RX FIFO again
while (!rxFifoEmpty()) read();
while (isBusy()) modm::fiber::yield();
while (isBusy()) modm::this_fiber::yield();

// Don't leave overrun flag set
spi{{ id }}_hw->icr = SPI_SSPICR_RORIC_BITS;
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/spi/sam/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)

// wait for previous transfer to finish
while (!isTransmitDataRegisterEmpty())
modm::fiber::yield();
modm::this_fiber::yield();

// start transfer by copying data into register
write(data);

// wait for current transfer to finish
while(!isReceiveDataRegisterFull())
modm::fiber::yield();
modm::this_fiber::yield();

return read();
%% else
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/spi/sam_x7x/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)
%% if use_fiber
// wait for previous transfer to finish
while(!(SpiHal{{ id }}::readStatusFlags() & StatusFlag::TxRegisterEmpty))
modm::fiber::yield();
modm::this_fiber::yield();

// start transfer by copying data into register
SpiHal{{ id }}::write(data);

// wait for current transfer to finish
while(!(SpiHal{{ id }}::readStatusFlags() & StatusFlag::RxRegisterFull))
modm::fiber::yield();
modm::this_fiber::yield();

// read the received byte
SpiHal{{ id }}::read(data);
Expand Down
4 changes: 2 additions & 2 deletions src/modm/platform/spi/stm32/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)
%% if use_fiber
// wait for previous transfer to finish
while(!SpiHal{{ id }}::isTransmitRegisterEmpty())
modm::fiber::yield();
modm::this_fiber::yield();

// start transfer by copying data into register
SpiHal{{ id }}::write(data);

// wait for current transfer to finish
while(!SpiHal{{ id }}::isReceiveRegisterNotEmpty())
modm::fiber::yield();
modm::this_fiber::yield();

// read the received byte
SpiHal{{ id }}::read(data);
Expand Down
Loading
Loading