/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>
29
30namespace HiCR::backend::boost
31{
32
40{
41 public:
42
49 using coroutineFc_t = std::function<void(void *)>;
50
54 __INLINE__ void resume()
55 {
56 if (_hasFinished == true) HICR_THROW_RUNTIME("Attempting to resume a coroutine that has already finished");
57 if (_runningContext == true) HICR_THROW_RUNTIME("Attempting to resume a coroutine that is already running");
58
59 // Setting coroutine to have entered the running context
60 _runningContext = true;
61
62 // Resuming
63 _context = _context.resume();
64 }
65
69 __INLINE__ void yield()
70 {
71 if (_hasFinished == true) HICR_THROW_RUNTIME("Attempting to suspend a coroutine that has already finished");
72 if (_runningContext == false) HICR_THROW_RUNTIME("Attempting to suspend a coroutine that is not running");
73
74 // Setting coroutine to have exited the running context
75 _runningContext = false;
76
77 // Yielding
78 _context = _context.resume();
79 }
80
89 __INLINE__ void start(const coroutineFc_t &fc, void *const arg)
90 {
91 const auto coroutineFc = [this, fc, arg](::boost::context::continuation &&sink) {
92 // Storing caller context
93 _context = std::move(sink);
94
95 // First yield allows the creation of a coroutine without running the function
96 yield();
97
98 // Executing coroutine function
99 fc(arg);
100
101 // Setting the coroutine as finished
102 _hasFinished = true;
103
104 // Setting coroutine to have exited the running context
105 _runningContext = false;
106
107 // Returning to caller context
108 return std::move(_context);
109 };
110
111 // Setting coroutine to have entered the running context
112 _runningContext = true;
113
114 // Creating new context
115 _context = ::boost::context::callcc(coroutineFc);
116 }
117
123 __INLINE__ bool hasFinished() { return _hasFinished; }
124
125 private:
126
130 bool _hasFinished = false;
131
135 bool _runningContext = false;
136
140 ::boost::context::continuation _context;
141};
142
143} // namespace HiCR::backend::boost
Definition coroutine.hpp:40
__INLINE__ void resume()
Definition coroutine.hpp:54
std::function< void(void *)> coroutineFc_t
Definition coroutine.hpp:49
__INLINE__ void start(const coroutineFc_t &fc, void *const arg)
Definition coroutine.hpp:89
__INLINE__ void yield()
Definition coroutine.hpp:69
__INLINE__ bool hasFinished()
Definition coroutine.hpp:123
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74