api/sched.h - improve const correctness

Some inline functions that do not modify a cpu_set cannot take a const
one. Noticed while trying to use this functions.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
sched: allow migrating an existing thread

We already allow pinning a new thread to a particular CPU. This patch
allows moving an existing thread to a particular CPU. The new API is
currently limited to a thread moving itself, via a new function thread::pin()
and cannot be used by one thread to moving another thread.

There's a subtle point in a running thread migrating itself: It must not
start to run on the target CPU before it stops running on this CPU.
So this thread's code cannot "wake" itself on a remote CPU, or it might
run in two places concurrently for a short duration. So we need an
auxiliary thread to do this migration for us. Instead of re-using an
existing auxiliary thread (e.g., the load balancer), this patch takes
the quick-of-dirty approach of temporarily creating a new thread just
for this purpose. Since this function will likely be called rarely,
I believe this inefficient approach is "good enough".

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Support Linux affinity-setting functions

We already support the Linux pthread_attr_setaffinity_np() function to
pin a newly-created thread to a particular CPU. This patch adds support
for the Linux sched_setaffinity() and pthread_setaffinity_np() functions
which pin an existing thread to a CPU.

Currently, our support is limited to a one-cpu cpu set, and only to
pinning the calling thread (not one thread pinning another thread).
This is a useful case, as some thread APIs (including the one in C++11)
have no support for creating a pinned thread, so the users need to create
a thread and then have it pin itself.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tests/misc-ctxsw - eliminate OSv-only code

Because OSv did not support the Linux function sched_setaffinity(),
we had in tests/misc-ctxsw.cc alternative OSv-only code. Now that
sched_setaffinity() is supported, we can drop this OSv-only code.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>