Skip to content
Snippets Groups Projects
Commit 35b4b01c authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Use a TimerHandle

parent 31fb6b23
No related branches found
No related tags found
1 merge request!94Implements #134 source groups
This commit is part of merge request !94. Comments created here will be created in the context of that merge request.
...@@ -23,6 +23,19 @@ enum timerlevel_t { ...@@ -23,6 +23,19 @@ enum timerlevel_t {
kTimerMAXLEVEL kTimerMAXLEVEL
}; };
/**
* Represents a timer job for control purposes. Use to remove timer jobs in
* a destructor, for example.
*/
struct TimerHandle {
const int id = -1;
void cancel() const;
void pause() const;
void unpause() const;
TimerHandle &operator=(const TimerHandle &h) { const_cast<int&>(id) = h.id; return *this; }
};
/** /**
* Milliseconds between calls. * Milliseconds between calls.
*/ */
...@@ -44,13 +57,7 @@ void setClockAdjustment(int64_t ms); ...@@ -44,13 +57,7 @@ void setClockAdjustment(int64_t ms);
* If all high precision callbacks together take more than 1ms to complete, a * If all high precision callbacks together take more than 1ms to complete, a
* warning is produced. * warning is produced.
*/ */
int add(timerlevel_t, const std::function<void(int64_t ts)> &); const TimerHandle add(timerlevel_t, const std::function<void(int64_t ts)> &);
/**
* Remove a callback using an ID returned when it was added.
*/
void remove(int);
/** /**
* Initiate the timer and optionally block the current process. * Initiate the timer and optionally block the current process.
......
...@@ -77,16 +77,16 @@ void ftl::timer::setClockAdjustment(int64_t ms) { ...@@ -77,16 +77,16 @@ void ftl::timer::setClockAdjustment(int64_t ms) {
clock_adjust = ms; clock_adjust = ms;
} }
int ftl::timer::add(timerlevel_t l, const std::function<void(int64_t ts)> &f) { const TimerHandle ftl::timer::add(timerlevel_t l, const std::function<void(int64_t ts)> &f) {
if (l < 0 || l >= kTimerMAXLEVEL) return -1; if (l < 0 || l >= kTimerMAXLEVEL) return {-1};
UNIQUE_LOCK(mtx, lk); UNIQUE_LOCK(mtx, lk);
int newid = last_id++; int newid = last_id++;
jobs[l].push_back({newid, f, false}); jobs[l].push_back({newid, f, false});
return newid; return {newid};
} }
void ftl::timer::remove(int id) { static void removeJob(int id) {
UNIQUE_LOCK(mtx, lk); UNIQUE_LOCK(mtx, lk);
if (id < 0) return; if (id < 0) return;
for (size_t j=0; j<kTimerMAXLEVEL; ++j) { for (size_t j=0; j<kTimerMAXLEVEL; ++j) {
...@@ -180,3 +180,17 @@ void ftl::timer::reset() { ...@@ -180,3 +180,17 @@ void ftl::timer::reset() {
jobs[i].clear(); jobs[i].clear();
} }
} }
// ===== TimerHandle ===========================================================
void ftl::timer::TimerHandle::cancel() const {
removeJob(id);
}
void ftl::timer::TimerHandle::pause() const {
}
void ftl::timer::TimerHandle::unpause() const {
}
...@@ -16,13 +16,13 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) { ...@@ -16,13 +16,13 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) {
didrun = true; didrun = true;
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::start(true); ftl::timer::start(true);
REQUIRE( didrun == true ); REQUIRE( didrun == true );
...@@ -33,14 +33,14 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) { ...@@ -33,14 +33,14 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) {
didrun = true; didrun = true;
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(5));
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::start(true); ftl::timer::start(true);
REQUIRE( didrun == true ); REQUIRE( didrun == true );
...@@ -51,13 +51,13 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) { ...@@ -51,13 +51,13 @@ TEST_CASE( "Timer::add() High Precision Accuracy" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) {
didrun[0] = true; didrun[0] = true;
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) { ftl::timer::add(ftl::timer::kTimerHighPrecision, [&didrun](int64_t ts) {
didrun[1] = true; didrun[1] = true;
...@@ -84,13 +84,13 @@ TEST_CASE( "Timer::add() Main job" ) { ...@@ -84,13 +84,13 @@ TEST_CASE( "Timer::add() Main job" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerMain, [&didrun](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerMain, [&didrun](int64_t ts) {
didrun = true; didrun = true;
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::start(true); ftl::timer::start(true);
REQUIRE( didrun == true ); REQUIRE( didrun == true );
...@@ -101,14 +101,14 @@ TEST_CASE( "Timer::add() Main job" ) { ...@@ -101,14 +101,14 @@ TEST_CASE( "Timer::add() Main job" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerMain, [&didrun](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerMain, [&didrun](int64_t ts) {
didrun = true; didrun = true;
std::this_thread::sleep_for(std::chrono::milliseconds(60)); std::this_thread::sleep_for(std::chrono::milliseconds(60));
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::start(true); ftl::timer::start(true);
REQUIRE( didrun == true ); REQUIRE( didrun == true );
...@@ -120,14 +120,14 @@ TEST_CASE( "Timer::add() Main job" ) { ...@@ -120,14 +120,14 @@ TEST_CASE( "Timer::add() Main job" ) {
ftl::timer::reset(); ftl::timer::reset();
int rc = ftl::timer::add(ftl::timer::kTimerMain, [&job1](int64_t ts) { auto rc = ftl::timer::add(ftl::timer::kTimerMain, [&job1](int64_t ts) {
job1++; job1++;
std::this_thread::sleep_for(std::chrono::milliseconds(60)); std::this_thread::sleep_for(std::chrono::milliseconds(60));
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
REQUIRE( (rc >= 0) ); REQUIRE( (rc.id >= 0) );
ftl::timer::add(ftl::timer::kTimerMain, [&job2](int64_t ts) { ftl::timer::add(ftl::timer::kTimerMain, [&job2](int64_t ts) {
job2++; job2++;
...@@ -139,7 +139,7 @@ TEST_CASE( "Timer::add() Main job" ) { ...@@ -139,7 +139,7 @@ TEST_CASE( "Timer::add() Main job" ) {
} }
} }
TEST_CASE( "Timer::remove()" ) { TEST_CASE( "TimerHandle::cancel()" ) {
SECTION( "Invalid id" ) { SECTION( "Invalid id" ) {
bool didjob = false; bool didjob = false;
ftl::timer::reset(); ftl::timer::reset();
...@@ -150,7 +150,9 @@ TEST_CASE( "Timer::remove()" ) { ...@@ -150,7 +150,9 @@ TEST_CASE( "Timer::remove()" ) {
return; return;
}); });
ftl::timer::remove(55); // Fake Handle
ftl::timer::TimerHandle h = {44};
h.cancel();
ftl::timer::start(true); ftl::timer::start(true);
REQUIRE( didjob ); REQUIRE( didjob );
} }
...@@ -159,13 +161,13 @@ TEST_CASE( "Timer::remove()" ) { ...@@ -159,13 +161,13 @@ TEST_CASE( "Timer::remove()" ) {
bool didjob = false; bool didjob = false;
ftl::timer::reset(); ftl::timer::reset();
int id = ftl::timer::add(ftl::timer::kTimerMain, [&didjob](int64_t ts) { auto id = ftl::timer::add(ftl::timer::kTimerMain, [&didjob](int64_t ts) {
didjob = true; didjob = true;
ftl::timer::stop(false); ftl::timer::stop(false);
return; return;
}); });
ftl::timer::remove(id); id.cancel();
ftl::timer::start(false); ftl::timer::start(false);
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
ftl::timer::stop(); ftl::timer::stop();
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define _FTL_RGBD_GROUP_HPP_ #define _FTL_RGBD_GROUP_HPP_
#include <ftl/threads.hpp> #include <ftl/threads.hpp>
#include <ftl/timer.hpp>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <vector> #include <vector>
...@@ -92,9 +93,9 @@ class Group { ...@@ -92,9 +93,9 @@ class Group {
int64_t last_ts_; int64_t last_ts_;
std::atomic<int> jobs_; std::atomic<int> jobs_;
volatile bool skip_; volatile bool skip_;
int cap_id_; ftl::timer::TimerHandle cap_id_;
int swap_id_; ftl::timer::TimerHandle swap_id_;
int main_id_; ftl::timer::TimerHandle main_id_;
/* Insert a new frameset into the buffer, along with all intermediate /* Insert a new frameset into the buffer, along with all intermediate
* framesets between the last in buffer and the new one. * framesets between the last in buffer and the new one.
......
...@@ -15,9 +15,6 @@ Group::Group() : framesets_(kFrameBufferSize), head_(0) { ...@@ -15,9 +15,6 @@ Group::Group() : framesets_(kFrameBufferSize), head_(0) {
framesets_[0].timestamp = -1; framesets_[0].timestamp = -1;
jobs_ = 0; jobs_ = 0;
skip_ = false; skip_ = false;
cap_id_ = -1;
swap_id_ = -1;
main_id_ = -1;
setFPS(20); setFPS(20);
setLatency(5); setLatency(5);
} }
...@@ -27,9 +24,9 @@ Group::~Group() { ...@@ -27,9 +24,9 @@ Group::~Group() {
s->removeCallback(); s->removeCallback();
} }
ftl::timer::remove(main_id_); main_id_.cancel();
ftl::timer::remove(swap_id_); swap_id_.cancel();
ftl::timer::remove(cap_id_); cap_id_.cancel();
UNIQUE_LOCK(mutex_, lk); UNIQUE_LOCK(mutex_, lk);
// Make sure all jobs have finished // Make sure all jobs have finished
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment