rogue
Loading...
Searching...
No Matches
Slave.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <memory>
22#include <string>
23#include <utility>
24
25#include "rogue/GeneralError.h"
26#include "rogue/GilRelease.h"
27#include "rogue/ScopedGil.h"
31
32#ifndef NO_PYTHON
33 #include <boost/python.hpp>
34namespace bp = boost::python;
35#endif
36
38
39// Init class counter
40uint32_t rim::Slave::classIdx_ = 0;
41
43std::mutex rim::Slave::classMtx_;
44
46rim::SlavePtr rim::Slave::create(uint32_t min, uint32_t max) {
47 rim::SlavePtr s = std::make_shared<rim::Slave>(min, max);
48 return (s);
49}
50
52rim::Slave::Slave(uint32_t min, uint32_t max) {
53 min_ = min;
54 max_ = max;
55
56 classMtx_.lock();
57 if (classIdx_ == 0) classIdx_ = 1;
58 id_ = classIdx_;
59 classIdx_++;
60 classMtx_.unlock();
61
62 name_ = std::string("Unnamed_") + std::to_string(id_);
63}
64
66rim::Slave::~Slave() {}
67
69void rim::Slave::stop() {}
70
72void rim::Slave::addTransaction(rim::TransactionPtr tran) {
74 std::lock_guard<std::mutex> lock(slaveMtx_);
75 tranMap_[tran->id()] = tran;
76}
77
79rim::TransactionPtr rim::Slave::getTransaction(uint32_t index) {
81 rim::TransactionPtr expired;
82 TransactionMap::iterator it;
83 TransactionMap::iterator exp;
84
85 {
87 std::lock_guard<std::mutex> lock(slaveMtx_);
88
89 if ((it = tranMap_.find(index)) != tranMap_.end()) {
90 ret = it->second;
91
92 // Remove from list
93 tranMap_.erase(it);
94
95 // Update any timers for transactions started after received transaction
96 exp = tranMap_.end();
97 for (it = tranMap_.begin(); it != tranMap_.end(); ++it) {
98 if (it->second->expired())
99 exp = it;
100 else
101 it->second->refreshTimer(ret);
102 }
103
104 // Clean up if we found an expired transaction, overtime this will clean up
105 // the list, even if it deletes one expired transaction per call.
106 //
107 // Moved out rather than erased in place: destroying it inside the
108 // GilRelease above would drop a Python-owned transaction with the GIL
109 // released. See the release below.
110 if (exp != tranMap_.end()) {
111 expired = std::move(exp->second);
112 tranMap_.erase(exp);
113 }
114 }
115 }
116
117 // A Python Slave or Hub that forwards a transaction back into C++ via
118 // super()._doTransaction() re-enters through the from-python shared_ptr
119 // converter, which attaches a Boost.Python deleter that calls Py_DECREF without
120 // the GIL. bp::no_init does not prevent this: it only suppresses __init__, not
121 // the converter. Released outside slaveMtx_ so the GIL is never acquired while
122 // holding it.
123#ifndef NO_PYTHON
124 if (expired && Py_IsInitialized()) {
126 expired.reset();
127 }
128#endif
129
130 return ret;
131}
132
134uint32_t rim::Slave::min() {
135 return min_;
136}
137
139uint32_t rim::Slave::max() {
140 return max_;
141}
142
144uint32_t rim::Slave::id() {
145 return id_;
146}
147
149std::string rim::Slave::name() {
150 return name_;
151}
152
154void rim::Slave::setName(std::string name) {
155 name_ = name;
156}
157
159uint32_t rim::Slave::doSlaveId() {
160 return (id());
161}
162
164std::string rim::Slave::doSlaveName() {
165 return (name());
166}
167
169uint32_t rim::Slave::doMinAccess() {
170 return (min());
171}
172
174uint32_t rim::Slave::doMaxAccess() {
175 return (max());
176}
177
179uint64_t rim::Slave::doAddress() {
180 return (0);
181}
182
184void rim::Slave::doTransaction(rim::TransactionPtr transaction) {
185 transaction->error("Unsupported transaction using unconnected memory bus");
186}
187
188void rim::Slave::setup_python() {
189#ifndef NO_PYTHON
190 bp::class_<rim::SlaveWrap, rim::SlaveWrapPtr, boost::noncopyable>("Slave", bp::init<uint32_t, uint32_t>())
191 .def("setName", &rim::Slave::setName)
192 .def("_addTransaction", &rim::Slave::addTransaction)
193 .def("_getTransaction", &rim::Slave::getTransaction)
194 .def("_doMinAccess", &rim::Slave::doMinAccess, &rim::SlaveWrap::defDoMinAccess)
195 .def("_doMaxAccess", &rim::Slave::doMaxAccess, &rim::SlaveWrap::defDoMaxAccess)
196 .def("_doAddress", &rim::Slave::doAddress, &rim::SlaveWrap::defDoAddress)
197 .def("_doTransaction", &rim::Slave::doTransaction, &rim::SlaveWrap::defDoTransaction)
198 .def("__lshift__", &rim::Slave::lshiftPy)
199 .def("_stop", &rim::Slave::stop);
200#endif
201}
202
203#ifndef NO_PYTHON
204
206rim::SlaveWrap::SlaveWrap(uint32_t min, uint32_t max) : rim::Slave(min, max) {}
207
209uint32_t rim::SlaveWrap::doMinAccess() {
210 // A C++ worker can still deliver a transaction while the interpreter
211 // shuts down; PyGILState_Ensure() aborts once finalization has started.
212 if (Py_IsInitialized()) {
214
215 if (boost::python::override pb = this->get_override("_doMinAccess")) {
216 try {
217 return (pb());
218 } catch (...) {
219 PyErr_Print();
220 }
221 }
222 }
223 return (rim::Slave::doMinAccess());
224}
225
227uint32_t rim::SlaveWrap::defDoMinAccess() {
228 return (rim::Slave::doMinAccess());
229}
230
232uint32_t rim::SlaveWrap::doMaxAccess() {
233 // A C++ worker can still deliver a transaction while the interpreter
234 // shuts down; PyGILState_Ensure() aborts once finalization has started.
235 if (Py_IsInitialized()) {
237
238 if (boost::python::override pb = this->get_override("_doMaxAccess")) {
239 try {
240 return (pb());
241 } catch (...) {
242 PyErr_Print();
243 }
244 }
245 }
246 return (rim::Slave::doMaxAccess());
247}
248
250uint32_t rim::SlaveWrap::defDoMaxAccess() {
251 return (rim::Slave::doMaxAccess());
252}
253
255uint64_t rim::SlaveWrap::doAddress() {
256 // A C++ worker can still deliver a transaction while the interpreter
257 // shuts down; PyGILState_Ensure() aborts once finalization has started.
258 if (Py_IsInitialized()) {
260
261 if (boost::python::override pb = this->get_override("_doAddress")) {
262 try {
263 return (pb());
264 } catch (...) {
265 PyErr_Print();
266 }
267 }
268 }
269 return (rim::Slave::doAddress());
270}
271
273uint64_t rim::SlaveWrap::defDoAddress() {
274 return (rim::Slave::doAddress());
275}
276
278void rim::SlaveWrap::doTransaction(rim::TransactionPtr transaction) {
279 // A C++ worker can still deliver a transaction while the interpreter
280 // shuts down; PyGILState_Ensure() aborts once finalization has started.
281 if (Py_IsInitialized()) {
283
284 if (boost::python::override pb = this->get_override("_doTransaction")) {
285 try {
286 pb(transaction);
287 return;
288 } catch (...) {
289 PyErr_Print();
290 }
291 }
292 }
293 rim::Slave::doTransaction(transaction);
294}
295
297void rim::SlaveWrap::defDoTransaction(rim::TransactionPtr transaction) {
298 rim::Slave::doTransaction(transaction);
299}
300
301void rim::Slave::lshiftPy(boost::python::object p) {
302 rim::MasterPtr mst;
303
304 // First Attempt to access object as a memory master
305 boost::python::extract<rim::MasterPtr> get_master(p);
306
307 // Test extraction
308 if (get_master.check()) {
309 mst = get_master();
310
311 // Otherwise look for indirect call
312 } else if (PyObject_HasAttrString(p.ptr(), "_getMemoryMaster")) {
313 // Attempt to convert returned object to master pointer
314 boost::python::extract<rim::MasterPtr> get_master(p.attr("_getMemoryMaster")());
315
316 // Test extraction
317 if (get_master.check()) mst = get_master();
318 }
319
320 // Success
321 if (mst != NULL)
322 mst->setSlave(shared_from_this());
323 else
324 throw(rogue::GeneralError::create("memory::Slave::lshiftPy",
325 "Attempt to use << operator with incompatible memory master"));
326}
327
328#endif
329
331rim::MasterPtr& rim::Slave::operator<<(rim::MasterPtr& other) {
332 other->setSlave(shared_from_this());
333 return other;
334}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
Memory slave device.
Definition Slave.h:54
uint32_t max()
Returns configured maximum transaction size.
Definition Slave.cpp:139
uint32_t min()
Returns configured minimum transaction size.
Definition Slave.cpp:134
std::shared_ptr< rogue::interfaces::memory::Slave > SlavePtr
Shared pointer alias for Slave.
Definition Slave.h:310
std::shared_ptr< rogue::interfaces::memory::Master > MasterPtr
Shared pointer alias for Master.
Definition Master.h:410
std::shared_ptr< rogue::interfaces::memory::Transaction > TransactionPtr
Shared pointer alias for Transaction.