core: introduce thread_priority_t, enhance `sched_switch()`

This PR introduces `thread_priority_t`, an integer typedef to denote a
thread priority. It also introduces the convenience functions
`thread_priority_less()`, which returns the higher of two prioties, and
`thread_priority_valid()` which tests if a given priority a valid
priority for user threads (e.g. PRIORITY_IDLE is not).

`sched_switch()` now yields if either the other priority is higher than
the current priority, or if the current thread is not on the runqueue.

Fixes #1935, and a few bugs I found in sched.c, which did not build
with `#define DEBUG 1`, and a minor bug in `semaphore.c`.