Skip to content

Commit

Permalink
fix linux build and make linked_queue exception free
Browse files Browse the repository at this point in the history
  • Loading branch information
IamSanjid committed Apr 22, 2022
1 parent efa57c0 commit 2c9259e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
25 changes: 15 additions & 10 deletions linked_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ class LinkedQueue
delete writing;
}

void Push(const T& element)
bool Push(const T& element)
{
Push_Element(element);
return Push_Element(element);
}

void Push(T&& element)
bool Push(T&& element)
{
Push_Element(std::move(element));
return Push_Element(std::move(element));
}

void Pop(std::vector<T>& ret_vals)
bool Pop(std::vector<T>& ret_vals)
{
std::lock_guard<std::mutex> lock{ read_mutex };

if (!reading || !reading->value)
{
return;
return false;
}

Linked* current_reading = reading;
Expand All @@ -63,6 +63,7 @@ class LinkedQueue

/* the next place where we will find the next data it either can be empty or have some value */
reading = current_reading;
return true;
}

void Clear()
Expand All @@ -86,27 +87,30 @@ class LinkedQueue

private:
template <class... _Tys>
void Push_Element(_Tys&&... vals)
bool Push_Element(_Tys&&... vals)
{
Linked* next_writing = new Linked{ 0 };

if (!next_writing)
{
throw std::exception("new memory not available!");
//new memory not available!
return false;
}

std::lock_guard<std::mutex> lock{ write_mutex };

if (!writing || writing->value)
{
throw std::exception("some race condition need to be fixed!");
//some race condition needs to be fixed!
return false;
}

writing->next = next_writing;
writing->value = new T(std::forward<_Tys>(vals)...);

/* pointing to the new memory where we will be writing new data next time */
writing = next_writing;
return true;
}

void Reset()
Expand All @@ -115,7 +119,8 @@ class LinkedQueue

if (!starting_linked)
{
throw std::exception("memory not available!");
// memory not available!
return;
}

reading = starting_linked;
Expand Down
5 changes: 3 additions & 2 deletions npad_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "npad_controller.h"
#include <stdint.h>
#include <cmath>

#include "native.h"
#include "Config.h"
Expand Down Expand Up @@ -76,8 +78,7 @@ class StickInputHandler final : public InputHandler
}

std::vector<ButtonTimeout> current_timeouts;
timeout_queue_.Pop(current_timeouts);
if (!current_timeouts.empty())
if (timeout_queue_.Pop(current_timeouts))
{
for (auto& timeout : current_timeouts)
{
Expand Down

0 comments on commit 2c9259e

Please sign in to comment.