lokinet/llarp/util/timer.cpp

289 lines
5.3 KiB
C++
Raw Normal View History

#include <util/logger.hpp>
#include <util/time.hpp>
#include <util/timer.hpp>
2018-04-30 18:18:18 +00:00
#include <atomic>
2018-04-30 18:18:34 +00:00
#include <condition_variable>
2018-04-30 18:18:18 +00:00
#include <list>
#include <queue>
2018-06-04 17:22:14 +00:00
#include <unordered_map>
2018-04-30 18:18:18 +00:00
namespace llarp
{
struct timer
{
void* user;
2018-06-04 11:46:02 +00:00
uint64_t called_at;
uint64_t started;
uint64_t timeout;
llarp_timer_handler_func func;
2018-06-04 11:49:37 +00:00
bool done;
2018-06-05 11:48:06 +00:00
bool canceled;
2018-10-29 16:48:36 +00:00
timer(llarp_time_t now, uint64_t ms = 0, void* _user = nullptr,
2018-06-04 11:46:02 +00:00
llarp_timer_handler_func _func = nullptr)
2018-06-04 11:49:37 +00:00
: user(_user)
, called_at(0)
2018-10-29 16:48:36 +00:00
, started(now)
2018-06-04 11:49:37 +00:00
, timeout(ms)
, func(_func)
, done(false)
2018-06-05 11:48:06 +00:00
, canceled(false)
{
}
~timer()
{
}
2018-04-30 18:18:34 +00:00
void
exec();
2018-05-18 17:50:21 +00:00
static void
call(void* user)
{
static_cast< timer* >(user)->exec();
}
2018-05-18 17:50:21 +00:00
bool
operator<(const timer& other) const
{
return (started + timeout) < (other.started + other.timeout);
}
};
2018-04-30 18:18:34 +00:00
}; // namespace llarp
2018-04-30 18:18:18 +00:00
struct llarp_timer_context
{
2018-08-12 17:22:29 +00:00
llarp::util::Mutex timersMutex;
2018-08-30 18:48:43 +00:00
std::unordered_map< uint32_t, std::unique_ptr< llarp::timer > > timers;
std::priority_queue< std::unique_ptr< llarp::timer > > calling;
2018-08-12 17:22:29 +00:00
llarp::util::Mutex tickerMutex;
llarp::util::Condition* ticker = nullptr;
2018-06-06 12:46:26 +00:00
std::chrono::milliseconds nextTickLen = std::chrono::milliseconds(100);
2018-04-30 18:18:18 +00:00
2018-10-29 16:48:36 +00:00
llarp_time_t m_Now;
llarp_timer_context()
{
2018-11-19 22:45:37 +00:00
m_Now = llarp::time_now_ms();
2018-10-29 16:48:36 +00:00
}
2018-05-28 20:51:15 +00:00
uint32_t ids = 0;
bool _run = true;
2018-04-30 18:18:18 +00:00
2018-06-06 12:46:26 +00:00
~llarp_timer_context()
{
if(ticker)
delete ticker;
}
bool
run()
{
2018-05-28 20:51:15 +00:00
return _run;
}
2018-04-30 18:18:34 +00:00
void
stop()
{
2018-05-28 20:51:15 +00:00
_run = false;
}
2018-04-30 18:18:18 +00:00
void
cancel(uint32_t id)
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(timersMutex);
2018-08-30 18:48:43 +00:00
const auto& itr = timers.find(id);
2018-06-05 11:48:06 +00:00
if(itr == timers.end())
return;
itr->second->canceled = true;
2018-04-30 18:18:18 +00:00
}
void
remove(uint32_t id)
2018-05-18 17:50:21 +00:00
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(timersMutex);
2018-08-30 18:48:43 +00:00
const auto& itr = timers.find(id);
2018-06-05 11:48:06 +00:00
if(itr == timers.end())
return;
itr->second->func = nullptr;
itr->second->canceled = true;
2018-05-18 17:50:21 +00:00
}
uint32_t
call_later(void* user, llarp_timer_handler_func func, uint64_t timeout_ms)
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(timersMutex);
2018-10-29 16:48:36 +00:00
2018-05-10 23:32:46 +00:00
uint32_t id = ++ids;
2018-08-30 18:48:43 +00:00
timers.insert(
std::make_pair(id,
std::unique_ptr< llarp::timer >(
2018-10-29 16:48:36 +00:00
new llarp::timer(m_Now, timeout_ms, user, func))));
2018-04-30 18:18:18 +00:00
return id;
}
2018-04-30 18:18:34 +00:00
void
cancel_all()
{
std::list< uint32_t > ids;
2018-04-30 18:18:34 +00:00
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(timersMutex);
for(auto& item : timers)
{
ids.push_back(item.first);
}
2018-04-30 18:18:18 +00:00
}
2018-04-30 18:18:34 +00:00
for(auto id : ids)
{
cancel(id);
2018-04-30 18:18:18 +00:00
}
}
};
struct llarp_timer_context*
llarp_init_timer()
{
2018-04-30 18:18:34 +00:00
return new llarp_timer_context;
}
2018-04-30 18:18:18 +00:00
uint32_t
llarp_timer_call_later(struct llarp_timer_context* t,
struct llarp_timeout_job job)
{
2018-04-30 18:18:34 +00:00
return t->call_later(job.user, job.handler, job.timeout);
}
2018-04-30 18:18:18 +00:00
void
llarp_free_timer(struct llarp_timer_context** t)
{
if(*t)
delete *t;
2018-04-30 18:18:34 +00:00
*t = nullptr;
}
2018-04-30 18:18:18 +00:00
void
llarp_timer_remove_job(struct llarp_timer_context* t, uint32_t id)
{
t->remove(id);
}
void
llarp_timer_stop(struct llarp_timer_context* t)
{
2018-05-28 20:51:15 +00:00
// destroy all timers
// don't call callbacks on timers
t->timers.clear();
2018-04-30 18:18:34 +00:00
t->stop();
2018-06-06 12:46:26 +00:00
if(t->ticker)
2018-08-12 17:22:29 +00:00
t->ticker->NotifyAll();
2018-04-30 18:18:34 +00:00
}
2018-04-30 18:18:18 +00:00
void
llarp_timer_cancel_job(struct llarp_timer_context* t, uint32_t id)
{
2018-04-30 18:18:34 +00:00
t->cancel(id);
}
2018-04-30 18:18:18 +00:00
2018-10-29 16:48:36 +00:00
void
llarp_timer_set_time(struct llarp_timer_context* t, llarp_time_t now)
{
if(now == 0)
2018-11-19 22:45:37 +00:00
now = llarp::time_now_ms();
2018-10-29 16:48:36 +00:00
t->m_Now = now;
}
2018-06-06 12:46:26 +00:00
void
2018-08-09 19:02:17 +00:00
llarp_timer_tick_all(struct llarp_timer_context* t)
2018-06-06 12:46:26 +00:00
{
if(!t->run())
return;
2018-10-29 16:48:36 +00:00
2018-08-30 18:48:43 +00:00
std::list< std::unique_ptr< llarp::timer > > hit;
2018-06-06 12:46:26 +00:00
{
2018-08-30 18:48:43 +00:00
llarp::util::Lock lock(t->timersMutex);
auto itr = t->timers.begin();
while(itr != t->timers.end())
2018-06-06 12:46:26 +00:00
{
2018-10-29 16:48:36 +00:00
if(t->m_Now - itr->second->started >= itr->second->timeout
2018-08-30 18:48:43 +00:00
|| itr->second->canceled)
2018-06-06 12:46:26 +00:00
{
// timer hit
2018-08-30 18:48:43 +00:00
hit.emplace_back(std::move(itr->second));
itr = t->timers.erase(itr);
2018-06-06 12:46:26 +00:00
}
2018-08-30 18:48:43 +00:00
else
++itr;
}
}
for(const auto& h : hit)
{
if(h->func)
{
2018-10-29 16:48:36 +00:00
h->called_at = t->m_Now;
2018-08-30 18:48:43 +00:00
h->exec();
2018-06-06 12:46:26 +00:00
}
}
}
2018-08-09 19:02:17 +00:00
static void
llarp_timer_tick_all_job(void* user)
{
llarp_timer_tick_all(static_cast< llarp_timer_context* >(user));
}
void
llarp_timer_tick_all_async(struct llarp_timer_context* t,
2018-10-29 16:48:36 +00:00
struct llarp_threadpool* pool, llarp_time_t now)
2018-08-09 19:02:17 +00:00
{
2018-10-29 16:48:36 +00:00
t->m_Now = now;
2018-08-09 19:02:17 +00:00
llarp_threadpool_queue_job(pool, {t, llarp_timer_tick_all_job});
}
void
llarp_timer_run(struct llarp_timer_context* t, struct llarp_threadpool* pool)
{
2018-08-12 17:22:29 +00:00
t->ticker = new llarp::util::Condition();
while(t->run())
{
2018-05-29 13:40:26 +00:00
// wait for timer mutex
2018-06-06 12:46:26 +00:00
if(t->ticker)
2018-05-18 17:50:21 +00:00
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(t->tickerMutex);
t->ticker->WaitFor(lock, t->nextTickLen);
2018-05-29 13:40:26 +00:00
}
2018-05-28 20:51:15 +00:00
2018-05-29 13:40:26 +00:00
if(t->run())
{
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(t->timersMutex);
2018-05-28 20:51:15 +00:00
// we woke up
2018-11-19 22:45:37 +00:00
llarp_timer_tick_all_async(t, pool, llarp::time_now_ms());
2018-04-30 18:18:18 +00:00
}
}
}
2018-05-18 17:50:21 +00:00
namespace llarp
{
void
timer::exec()
2018-05-18 17:50:21 +00:00
{
if(func)
{
2018-06-04 11:46:02 +00:00
auto diff = called_at - started;
// zero out function pointer before call to prevent multiple calls being
// queued if call takes longer than 1 timer tick
auto call = func;
func = nullptr;
if(diff >= timeout)
2018-06-04 11:46:02 +00:00
call(user, timeout, 0);
2018-05-18 17:50:21 +00:00
else
2018-06-04 11:46:02 +00:00
call(user, timeout, diff);
2018-05-18 17:50:21 +00:00
}
2018-06-05 11:48:06 +00:00
done = true;
2018-05-18 17:50:21 +00:00
}
2018-07-09 03:34:29 +00:00
} // namespace llarp