status_t semaphore_alarm (void * data) /* * ARGUMENTS * * data : a thread_t element. * * RETURN * * DNA_BAD_ARGUMENT: the thread parameter is not valid * * DNA_ERROR: the thread is not a valid thread * * DNA_OK: the operation succeeded * * SOURCE */ { thread_t thread = data; status_t status = DNA_OK; watch (status_t) { ensure (thread != NULL, DNA_BAD_ARGUMENT); ensure (thread -> resource_queue != NULL, DNA_ERROR); ensure (thread -> info . status == DNA_THREAD_WAITING, DNA_ERROR); ensure (thread -> info . resource == DNA_RESOURCE_SEMAPHORE, DNA_ERROR); /* * Lock the thread's resource queue. * Extract the thread from the waiting list. */ lock_acquire (& thread -> resource_queue -> lock); status = queue_extract (thread -> resource_queue, thread); lock_release (& thread -> resource_queue -> lock); /* * If the thread was waiting, we can dispatch it */ if (status == DNA_OK) { lock_acquire (& thread -> lock); dna_log(VERBOSE_LEVEL, " %s DNA_NO_RESOURCE ", thread -> info . name); thread -> resource_queue = NULL; thread -> info . sem_tokens = 0; thread -> info . resource = DNA_NO_RESOURCE; thread -> info . resource_id = -1; thread -> info . status = DNA_THREAD_READY; status = scheduler_dispatch (thread); } return status; } }
bool exec_client(fds c, double tdt) { char *s; s = getcmd(c); if (((c && (c->trick) && ((t_client*)c->trick)->close) || !fds_alive(c)) && (c->write || net_close(c))) return (true); if (!(c->trick) || !(((t_client*)c->trick)->_m)) return (mod_discovery(c, &s)); s = flood_check(c, s); if (scheduler_(c, tdt)) return (scheduler_dispatch(c)); else if (!scheduler_active(c) && callback_(c, s, tdt)) return (callback_handler(c, s)); else if (s && !scheduler_active(c) && (find_action(c, s) != -1)) flood_read(c); return (true); }
static void rr_loop(void *args){ RRThreadArgs *thread_args = args; int cpu = thread_args->cpu; ProcessControlBlock *proc; int quantum_ms = 1000; while(1){ sem_wait(&rr_cpu_available[cpu]); proc = rr_next(); assert( proc->state == READY ); scheduler_dispatch(proc, cpu); sleep_ms(quantum_ms); if(proc->state == RUNNING) scheduler_interrupt(proc); } }
status_t semaphore_destroy (int32_t id) /* * ARGUMENTS * * id : the semaphore id. * * RESULT * * DNA_BAD_SEM_ID: the id parameter is invalid * * DNA_OK: the operation succeeded * * SOURCE */ { thread_t thread = NULL; semaphore_t sem = NULL; semaphore_id_t sid = { .raw = id }; interrupt_status_t it_status = 0; bool smart_to_reschedule = false; status_t status; watch (status_t) { ensure (sid . s . index < DNA_MAX_SEM, DNA_BAD_SEM_ID); it_status = cpu_trap_mask_and_backup(); lock_acquire (& semaphore_pool . lock); /* * Look for the semaphore with ID id. If found, * remove its entry from the pool. */ sem = semaphore_pool . semaphore[sid . s . index]; check (invalid_semaphore, sem != NULL, DNA_BAD_SEM_ID); check (invalid_semaphore, sem -> id . raw == sid . raw, DNA_BAD_SEM_ID); semaphore_pool . semaphore[sid . s . index] = NULL; lock_acquire (& sem -> lock); lock_release (& semaphore_pool . lock); /* * Reschedule each waiting thread, and * reset its information. */ lock_acquire (& sem -> waiting_queue . lock); while ((thread = queue_rem (& sem -> waiting_queue)) != NULL) { lock_acquire (& thread -> lock); thread -> info . sem_tokens = 0; thread -> info . resource = DNA_NO_RESOURCE; thread -> info . resource_id = -1; if (thread -> info . status == DNA_THREAD_WAITING) { thread -> info . status = DNA_THREAD_READY; status = scheduler_dispatch (thread); smart_to_reschedule = smart_to_reschedule || (status == DNA_INVOKE_SCHEDULER); } lock_release (& thread -> lock); } lock_release (& sem -> waiting_queue . lock); /* * Delete the semaphore's memory. */ kernel_free (sem); return smart_to_reschedule ? DNA_INVOKE_SCHEDULER : DNA_OK; } rescue (invalid_semaphore) { lock_release (& semaphore_pool . lock); cpu_trap_restore(it_status); leave; } }