int task_activate(FAR _TCB *tcb) { irqstate_t flags = irqsave(); #ifdef CONFIG_SCHED_INSTRUMENTATION /* Check if this is really a re-start */ if (tcb->task_state != TSTATE_TASK_INACTIVE) { /* Inform the instrumentation layer that the task * has stopped */ sched_note_stop(tcb); } /* Inform the instrumentation layer that the task * has started */ sched_note_start(tcb); #endif up_unblock_task(tcb); irqrestore(flags); return OK; }
void task_exithook(FAR _TCB *tcb, int status) { /* Inform the instrumentation layer that the task has stopped */ sched_note_stop(tcb); /* Flush all streams (File descriptors will be closed when * the TCB is deallocated). */ #if CONFIG_NFILE_STREAMS > 0 (void)lib_flushall(tcb->streams); #endif /* Deallocate anything left in the TCB's queues */ #ifndef CONFIG_DISABLE_SIGNALS sig_cleanup(tcb); /* Deallocate Signal lists */ #endif /* Wakeup any tasks waiting for this task to exit */ #ifdef CONFIG_SCHED_WAITPID /* Experimental */ while (tcb->exitsem.semcount < 0) { /* "If more than one thread is suspended in waitpid() awaiting * termination of the same process, exactly one thread will return * the process status at the time of the target process termination." * Hmmm.. what do we return to the others? */ if (tcb->stat_loc) { *tcb->stat_loc = status << 8; tcb->stat_loc = NULL; } /* Wake up the thread */ sem_post(&tcb->exitsem); } #endif /* If an exit function was registered, call it now. NOTE: In the case * of task_delete(), the exit function will *not* be called on the thread * execution of the task being deleted! */ #ifdef CONFIG_SCHED_ATEXIT if (tcb->exitfunc) { (*tcb->exitfunc)(); } #endif }
int task_terminate(pid_t pid, bool nonblocking) { FAR struct tcb_s *dtcb; FAR dq_queue_t *tasklist; irqstate_t flags; #ifdef CONFIG_SMP int cpu; #endif int ret; /* Make sure the task does not become ready-to-run while we are futzing * with its TCB. Within the critical section, no new task may be started * or terminated (even in the SMP case). */ flags = enter_critical_section(); /* Find for the TCB associated with matching PID */ dtcb = sched_gettcb(pid); if (!dtcb) { /* This PID does not correspond to any known task */ ret = -ESRCH; goto errout_with_lock; } /* Verify our internal sanity */ #ifdef CONFIG_SMP DEBUGASSERT(dtcb->task_state < NUM_TASK_STATES); #else DEBUGASSERT(dtcb->task_state != TSTATE_TASK_RUNNING && dtcb->task_state < NUM_TASK_STATES); #endif /* Remove the task from the OS's task lists. We must be in a critical * section and the must must not be running to do this. */ #ifdef CONFIG_SMP /* In the SMP case, the thread may be running on another CPU. If that is * the case, then we will pause the CPU that the thread is running on. */ cpu = sched_cpu_pause(dtcb); /* Get the task list associated with the the thread's state and CPU */ tasklist = TLIST_HEAD(dtcb->task_state, cpu); #else /* In the non-SMP case, we can be assured that the task to be terminated * is not running. get the task list associated with the task state. */ tasklist = TLIST_HEAD(dtcb->task_state); #endif /* Remove the task from the task list */ dq_rem((FAR dq_entry_t *)dtcb, tasklist); dtcb->task_state = TSTATE_TASK_INVALID; /* At this point, the TCB should no longer be accessible to the system */ #ifdef CONFIG_SMP /* Resume the paused CPU (if any) */ if (cpu >= 0) { /* I am not yet sure how to handle a failure here. */ DEBUGVERIFY(up_cpu_resume(cpu)); } #endif /* CONFIG_SMP */ leave_critical_section(flags); /* Perform common task termination logic (flushing streams, calling * functions registered by at_exit/on_exit, etc.). We need to do * this as early as possible so that higher level clean-up logic * can run in a healthy tasking environment. * * In the case where the task exits via exit(), task_exithook() * may be called twice. * * I suppose EXIT_SUCCESS is an appropriate return value??? */ task_exithook(dtcb, EXIT_SUCCESS, nonblocking); /* Since all tasks pass through this function as the final step in their * exit sequence, this is an appropriate place to inform any instrumentation * layer that the task no longer exists. */ sched_note_stop(dtcb); /* Deallocate its TCB */ return sched_releasetcb(dtcb, dtcb->flags & TCB_FLAG_TTYPE_MASK); errout_with_lock: leave_critical_section(flags); return ret; }