summaryrefslogtreecommitdiff
path: root/commands/bthread.c
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-06-28 09:07:30 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-06-28 14:44:31 +0200
commitc32343519de1774c09995118d0bae56999902a37 (patch)
tree2c923abf526e3f743b75da34e9dd2bd3493fb581 /commands/bthread.c
parent57313f83e83e1fac87c1a9088175f12ebd3577a0 (diff)
downloadbarebox-c32343519de1774c09995118d0bae56999902a37.tar.gz
bthread: remove thread exit codes
Follow-up commit will replace blocking bthread_stop with non-blocking bthread_cancel. Prepare for this by dropping exit codes. This is not much of a loss, because most users of bthreads will only call bthread_stop at cleanup time. bthread command is an exception, so have it take manual care of passing around exit codes. As we touch the bthread_stop prototype anyway, rename it to __bthread_stop. This will make it clearer in the future, that it's not meant for driver use. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210628070732.16812-1-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/bthread.c')
-rw-r--r--commands/bthread.c48
1 files changed, 30 insertions, 18 deletions
diff --git a/commands/bthread.c b/commands/bthread.c
index 0c9c221884..446fe9c37a 100644
--- a/commands/bthread.c
+++ b/commands/bthread.c
@@ -33,12 +33,10 @@ static int bthread_time(void)
return i;
}
-static int bthread_infinite(void *data)
+static void bthread_infinite(void *data)
{
while (!bthread_should_stop())
;
-
- return 0;
}
static int bthread_isolated_time(void)
@@ -62,14 +60,20 @@ static int bthread_isolated_time(void)
i += 2;
}
- bthread_stop(bthread);
+ __bthread_stop(bthread);
bthread_free(bthread);
return i;
}
-static int bthread_printer(void *arg)
+struct arg {
+ unsigned long in;
+ long out;
+};
+
+static void bthread_printer(void *_arg)
{
+ struct arg *arg = _arg;
volatile u64 start;
volatile unsigned long i = 0;
start = get_time_ns();
@@ -78,28 +82,30 @@ static int bthread_printer(void *arg)
if (!is_timeout_non_interruptible(start, 225 * MSECOND))
continue;
- if ((unsigned long)arg == i++)
+ if (arg->in == i++)
printf("%s yield #%lu\n", bthread_name(current), i);
start = get_time_ns();
}
- return i;
+ arg->out = i;
}
static int yields;
-static int bthread_spawner(void *arg)
+static void bthread_spawner(void *_spawner_arg)
{
+ struct arg *arg, *spawner_arg = _spawner_arg;
struct bthread *bthread[4];
volatile u64 start;
volatile unsigned long i = 0;
int ret = 0;
- int ecode;
start = get_time_ns();
for (i = 0; i < ARRAY_SIZE(bthread); i++) {
- bthread[i] = bthread_run(bthread_printer, (void *)(long)i,
+ arg = malloc(sizeof(*arg));
+ arg->in = i;
+ bthread[i] = bthread_run(bthread_printer, arg,
"%s-bthread%u", bthread_name(current), i+1);
if (!bthread[i]) {
ret = -ENOMEM;
@@ -112,14 +118,16 @@ static int bthread_spawner(void *arg)
cleanup:
while (i--) {
- ecode = bthread_stop(bthread[i]);
+ arg = bthread_data(bthread[i]);
+ __bthread_stop(bthread[i]);
bthread_free(bthread[i]);
- if (!ret && (ecode != 4 || yields < ecode))
+ if (!ret && (arg->out != 4 || yields < arg->out))
ret = -EIO;
+ free(arg);
}
- return ret;
+ spawner_arg->out = ret;
}
struct spawn {
@@ -132,8 +140,9 @@ static int do_bthread(int argc, char *argv[])
LIST_HEAD(spawners);
struct spawn *spawner, *tmp;
int ret = 0;
- int ecode, opt, i = 0;
+ int opt, i = 0;
bool time = false;
+ struct arg *arg;
while ((opt = getopt(argc, argv, "itcv")) > 0) {
switch (opt) {
@@ -146,7 +155,8 @@ static int do_bthread(int argc, char *argv[])
break;
case 'v':
spawner = xzalloc(sizeof(*spawner));
- spawner->bthread = bthread_run(bthread_spawner, NULL,
+ arg = malloc(sizeof(*arg));
+ spawner->bthread = bthread_run(bthread_spawner, arg,
"spawner%u", ++i);
if (!spawner->bthread) {
free(spawner);
@@ -172,10 +182,12 @@ static int do_bthread(int argc, char *argv[])
cleanup:
list_for_each_entry_safe(spawner, tmp, &spawners, list) {
- ecode = bthread_stop(spawner->bthread);
+ arg = bthread_data(spawner->bthread);
+ __bthread_stop(spawner->bthread);
bthread_free(spawner->bthread);
- if (!ret && ecode)
- ret = ecode;
+ if (!ret && arg->out)
+ ret = arg->out;
+ free(arg);
free(spawner);
}