Deleted Added
sdiff udiff text old ( 11988:665cd5f8b52b ) new ( 12041:52b3b120dbc0 )
full compact
1/*
2 * Copyright (c) 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

--- 32 unchanged lines hidden (view full) ---

41 *
42 * Authors: Nathan Binkert
43 * Andreas Sandberg
44 */
45
46#include "pybind11/pybind11.h"
47#include "pybind11/stl.h"
48
49#include "base/misc.hh"
50#include "sim/eventq.hh"
51#include "sim/sim_events.hh"
52#include "sim/sim_exit.hh"
53#include "sim/simulate.hh"
54
55namespace py = pybind11;
56
57
58/**
59 * PyBind wrapper for Events
60 *
61 * We need to wrap the Event class with some Python glue code to
62 * enable method overrides in Python and memory management. Unlike its
63 * C++ cousin, PyEvents need to override __call__ instead of
64 * Event::process().
65 *
66 * Memory management is done using reference counting in Python.
67 */
68class PyEvent : public Event
69{
70 public:
71 PyEvent(Event::Priority priority)
72 : Event(priority, Event::Managed)
73 {
74 }
75
76 void process() override {
77 // Call the Python implementation as __call__. This provides a
78 // slightly more Python-friendly interface.
79 PYBIND11_OVERLOAD_PURE_NAME(void, PyEvent, "__call__", process);
80 }
81
82 protected:
83 void acquireImpl() override {
84 py::object obj = py::cast(this);
85
86 if (obj) {
87 obj.inc_ref();
88 } else {
89 panic("Failed to get PyBind object to increase ref count\n");
90 }
91 }
92
93 void releaseImpl() override {
94 py::object obj = py::cast(this);
95
96 if (obj) {
97 obj.dec_ref();
98 } else {
99 panic("Failed to get PyBind object to decrease ref count\n");
100 }
101 }
102};
103
104void
105pybind_init_event(py::module &m_native)
106{
107 py::module m = m_native.def_submodule("event");
108
109 m.def("simulate", &simulate,

--- 6 unchanged lines hidden (view full) ---

116 py::return_value_policy::reference);
117
118 py::class_<EventQueue>(m, "EventQueue")
119 .def("name", [](EventQueue *eq) { return eq->name(); })
120 .def("dump", &EventQueue::dump)
121 .def("schedule", [](EventQueue *eq, PyEvent *e, Tick t) {
122 eq->schedule(e, t);
123 }, py::arg("event"), py::arg("when"))
124 .def("deschedule", &EventQueue::deschedule,
125 py::arg("event"))
126 .def("reschedule", &EventQueue::reschedule,
127 py::arg("event"), py::arg("tick"), py::arg("always") = false)
128 ;
129
130 // TODO: Ownership of global exit events has always been a bit
131 // questionable. We currently assume they are owned by the C++
132 // world. This is what the old SWIG code did, but that will result
133 // in memory leaks.
134 py::class_<GlobalSimLoopExitEvent,
135 std::unique_ptr<GlobalSimLoopExitEvent, py::nodelete>>(
136 m, "GlobalSimLoopExitEvent")
137 .def("getCause", &GlobalSimLoopExitEvent::getCause)
138 .def("getCode", &GlobalSimLoopExitEvent::getCode)
139 ;
140
141 // Event base class. These should never be returned directly to
142 // Python since they don't have a well-defined life cycle. Python
143 // events should be derived from PyEvent instead.
144 py::class_<Event> c_event(
145 m, "Event");
146 c_event
147 .def("name", &Event::name)
148 .def("dump", &Event::dump)
149 .def("scheduled", &Event::scheduled)
150 .def("squash", &Event::squash)
151 .def("squashed", &Event::squashed)
152 .def("isExitEvent", &Event::isExitEvent)
153 .def("when", &Event::when)
154 .def("priority", &Event::priority)
155 ;
156
157 py::class_<PyEvent, Event>(m, "PyEvent")
158 .def(py::init<Event::Priority>(),
159 py::arg("priority") = (int)Event::Default_Pri)
160 ;
161
162#define PRIO(n) c_event.attr(# n) = py::cast((int)Event::n)
163 PRIO(Minimum_Pri);
164 PRIO(Minimum_Pri);
165 PRIO(Debug_Enable_Pri);
166 PRIO(Debug_Break_Pri);
167 PRIO(CPU_Switch_Pri);
168 PRIO(Delayed_Writeback_Pri);
169 PRIO(Default_Pri);
170 PRIO(DVFS_Update_Pri);
171 PRIO(Serialize_Pri);
172 PRIO(CPU_Tick_Pri);
173 PRIO(Stat_Event_Pri);
174 PRIO(Progress_Event_Pri);
175 PRIO(Sim_Exit_Pri);
176 PRIO(Maximum_Pri);
177}