void sched_run() { sched_context_switch_request = 0; tcb_t *my_active_thread = (tcb_t*)active_thread; if (my_active_thread) { if( my_active_thread->status == STATUS_RUNNING) { my_active_thread->status = STATUS_PENDING; } #ifdef SCHED_TEST_STACK if (*((unsigned int*)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) { printf("scheduler(): stack overflow detected, task=%s pid=%u\n", my_active_thread->name, my_active_thread->pid); } #endif } #if SCHEDSTATISTICS extern unsigned long hwtimer_now(void); unsigned int time = hwtimer_now(); if (my_active_thread && (pidlist[my_active_thread->pid].laststart)) { pidlist[my_active_thread->pid].runtime += time - pidlist[my_active_thread->pid].laststart; } #endif DEBUG("\nscheduler: previous task: %s\n", ( my_active_thread == NULL) ? "none" : my_active_thread->name ); if (num_tasks == 0) { DEBUG("scheduler: no tasks left.\n"); while(! num_tasks); DEBUG("scheduler: new task created.\n"); } my_active_thread = NULL; while(! my_active_thread) { // for (int i = 0; i < SCHED_PRIO_LEVELS; i++) { /* TODO: introduce bitfield cache */ // if (runqueues[i]) { int nextrq = number_of_lowest_bit(runqueue_bitcache); clist_node_t next = *(runqueues[nextrq]); DEBUG("scheduler: first in queue: %s\n", ((tcb_t*)next.data)->name); clist_advance(&(runqueues[nextrq])); my_active_thread = (tcb_t*)next.data; thread_pid = (volatile int) my_active_thread->pid; #if SCHEDSTATISTICS pidlist[my_active_thread->pid].laststart = time; pidlist[my_active_thread->pid].schedules ++; #endif // break; // } // } if (active_thread->pid != last_pid) { last_pid = active_thread->pid; } } DEBUG("scheduler: next task: %s\n", my_active_thread->name); if (my_active_thread != active_thread) { if (active_thread != NULL) { //TODO: necessary? if (active_thread->status == STATUS_RUNNING) { active_thread->status = STATUS_PENDING ; } } sched_set_status((tcb_t*)my_active_thread, STATUS_RUNNING); } active_thread = (volatile tcb_t*) my_active_thread; DEBUG("scheduler: done.\n"); }
void sched_run() { sched_context_switch_request = 0; tcb_t *my_active_thread = (tcb_t *)active_thread; if (my_active_thread) { if (my_active_thread->status == STATUS_RUNNING) { my_active_thread->status = STATUS_PENDING; } #ifdef SCHED_TEST_STACK if (*((unsigned int *)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) { printf("scheduler(): stack overflow detected, task=%s pid=%u\n", my_active_thread->name, my_active_thread->pid); } #endif } #ifdef SCHEDSTATISTICS unsigned long time = hwtimer_now(); if (my_active_thread && (pidlist[my_active_thread->pid].laststart)) { pidlist[my_active_thread->pid].runtime_ticks += time - pidlist[my_active_thread->pid].laststart; } #endif DEBUG("\nscheduler: previous task: %s\n", (my_active_thread == NULL) ? "none" : my_active_thread->name); if (num_tasks == 0) { DEBUG("scheduler: no tasks left.\n"); while (!num_tasks) { /* loop until a new task arrives */ ; } DEBUG("scheduler: new task created.\n"); } my_active_thread = NULL; while (!my_active_thread) { int nextrq = number_of_lowest_bit(runqueue_bitcache); clist_node_t next = *(runqueues[nextrq]); DEBUG("scheduler: first in queue: %s\n", ((tcb_t *)next.data)->name); clist_advance(&(runqueues[nextrq])); my_active_thread = (tcb_t *)next.data; thread_pid = (volatile int) my_active_thread->pid; #if SCHEDSTATISTICS pidlist[my_active_thread->pid].laststart = time; pidlist[my_active_thread->pid].schedules++; if ((sched_cb) && (my_active_thread->pid != last_pid)) { sched_cb(hwtimer_now(), my_active_thread->pid); last_pid = my_active_thread->pid; } #endif #ifdef MODULE_NSS if (active_thread && active_thread->pid != last_pid) { last_pid = active_thread->pid; } #endif } DEBUG("scheduler: next task: %s\n", my_active_thread->name); if (my_active_thread != active_thread) { if (active_thread != NULL) { /* TODO: necessary? */ if (active_thread->status == STATUS_RUNNING) { active_thread->status = STATUS_PENDING ; } } sched_set_status((tcb_t *)my_active_thread, STATUS_RUNNING); } active_thread = (volatile tcb_t *) my_active_thread; DEBUG("scheduler: done.\n"); }