2013-08-26 13:58:58 +02:00
|
|
|
/*
|
|
|
|
* \brief Signal context for timer events
|
|
|
|
* \author Sebastian Sumpf <sebastian.sumpf@genode-labs.com>
|
|
|
|
* \date 2012-05-23
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-05-18 18:49:02 +02:00
|
|
|
* Copyright (C) 2012-2016 Genode Labs GmbH
|
2013-08-26 13:58:58 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
/* Genode includes */
|
2013-08-26 13:58:58 +02:00
|
|
|
#include <base/env.h>
|
|
|
|
#include <base/printf.h>
|
2015-05-19 11:33:29 +02:00
|
|
|
#include <base/tslab.h>
|
|
|
|
#include <os/server.h>
|
|
|
|
#include <timer_session/connection.h>
|
|
|
|
#include <util/volatile_object.h>
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
/* Linux kit includes */
|
|
|
|
#include <lx_kit/internal/list.h>
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
/* local includes */
|
|
|
|
#include <lx_emul.h>
|
|
|
|
#include <lx.h>
|
2015-05-19 11:33:29 +02:00
|
|
|
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
/*********************
|
|
|
|
** linux/jiffies.h **
|
|
|
|
*********************/
|
|
|
|
|
|
|
|
unsigned long jiffies;
|
|
|
|
|
2013-08-26 13:58:58 +02:00
|
|
|
/**
|
2015-05-19 11:33:29 +02:00
|
|
|
* Lx::Timer
|
2013-08-26 13:58:58 +02:00
|
|
|
*/
|
2015-05-19 11:33:29 +02:00
|
|
|
namespace Lx {
|
|
|
|
class Timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Lx::Timer
|
2013-08-26 13:58:58 +02:00
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context encapsulates a regular linux timer_list
|
|
|
|
*/
|
2016-05-18 18:49:02 +02:00
|
|
|
struct Context : public Lx_kit::List<Context>::Element
|
2015-05-19 11:33:29 +02:00
|
|
|
{
|
|
|
|
enum { INVALID_TIMEOUT = ~0UL };
|
|
|
|
enum Type { LIST };
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
void *timer;
|
|
|
|
bool pending { false };
|
|
|
|
unsigned long timeout { INVALID_TIMEOUT }; /* absolute in jiffies */
|
|
|
|
bool programmed { false };
|
|
|
|
|
|
|
|
Context(struct timer_list *timer) : type(LIST), timer(timer) { }
|
|
|
|
|
|
|
|
void expires(unsigned long e)
|
|
|
|
{
|
|
|
|
if (type == LIST)
|
|
|
|
static_cast<timer_list *>(timer)->expires = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void function()
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case LIST:
|
|
|
|
{
|
|
|
|
timer_list *t = static_cast<timer_list *>(timer);
|
|
|
|
if (t->function)
|
|
|
|
t->function(t->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
::Timer::Connection _timer_conn;
|
2016-05-18 18:49:02 +02:00
|
|
|
Lx_kit::List<Context> _list;
|
2015-05-19 11:33:29 +02:00
|
|
|
Genode::Signal_dispatcher<Lx::Timer> _dispatcher;
|
|
|
|
Genode::Tslab<Context, 32 * sizeof(Context)> _timer_alloc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
bool ready { true };
|
|
|
|
|
2013-08-26 13:58:58 +02:00
|
|
|
private:
|
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
/**
|
|
|
|
* Lookup local timer
|
|
|
|
*/
|
|
|
|
Context *_find_context(void const *timer)
|
|
|
|
{
|
|
|
|
for (Context *c = _list.first(); c; c = c->next())
|
|
|
|
if (c->timer == timer)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Program the first timer in the list
|
|
|
|
*
|
|
|
|
* The first timer is programmed if the 'programmed' flag was not set
|
|
|
|
* before. The second timer is flagged as not programmed as
|
|
|
|
* 'Timer::trigger_once' invalidates former registered one-shot
|
|
|
|
* timeouts.
|
|
|
|
*/
|
|
|
|
void _program_first_timer()
|
|
|
|
{
|
|
|
|
Context *ctx = _list.first();
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctx->programmed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* calculate relative microseconds for trigger */
|
|
|
|
unsigned long us = ctx->timeout > jiffies ?
|
|
|
|
jiffies_to_msecs(ctx->timeout - jiffies) * 1000 : 0;
|
|
|
|
_timer_conn.trigger_once(us);
|
|
|
|
|
|
|
|
ctx->programmed = true;
|
|
|
|
|
|
|
|
/* possibly programmed successor must be reprogrammed later */
|
|
|
|
if (Context *next = ctx->next())
|
|
|
|
next->programmed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule timer
|
|
|
|
*
|
|
|
|
* Add the context to the scheduling list depending on its timeout
|
|
|
|
* and reprogram the first timer.
|
|
|
|
*/
|
|
|
|
void _schedule_timer(Context *ctx, unsigned long expires)
|
|
|
|
{
|
|
|
|
_list.remove(ctx);
|
|
|
|
|
|
|
|
ctx->timeout = expires;
|
|
|
|
ctx->pending = true;
|
|
|
|
ctx->programmed = false;
|
|
|
|
/*
|
|
|
|
* Also write the timeout value to the expires field in
|
|
|
|
* struct timer_list because the checks
|
|
|
|
* it directly.
|
|
|
|
*/
|
|
|
|
ctx->expires(expires);
|
|
|
|
|
|
|
|
Context *c;
|
|
|
|
for (c = _list.first(); c; c = c->next())
|
|
|
|
if (ctx->timeout <= c->timeout)
|
|
|
|
break;
|
|
|
|
_list.insert_before(ctx, c);
|
|
|
|
|
|
|
|
_program_first_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle trigger_once signal
|
|
|
|
*/
|
|
|
|
void _handle(unsigned)
|
|
|
|
{
|
|
|
|
update_jiffies();
|
|
|
|
|
|
|
|
while (Lx::Timer::Context *ctx = _list.first()) {
|
|
|
|
if (ctx->timeout > jiffies)
|
|
|
|
break;
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
ctx->function();
|
|
|
|
del(ctx->timer);
|
|
|
|
}
|
|
|
|
}
|
2013-08-26 13:58:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2016-05-18 18:49:02 +02:00
|
|
|
Timer(Genode::Signal_receiver &sig_rec)
|
2015-05-19 11:33:29 +02:00
|
|
|
:
|
2016-05-18 18:49:02 +02:00
|
|
|
_dispatcher(sig_rec, *this, &Lx::Timer::_handle),
|
2015-05-19 11:33:29 +02:00
|
|
|
_timer_alloc(Genode::env()->heap())
|
|
|
|
{
|
|
|
|
_timer_conn.sigh(_dispatcher);
|
|
|
|
jiffies = 0;
|
|
|
|
}
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
/**
|
|
|
|
* Add new linux timer
|
|
|
|
*/
|
|
|
|
template <typename TIMER>
|
|
|
|
void add(TIMER *timer)
|
2013-08-26 13:58:58 +02:00
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
Context *t = new (&_timer_alloc) Context(timer);
|
|
|
|
_list.append(t);
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 11:33:29 +02:00
|
|
|
* Delete linux timer
|
2013-08-26 13:58:58 +02:00
|
|
|
*/
|
2015-05-19 11:33:29 +02:00
|
|
|
int del(void *timer)
|
2013-08-26 13:58:58 +02:00
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
Context *ctx = _find_context(timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the timer expired it was already cleaned up after its
|
|
|
|
* execution.
|
|
|
|
*/
|
|
|
|
if (!ctx)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int rv = ctx->timeout != Context::INVALID_TIMEOUT ? 1 : 0;
|
|
|
|
|
|
|
|
_list.remove(ctx);
|
|
|
|
destroy(&_timer_alloc, ctx);
|
|
|
|
|
|
|
|
return rv;
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 11:33:29 +02:00
|
|
|
* Initial scheduling of linux timer
|
2013-08-26 13:58:58 +02:00
|
|
|
*/
|
2015-05-19 11:33:29 +02:00
|
|
|
int schedule(void *timer, unsigned long expires)
|
|
|
|
{
|
|
|
|
Context *ctx = _find_context(timer);
|
|
|
|
if (!ctx) {
|
|
|
|
PERR("schedule unknown timer %p", timer);
|
|
|
|
return -1; /* XXX better use 0 as rv? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If timer was already active return 1, otherwise 0. The return
|
|
|
|
* value is needed by mod_timer().
|
|
|
|
*/
|
|
|
|
int rv = ctx->timeout != Context::INVALID_TIMEOUT ? 1 : 0;
|
|
|
|
|
|
|
|
_schedule_timer(ctx, expires);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2013-08-26 13:58:58 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-19 11:33:29 +02:00
|
|
|
* Schedule next linux timer
|
2013-08-26 13:58:58 +02:00
|
|
|
*/
|
2015-05-19 11:33:29 +02:00
|
|
|
void schedule_next() { _program_first_timer(); }
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
/**
|
|
|
|
* Check if the timer is currently pending
|
|
|
|
*/
|
|
|
|
bool pending(void const *timer)
|
2013-08-26 13:58:58 +02:00
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
Context *ctx = _find_context(timer);
|
|
|
|
if (!ctx) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
return ctx->pending;
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|
2015-05-19 11:33:29 +02:00
|
|
|
|
|
|
|
Context *find(struct timer_list const *timer) {
|
|
|
|
return _find_context(timer); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update jiffie counter
|
|
|
|
*/
|
|
|
|
void update_jiffies()
|
|
|
|
{
|
|
|
|
jiffies = msecs_to_jiffies(_timer_conn.elapsed_ms());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get first timer context
|
|
|
|
*/
|
|
|
|
Context* first() { return _list.first(); }
|
2016-05-18 18:49:02 +02:00
|
|
|
};
|
2015-05-19 11:33:29 +02:00
|
|
|
|
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
static Lx::Timer *_timer;
|
|
|
|
|
|
|
|
|
|
|
|
void Lx::timer_init(Genode::Signal_receiver &sig_rec)
|
|
|
|
{
|
|
|
|
static Lx::Timer inst(sig_rec);
|
|
|
|
_timer = &inst;
|
|
|
|
}
|
2013-08-26 13:58:58 +02:00
|
|
|
|
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
void update_jiffies()
|
2013-08-26 13:58:58 +02:00
|
|
|
{
|
2016-05-18 18:49:02 +02:00
|
|
|
_timer->update_jiffies();
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************
|
|
|
|
** linux/timer.h **
|
|
|
|
*******************/
|
|
|
|
|
2015-05-19 11:33:29 +02:00
|
|
|
void init_timer(struct timer_list *timer) { }
|
2013-08-26 13:58:58 +02:00
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
|
2013-08-26 13:58:58 +02:00
|
|
|
void add_timer(struct timer_list *timer)
|
|
|
|
{
|
|
|
|
BUG_ON(timer_pending(timer));
|
|
|
|
mod_timer(timer, timer->expires);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mod_timer(struct timer_list *timer, unsigned long expires)
|
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
update_jiffies();
|
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
if (!_timer->find(timer))
|
|
|
|
_timer->add(timer);
|
2015-05-19 11:33:29 +02:00
|
|
|
|
2016-05-18 18:49:02 +02:00
|
|
|
return _timer->schedule(timer, expires);
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setup_timer(struct timer_list *timer,void (*function)(unsigned long),
|
|
|
|
unsigned long data)
|
|
|
|
{
|
|
|
|
timer->function = function;
|
|
|
|
timer->data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int timer_pending(const struct timer_list * timer)
|
|
|
|
{
|
2016-05-18 18:49:02 +02:00
|
|
|
bool pending = _timer->pending(timer);
|
2015-05-19 11:33:29 +02:00
|
|
|
lx_log(DEBUG_TIMER, "Pending %p %u", timer, pending);
|
2013-08-26 13:58:58 +02:00
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int del_timer(struct timer_list *timer)
|
|
|
|
{
|
2015-05-19 11:33:29 +02:00
|
|
|
update_jiffies();
|
|
|
|
lx_log(DEBUG_TIMER, "Delete timer %p", timer);
|
2016-05-18 18:49:02 +02:00
|
|
|
int rv = _timer->del(timer);
|
|
|
|
_timer->schedule_next();
|
2015-05-19 11:33:29 +02:00
|
|
|
|
|
|
|
return rv;
|
2013-08-26 13:58:58 +02:00
|
|
|
}
|