gem5  v22.0.0.2
init.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2017 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  * Copyright (c) 2000-2005 The Regents of The University of Michigan
15  * Copyright (c) 2008 The Hewlett-Packard Development Company
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "pybind11/embed.h"
43 
44 #include "sim/init.hh"
45 
46 #include <map>
47 #include <string>
48 
49 #include "base/cprintf.hh"
51 #include "python/pybind_init.hh"
52 
53 namespace py = pybind11;
54 
55 namespace gem5
56 {
57 
58 pybind11::module_ *EmbeddedPyBind::mod = nullptr;
59 
60 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
61  void (*init_func)(py::module_ &),
62  const char *_base) :
63  initFunc(init_func), name(_name), base(_base)
64 {
65  init();
66 }
67 
68 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
69  void (*init_func)(py::module_ &)) :
70  EmbeddedPyBind(_name, init_func, "")
71 {}
72 
73 void
75 {
76  // If this module is already registered, complain and stop.
77  if (registered) {
78  cprintf("Warning: %s already registered.\n", name);
79  return;
80  }
81 
82  auto &ready = getReady();
83  auto &pending = getPending();
84 
85  // If we're not ready for this module yet, defer intialization.
86  if (!mod || (!base.empty() && ready.find(base) == ready.end())) {
87  pending.insert({std::string(base), this});
88  return;
89  }
90 
91  // We must be ready, so set this module up.
92  initFunc(*mod);
93  ready[name] = this;
94  registered = true;
95 
96  // Find any other modules that were waiting for this one and init them.
97  initPending(name);
98 }
99 
100 void
101 EmbeddedPyBind::initPending(const std::string &finished)
102 {
103  auto &pending = getPending();
104 
105  auto range = pending.equal_range(finished);
107  range.first, range.second);
108  pending.erase(range.first, range.second);
109 
110  for (auto &entry: todo)
111  entry.second->init();
112 }
113 
114 std::map<std::string, EmbeddedPyBind *> &
116 {
117  static std::map<std::string, EmbeddedPyBind *> ready;
118  return ready;
119 }
120 
121 std::multimap<std::string, EmbeddedPyBind *> &
123 {
124  static std::multimap<std::string, EmbeddedPyBind *> pending;
125  return pending;
126 }
127 
128 void
129 EmbeddedPyBind::initAll(py::module_ &_m5)
130 {
131  pybind_init_core(_m5);
132  pybind_init_debug(_m5);
133 
134  pybind_init_event(_m5);
135  pybind_init_stats(_m5);
136 
137  mod = &_m5;
138 
139  // Init all the modules that were waiting on the _m5 module itself.
140  initPending("");
141 }
142 
144 
145 } // namespace gem5
gem5::EmbeddedPyBind::initPending
static void initPending(const std::string &finished)
Definition: init.cc:101
gem5::cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
gem5::pybind_init_event
void pybind_init_event(py::module_ &m_native)
Definition: event.cc:104
gem5::pybind_init_debug
void pybind_init_debug(py::module_ &m_native)
Definition: debug.cc:79
gem5::X86ISA::mod
mod
Definition: types.hh:91
gem5::pybind_init_stats
void pybind_init_stats(pybind11::module_ &m_native)
gem5::EmbeddedPyBind::getPending
static std::multimap< std::string, EmbeddedPyBind * > & getPending()
Definition: init.cc:122
gem5::X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
gem5::pybind_init_core
void pybind_init_core(py::module_ &m_native)
Definition: core.cc:201
pybind.hh
pybind_init.hh
GEM5_PYBIND_MODULE_INIT
#define GEM5_PYBIND_MODULE_INIT(name, func)
Definition: pybind_init.hh:56
gem5::EmbeddedPyBind::EmbeddedPyBind
EmbeddedPyBind(const char *_name, void(*init_func)(pybind11::module_ &), const char *_base)
cprintf.hh
gem5::EmbeddedPyBind::init
void init()
Definition: init.cc:74
name
const std::string & name()
Definition: trace.cc:49
gem5::EmbeddedPyBind::mod
static pybind11::module_ * mod
Definition: init.hh:75
gem5::EmbeddedPyBind::getReady
static std::map< std::string, EmbeddedPyBind * > & getReady()
Definition: init.cc:115
gem5::EmbeddedPyBind::initAll
static void initAll(pybind11::module_ &_m5)
Definition: init.cc:129
gem5::statistics::init
const FlagsType init
This Stat is Initialized.
Definition: info.hh:56
std::list
STL list class.
Definition: stl.hh:51
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
init.hh

Generated on Thu Jul 28 2022 13:32:36 for gem5 by doxygen 1.8.17