Thread migration example.
Thread migration example.
#include <pthread-l4.h>
#include <unistd.h>
#include <stdio.h>
enum { NR_THREADS = 12 };
static void *thread_fn(void *)
{
while (1)
sleep(1);
return 0;
}
static int check_cpus(void)
{
return 1;
printf("%ld maximal supported CPUs.\n", cpu_nrs);
{
printf("Will only handle %ld CPUs.\n", cpu_nrs);
}
else if (cpu_nrs == 1)
printf("Only found 1 CPU.\n");
return cpu_nrs < 2;
}
static int create_threads(void)
{
unsigned i;
for (i = 0; i < NR_THREADS; ++i)
{
pthread_t t;
if (pthread_create(&t, NULL, thread_fn, NULL))
return 1;
}
printf("Created %d threads.\n", NR_THREADS);
return 0;
}
static unsigned get_next_cpu(unsigned c)
{
unsigned x = c;
for (;;)
{
x = (x + 1) % cpu_nrs;
return x;
if (x == c)
return c;
}
}
static void shuffle(void)
{
unsigned start = 0;
while (1)
{
unsigned t;
unsigned c = start;
for (t = 0; t < NR_THREADS; ++t)
{
c = get_next_cpu(c);
printf("Error migrating thread%02d to CPU%02d\n", t, c);
printf("Migrated Thread%02d -> CPU%02d\n", t, c);
}
start++;
if (start == cpu_nrs)
start = 0;
sleep(1);
}
}
int main(void)
{
if (check_cpus())
return 1;
if (create_threads())
return 1;
shuffle();
return 0;
}
L4::Cap< L4::Scheduler > scheduler() const noexcept
Get the scheduler capability for the task.
static Env const * env() noexcept
Returns the initial environment for the current task.
C++ interface for capabilities.
unsigned long l4_umword_t
Unsigned machine word.
#define L4_MWORD_BITS
Size of machine words in bits.
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
l4_sched_cpu_set_t l4_sched_cpu_set(l4_umword_t offset, unsigned char granularity, l4_umword_t map=1) L4_NOTHROW
l4_sched_param_t l4_sched_param(unsigned prio, l4_umword_t quantum=0) L4_NOTHROW
Construct scheduler parameter.
Scheduler object functions.
l4_umword_t map
Bitmap of CPUs.
l4_sched_cpu_set_t affinity
CPU affinity.