/home/runner/work/HiCR/HiCR/include/hicr/backends/boost/coroutine.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/boost/coroutine.hpp Source File
HiCR
coroutine.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <functional>
27#include <boost/context/continuation.hpp>
30
31namespace HiCR::backend::boost
32{
33
41{
42 public:
43
47 __INLINE__ void resume()
48 {
49 if (_hasFinished == true) HICR_THROW_RUNTIME("Attempting to resume a coroutine that has already finished");
50 if (_runningContext == true) HICR_THROW_RUNTIME("Attempting to resume a coroutine that is already running");
51
52 // Setting coroutine to have entered the running context
53 _runningContext = true;
54
55 // Resuming
56 _context = _context.resume();
57 }
58
62 __INLINE__ void yield()
63 {
64 if (_hasFinished == true) HICR_THROW_RUNTIME("Attempting to suspend a coroutine that has already finished");
65 if (_runningContext == false) HICR_THROW_RUNTIME("Attempting to suspend a coroutine that is not running");
66
67 // Setting coroutine to have exited the running context
68 _runningContext = false;
69
70 // Yielding
71 _context = _context.resume();
72 }
73
82 __INLINE__ void start(const HiCR::replicableFc_t &fc, void *const arg)
83 {
84 const auto coroutineFc = [this, fc, arg](::boost::context::continuation &&sink) {
85 // Storing caller context
86 _context = std::move(sink);
87
88 // First yield allows the creation of a coroutine without running the function
89 yield();
90
91 // Executing coroutine function
92 fc(arg);
93
94 // Setting the coroutine as finished
95 _hasFinished = true;
96
97 // Setting coroutine to have exited the running context
98 _runningContext = false;
99
100 // Returning to caller context
101 return std::move(_context);
102 };
103
104 // Setting coroutine to have entered the running context
105 _runningContext = true;
106
107 // Creating new context
108 _context = ::boost::context::callcc(coroutineFc);
109 }
110
116 __INLINE__ bool hasFinished() { return _hasFinished; }
117
118 private:
119
123 bool _hasFinished = false;
124
128 bool _runningContext = false;
129
133 ::boost::context::continuation _context;
134};
135
136} // namespace HiCR::backend::boost
Definition coroutine.hpp:41
__INLINE__ void resume()
Definition coroutine.hpp:47
__INLINE__ void yield()
Definition coroutine.hpp:62
__INLINE__ void start(const HiCR::replicableFc_t &fc, void *const arg)
Definition coroutine.hpp:82
__INLINE__ bool hasFinished()
Definition coroutine.hpp:116
Provides a base definition for a HiCR Execution Unit class.
std::function< void(void *)> replicableFc_t
Definition executionUnit.hpp:34
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74