unsigned bitarithm_msb(unsigned v) { if ((signed) v >= 0) { v &= -v; return bitarithm_lsb(v); } else { return sizeof (v) * 8 - 1; } }
static void test_bitarithm_lsb_random(void) { TEST_ASSERT_EQUAL_INT(3, bitarithm_lsb(8)); /* randomized by fair dice roll ;-) */ }
static void test_bitarithm_lsb_limit(void) { TEST_ASSERT_EQUAL_INT(0, bitarithm_lsb(UINT_MAX)); }
static void test_bitarithm_lsb_one(void) { TEST_ASSERT_EQUAL_INT(0, bitarithm_lsb(1)); }
int sched_run(void) { sched_context_switch_request = 0; thread_t *active_thread = (thread_t *)sched_active_thread; /* The bitmask in runqueue_bitcache is never empty, * since the threading should not be started before at least the idle thread was started. */ int nextrq = bitarithm_lsb(runqueue_bitcache); thread_t *next_thread = clist_get_container(sched_runqueues[nextrq], thread_t, rq_entry); DEBUG("sched_run: active thread: %" PRIkernel_pid ", next thread: %" PRIkernel_pid "\n", (active_thread == NULL) ? KERNEL_PID_UNDEF : active_thread->pid, next_thread->pid); if (active_thread == next_thread) { DEBUG("sched_run: done, sched_active_thread was not changed.\n"); return 0; } #ifdef MODULE_SCHEDSTATISTICS unsigned long time = xtimer_now(); #endif if (active_thread) { if (active_thread->status == STATUS_RUNNING) { active_thread->status = STATUS_PENDING; } #ifdef SCHED_TEST_STACK if (*((uintptr_t *) active_thread->stack_start) != (uintptr_t) active_thread->stack_start) { LOG_WARNING("scheduler(): stack overflow detected, pid=%" PRIkernel_pid "\n", active_thread->pid); } #endif #ifdef MODULE_SCHEDSTATISTICS schedstat *active_stat = &sched_pidlist[active_thread->pid]; if (active_stat->laststart) { active_stat->runtime_ticks += time - active_stat->laststart; } #endif } #ifdef MODULE_SCHEDSTATISTICS schedstat *next_stat = &sched_pidlist[next_thread->pid]; next_stat->laststart = time; next_stat->schedules++; if (sched_cb) { sched_cb(time, next_thread->pid); } #endif next_thread->status = STATUS_RUNNING; sched_active_pid = next_thread->pid; sched_active_thread = (volatile thread_t *) next_thread; DEBUG("sched_run: done, changed sched_active_thread.\n"); return 1; }
static int _ctx(gpio_t pin) { int i = bitarithm_lsb(_pin(pin)); return (_port(pin) == PORT_1) ? i : (i + 8); }