gem5  v22.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 // Avoid fortify source for longjmp to work between ucontext stacks.
43 #pragma push_macro("__USE_FORTIFY_LEVEL")
44 #undef __USE_FORTIFY_LEVEL
45 #include <setjmp.h>
46 #pragma pop_macro("__USE_FORTIFY_LEVEL")
47 
48 #include <cstddef>
49 #include <cstdint>
50 
51 #include "config/have_valgrind.hh"
52 
53 namespace gem5
54 {
55 
71 class Fiber
72 {
73  public:
77  const static size_t DefaultStackSize = 0x50000;
78 
87  Fiber(size_t stack_size=DefaultStackSize);
88  Fiber(Fiber *link, size_t stack_size=DefaultStackSize); // end of api_fiber
90 
94  virtual ~Fiber();
95 
102  void run();
103 
109  bool finished() const { return _finished; };
110 
116  bool started() const { return _started; };
117 
123  static Fiber *currentFiber();
124 
132  static Fiber *primaryFiber();
133 
134  protected:
140  virtual void main() = 0;
141 
142  void setStarted() { _started = true; }
143 
144  private:
145  static void entryTrampoline();
146  void start();
147 
148  ucontext_t ctx;
149  // ucontext is slow in swapcontext. Here we use _setjmp/_longjmp to avoid
150  // the additional signals for speed up.
151  jmp_buf jmp;
152 
154 
155  // The stack for this context, or a nullptr if allocated elsewhere.
156  void *stack;
157  size_t stackSize;
158  void *guardPage;
160 #if HAVE_VALGRIND
161  unsigned valgrindStackId;
162 #endif
163 
164  bool _started;
165  bool _finished;
166  void createContext();
167 };
168 
169 } // namespace gem5
170 
171 #endif // __BASE_FIBER_HH__
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition: fiber.hh:72
void * guardPage
Definition: fiber.hh:158
size_t guardPageSize
Definition: fiber.hh:159
bool _started
Definition: fiber.hh:164
jmp_buf jmp
Definition: fiber.hh:151
size_t stackSize
Definition: fiber.hh:157
virtual void main()=0
This method is called when this fiber is first run.
static void entryTrampoline()
Definition: fiber.cc:80
void * stack
Definition: fiber.hh:156
Fiber * link
Definition: fiber.hh:153
void createContext()
Definition: fiber.cc:123
void setStarted()
Definition: fiber.hh:142
bool _finished
Definition: fiber.hh:165
void start()
Definition: fiber.cc:142
ucontext_t ctx
Definition: fiber.hh:148
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
Definition: fiber.cc:185
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition: fiber.cc:186
bool started() const
Returns whether the "main" function of this fiber has started.
Definition: fiber.hh:116
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:109
Fiber(size_t stack_size=DefaultStackSize)
Definition: fiber.cc:85
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:166
static const size_t DefaultStackSize
Definition: fiber.hh:77
virtual ~Fiber()
Definition: fiber.cc:112
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....

Generated on Wed Dec 21 2022 10:22:28 for gem5 by doxygen 1.9.1