Fix race condition on ipc_server init
This commit is contained in:
@@ -26,7 +26,6 @@
|
||||
#include "main/comp_main_interface.h"
|
||||
#include "main/comp_target.h"
|
||||
#include "server/ipc_server.h"
|
||||
#include "target_instance_wivrn.h"
|
||||
#include "util/u_builders.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "util/u_system.h"
|
||||
@@ -141,7 +140,7 @@ bool wivrn::tracking_control_t::set_enabled(to_headset::tracking_control::id id,
|
||||
return changed;
|
||||
}
|
||||
|
||||
wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection, instance & inst, u_system & system) :
|
||||
wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection, u_system & system) :
|
||||
xrt_system_devices{
|
||||
.get_roles = [](xrt_system_devices * self, xrt_system_roles * out_roles) { return ((wivrn_session *)self)->get_roles(out_roles); },
|
||||
.feature_inc = [](xrt_system_devices * self, xrt_device_feature_type f) { return ((wivrn_session *)self)->feature_inc(f); },
|
||||
@@ -149,7 +148,6 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
|
||||
.destroy = [](xrt_system_devices * self) { delete ((wivrn_session *)self); },
|
||||
},
|
||||
connection(std::move(connection)),
|
||||
inst(inst),
|
||||
xrt_system(system),
|
||||
hmd(this, get_info()),
|
||||
left_controller(0, &hmd, this),
|
||||
@@ -308,7 +306,6 @@ wivrn_session::~wivrn_session()
|
||||
}
|
||||
|
||||
xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connection> connection,
|
||||
instance & inst,
|
||||
u_system & system,
|
||||
xrt_system_devices ** out_xsysd,
|
||||
xrt_space_overseer ** out_xspovrs,
|
||||
@@ -317,7 +314,7 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
|
||||
std::unique_ptr<wivrn_session> self;
|
||||
try
|
||||
{
|
||||
self.reset(new wivrn_session(std::move(connection), inst, system));
|
||||
self.reset(new wivrn_session(std::move(connection), system));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
@@ -355,11 +352,22 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
|
||||
self->feedback_csv.open(dump_file);
|
||||
}
|
||||
|
||||
self->thread = std::jthread([s = self.get()](auto stop_token) { return s->run(stop_token); });
|
||||
*out_xsysd = self.release();
|
||||
return XRT_SUCCESS;
|
||||
}
|
||||
|
||||
void wivrn_session::start(ipc_server * server)
|
||||
{
|
||||
assert(not thread);
|
||||
mnd_ipc_server = server;
|
||||
thread = std::jthread([this](auto stop_token) { return run(stop_token); });
|
||||
}
|
||||
|
||||
void wivrn_session::stop()
|
||||
{
|
||||
thread = std::jthread();
|
||||
}
|
||||
|
||||
clock_offset wivrn_session::get_offset()
|
||||
{
|
||||
return offset_est.get_offset();
|
||||
@@ -671,6 +679,7 @@ void wivrn_session::operator()(from_headset::visibility_mask_changed && mask)
|
||||
|
||||
void wivrn_session::operator()(from_headset::session_state_changed && event)
|
||||
{
|
||||
assert(server);
|
||||
U_LOG_I("Session state changed: %s", xr::to_string(event.state));
|
||||
bool visible, focused;
|
||||
switch (event.state)
|
||||
@@ -688,13 +697,13 @@ void wivrn_session::operator()(from_headset::session_state_changed && event)
|
||||
focused = false;
|
||||
break;
|
||||
}
|
||||
scoped_lock lock(inst.server->global_state.lock);
|
||||
for (auto & t: inst.server->threads)
|
||||
scoped_lock lock(mnd_ipc_server->global_state.lock);
|
||||
for (auto & t: mnd_ipc_server->threads)
|
||||
{
|
||||
if (t.ics.server_thread_index < 0 or t.ics.xc == nullptr)
|
||||
continue;
|
||||
bool current = t.ics.client_state.session_overlay or
|
||||
inst.server->global_state.active_client_index == t.ics.server_thread_index;
|
||||
mnd_ipc_server->global_state.active_client_index == t.ics.server_thread_index;
|
||||
xrt_syscomp_set_state(system_compositor, t.ics.xc, visible and current, focused and current);
|
||||
}
|
||||
}
|
||||
@@ -785,9 +794,10 @@ void wivrn_session::operator()(const from_headset::start_app & request)
|
||||
|
||||
void wivrn_session::operator()(const from_headset::get_running_applications &)
|
||||
{
|
||||
scoped_lock lock(inst.server->global_state.lock);
|
||||
assert(server);
|
||||
scoped_lock lock(mnd_ipc_server->global_state.lock);
|
||||
to_headset::running_applications msg{};
|
||||
for (auto & t: inst.server->threads)
|
||||
for (auto & t: mnd_ipc_server->threads)
|
||||
{
|
||||
if (t.ics.server_thread_index < 0 or t.ics.xc == nullptr)
|
||||
continue;
|
||||
@@ -801,7 +811,7 @@ void wivrn_session::operator()(const from_headset::get_running_applications &)
|
||||
.name = std::string(tmp.data()),
|
||||
.id = t.ics.client_state.id,
|
||||
.overlay = t.ics.client_state.session_overlay,
|
||||
.active = t.ics.server_thread_index == inst.server->global_state.active_client_index,
|
||||
.active = t.ics.server_thread_index == mnd_ipc_server->global_state.active_client_index,
|
||||
});
|
||||
}
|
||||
connection->send_control(std::move(msg));
|
||||
@@ -809,16 +819,18 @@ void wivrn_session::operator()(const from_headset::get_running_applications &)
|
||||
|
||||
void wivrn_session::operator()(const from_headset::set_active_application & req)
|
||||
{
|
||||
ipc_server_set_active_client(inst.server, req.id);
|
||||
ipc_server_update_state(inst.server);
|
||||
assert(server);
|
||||
ipc_server_set_active_client(mnd_ipc_server, req.id);
|
||||
ipc_server_update_state(mnd_ipc_server);
|
||||
// Send a refreshed application list
|
||||
(*this)(from_headset::get_running_applications{});
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(const from_headset::stop_application & req)
|
||||
{
|
||||
scoped_lock lock(inst.server->global_state.lock);
|
||||
for (auto & t: inst.server->threads)
|
||||
assert(server);
|
||||
scoped_lock lock(mnd_ipc_server->global_state.lock);
|
||||
for (auto & t: mnd_ipc_server->threads)
|
||||
{
|
||||
if (t.ics.client_state.id == req.id)
|
||||
{
|
||||
@@ -1021,15 +1033,16 @@ void wivrn_session::reconnect()
|
||||
|
||||
void wivrn_session::poll_session_loss()
|
||||
{
|
||||
assert(server);
|
||||
auto locked = session_loss.lock();
|
||||
auto now = os_monotonic_get_ns();
|
||||
if (locked->empty())
|
||||
return;
|
||||
auto it = locked->begin();
|
||||
scoped_lock lock(inst.server->global_state.lock);
|
||||
scoped_lock lock(mnd_ipc_server->global_state.lock);
|
||||
while (it != locked->end() and it->first <= now)
|
||||
{
|
||||
for (auto & t: inst.server->threads)
|
||||
for (auto & t: mnd_ipc_server->threads)
|
||||
{
|
||||
if (t.ics.client_state.id == it->second)
|
||||
{
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include <shared_mutex>
|
||||
#include <thread>
|
||||
|
||||
struct ipc_server;
|
||||
struct u_system;
|
||||
struct xrt_space_overseer;
|
||||
struct xrt_system_compositor;
|
||||
@@ -45,7 +46,6 @@ union xrt_session_event;
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class instance;
|
||||
class wivrn_eye_tracker;
|
||||
class wivrn_fb_face2_tracker;
|
||||
class wivrn_htc_face_tracker;
|
||||
@@ -95,10 +95,10 @@ class wivrn_session : public xrt_system_devices
|
||||
{
|
||||
friend wivrn_comp_target_factory;
|
||||
std::unique_ptr<wivrn_connection> connection;
|
||||
instance & inst;
|
||||
pacing_app_factory app_pacers;
|
||||
|
||||
u_system & xrt_system;
|
||||
ipc_server * mnd_ipc_server;
|
||||
xrt_space_overseer * space_overseer;
|
||||
xrt_system_compositor * system_compositor;
|
||||
|
||||
@@ -141,18 +141,20 @@ class wivrn_session : public xrt_system_devices
|
||||
|
||||
std::jthread thread;
|
||||
|
||||
wivrn_session(std::unique_ptr<wivrn_connection> connection, instance &, u_system &);
|
||||
wivrn_session(std::unique_ptr<wivrn_connection> connection, u_system &);
|
||||
|
||||
public:
|
||||
~wivrn_session();
|
||||
|
||||
static xrt_result_t create_session(std::unique_ptr<wivrn_connection> connection,
|
||||
instance & instance,
|
||||
u_system & system,
|
||||
xrt_system_devices ** out_xsysd,
|
||||
xrt_space_overseer ** out_xspovrs,
|
||||
xrt_system_compositor ** out_xsysc);
|
||||
|
||||
void start(ipc_server *);
|
||||
void stop();
|
||||
|
||||
clock_offset get_offset();
|
||||
bool connected();
|
||||
const from_headset::headset_info_packet & get_info()
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "ipc_server_cb.h"
|
||||
#include "driver/wivrn_session.h"
|
||||
#include "target_instance_wivrn.h"
|
||||
#include "utils/method.h"
|
||||
|
||||
@@ -29,13 +30,13 @@ void ipc_server_cb::init_failed(xrt_result res)
|
||||
void ipc_server_cb::mainloop_entering(ipc_server * server, xrt_instance * xrt_inst)
|
||||
{
|
||||
auto inst = static_cast<wivrn::instance *>(xrt_inst);
|
||||
inst->server = server;
|
||||
inst->set_ipc_server(server);
|
||||
}
|
||||
|
||||
void ipc_server_cb::mainloop_leaving(ipc_server * server, xrt_instance * xrt_inst)
|
||||
{
|
||||
auto inst = static_cast<wivrn::instance *>(xrt_inst);
|
||||
inst->server = nullptr;
|
||||
inst->set_ipc_server(nullptr);
|
||||
}
|
||||
|
||||
ipc_server_cb::ipc_server_cb() :
|
||||
|
||||
@@ -48,11 +48,13 @@ xrt_result_t instance::create_system(
|
||||
struct xrt_system_compositor * xsysc = NULL;
|
||||
auto res = wivrn::wivrn_session::create_session(
|
||||
std::move(connection),
|
||||
*this,
|
||||
*u_sys,
|
||||
out_xsysd,
|
||||
out_xspovrs,
|
||||
out_xsysc);
|
||||
if (res != XRT_SUCCESS)
|
||||
return res;
|
||||
session = (wivrn_session *)*out_xsysd;
|
||||
u_system_set_system_compositor(u_sys, *out_xsysc);
|
||||
return res;
|
||||
}
|
||||
@@ -73,6 +75,15 @@ instance::instance() :
|
||||
{
|
||||
}
|
||||
|
||||
void instance::set_ipc_server(ipc_server * server)
|
||||
{
|
||||
assert(session);
|
||||
if (server)
|
||||
session->start(server);
|
||||
else
|
||||
session->stop();
|
||||
}
|
||||
|
||||
} // namespace wivrn
|
||||
xrt_result_t
|
||||
xrt_instance_create(struct xrt_instance_info * ii, struct xrt_instance ** out_xinst)
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class wivrn_session;
|
||||
class instance : public xrt_instance
|
||||
{
|
||||
xrt_result_t create_system(
|
||||
@@ -33,9 +34,11 @@ class instance : public xrt_instance
|
||||
|
||||
xrt_result_t get_prober(xrt_prober **);
|
||||
|
||||
wivrn_session * session = nullptr;
|
||||
|
||||
public:
|
||||
ipc_server * server = nullptr;
|
||||
using base = xrt_instance;
|
||||
instance();
|
||||
void set_ipc_server(ipc_server *);
|
||||
};
|
||||
} // namespace wivrn
|
||||
|
||||
Reference in New Issue
Block a user