ovni/test/rt/nanos6/spawn-task.c

76 lines
1.2 KiB
C
Raw Permalink Normal View History

2023-02-27 13:40:20 +01:00
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
2023-02-20 18:42:15 +01:00
* SPDX-License-Identifier: GPL-3.0-or-later */
/* Spawn a task from the main thread that calls some nanos6
* functions. */
#define _DEFAULT_SOURCE
#include <nanos6.h>
#include <nanos6/library-mode.h>
#include <stdlib.h>
2023-02-20 18:42:15 +01:00
#include <time.h>
#include "common.h"
#include "compat.h"
volatile int complete = 0;
2023-02-20 18:42:15 +01:00
static double
get_time_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double) ts.tv_sec + (double) ts.tv_nsec * 1.0e-9;
}
static void
dummy_work(double ms)
{
double end = get_time_ms() + ms * 1e-3;
while (get_time_ms() < end);
}
static void
polling_func(void *arg)
{
double ms = *((double *) arg);
double end = get_time_ms() + ms * 1e-3;
while (get_time_ms() < end) {
dummy_work(1.0); /* 1 ms */
nanos6_wait_for(1000UL); /* 1 ms */
}
}
static void
complete_func(void *arg)
{
UNUSED(arg);
complete = 1;
}
2023-02-20 18:42:15 +01:00
int
main(void)
{
double *T = malloc(sizeof(double));
if (T == NULL)
die("malloc failed:");
2023-02-20 18:42:15 +01:00
*T = 100.0;
nanos6_spawn_function(polling_func, T,
complete_func, NULL, "polling_task");
2023-02-20 18:42:15 +01:00
#pragma oss task label("dummy_task")
dummy_work(*T);
2023-02-20 18:42:15 +01:00
#pragma oss taskwait
while (!complete)
sleep_us(100);
free(T);
2023-02-20 18:42:15 +01:00
return 0;
}