Skip to content

Commit

Permalink
Revert "use a jthread"
Browse files Browse the repository at this point in the history
This reverts commit 9500000.
  • Loading branch information
goeiecool9999 committed Oct 20, 2024
1 parent 9500000 commit d090a97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
38 changes: 22 additions & 16 deletions src/gui/input/InputAPIAddWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void InputAPIAddWindow::on_api_selected(wxCommandEvent& event)

void InputAPIAddWindow::on_controller_dropdown(wxCommandEvent& event)
{
if (m_search_thread.joinable())
if (m_search_running)
return;

int selection = m_input_api->GetSelection();
Expand All @@ -238,31 +238,38 @@ void InputAPIAddWindow::on_controller_dropdown(wxCommandEvent& event)
// TODO selected_uuid
}

m_search_running = true;

wxWindowUpdateLocker lock(m_controller_list);
m_controller_list->Clear();

m_controller_list->Append(_("Searching for controllers..."), (wxClientData*)nullptr);
m_controller_list->SetSelection(wxNOT_FOUND);

m_search_thread = std::jthread([this, provider, selected_uuid](std::stop_token stopToken)
m_search_thread_data = std::make_unique<AsyncThreadData>();
std::thread([this, provider, selected_uuid](std::shared_ptr<AsyncThreadData> data)
{
auto available_controllers = provider->get_controllers();

if(!stopToken.stop_requested())
{
wxCommandEvent event(wxControllersRefreshed);
event.SetEventObject(m_controller_list);
event.SetClientObject(new wxCustomData(std::move(available_controllers)));
event.SetInt(provider->api());
event.SetString(selected_uuid);
wxPostEvent(this, event);
std::lock_guard lock{data->mutex};
if(!data->discardResult)
{
wxCommandEvent event(wxControllersRefreshed);
event.SetEventObject(m_controller_list);
event.SetClientObject(new wxCustomData(std::move(available_controllers)));
event.SetInt(provider->api());
event.SetString(selected_uuid);
wxPostEvent(this, event);
m_search_running = false;
}
}
});
}, m_search_thread_data).detach();
}

void InputAPIAddWindow::on_controller_selected(wxCommandEvent& event)
{
if (m_search_thread.joinable())
if (m_search_running)
{
return;
}
Expand All @@ -282,7 +289,6 @@ void InputAPIAddWindow::on_controller_selected(wxCommandEvent& event)

void InputAPIAddWindow::on_controllers_refreshed(wxCommandEvent& event)
{
m_search_thread = {};
const auto type = event.GetInt();
wxASSERT(0 <= type && type < InputAPI::MAX);

Expand Down Expand Up @@ -311,10 +317,10 @@ void InputAPIAddWindow::on_controllers_refreshed(wxCommandEvent& event)

void InputAPIAddWindow::discard_thread_result()
{
if(m_search_thread.joinable())
m_search_running = false;
if(m_search_thread_data)
{
m_search_thread.request_stop();
m_search_thread.detach();
m_search_thread = {};
std::lock_guard lock{m_search_thread_data->mutex};
m_search_thread_data->discardResult = true;
}
}
8 changes: 7 additions & 1 deletion src/gui/input/InputAPIAddWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@ class InputAPIAddWindow : public wxDialog
std::shared_ptr<ControllerBase> m_controller;

std::vector<ControllerPtr> m_controllers;
std::jthread m_search_thread;
std::atomic_bool m_search_running = false;
struct AsyncThreadData
{
std::atomic_bool discardResult = false;
std::mutex mutex;
};
std::shared_ptr<AsyncThreadData> m_search_thread_data;
};

0 comments on commit d090a97

Please sign in to comment.