refactor wifi lock
This commit is contained in:
+30
-30
@@ -58,19 +58,6 @@ void wifi_lock::print_wifi()
|
|||||||
spdlog::info("WifiLock low latency released");
|
spdlog::info("WifiLock low latency released");
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifi_lock::acquire_wifi()
|
|
||||||
{
|
|
||||||
std::lock_guard lock(mutex);
|
|
||||||
wifi_.call<void>("acquire");
|
|
||||||
print_wifi();
|
|
||||||
}
|
|
||||||
void wifi_lock::release_wifi()
|
|
||||||
{
|
|
||||||
std::lock_guard lock(mutex);
|
|
||||||
wifi_.call<void>("release");
|
|
||||||
print_wifi();
|
|
||||||
}
|
|
||||||
|
|
||||||
void wifi_lock::print_multicast()
|
void wifi_lock::print_multicast()
|
||||||
{
|
{
|
||||||
if (multicast_.call<jni::Bool>("isHeld"))
|
if (multicast_.call<jni::Bool>("isHeld"))
|
||||||
@@ -79,30 +66,43 @@ void wifi_lock::print_multicast()
|
|||||||
spdlog::info("MulticastLock released");
|
spdlog::info("MulticastLock released");
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifi_lock::acquire_multicast()
|
|
||||||
{
|
|
||||||
std::lock_guard lock(mutex);
|
|
||||||
multicast_.call<void>("acquire");
|
|
||||||
print_multicast();
|
|
||||||
}
|
|
||||||
void wifi_lock::release_multicast()
|
|
||||||
{
|
|
||||||
std::lock_guard lock(mutex);
|
|
||||||
multicast_.call<void>("release");
|
|
||||||
print_multicast();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<void> wifi_lock::get_wifi_lock()
|
std::shared_ptr<void> wifi_lock::get_wifi_lock()
|
||||||
{
|
{
|
||||||
acquire_wifi();
|
std::lock_guard lock(mutex);
|
||||||
|
if (auto l = wifi_weak.lock())
|
||||||
|
return l;
|
||||||
|
|
||||||
|
wifi_.call<void>("acquire");
|
||||||
|
print_wifi();
|
||||||
|
|
||||||
// The shared pointer has a separate reference counter: destructor only calls a method
|
// The shared pointer has a separate reference counter: destructor only calls a method
|
||||||
return std::shared_ptr<void>(this, [self = shared_from_this()](void *) { self->release_wifi(); });
|
auto res = std::shared_ptr<void>(
|
||||||
|
this,
|
||||||
|
[self = shared_from_this()](void *) {
|
||||||
|
std::lock_guard lock(self->mutex);
|
||||||
|
self->wifi_.call<void>("release");
|
||||||
|
self->print_wifi(); });
|
||||||
|
wifi_weak = res;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
std::shared_ptr<void> wifi_lock::get_multicast_lock()
|
std::shared_ptr<void> wifi_lock::get_multicast_lock()
|
||||||
{
|
{
|
||||||
acquire_multicast();
|
std::lock_guard lock(mutex);
|
||||||
|
if (auto l = multicast_weak.lock())
|
||||||
|
return l;
|
||||||
|
|
||||||
|
multicast_.call<void>("acquire");
|
||||||
|
print_multicast();
|
||||||
|
|
||||||
// The shared pointer has a separate reference counter: destructor only calls a method
|
// The shared pointer has a separate reference counter: destructor only calls a method
|
||||||
return std::shared_ptr<void>(this, [self = shared_from_this()](void *) { self->release_multicast(); });
|
auto res = std::shared_ptr<void>(
|
||||||
|
this,
|
||||||
|
[self = shared_from_this()](void *) {
|
||||||
|
std::lock_guard lock(self->mutex);
|
||||||
|
self->multicast_.call<void>("release");
|
||||||
|
self->print_multicast(); });
|
||||||
|
multicast_weak = res;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|||||||
+3
-4
@@ -34,11 +34,10 @@ class wifi_lock : public std::enable_shared_from_this<wifi_lock>
|
|||||||
wifi_lock(decltype(multicast_), decltype(wifi_));
|
wifi_lock(decltype(multicast_), decltype(wifi_));
|
||||||
|
|
||||||
void print_wifi();
|
void print_wifi();
|
||||||
void acquire_wifi();
|
|
||||||
void release_wifi();
|
|
||||||
void print_multicast();
|
void print_multicast();
|
||||||
void acquire_multicast();
|
|
||||||
void release_multicast();
|
std::weak_ptr<void> multicast_weak;
|
||||||
|
std::weak_ptr<void> wifi_weak;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<wifi_lock> make_wifi_lock(jobject activity);
|
static std::shared_ptr<wifi_lock> make_wifi_lock(jobject activity);
|
||||||
|
|||||||
Reference in New Issue
Block a user