forked from Maelic/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpysession.cpp
More file actions
78 lines (64 loc) · 1.72 KB
/
pysession.cpp
File metadata and controls
78 lines (64 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
** Copyright (C) 2020 SoftBank Robotics Europe
** See COPYING for the license
*/
#include <qipython/pysession.hpp>
#include <qipython/common.hpp>
#include <qipython/pyguard.hpp>
#include <qipython/pytypes.hpp>
#include <qipython/pyobject.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <pybind11/operators.h>
#include <thread>
#include <future>
namespace py = pybind11;
namespace qi
{
namespace py
{
namespace
{
::py::object makeSession()
{
// Ensures that the Session is deleted after the shared_pointer on it is deleted.
// This is to mimic the behavior of qiApplication.
// This is a fix for #38167.
struct Deleter
{
boost::weak_ptr<Session> wptr;
void operator()(Session* p) const
{
constexpr const auto category = "qi.python.session.deleter";
constexpr const auto msg = "Waiting for the shared pointer destruction...";
GILRelease x;
auto fut = std::async(std::launch::async, [=](std::unique_ptr<Session>) {
while (!wptr.expired())
{
qiLogDebug(category) << msg;
constexpr const std::chrono::milliseconds delay(100);
std::this_thread::sleep_for(delay);
}
qiLogDebug(category) << msg << " done.";
}, std::unique_ptr<Session>(p));
QI_IGNORE_UNUSED(fut);
}
};
auto ptr = SessionPtr(new qi::Session{}, Deleter{});
boost::get_deleter<Deleter>(ptr)->wptr = ka::weak_ptr(ptr);
GILAcquire lock;
return py::makeSession(ptr);
}
} // namespace
::py::object makeSession(SessionPtr sess)
{
GILAcquire lock;
return toPyObject(sess);
}
void exportSession(::py::module& m)
{
using namespace ::py;
GILAcquire lock;
m.def("Session", []{ return makeSession(); });
}
} // namespace py
} // namespace qi