gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
53namespace py = pybind11;
54
55namespace gem5
56{
57
58pybind11::module_ *EmbeddedPyBind::mod = nullptr;
59
60EmbeddedPyBind::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
68EmbeddedPyBind::EmbeddedPyBind(const char *_name,
69 void (*init_func)(py::module_ &)) :
70 EmbeddedPyBind(_name, init_func, "")
71{}
72
73void
74EmbeddedPyBind::init()
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
100void
101EmbeddedPyBind::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
114std::map<std::string, EmbeddedPyBind *> &
115EmbeddedPyBind::getReady()
116{
117 static std::map<std::string, EmbeddedPyBind *> ready;
118 return ready;
119}
120
121std::multimap<std::string, EmbeddedPyBind *> &
122EmbeddedPyBind::getPending()
123{
124 static std::multimap<std::string, EmbeddedPyBind *> pending;
125 return pending;
126}
127
128void
129EmbeddedPyBind::initAll(py::module_ &_m5)
130{
131 pybind_init_core(_m5);
133
136
137 mod = &_m5;
138
139 // Init all the modules that were waiting on the _m5 module itself.
140 initPending("");
141}
142
143GEM5_PYBIND_MODULE_INIT(_m5, EmbeddedPyBind::initAll)
144
145} // namespace gem5
static pybind11::module_ * mod
Definition init.hh:75
EmbeddedPyBind(const char *_name, void(*init_func)(pybind11::module_ &), const char *_base)
STL list class.
Definition stl.hh:51
Bitfield< 51, 12 > base
Definition pagetable.hh:141
const FlagsType init
This Stat is Initialized.
Definition info.hh:55
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
void pybind_init_debug(py::module_ &m_native)
Definition debug.cc:87
void pybind_init_event(py::module_ &m_native)
Definition event.cc:104
void cprintf(const char *format, const Args &...args)
Definition cprintf.hh:155
void pybind_init_core(py::module_ &m_native)
Definition core.cc:241
void pybind_init_stats(pybind11::module_ &m_native)
#define GEM5_PYBIND_MODULE_INIT(name, func)
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0