gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Gabe Black
28  */
29 
30 #ifndef __BASE_FIBER_HH__
31 #define __BASE_FIBER_HH__
32 
33 // ucontext functions (like getcontext, setcontext etc) have been marked
34 // as deprecated and are hence hidden in latest macOS releases.
35 // By defining _XOPEN_SOURCE we make them available at compilation time.
36 #if defined(__APPLE__) && defined(__MACH__)
37 #define _XOPEN_SOURCE 600
38 #include <ucontext.h>
39 #undef _XOPEN_SOURCE
40 #else
41 #include <ucontext.h>
42 #endif
43 
44 #include <cstddef>
45 #include <cstdint>
46 
47 #include "config/have_valgrind.hh"
48 
64 class Fiber
65 {
66  public:
67  const static size_t DefaultStackSize = 0x50000;
68 
72  Fiber(size_t stack_size=DefaultStackSize);
73  Fiber(Fiber *link, size_t stack_size=DefaultStackSize);
74 
75  virtual ~Fiber();
76 
79  void run();
80 
83  bool finished() const { return _finished; };
84 
87  bool started() const { return _started; };
88 
91  static Fiber *currentFiber();
95  static Fiber *primaryFiber();
96 
97  protected:
101  virtual void main() = 0;
102 
103  void setStarted() { _started = true; }
104 
105  private:
106  static void entryTrampoline();
107  void start();
108 
109  ucontext_t ctx;
111 
112  // The stack for this context, or a nullptr if allocated elsewhere.
113  void *stack;
114  size_t stackSize;
115  void *guardPage;
117 #if HAVE_VALGRIND
118  unsigned valgrindStackId;
119 #endif
120 
121  bool _started;
122  bool _finished;
123  void createContext();
124 };
125 
126 #endif // __BASE_FIBER_HH__
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:165
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:83
void * guardPage
Definition: fiber.hh:115
void createContext()
Definition: fiber.cc:124
ucontext_t ctx
Definition: fiber.hh:109
void start()
Definition: fiber.cc:143
static void entryTrampoline()
Definition: fiber.cc:81
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition: fiber.cc:184
void * stack
Definition: fiber.hh:113
bool _started
Definition: fiber.hh:121
static const size_t DefaultStackSize
Definition: fiber.hh:67
size_t guardPageSize
Definition: fiber.hh:116
size_t stackSize
Definition: fiber.hh:114
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition: fiber.hh:64
void setStarted()
Definition: fiber.hh:103
bool started() const
Returns whether the "main" function of this fiber has started.
Definition: fiber.hh:87
Fiber(size_t stack_size=DefaultStackSize)
stack_size is the size of the stack available to this fiber.
Definition: fiber.cc:86
bool _finished
Definition: fiber.hh:122
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
Definition: fiber.cc:183
virtual void main()=0
This method is called when this fiber is first run.
Fiber * link
Definition: fiber.hh:110
virtual ~Fiber()
Definition: fiber.cc:113

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13