gem5  v20.1.0.0
fiber.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __BASE_FIBER_HH__
29 #define __BASE_FIBER_HH__
30 
31 // ucontext functions (like getcontext, setcontext etc) have been marked
32 // as deprecated and are hence hidden in latest macOS releases.
33 // By defining _XOPEN_SOURCE we make them available at compilation time.
34 #if defined(__APPLE__) && defined(__MACH__)
35 #define _XOPEN_SOURCE 600
36 #include <ucontext.h>
37 #undef _XOPEN_SOURCE
38 #else
39 #include <ucontext.h>
40 #endif
41 
42 #include <cstddef>
43 #include <cstdint>
44 
45 #include "config/have_valgrind.hh"
46 
62 class Fiber
63 {
64  public:
68  const static size_t DefaultStackSize = 0x50000;
69 
78  Fiber(size_t stack_size=DefaultStackSize);
79  Fiber(Fiber *link, size_t stack_size=DefaultStackSize); // end of api_fiber
81 
85  virtual ~Fiber();
86 
93  void run();
94 
100  bool finished() const { return _finished; };
101 
107  bool started() const { return _started; };
108 
114  static Fiber *currentFiber();
115 
123  static Fiber *primaryFiber();
124 
125  protected:
131  virtual void main() = 0;
132 
133  void setStarted() { _started = true; }
134 
135  private:
136  static void entryTrampoline();
137  void start();
138 
139  ucontext_t ctx;
141 
142  // The stack for this context, or a nullptr if allocated elsewhere.
143  void *stack;
144  size_t stackSize;
145  void *guardPage;
147 #if HAVE_VALGRIND
148  unsigned valgrindStackId;
149 #endif
150 
151  bool _started;
152  bool _finished;
153  void createContext();
154 };
155 
156 #endif // __BASE_FIBER_HH__
Fiber::Fiber
Fiber(size_t stack_size=DefaultStackSize)
Definition: fiber.cc:84
Fiber::start
void start()
Definition: fiber.cc:141
Fiber::stack
void * stack
Definition: fiber.hh:143
Fiber::finished
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:100
Fiber::ctx
ucontext_t ctx
Definition: fiber.hh:139
Fiber::createContext
void createContext()
Definition: fiber.cc:122
Fiber::primaryFiber
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition: fiber.cc:182
Fiber::DefaultStackSize
const static size_t DefaultStackSize
Definition: fiber.hh:68
Fiber::entryTrampoline
static void entryTrampoline()
Definition: fiber.cc:79
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
Fiber::_started
bool _started
Definition: fiber.hh:151
Fiber::run
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:163
Fiber::guardPageSize
size_t guardPageSize
Definition: fiber.hh:146
Fiber::~Fiber
virtual ~Fiber()
Definition: fiber.cc:111
Fiber::started
bool started() const
Returns whether the "main" function of this fiber has started.
Definition: fiber.hh:107
Fiber::stackSize
size_t stackSize
Definition: fiber.hh:144
Fiber::main
virtual void main()=0
This method is called when this fiber is first run.
Fiber::_finished
bool _finished
Definition: fiber.hh:152
Fiber::guardPage
void * guardPage
Definition: fiber.hh:145
Fiber::link
Fiber * link
Definition: fiber.hh:140
Fiber::setStarted
void setStarted()
Definition: fiber.hh:133

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