summaryrefslogtreecommitdiffstats
path: root/commands/bthread.c
diff options
context:
space:
mode:
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);
}