Skip to content

Commit

Permalink
[fiber] Implement resumable functions as fibers
Browse files Browse the repository at this point in the history
Add experimental option for implementing resumable functions in terms of
fibers.
  • Loading branch information
chris-durand committed Jan 21, 2022
1 parent 77a761d commit 5f3c900
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 7 deletions.
59 changes: 59 additions & 0 deletions src/modm/processing/resumable/fiber/macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2021, Christopher Durand
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_RF_MACROS_HPP
#define MODM_RF_MACROS_HPP

#include <modm/architecture/interface/assert.hpp>
#include <modm/architecture/utils.hpp>
#include <modm/processing/fiber/scheduler.hpp>

/// @ingroup modm_processing_resumable
/// @{

#define RF_BEGIN(index)

#define RF_BEGIN()

#define RF_END_RETURN(...) return __VA_ARGS__

#define RF_END()

#define RF_END_RETURN(...) return __VA_ARGS__

#define RF_YIELD() modm::fiber::yield()

#define RF_WAIT_WHILE(...) \
do { \
while (__VA_ARGS__) { \
modm::fiber::yield(); \
} \
} while(0)

#define RF_WAIT_UNTIL(...) \
RF_WAIT_WHILE(!(__VA_ARGS__))

#define RF_CALL(...) __VA_ARGS__

// TODO: implement blocking
#define RF_CALL(...) __VA_ARGS__

#define RF_RETURN_CALL(...) return __VA_ARGS__

#define RF_RETURN(...) return __VA_ARGS__

#define RF_RETURN() return

/// @}

#endif

#endif // MODM_RF_MACROS_HPP
33 changes: 33 additions & 0 deletions src/modm/processing/resumable/fiber/nested_resumable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021, Christopher Durand
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_NESTED_RESUMABLE_HPP
#define MODM_NESTED_RESUMABLE_HPP

#include "macros.hpp"
#include <cstdint>

namespace modm
{

/**
* Dummy implementation to be used with fibers
*
* @ingroup modm_processing_resumable
*/
template<uint8_t Levels = 1>
class NestedResumable
{};
/// @endcond

} // namespace modm

#endif // MODM_NESTED_RESUMABLE_HPP
40 changes: 40 additions & 0 deletions src/modm/processing/resumable/fiber/resumable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2021, Christopher Durand
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_RESUMABLE_HPP
#define MODM_RESUMABLE_HPP

#include "macros.hpp"
#include <cstdint>

namespace modm
{

/**
* Dummy implementation to be used with fibers
*
* @ingroup modm_processing_resumable
*/
template <typename T>
using ResumableResult = T;

/**
* Dummy implementation to be used with fibers
*
* @ingroup modm_processing_resumable
*/
template<uint8_t Functions = 1>
class Resumable
{};

} // namespace modm

#endif // MODM_RESUMABLE_HPP
27 changes: 20 additions & 7 deletions src/modm/processing/resumable/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,32 @@ def init(module):
def prepare(module, options):
module.depends(":architecture:assert")

module.add_option(
BooleanOption(
name="check_nesting_depth",
default=True,
description=descr_nesting_depth))
if not options[":processing:resumable:__use_fibers"]:
module.add_option(
BooleanOption(
name="check_nesting_depth",
default=True,
description=descr_nesting_depth))

if options[":__fibers"]:
module.add_option(
BooleanOption(
name="__use_fibers",
default=False,
description="Implement resumable functions with fibers for compatibility with drivers"))

return True

def build(env):
env.outbasepath = "modm/src/modm/processing/resumable"
env.copy(".", ignore=env.ignore_files("*.md", "*.in"))
if env.get(":processing:resumable:__use_fibers", False):
env.copy("fiber/nested_resumable.hpp", "nested_resumable.hpp")
env.copy("fiber/resumable.hpp", "resumable.hpp")
env.copy("fiber/macros.hpp", "macros.hpp")
else:
env.copy(".", ignore=env.ignore_files("*.md", "*.in"))
env.template("nested_resumable.hpp.in")
env.copy("../resumable.hpp")
env.template("nested_resumable.hpp.in")


# ============================ Option Descriptions ============================
Expand Down

0 comments on commit 5f3c900

Please sign in to comment.