Replace condition variable with atomic
This commit is contained in:
@@ -71,10 +71,8 @@ static inline struct vk_bundle * get_vk(struct wivrn_comp_target * cn)
|
|||||||
|
|
||||||
static void destroy_images(struct wivrn_comp_target * cn)
|
static void destroy_images(struct wivrn_comp_target * cn)
|
||||||
{
|
{
|
||||||
if (cn->images == NULL)
|
if (cn->images == nullptr)
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
cn->encoder_threads.clear();
|
cn->encoder_threads.clear();
|
||||||
cn->encoders.clear();
|
cn->encoders.clear();
|
||||||
@@ -123,7 +121,7 @@ static void create_encoders(wivrn_comp_target * cn)
|
|||||||
for (auto & [group, params]: thread_params)
|
for (auto & [group, params]: thread_params)
|
||||||
{
|
{
|
||||||
auto params_ptr = new encoder_thread_param(params);
|
auto params_ptr = new encoder_thread_param(params);
|
||||||
auto & thread = cn->encoder_threads.emplace_back();
|
auto & thread = cn->encoder_threads.emplace_back(cn->psc.present_gen);
|
||||||
thread.index = cn->encoder_threads.size() - 1;
|
thread.index = cn->encoder_threads.size() - 1;
|
||||||
params_ptr->thread = &thread;
|
params_ptr->thread = &thread;
|
||||||
params_ptr->cn = cn;
|
params_ptr->cn = cn;
|
||||||
@@ -386,7 +384,6 @@ static void * comp_wivrn_present_thread(void * void_param)
|
|||||||
U_LOG_I("Starting encoder thread %d", param->thread->index);
|
U_LOG_I("Starting encoder thread %d", param->thread->index);
|
||||||
|
|
||||||
uint8_t status_bit = 1 << (param->thread->index + 1);
|
uint8_t status_bit = 1 << (param->thread->index + 1);
|
||||||
std::mutex dummy_mutex;
|
|
||||||
|
|
||||||
std::vector<VkFence> fences(cn->image_count);
|
std::vector<VkFence> fences(cn->image_count);
|
||||||
std::vector<int> indices(cn->image_count);
|
std::vector<int> indices(cn->image_count);
|
||||||
@@ -396,6 +393,7 @@ static void * comp_wivrn_present_thread(void * void_param)
|
|||||||
int nb_fences = 0;
|
int nb_fences = 0;
|
||||||
{
|
{
|
||||||
uint64_t timestamp = 0;
|
uint64_t timestamp = 0;
|
||||||
|
auto gen = cn->psc.present_gen.load();
|
||||||
|
|
||||||
for (uint32_t i = 0; i < cn->image_count; i++)
|
for (uint32_t i = 0; i < cn->image_count; i++)
|
||||||
{
|
{
|
||||||
@@ -412,10 +410,7 @@ static void * comp_wivrn_present_thread(void * void_param)
|
|||||||
|
|
||||||
if (presenting_index < 0)
|
if (presenting_index < 0)
|
||||||
{
|
{
|
||||||
std::unique_lock lock(dummy_mutex);
|
cn->psc.present_gen.wait(gen);
|
||||||
// condition variable is not notified when we want to stop thread,
|
|
||||||
// use a timeout, but longer than a typical frame
|
|
||||||
cn->psc.cv.wait_for(lock, std::chrono::milliseconds(50));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -544,7 +539,8 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
|
|||||||
view_info.pose[eye] = xrt_cast(result.pose);
|
view_info.pose[eye] = xrt_cast(result.pose);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cn->psc.cv.notify_all();
|
cn->psc.present_gen += 1;
|
||||||
|
cn->psc.present_gen.notify_all();
|
||||||
|
|
||||||
return VK_SUCCESS;
|
return VK_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#include "driver/wivrn_pacer.h"
|
#include "driver/wivrn_pacer.h"
|
||||||
#include "encoder/encoder_settings.h"
|
#include "encoder/encoder_settings.h"
|
||||||
#include <condition_variable>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -61,7 +60,8 @@ struct pseudo_swapchain
|
|||||||
to_headset::video_stream_data_shard::view_info_t view_info{};
|
to_headset::video_stream_data_shard::view_info_t view_info{};
|
||||||
};
|
};
|
||||||
std::unique_ptr<item[]> images;
|
std::unique_ptr<item[]> images;
|
||||||
std::condition_variable cv;
|
// Incremented and notified when a new image is available for encoders
|
||||||
|
status_type present_gen;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct wivrn_comp_target : public comp_target
|
struct wivrn_comp_target : public comp_target
|
||||||
@@ -86,14 +86,19 @@ struct wivrn_comp_target : public comp_target
|
|||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
os_thread_helper thread;
|
os_thread_helper thread;
|
||||||
|
pseudo_swapchain::status_type & gen;
|
||||||
|
|
||||||
encoder_thread()
|
encoder_thread(pseudo_swapchain::status_type & gen) :
|
||||||
|
gen(gen)
|
||||||
{
|
{
|
||||||
os_thread_helper_init(&thread);
|
os_thread_helper_init(&thread);
|
||||||
}
|
}
|
||||||
~encoder_thread()
|
~encoder_thread()
|
||||||
{
|
{
|
||||||
os_thread_helper_stop_and_wait(&thread);
|
os_thread_helper_signal_stop(&thread);
|
||||||
|
gen += 1;
|
||||||
|
gen.notify_all();
|
||||||
|
os_thread_helper_wait_locked(&thread);
|
||||||
os_thread_helper_destroy(&thread);
|
os_thread_helper_destroy(&thread);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user