#include #include #include #include #include #include #include #include #include void proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock, spin_lock_ctx_t* ctxrl) { spin_lock_ctx_t ctxpr, ctxcpu, ctxsq; struct cpu* cpu = proc->cpu; struct proc_sq_entry* sq_entry = malloc (sizeof (*sq_entry)); if (!sq_entry) { spin_unlock (resource_lock, ctxrl); return; } sq_entry->proc = proc; sq_entry->sq = sq; spin_lock (&cpu->lock, &ctxcpu); spin_lock (&proc->lock, &ctxpr); spin_lock (&sq->lock, &ctxsq); spin_unlock (resource_lock, ctxrl); atomic_store (&proc->state, PROC_SUSPENDED); /* append to sq's list */ list_append (sq->proc_list, &sq_entry->sq_link); /* append to proc's list */ list_append (proc->sq_entries, &sq_entry->proc_link); list_remove (cpu->proc_run_q, &proc->cpu_run_q_link); atomic_fetch_sub (&cpu->proc_run_q_count, 1); if (cpu->proc_current == proc) cpu->proc_current = NULL; proc->cpu = NULL; spin_unlock (&sq->lock, &ctxsq); spin_unlock (&proc->lock, &ctxpr); spin_unlock (&cpu->lock, &ctxcpu); cpu_request_sched (cpu); } void proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry) { spin_lock_ctx_t ctxsq, ctxpr, ctxcpu; struct cpu* cpu = cpu_find_lightest (); struct proc_suspension_q* sq = sq_entry->sq; spin_lock (&cpu->lock, &ctxcpu); spin_lock (&proc->lock, &ctxpr); spin_lock (&sq->lock, &ctxsq); /* remove from sq's list */ list_remove (sq->proc_list, &sq_entry->sq_link); /* remove from proc's list */ list_remove (proc->sq_entries, &sq_entry->proc_link); proc->cpu = cpu; if (proc->sq_entries == NULL) atomic_store (&proc->state, PROC_READY); list_append (cpu->proc_run_q, &proc->cpu_run_q_link); atomic_fetch_add (&cpu->proc_run_q_count, 1); spin_unlock (&sq->lock, &ctxsq); spin_unlock (&proc->lock, &ctxpr); spin_unlock (&cpu->lock, &ctxcpu); free (sq_entry); cpu_request_sched (cpu); } void proc_sqs_cleanup (struct proc* proc) { spin_lock_ctx_t ctxsq, ctxpr; spin_lock (&proc->lock, &ctxpr); /* clean suspension queue entries */ struct list_node_link *sq_link, *sq_link_tmp; list_foreach (proc->sq_entries, sq_link, sq_link_tmp) { struct proc_sq_entry* sq_entry = list_entry (sq_link, struct proc_sq_entry, proc_link); struct proc_suspension_q* sq = sq_entry->sq; spin_lock (&sq->lock, &ctxsq); /* remove from sq's list */ list_remove (sq->proc_list, &sq_entry->sq_link); /* remove from proc's list */ list_remove (proc->sq_entries, &sq_entry->proc_link); spin_unlock (&sq->lock, &ctxsq); free (sq_entry); } spin_unlock (&proc->lock, &ctxpr); }