gem5  v20.1.0.0
coroutine.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __BASE_COROUTINE_HH__
39 #define __BASE_COROUTINE_HH__
40 
41 #include <functional>
42 #include <stack>
43 
44 #include "base/fiber.hh"
45 
46 namespace m5
47 {
48 
61 template <typename Arg, typename Ret>
62 class Coroutine : public Fiber
63 {
64 
65  // This empty struct type is meant to replace coroutine channels
66  // in case the channel should be void (Coroutine template parameters
67  // are void. (See following ArgChannel, RetChannel typedef)
68  struct Empty {};
69  using ArgChannel = typename std::conditional<
70  std::is_same<Arg, void>::value, Empty, std::stack<Arg>>::type;
71 
72  using RetChannel = typename std::conditional<
73  std::is_same<Ret, void>::value, Empty, std::stack<Ret>>::type;
74 
75  public:
83  class CallerType
84  {
85  friend class Coroutine;
86  protected:
87  CallerType(Coroutine& _coro) : coro(_coro), callerFiber(nullptr) {}
88 
89  public:
99  template <typename T = Ret>
100  CallerType&
101  operator()(typename std::enable_if<
102  !std::is_same<T, void>::value, T>::type param)
103  {
104  retChannel.push(param);
105  callerFiber->run();
106  return *this;
107  }
108 
117  template <typename T = Ret>
118  typename std::enable_if<std::is_same<T, void>::value,
121  {
122  callerFiber->run();
123  return *this;
124  }
125 
138  template <typename T = Arg>
139  typename std::enable_if<!std::is_same<T, void>::value, T>::type
140  get()
141  {
142  auto& args_channel = coro.argsChannel;
143  while (args_channel.empty()) {
144  callerFiber->run();
145  }
146 
147  auto ret = args_channel.top();
148  args_channel.pop();
149  return ret;
150  }
151 
152  private:
156  };
157 
162  Coroutine() = delete;
163  Coroutine(const Coroutine& rhs) = delete;
164  Coroutine& operator=(const Coroutine& rhs) = delete; // end of api_coroutine
166 
184  Coroutine(std::function<void(CallerType&)> f, bool run_coroutine = true)
185  : Fiber(), task(f), caller(*this)
186  {
187  // When desired, run the Coroutine after it is created
188  if (run_coroutine)
189  this->call();
190  }
191 
195  virtual ~Coroutine() {}
196 
197  public:
209  template <typename T = Arg>
210  Coroutine&
211  operator()(typename std::enable_if<
212  !std::is_same<T, void>::value, T>::type param)
213  {
214  argsChannel.push(param);
215  this->call();
216  return *this;
217  }
218 
227  template <typename T = Arg>
228  typename std::enable_if<std::is_same<T, void>::value, Coroutine>::type&
230  {
231  this->call();
232  return *this;
233  }
234 
247  template <typename T = Ret>
248  typename std::enable_if<!std::is_same<T, void>::value, T>::type
249  get()
250  {
251  auto& ret_channel = caller.retChannel;
252  while (ret_channel.empty()) {
253  this->call();
254  }
255 
256  auto ret = ret_channel.top();
257  ret_channel.pop();
258  return ret;
259  }
260 
266  operator bool() const { return !this->finished(); }
267 
268  private:
275  void main() override { this->task(caller); }
276 
277  void
279  {
281  run();
282  }
283 
284  private:
287 
289  std::function<void(CallerType&)> task;
290 
293 };
294 
295 } //namespace m5
296 
297 #endif // __BASE_COROUTINE_HH__
m5::Coroutine::CallerType::get
std::enable_if<!std::is_same< T, void >::value, T >::type get()
get() is the way we can extrapolate arguments from the coroutine caller.
Definition: coroutine.hh:140
m5::Coroutine::task
std::function< void(CallerType &)> task
Coroutine task.
Definition: coroutine.hh:289
m5::Coroutine::argsChannel
ArgChannel argsChannel
Arguments for the coroutine.
Definition: coroutine.hh:286
m5::Coroutine::CallerType
CallerType: A reference to an object of this class will be passed to the coroutine task.
Definition: coroutine.hh:83
Fiber::finished
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:100
type
uint8_t type
Definition: inet.hh:421
m5::Coroutine::ArgChannel
typename std::conditional< std::is_same< Arg, void >::value, Empty, std::stack< Arg > >::type ArgChannel
Definition: coroutine.hh:70
m5::Coroutine::Coroutine
Coroutine()=delete
m5::Coroutine::CallerType::callerFiber
Fiber * callerFiber
Definition: coroutine.hh:154
m5
Definition: compiler.hh:90
m5::Coroutine::operator()
Coroutine & operator()(typename std::enable_if< !std::is_same< T, void >::value, T >::type param)
Coroutine interface.
Definition: coroutine.hh:211
m5::Coroutine::Empty
Definition: coroutine.hh:68
m5::Coroutine::operator()
std::enable_if< std::is_same< T, void >::value, Coroutine >::type & operator()()
operator() is the way we can jump inside the coroutine.
Definition: coroutine.hh:229
m5::Coroutine::get
std::enable_if<!std::is_same< T, void >::value, T >::type get()
get() is the way we can extrapolate return values (yielded) from the coroutine.
Definition: coroutine.hh:249
Fiber
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition: fiber.hh:62
Fiber::currentFiber
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
Definition: fiber.cc:181
m5::Coroutine::operator=
Coroutine & operator=(const Coroutine &rhs)=delete
Fiber::run
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:163
m5::Coroutine
This template defines a Coroutine wrapper type with a Boost-like interface.
Definition: coroutine.hh:62
m5::Coroutine::RetChannel
typename std::conditional< std::is_same< Ret, void >::value, Empty, std::stack< Ret > >::type RetChannel
Definition: coroutine.hh:73
m5::Coroutine::call
void call()
Definition: coroutine.hh:278
m5::Coroutine::CallerType::CallerType
CallerType(Coroutine &_coro)
Definition: coroutine.hh:87
m5::Coroutine::main
void main() override
Overriding base (Fiber) main.
Definition: coroutine.hh:275
m5::Coroutine::CallerType::operator()
std::enable_if< std::is_same< T, void >::value, CallerType >::type & operator()()
operator() is the way we can jump outside the coroutine
Definition: coroutine.hh:120
m5::Coroutine::CallerType::retChannel
RetChannel retChannel
Definition: coroutine.hh:155
m5::Coroutine::caller
CallerType caller
Coroutine caller.
Definition: coroutine.hh:292
m5::Coroutine::CallerType::operator()
CallerType & operator()(typename std::enable_if< !std::is_same< T, void >::value, T >::type param)
operator() is the way we can jump outside the coroutine and return a value to the caller.
Definition: coroutine.hh:101
m5::Coroutine::~Coroutine
virtual ~Coroutine()
Definition: coroutine.hh:195
fiber.hh
m5::Coroutine::Coroutine
Coroutine(std::function< void(CallerType &)> f, bool run_coroutine=true)
Coroutine constructor.
Definition: coroutine.hh:184
m5::Coroutine::CallerType::coro
Coroutine & coro
Definition: coroutine.hh:153
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17