From 0277a663bcbebe5be82dc88235fcd0a1465bd5e8 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sat, 18 Dec 2010 11:25:34 +0100 Subject: Add generic poll infrastructure Barebox does not have interrupt functionality. Nevertheless it's sometimes useful to periodically call functions, like for example a heartbeat LED or watchdog reset. Instead of cluttering the code with calls to these functions this patch adds a generic polling infrastructure. Code which might run for longer now can call poller_call() periodically which in turn will call all registered pollers. This patch adds a call to poller_call in two generic pathes. First of them is getc() which covers waiting for uart input. Second is ctrlc() which should be called anyway from code which might run for longer. So instead adding poller_call directly to your code, consider checking ctrlc instead which also gives additional convenience to the user. The poller code is safe against reentrancy which means that it's safe to call poller_call inside a poller. Signed-off-by: Marc Kleine-Budde Signed-off-by: Sascha Hauer --- common/poller.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 common/poller.c (limited to 'common/poller.c') diff --git a/common/poller.c b/common/poller.c new file mode 100644 index 0000000000..0583a532cf --- /dev/null +++ b/common/poller.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2010 Marc Kleine-Budde + * + * This file is released under the GPLv2 + * + */ + +#include +#include +#include +#include +#include +#include + +static LIST_HEAD(poller_list); +static int poller_active; + +int poller_register(struct poller_struct *poller) +{ + list_add_tail(&poller->list, &poller_list); + + return 0; +} + +int poller_unregister(struct poller_struct *poller) +{ + list_del(&poller->list); + + return 0; +} + +void poller_call(void) +{ + struct poller_struct *poller, *tmp; + + if (poller_active) + return; + + poller_active = 1; + + list_for_each_entry_safe(poller, tmp, &poller_list, list) + poller->func(poller); + + poller_active = 0; +} -- cgit v1.2.3