Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ set( CMAKE_CXX_STANDARD 17 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
include( CheckCXXCompilerFlag )

find_package( PkgConfig REQUIRED )
pkg_check_modules( GStreamerApp REQUIRED IMPORTED_TARGET gstreamer-app-1.0 gstreamer-pbutils-1.0 gstreamer-audio-1.0)

add_subdirectory(public)

add_library(
Expand Down Expand Up @@ -50,6 +53,7 @@ target_include_directories(

PRIVATE
include
${GStreamerApp_INCLUDE_DIRS}
)

target_link_libraries (
Expand All @@ -60,4 +64,5 @@ target_link_libraries (

PRIVATE
RialtoLogging
${GStreamerApp_LIBRARIES}
)
10 changes: 2 additions & 8 deletions common/include/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
#include "ITimer.h"

#include <atomic>
#include <condition_variable>
#include <glib.h>
#include <memory>
#include <mutex>
#include <thread>

namespace firebolt::rialto::common
Expand Down Expand Up @@ -60,12 +59,7 @@ class Timer : public ITimer
bool isActive() const override;

private:
std::atomic<bool> m_active;
std::chrono::milliseconds m_timeout;
std::function<void()> m_callback;
mutable std::mutex m_mutex;
std::thread m_thread;
std::condition_variable m_cv;
std::atomic<guint> m_timerId{0};
};
} // namespace firebolt::rialto::common

Expand Down
124 changes: 89 additions & 35 deletions common/source/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,63 @@

#include "Timer.h"
#include "RialtoCommonLogging.h"
#include <mutex>
#include <thread>
#include <unordered_map>

namespace
{
class CommonTimerLoop
{
public:
static CommonTimerLoop &instance()
{
static CommonTimerLoop instance;
return instance;
}

void storeTimerCallback(const firebolt::rialto::common::Timer *timer, const std::function<void()> &callback)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_activeTimers[timer] = callback;
Comment thread
skywojciechowskim marked this conversation as resolved.
}

void removeTimerCallback(const firebolt::rialto::common::Timer *timer)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_activeTimers.erase(timer);
}

std::function<void()> getTimerCallback(const firebolt::rialto::common::Timer *timer)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_activeTimers.find(timer);
return it != m_activeTimers.end() ? it->second : nullptr;
}

private:
CommonTimerLoop()
{
m_loop = g_main_loop_new(nullptr, FALSE);
m_thread = std::thread([this]() { g_main_loop_run(m_loop); });
}

~CommonTimerLoop()
{
g_main_loop_quit(m_loop);
if (m_thread.joinable())
{
m_thread.join();
}
g_main_loop_unref(m_loop);
}

GMainLoop *m_loop;
std::thread m_thread;
std::mutex m_mutex;
std::unordered_map<const firebolt::rialto::common::Timer *, std::function<void()>> m_activeTimers;
};
} // namespace

namespace firebolt::rialto::common
{
Expand Down Expand Up @@ -52,32 +109,39 @@ std::unique_ptr<ITimer> TimerFactory::createTimer(const std::chrono::millisecond
}

Timer::Timer(const std::chrono::milliseconds &timeout, const std::function<void()> &callback, TimerType timerType)
: m_active{true}, m_timeout{timeout}, m_callback{callback}
{
m_thread = std::thread(
[this, timerType]()
{
do
CommonTimerLoop::instance().storeTimerCallback(this, callback);
if (timerType == TimerType::PERIODIC)
{
m_timerId = g_timeout_add(
Comment thread
skywojciechowskim marked this conversation as resolved.
static_cast<guint>(timeout.count()),
[](gpointer data) -> gboolean
{
bool shouldExecuteCallback = false;
auto callback = CommonTimerLoop::instance().getTimerCallback(static_cast<Timer *>(data));
if (callback)
{
std::unique_lock<std::mutex> lock{m_mutex};
if (!m_cv.wait_for(lock, m_timeout, [this]() { return !m_active; }))
{
if (m_active && m_callback)
{
shouldExecuteCallback = true;
}
}
callback();
return CommonTimerLoop::instance().getTimerCallback(static_cast<Timer *>(data)) ? TRUE : FALSE;
}

if (shouldExecuteCallback)
return FALSE;
},
this);
}
else
{
m_timerId = g_timeout_add_once(
static_cast<guint>(timeout.count()),
Comment thread
skywojciechowskim marked this conversation as resolved.
[](gpointer data)
{
auto callback = CommonTimerLoop::instance().getTimerCallback(static_cast<Timer *>(data));
Comment thread
skywojciechowskim marked this conversation as resolved.
if (callback)
{
m_callback();
callback();
CommonTimerLoop::instance().removeTimerCallback(static_cast<Timer *>(data));
}
} while (timerType == TimerType::PERIODIC && m_active);
m_active = false;
});
},
this);
}
}

Timer::~Timer()
Expand All @@ -87,26 +151,16 @@ Timer::~Timer()

void Timer::cancel()
{
m_active = false;
m_cv.notify_one();

if (std::this_thread::get_id() == m_thread.get_id())
{
if (m_thread.joinable())
{
m_thread.detach();
}
return;
}

if (m_thread.joinable())
CommonTimerLoop::instance().removeTimerCallback(this);
if (m_timerId != 0)
{
m_thread.join();
g_source_remove(m_timerId);
m_timerId = 0;
Comment thread
skywojciechowskim marked this conversation as resolved.
}
}

bool Timer::isActive() const
{
return m_active;
return CommonTimerLoop::instance().getTimerCallback(this) != nullptr;
}
} // namespace firebolt::rialto::common
Loading