Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: immediately call new entry #1257

Merged
merged 1 commit into from
Nov 17, 2020
Merged
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
10 changes: 7 additions & 3 deletions src/core/call_every_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ CallEveryHandler::CallEveryHandler(Time& time) : _time(time) {}

CallEveryHandler::~CallEveryHandler() {}

void CallEveryHandler::add(std::function<void()> callback, float interval_s, void** cookie)
void CallEveryHandler::add(std::function<void()> callback, double interval_s, void** cookie)
{
auto new_entry = std::make_shared<Entry>();
new_entry->callback = callback;
new_entry->last_time = _time.steady_time();
auto before = _time.steady_time();
// Make sure it gets run straightaway. The epsilon seemed not enough so
// we use the arbitrary value of 1 ms.
_time.shift_steady_time_by(before, -interval_s - 0.001);
new_entry->last_time = before;
new_entry->interval_s = interval_s;

void* new_cookie = static_cast<void*>(new_entry.get());
Expand All @@ -25,7 +29,7 @@ void CallEveryHandler::add(std::function<void()> callback, float interval_s, voi
}
}

void CallEveryHandler::change(float interval_s, const void* cookie)
void CallEveryHandler::change(double interval_s, const void* cookie)
{
std::lock_guard<std::mutex> lock(_entries_mutex);

Expand Down
6 changes: 3 additions & 3 deletions src/core/call_every_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CallEveryHandler {
CallEveryHandler& operator=(CallEveryHandler const&) = delete; // Copy assign
CallEveryHandler& operator=(CallEveryHandler&&) = delete; // Move assign

void add(std::function<void()> callback, float interval_s, void** cookie);
void change(float interval_s, const void* cookie);
void add(std::function<void()> callback, double interval_s, void** cookie);
void change(double interval_s, const void* cookie);
void reset(const void* cookie);
void remove(const void* cookie);

Expand All @@ -30,7 +30,7 @@ class CallEveryHandler {
struct Entry {
std::function<void()> callback{nullptr};
dl_time_t last_time{};
float interval_s{0.0f};
double interval_s{0.0f};
};

std::unordered_map<void*, std::shared_ptr<Entry>> _entries{};
Expand Down
34 changes: 24 additions & 10 deletions src/core/call_every_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST(CallEveryHandler, Single)
time.sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
}
EXPECT_EQ(num_called, 1);
EXPECT_EQ(num_called, 2);

UNUSED(cookie);
}
Expand All @@ -40,8 +40,8 @@ TEST(CallEveryHandler, Multiple)
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);

for (int i = 0; i < 10; ++i) {
time.sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
time.sleep_for(std::chrono::milliseconds(100));
}
EXPECT_EQ(num_called, 10);

Expand Down Expand Up @@ -76,8 +76,8 @@ TEST(CallEveryHandler, InParallel)
ceh.add([&num_called2]() { ++num_called2; }, 0.2f, &cookie2);

for (int i = 0; i < 10; ++i) {
time.sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
time.sleep_for(std::chrono::milliseconds(100));
}

EXPECT_EQ(num_called1, 10);
Expand All @@ -90,8 +90,8 @@ TEST(CallEveryHandler, InParallel)
ceh.change(0.1f, cookie2);

for (int i = 0; i < 10; ++i) {
time.sleep_for(std::chrono::milliseconds(100));
ceh.run_once();
time.sleep_for(std::chrono::milliseconds(100));
}

EXPECT_EQ(num_called1, 2);
Expand All @@ -109,23 +109,37 @@ TEST(CallEveryHandler, Reset)
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);

for (int i = 0; i < 8; ++i) {
time.sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
if (i == 8) {
}
time.sleep_for(std::chrono::milliseconds(10));
}
EXPECT_EQ(num_called, 0);
EXPECT_EQ(num_called, 1);

ceh.reset(cookie);

for (int i = 0; i < 8; ++i) {
time.sleep_for(std::chrono::milliseconds(10));
ceh.run_once();
time.sleep_for(std::chrono::milliseconds(10));
}
EXPECT_EQ(num_called, 0);
EXPECT_EQ(num_called, 1);

for (int i = 0; i < 3; ++i) {
ceh.run_once();
time.sleep_for(std::chrono::milliseconds(10));
}
EXPECT_EQ(num_called, 2);
}

TEST(CallEveryHandler, CallImmediately)
{
Time time{};
CallEveryHandler ceh(time);

int num_called = 0;

void* cookie = nullptr;
ceh.add([&num_called]() { ++num_called; }, 0.1f, &cookie);

for (int i = 0; i < 1; ++i) {
ceh.run_once();
}
EXPECT_EQ(num_called, 1);
Expand Down