Some code can't foresee which resources will be used by its poller. This
is the case especially in pollers that will execute arbitrary commands
input by the user. With this commit a special slice is introduced that is
released only when Barebox is waiting for console input and only when it
is doing that outside of any command. Code that wants to depend on the
idle slice must select CONFIG_IDLE_SLICE to make it available.

Signed-off-by: Daniel Glöckner <d...@emlix.com>
---
 common/Kconfig             |  4 ++++
 common/binfmt.c            |  3 +++
 common/command.c           |  3 +++
 common/console_countdown.c |  3 +++
 common/poller.c            |  2 ++
 common/slice.c             | 24 ++++++++++++++++++++++++
 include/slice.h            | 10 ++++++++++
 lib/readline.c             |  8 +++++++-
 8 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/common/Kconfig b/common/Kconfig
index 400c0553c..2fa9140de 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -913,6 +913,10 @@ config BAREBOXCRC32_TARGET
 config POLLER
        bool "generic polling infrastructure"
 
+config IDLE_SLICE
+       bool
+       default n
+
 config STATE
        bool "generic state infrastructure"
        select CRC32
diff --git a/common/binfmt.c b/common/binfmt.c
index f2ff62458..899f270d4 100644
--- a/common/binfmt.c
+++ b/common/binfmt.c
@@ -10,6 +10,7 @@
 #include <malloc.h>
 #include <command.h>
 #include <errno.h>
+#include <slice.h>
 
 static LIST_HEAD(binfmt_hooks);
 
@@ -23,7 +24,9 @@ static int binfmt_run(char *file, int argc, char **argv)
                if (b->type != type)
                        continue;
 
+               idle_slice_acquire();
                ret = b->hook(b, file, argc, argv);
+               idle_slice_release();
                if (ret != -ERESTARTNOHAND)
                        return ret;
        }
diff --git a/common/command.c b/common/command.c
index 49845938a..2b10248f5 100644
--- a/common/command.c
+++ b/common/command.c
@@ -30,6 +30,7 @@
 #include <init.h>
 #include <complete.h>
 #include <getopt.h>
+#include <slice.h>
 
 LIST_HEAD(command_list);
 EXPORT_SYMBOL(command_list);
@@ -72,7 +73,9 @@ int execute_command(int argc, char **argv)
        /* Look up command in command table */
        if ((cmdtp = find_cmd(argv[0]))) {
                /* OK - call function to do the command */
+               idle_slice_acquire();
                ret = cmdtp->cmd(argc, argv);
+               idle_slice_release();
                if (ret == COMMAND_ERROR_USAGE) {
                        barebox_cmd_usage(cmdtp);
                        ret = COMMAND_ERROR;
diff --git a/common/console_countdown.c b/common/console_countdown.c
index 8d09894c3..f161cf7df 100644
--- a/common/console_countdown.c
+++ b/common/console_countdown.c
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <console_countdown.h>
 #include <stdio.h>
+#include <slice.h>
 
 static bool console_countdown_timeout_abort;
 
@@ -59,6 +60,7 @@ int console_countdown(int timeout_s, unsigned flags, const 
char *keys,
        if (!(flags & CONSOLE_COUNTDOWN_SILENT))
                printf("%4d", countdown--);
 
+       idle_slice_release();
        do {
                if (tstc()) {
                        key = getchar();
@@ -91,6 +93,7 @@ int console_countdown(int timeout_s, unsigned flags, const 
char *keys,
        ret = 0;
 
  out:
+       idle_slice_acquire();
        if (!(flags & CONSOLE_COUNTDOWN_SILENT))
                printf("\n");
        if (key && out_key)
diff --git a/common/poller.c b/common/poller.c
index cccffcb88..60a3461b2 100644
--- a/common/poller.c
+++ b/common/poller.c
@@ -116,7 +116,9 @@ void poller_call(void)
                        continue;
 
                slice_acquire(&poller->slice);
+               idle_slice_acquire();
                poller->func(poller);
+               idle_slice_release();
                slice_release(&poller->slice);
        }
 }
diff --git a/common/slice.c b/common/slice.c
index b4460335d..960bb2ff8 100644
--- a/common/slice.c
+++ b/common/slice.c
@@ -3,6 +3,7 @@
 #define pr_fmt(fmt) "slice: " fmt
 
 #include <common.h>
+#include <init.h>
 #include <slice.h>
 
 /*
@@ -221,6 +222,29 @@ void slice_init(struct slice *slice, const char *name)
        list_add_tail(&slice->list, &slices);
 }
 
+#ifdef CONFIG_IDLE_SLICE
+struct slice idle_slice;
+
+void idle_slice_acquire(void)
+{
+       slice_acquire(&idle_slice);
+}
+
+void idle_slice_release(void)
+{
+       slice_release(&idle_slice);
+}
+
+static int idle_slice_init(void)
+{
+       slice_init(&idle_slice, "idle");
+       slice_acquire(&idle_slice);
+       return 0;
+}
+
+pure_initcall(idle_slice_init);
+#endif
+
 #if defined CONFIG_CMD_SLICE
 
 #include <command.h>
diff --git a/include/slice.h b/include/slice.h
index 4874f1afc..068daaa7c 100644
--- a/include/slice.h
+++ b/include/slice.h
@@ -28,4 +28,14 @@ void slice_init(struct slice *slice, const char *name);
 
 void slice_debug_acquired(struct slice *slice);
 
+#ifdef CONFIG_IDLE_SLICE
+extern struct slice idle_slice;
+
+void idle_slice_acquire(void);
+void idle_slice_release(void);
+#else
+static inline void idle_slice_acquire(void) {}
+static inline void idle_slice_release(void) {}
+#endif
+
 #endif /* __SLICE_H */
diff --git a/lib/readline.c b/lib/readline.c
index 3d16c1838..709efb375 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -3,6 +3,7 @@
 #include <init.h>
 #include <libbb.h>
 #include <poller.h>
+#include <slice.h>
 #include <ratp_bb.h>
 #include <xfuncs.h>
 #include <complete.h>
@@ -200,11 +201,16 @@ int readline(const char *prompt, char *buf, int len)
        puts (prompt);
 
        while (1) {
+               idle_slice_release();
                while (!tstc()) {
                        poller_call();
-                       if (IS_ENABLED(CONFIG_CONSOLE_RATP))
+                       if (IS_ENABLED(CONFIG_CONSOLE_RATP)) {
+                               idle_slice_acquire();
                                barebox_ratp_command_run();
+                               idle_slice_release();
+                       }
                }
+               idle_slice_acquire();
 
                ichar = read_key();
 
-- 
2.17.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to